3

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?

JMcCarty
  • 759
  • 5
  • 17
  • To clarify, you want to pass a string that contains the literal _${CTEST_CONFIGURATION_TYPE}_, as opposed to CMake substituting it by the variable's content? – pezcode Feb 27 '12 at 15:53
  • yes, that way the generated CTestTestfile.cmake contains the line set_tests_properties(test WORKING_DIRECTORY "/${CTEST_CONFIGURATION_TYPE}") – JMcCarty Feb 27 '12 at 16:00

2 Answers2

2

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
mathstuf
  • 188
  • 8
  • Good call. +1 Just to be clear, we're talking about this being available as of v2.8.12 I think. Also, generator expressions have been available for a while in `add_test`; the difference now is that they can be used within the `WORKING_DIRECTORY` argument as well as the `COMMAND` argument. – Fraser Sep 18 '13 at 20:39
1

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.

Community
  • 1
  • 1
Fraser
  • 74,704
  • 20
  • 238
  • 215