include_guard() # # jpp_add_test_temporary_dir(testName) add two fixtures to the test named `testName` : # # - one setup fixture which creates a temporary directory specific to the test # - one cleanup fixture that remove the temporary directory # # the temporary directory is also added to the test environment as environment variable # TMPDIR # function(jpp_add_test_temporary_dir testName) # some base UUID (exact value not really relevant) set(baseUUID 29054F81-A5AD-49C5-9CE6-E9EAA3DC7D00) # build a UUID for the temporary directory string(UUID suffix NAMESPACE ${baseUUID} NAME ${testName} TYPE SHA1) if (NOT DEFINED ENV{TMPDIR}) # usually there's already a TMPDIR defined, but still consider the case it's not set(CUSTOM_TMPDIR ${CMAKE_CURRENT_BINARY_DIR}/tmp/${PROJECT_NAME}/${suffix}) else() set(CUSTOM_TMPDIR $ENV{TMPDIR}/${PROJECT_NAME}/${suffix}) endif() cmake_path(NORMAL_PATH CUSTOM_TMPDIR) set_property(TEST ${testName} APPEND PROPERTY ENVIRONMENT TMPDIR=${CUSTOM_TMPDIR}) # the fixture name that will be used by the three "tests" setup, testName, cleanup set(fixtureName "${suffix}-fixture") # create setup "test" (not really a test but a command that will run _before_ testName) set(testSetup "${testName}-setup") add_test(NAME ${testSetup} COMMAND ${CMAKE_COMMAND} -E make_directory ${CUSTOM_TMPDIR}) set_tests_properties(${testSetup} PROPERTIES LABELS setup FIXTURES_SETUP ${fixtureName}) # create cleanup "test" (not really a test either but a command that will run _after_ testName) set(testCleanup "${testName}-cleanup") add_test(NAME ${testCleanup} COMMAND ${CMAKE_COMMAND} -E rm -rf ${CUSTOM_TMPDIR}) set_tests_properties(${testCleanup} PROPERTIES LABELS cleanup FIXTURES_CLEANUP ${fixtureName}) set_tests_properties(${testName} PROPERTIES FIXTURES_REQUIRED ${fixtureName}) endfunction()