include_guard() include(JPPAddExecutable) include(JPPAddTestTemporaryDir) include(JPPNameTarget) # # jpp_add_test(NAME testName COMMAND exe ...) add a test, that can be : # # - an existing target (if exe is a target already registered) # - an external executable or script (if exe is not an existing target) # - a new executable (if both COMMAND and SOURCES are given) # # If BUILD_TESTING if not set this function does nothing at all. # # Description of the arguments : # # usally required : # # * NAME: the test name # * COMMAND: the actual test # * COMMAND_LINE_ARGS : the arguments to pass to the executable, if any # * LABELS : labels attached to the test, that can then be used to filter # which tests are executed with the `ctest -L` command # * WORKING_DIRECTORY: the directory in which the test will be ran # # needed when the test is for an executable that does not yet exist : # # * SOURCES : same meaning as for jpp_add_executable. Note that in this case # COMMAND is the name of the target executable. # * LINK_LIBRARIES : same meaning as for jpp_add_executable # # might be needed to tweak the test environment and/or behavior : # # * USE_EXAMPLE: if the test is using executables from the examples hierarchy # * USE_TEMPORARY_DIR: if the test is expected to create temporary file(s) # this option will ensure the test gets its own temporary directory # that will be created before the test run and deleted after the test ran # (this in turn makes it easier/possible to run tests in parallel) # * ENVIRONMENT: extra environment needed by the test to run properly # * ENVIRONMENT_MODIFICATION: modification of environment needed by the test # to run properly # * TIMEOUT : the number of seconds allowed for the test to run. Past this time # failure is assumed. # # rarely needed/for special cases : # # * CONFIGURATIONS : the test will only be ran for those named configurations # * TARGETVARNAME : same meaning as for jpp_add_executable # function(jpp_add_test) if(NOT BUILD_TESTING) return() endif() cmake_parse_arguments( PARSE_ARGV 0 A "USE_EXAMPLES;USE_TEMPORARY_DIR" "TIMEOUT;WORKING_DIRECTORY;NAME;TARGETVARNAME;COMMAND" "SOURCES;LINK_LIBRARIES;COMMAND_LINE_ARGS;LABELS;CONFIGURATIONS;ENVIRONMENT;ENVIRONMENT_MODIFICATION" ) if(A_UNPARSED_ARGUMENTS) message(FATAL_ERROR "Unexpected unparsed arguments: ${A_UNPARSED_ARGUMENTS}") endif() if(NOT A_NAME) message(FATAL_ERROR "Must give a NAME to the test") endif() set(command "") if(A_SOURCES) # if we have some sources, we assume we need to create # an executable that will be used as the test command # build a name for the executable from the first source file list(GET A_SOURCES 0 firstSource) get_filename_component(src ${firstSource} ABSOLUTE) file(RELATIVE_PATH name ${CMAKE_CURRENT_SOURCE_DIR} ${src}) cmake_path(GET name STEM exeName) # create the executable jpp_add_executable(${exeName} SOURCES ${A_SOURCES} LINK_LIBRARIES ${A_LINK_LIBRARIES} TARGETVARNAME targetName) if(A_TARGETVARNAME) set(${A_TARGETVARNAME} ${targetName} PARENT_SCOPE) endif() if(NOT A_COMMAND) set(command ${targetName}) endif() else() if(NOT A_COMMAND) message(FATAL_ERROR "Must give a COMMAND (or SOURCES to compile) for the test") endif() endif() if(NOT command) jpp_name_target(${A_COMMAND} IS_EXE NAME exe) if(TARGET ${exe}) # if the command is a target we're building then we use that binary as command set(command $) else() # use the given command # if it's not an absolute path name, prepend it with current source dir cmake_path(IS_ABSOLUTE A_COMMAND isAbsPath) if(NOT isAbsPath) set(command "${CMAKE_CURRENT_SOURCE_DIR}/${A_COMMAND}") else() set(command ${A_COMMAND}) endif() endif() endif() # and add some default environment to get the current scripts working w/o # touching them (too much at least) # FIXME: the LD_LIBRARY_PATH is just there because otherwise JEEP::getPath # might fail if(NOT A_ENVIRONMENT) list(APPEND A_ENVIRONMENT JPP_DIR=${KM3NET_JPP_STAGE_DIR}) list(APPEND A_ENVIRONMENT JPP_BIN=${KM3NET_JPP_STAGE_DIR}/${CMAKE_INSTALL_BINDIR}) list(APPEND A_ENVIRONMENT JPP_DATA=${KM3NET_JPP_STAGE_DIR}/${CMAKE_INSTALL_DATADIR}) list(APPEND A_ENVIRONMENT LD_LIBRARY_PATH=${KM3NET_JPP_STAGE_DIR}/${CMAKE_INSTALL_LIBDIR}) endif() if(NOT A_ENVIRONMENT_MODIFICATION) # Some tests rely on some executables which name are shared # by more than one executable (typically, in the original Makefile build # in-source, one in software/xxx dir and one in examples/xxx) # So we try to adapt to this state of affairs by tweaking # the PATH depending on the USE_EXAMPLES parameter jpp_get_dir_info(PACKAGE package) if(A_USE_EXAMPLES) set(A_ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${KM3NET_JPP_STAGE_DIR}/examples/${package}:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") else() set(A_ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") endif() endif() add_test(NAME ${A_NAME} COMMAND ${command} ${A_COMMAND_LINE_ARGS} WORKING_DIRECTORY ${A_WORKING_DIRECTORY} CONFIGURATIONS ${A_CONFIGURATIONS} ) set_property(TEST ${A_NAME} PROPERTY LABELS ${A_LABELS}) if(A_ENVIRONMENT_MODIFICATION) set_property(TEST ${A_NAME} PROPERTY ENVIRONMENT_MODIFICATION ${A_ENVIRONMENT_MODIFICATION}) endif() if(A_TIMEOUT) set_property(TEST ${A_NAME} PROPERTY TIMEOUT ${A_TIMEOUT}) endif() if(A_ENVIRONMENT) set_property(TEST ${A_NAME} PROPERTY ENVIRONMENT ${A_ENVIRONMENT}) endif() if(A_USE_TEMPORARY_DIR) jpp_add_test_temporary_dir(${A_NAME}) endif() endfunction()