My goal was to execute a diff command after the test execution finished. This is very similar to the problem you are facing. I used this cmake
script technique. Leaving it as an answer in case it helps someone else.
In the original CMakeLists.txt file I added a test
add_test(NAME testCommand
COMMAND ${CMAKE_COMMAND}
-DCMD1=$<TARGET_FILE:target>
-DTEST_DATA_DIR=${CMAKE_SOURCE_DIR}
-P ${CMAKE_SOURCE_DIR}/runtests.cmake)
Create a file called runtests.cmake in the directory which has the CMakeLists.txt file. This new file will hold the commands to execute the test and run commands after
include(FindUnixCommands)
macro(EXEC_CHECK CMD)
execute_process(COMMAND ${CMD} ${TEST_DATA_DIR}/test_input.txt RESULT_VARIABLE CMD_RESULT)
if(CMD_RESULT)
message(FATAL_ERROR "Error running ${CMD}")
else()
if (BASH)
execute_process(COMMAND ${BASH} -c "diff -b ${TEST_DATA_DIR}/test_input.txt ${TEST_DATA_DIR}/test_output.txt" RESULT_VARIABLE RES)
if(RES)
message(FATAL_ERROR "Diff is not clean")
endif()
else(BASH)
message(FATAL_ERROR "BASH not found : no diff script run")
endif(BASH)
endif()
endmacro()
exec_check(${CMD1})
From the build directory I can now issue make test
and it picks up the cmake script properly.