Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

CamiTKTestMacros.cmake 6.87 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# see also http://www.cmake.org/cmake/help/v2.8.8/cmake.html#section_PropertiesonTests

#!
#! \addtogroup CamiTKMacros
#!
#! camitk_init_test is a macro to initialize a group of test (for the same command)
#! It is used to initialize a series of tests for a given target.
#! Usually this is placed in the same CMakeLists.txt as the add_executable() cmake instruction or
#! camitk_application() macro
#!
#! It does few useful things:
#! - check if the target (name of the executable to run) is properly defined
#! - initialize test id (then automatically incremented in camitk_test_declare
#! - initialize test output directory
#!
#! Usage:
#! \code
#! camitk_init_test(target)
#! \endcode
#!
#! \param target (required)   The executable target to use during all the test series
#!
#! Example invocation:
#!
#! \code
#!
#! add_executable(myprogram)
#!
#! ...
#! # Start the test series for myprogram
#! camitk_init_test(myprogram)
#! camitk_add_test(...) # will be called myprogram_1
#! ...
#! camitk_add_test(...) # myprogram_2
#!
#! \endcode
#
#! @sa camitk_add_test
macro(camitk_init_test)
    parse_arguments(CAMITK_INIT_TEST
        ""  # possible lists
        ""
        ${ARGN}
    )

    set(CAMITK_TEST_ID "0")
    set(CAMITK_TEST_LIST "")

    # check for executable
    if(NOT CAMITK_INIT_TEST_DEFAULT_ARGS)
        message(FATAL_ERROR "Initializing test ${CAMITK_TEST_BASENAME} cannot proceed: please specify the target in brackets")
    else()
        if(NOT TARGET ${CAMITK_INIT_TEST_DEFAULT_ARGS})
        message(FATAL_ERROR "Initializing test ${CAMITK_TEST_BASENAME} cannot proceed: ${CAMITK_INIT_TEST_DEFAULT_ARGS} is not a proper target")
        endif()
    endif()

    set(CAMITK_TEST_BASENAME ${CAMITK_INIT_TEST_DEFAULT_ARGS})
    message(STATUS "Initializing test series for ${CAMITK_TEST_BASENAME}")

    set(CAMITK_INIT_TEST_EXECUTABLE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CAMITK_INIT_TEST_DEFAULT_ARGS})

endmacro()

#!
#! \addtogroup CamiTKMacros
#!
#! camitk_add_test is a macro to add a new test to the CTest infrastructure
#! It encapsulates CMake add_test and adds useful way of testing programs.
#! It cannot operate on its on, you need to call camitk_init_test first (and only once) before
#! calling camitk_add_test
#!
#! Details on the runned test can be found in ${CMAKE_BINARY_DIR}/Testing/Temporary/target#
#! where target is the executable name (see camitk_init_test() macro), and # is the test order number.
#!
#! Usage:
#! \code
#! camitk_add_test(EXECUTABLE_ARGS "arg1 arg2 arg3"
#!                 PASS_FILE pristineOutput
#!                 PASS_REGULAR_EXPRESSION regexp
#!                 FAIL_REGULAR_EXPRESSION regexp
#! )
#! \endcode
#!
#! \param EXECUTABLE_ARGS (optional)          The executable arguments (all in double quotes), typically each test will have different arguments. Can be empty
#! \param PASS_FILE (optional)                If specified the test to perform is to compare the output of the command to this file. It the output is the same, then the test is passed, otherwise it is failed.
#! \param PASS_REGULAR_EXPRESSION (optional)  This is equivalent to "PASS_REGULAR_EXPRESSION regexp" property for the test, see http://www.cmake.org/Wiki/CTest:FAQ#My_test_does_not_return_non-zero_on_failure._What_can_I_do.3F
#! \param FAIL_REGULAR_EXPRESSION             This is equivalent to "FAIL_REGULAR_EXPRESSION regexp" property for the test, see http://www.cmake.org/Wiki/CTest:FAQ#My_test_does_not_return_non-zero_on_failure._What_can_I_do.3F
#!
#! If no argument are given, it does the equivalent as add_test(name target)
#!
#! \note
#!   You can only choose one between nothing, PASS_FILE, PASS_REGULAR_EXPRESSION and FAIL_REGULAR_EXPRESSION
#!
#! Example invocation:
#!
#! \code
#!
#! add_executable(myprogram)
#!
#! ...
#! # Start the test series for myprogram
#! camitk_init_test(myprogram)
#! camitk_add_test(EXECUTABLE_ARGS "-a inputfile.xml -c" PASS_FILE pristineOuputToCompareTo.xml) # will be called myprogram1
#! ...
#! camitk_add_test(...) # myprogram2
#!
#! \endcode
#
#! @sa camitk_init_test
macro(camitk_add_test)
    parse_arguments(CAMITK_ADD_TEST
        "EXECUTABLE_ARGS;PASS_FILE;PASS_REGULAR_EXPRESSION;FAIL_REGULAR_EXPRESSION"  # possible lists
        "" # possible options
        ${ARGN}
    )

    math(EXPR CAMITK_TEST_ID "${CAMITK_TEST_ID} + 1")
    set(CAMITK_TEST_NAME "${CAMITK_TEST_BASENAME}${CAMITK_TEST_ID}")
    set(CAMITK_TEST_LIST ${CAMITK_TEST_LIST} ${CAMITK_TEST_NAME})
    message(STATUS "Configuring test ${CAMITK_TEST_NAME}")

    # create test output directory
    set(CAMITK_TEST_OUTPUT_DIR "${CMAKE_BINARY_DIR}/Testing/Temporary/${CAMITK_TEST_NAME}")
125
    message(STATUS "Creating test output dir ${CAMITK_TEST_OUTPUT_DIR}")
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory ${CAMITK_TEST_OUTPUT_DIR})

    # check which test is to be done
    if(CAMITK_ADD_TEST_PASS_FILE)
        add_test(NAME ${CAMITK_TEST_NAME}
                COMMAND ${CMAKE_COMMAND}
                                -DCAMITK_TEST_COMMAND=${CAMITK_INIT_TEST_EXECUTABLE}
                                -DCAMITK_TEST_COMMAND_ARG=${CAMITK_ADD_TEST_EXECUTABLE_ARGS}
                                -DCAMITK_TEST_PASS_FILE=${CAMITK_ADD_TEST_PASS_FILE}
                                -DCAMITK_TEST_OUTPUT_DIR=${CAMITK_TEST_OUTPUT_DIR}
                                -DCAMITK_TEST_NAME=${CAMITK_TEST_NAME}
                                -P ${CAMITK_SOURCE_DIR}/cmake/modules/CamiTKTestPassFile.cmake
        )
    else()
        # set output files for more advanced checking/debugging
        set(CAMITK_TEST_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/command) # which command is run to test the exectable
        # cleanup
        execute_process(COMMAND ${CMAKE_COMMAND} -E remove -f ${CAMITK_TEST_COMMAND_FILE} )
        file(WRITE ${CAMITK_TEST_COMMAND_FILE} "Test command: ${CAMITK_INIT_TEST_EXECUTABLE} ${CAMITK_ADD_TEST_EXECUTABLE_ARGS}\nPASS_REGULAR_EXPRESSION: '${CAMITK_ADD_TEST_PASS_REGULAR_EXPRESSION}'\n")

        if(CAMITK_ADD_TEST_EXECUTABLE_ARGS)
            # expands all arguments
            string(REPLACE " " ";" CAMITK_TEST_COMMAND_ARG_LIST ${CAMITK_ADD_TEST_EXECUTABLE_ARGS})
        else()
            set(CAMITK_TEST_COMMAND_ARG_LIST)
        endif()
        
        # add the simple test command
        add_test(NAME ${CAMITK_TEST_NAME}
                COMMAND ${CAMITK_INIT_TEST_EXECUTABLE} ${CAMITK_TEST_COMMAND_ARG_LIST}
        )

        # add properties if needed
        if(CAMITK_ADD_TEST_PASS_REGULAR_EXPRESSION)
        set_tests_properties(${CAMITK_TEST_NAME} PROPERTIES PASS_REGULAR_EXPRESSION ${CAMITK_ADD_TEST_PASS_REGULAR_EXPRESSION})
        else()
        if(CAMITK_ADD_TEST_FAIL_REGULAR_EXPRESSION)
            set_tests_properties(${CAMITK_TEST_NAME} PROPERTIES FAIL_REGULAR_EXPRESSION ${CAMITK_ADD_TEST_FAIL_REGULAR_EXPRESSION})
        endif()
        endif()
    endif()

168
169
170
    # set the label for tests
    set_tests_properties(${CAMITK_TEST_NAME} PROPERTIES LABELS ${CAMITK_TEST_BASENAME})

171
endmacro()