How to use redirection in cmake add_test

前端 未结 2 1462
走了就别回头了
走了就别回头了 2020-12-11 07:37

I have a console application called \"foo\", which takes a reference text file as input (in.txt) and generates text at standard output (I want to keep this behaviour).

2条回答
  •  时光说笑
    2020-12-11 07:52

    Turning my comment into an answer

    As @Tsyvarev has stated, CTest commands are not run in a shell's context. But you could just add the shell needed yourself and use e.g. sh as the command to be called with add_test().

    I've run some tests with your example code and the following did work successfully:

    add_test(NAME test1 COMMAND sh -c "$ ../test/in.txt > ../test/out.txt")
    

    This solution is not platform independent (it depends on sh to be available in the search paths).


    So if you want to be more flexible you could do something like:

    include(FindUnixCommands)
    
    file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/in.txt" _in)
    file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/out.txt" _out)
    if (BASH)
        add_test(
            NAME test1 
            COMMAND ${BASH} -c "$ ${_in} > ${_out}"
        )
    else()
        if (WIN32)
            add_test(
                NAME test1 
                COMMAND ${CMAKE_COMMAND} -E chdir $ $ENV{ComSpec} /c "$ ${_in} > ${_out}"
            )
        else()
            message(FATAL_ERROR "Unknown shell command for ${CMAKE_HOST_SYSTEM_NAME}")
        endif()
    endif()
    

    Additionally there is the possibility to execute a more platform independent diff with ${CMAKE_COMMAND} -E compare_files . So you could simplify your complete makefile based example in CMake with:

    add_custom_command(
        TARGET foo
        POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Running $ ..."
        COMMAND foo in.txt > out.txt
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
    )
    
    add_test(
        NAME test1 
        COMMAND ${CMAKE_COMMAND} -E compare_files in.txt out.txt
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test
    )
    

    References

    • Integrate bash test scripts in cmake
    • CMake: piping commands to executable
    • cmake: make tests successfully passing part of the build process

提交回复
热议问题