JMcCarty
JMcCarty

Reputation: 759

How to pass ${CTEST_CONFIGURATION_TYPE} using add_test in cmake

I can't seem to figure out how to pass ${CTEST_CONFIGURATION_TYPE} using add_test in cmake without having CMake add additional escape characters.

I am currently using cmake to generate VS project files and setting the RUNTIME_OUTPUT_DIRECTORY and need to use this directory + configuration as the working directory for a few of the tests as they depend on other built executables.

I have tried using

add_test(NAME test 
     WORKING_DIRECTORY "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/\${CTEST_CONFIGURATION_TYPE}"
     COMMAND test ${TEST_ARGS})

This correctly finds my test executable, however the CTestTestfile.cmake file that is generated contains

WORKING_DIRECTORY "<CORRECT_RUNTIME_PATH>/\${CTEST_CONFIGURATION_TYPE}"

Is there anyway I can get this to work or am i just going to have to pass the correct directory to the test via command line every time?

Upvotes: 3

Views: 2508

Answers (2)

Fraser
Fraser

Reputation: 78280

Update:

As @mathstuf has pointed out in his own answer, as of version 2.8.12 CMake supports generator expressions within the WORKING_DIRECTORY argument of add_test. This makes the rest of my answer only applicable to CMake v2.8.11 and below.


As far as I know, it's not really possible to pass a "$" through ADD_TEST without it ending up escaped in CTestTestfile.cmake.

Really the "CMake" way to handle this situation is probably to pass the dependent exes to the test exe as a command line parameter, which would involve changing the test code. If the dependent executables are all CMake targets, they can be referenced in the ADD_TEST command using "$<TARGET_FILE:tgt>" where tgt is the name of the CMake target.

There is however a big, dirty hack you could use to get round this. Replace your ADD_TEST command with:

ADD_TEST(NAME test WORKING_DIRECTORY "@WORKING_DIR@" COMMAND test ${TEST_ARGS})
FILE(WRITE ${CMAKE_BINARY_DIR}/CTestCustom.cmake
     "SET(WORKING_DIR \"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/\\\${CTEST_CONFIGURATION_TYPE}\")\n")
FILE(APPEND ${CMAKE_BINARY_DIR}/CTestCustom.cmake
     "CONFIGURE_FILE(${CMAKE_BINARY_DIR}/CTestTestfile.cmake ${CMAKE_BINARY_DIR}/CTestTestfile.cmake @ONLY)\n")

This is (ab)using CTest's behaviour by creating a CTestCustom.cmake file which is invoked before the CTestTestfile.cmake. After running CMake, CTestTestfile.cmake has the line

SET_TESTS_PROPERTIES(test PROPERTIES  WORKING_DIRECTORY "@WORKING_DIR@")

By running CTest and invoking CTestCustom.cmake, the "@WORKING_DIR@" is replaced with the correct value.

It really is a hack; messing with auto-generated CMake files is asking for trouble, but it could do until you get time to refactor your tests or CMake becomes better able to support a per-configuration WORKING_DIRECTORY.

Upvotes: 1

mathstuf
mathstuf

Reputation: 318

This is currently impossible in the general case. In the next version of CMake, you should be able to use the feature in this commit to use generator expressions in your test properties.

Upvotes: 2

Related Questions