Debian Codegraph @

BLOSC/config/cmake/runTest.cmake

raw
1 # runTest.cmake executes a command and captures the output in a file. File is then compared
2 # against a reference file. Exit status of command can also be compared.
3 cmake_policy(SET CMP0007 NEW)
4
5 # arguments checking
6 if (NOT TEST_PROGRAM)
7 message (FATAL_ERROR "Require TEST_PROGRAM to be defined")
8 endif (NOT TEST_PROGRAM)
9 #if (NOT TEST_ARGS)
10 # message (STATUS "Require TEST_ARGS to be defined")
11 #endif (NOT TEST_ARGS)
12 if (NOT TEST_FOLDER)
13 message ( FATAL_ERROR "Require TEST_FOLDER to be defined")
14 endif (NOT TEST_FOLDER)
15 if (NOT TEST_OUTPUT)
16 message (FATAL_ERROR "Require TEST_OUTPUT to be defined")
17 endif (NOT TEST_OUTPUT)
18 if (NOT TEST_EXPECT)
19 message (STATUS "Require TEST_EXPECT to be defined")
20 endif (NOT TEST_EXPECT)
21 #if (NOT TEST_FILTER)
22 # message (STATUS "Require TEST_FILTER to be defined")
23 #endif (NOT TEST_FILTER)
24 if (NOT TEST_SKIP_COMPARE AND NOT TEST_REFERENCE)
25 message (FATAL_ERROR "Require TEST_REFERENCE to be defined")
26 endif (NOT TEST_SKIP_COMPARE AND NOT TEST_REFERENCE)
27
28 if (NOT TEST_ERRREF)
29 set (ERROR_APPEND 1)
30 endif (NOT TEST_ERRREF)
31
32 message (STATUS "COMMAND: ${TEST_PROGRAM} ${TEST_ARGS}")
33
34 if (TEST_ENV_VAR)
35 set (ENV{${TEST_ENV_VAR}} "${TEST_ENV_VALUE}")
36 endif (TEST_ENV_VAR)
37
38 if (NOT TEST_INPUT)
39 # run the test program, capture the stdout/stderr and the result var
40 execute_process (
41 COMMAND ${TEST_PROGRAM} ${TEST_ARGS}
42 WORKING_DIRECTORY ${TEST_FOLDER}
43 RESULT_VARIABLE TEST_RESULT
44 OUTPUT_FILE ${TEST_OUTPUT}
45 ERROR_FILE ${TEST_OUTPUT}.err
46 OUTPUT_VARIABLE TEST_OUT
47 ERROR_VARIABLE TEST_ERROR
48 )
49 else (NOT TEST_INPUT)
50 # run the test program with stdin, capture the stdout/stderr and the result var
51 execute_process (
52 COMMAND ${TEST_PROGRAM} ${TEST_ARGS}
53 WORKING_DIRECTORY ${TEST_FOLDER}
54 RESULT_VARIABLE TEST_RESULT
55 INPUT_FILE ${TEST_INPUT}
56 OUTPUT_FILE ${TEST_OUTPUT}
57 ERROR_FILE ${TEST_OUTPUT}.err
58 OUTPUT_VARIABLE TEST_OUT
59 ERROR_VARIABLE TEST_ERROR
60 )
61 endif (NOT TEST_INPUT)
62
63 message (STATUS "COMMAND Result: ${TEST_RESULT}")
64
65 if (ERROR_APPEND)
66 file (READ ${TEST_FOLDER}/${TEST_OUTPUT}.err TEST_STREAM)
67 file (APPEND ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_STREAM}")
68 endif (ERROR_APPEND)
69
70 if (TEST_APPEND)
71 file (APPEND ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_APPEND} ${TEST_RESULT}\n")
72 endif (TEST_APPEND)
73
74 # if the return value is !=${TEST_EXPECT} bail out
75 if (NOT ${TEST_RESULT} STREQUAL ${TEST_EXPECT})
76 message ( FATAL_ERROR "Failed: Test program ${TEST_PROGRAM} exited != ${TEST_EXPECT}.\n${TEST_ERROR}")
77 endif (NOT ${TEST_RESULT} STREQUAL ${TEST_EXPECT})
78
79 message (STATUS "COMMAND Error: ${TEST_ERROR}")
80
81 if (TEST_MASK)
82 file (READ ${TEST_FOLDER}/${TEST_OUTPUT} TEST_STREAM)
83 string (REGEX REPLACE "Storage:[^\n]+\n" "Storage: <details removed for portability>\n" TEST_STREAM "${TEST_STREAM}")
84 file (WRITE ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_STREAM}")
85 endif (TEST_MASK)
86
87 if (TEST_MASK_MOD)
88 file (READ ${TEST_FOLDER}/${TEST_OUTPUT} TEST_STREAM)
89 string (REGEX REPLACE "Modified:[^\n]+\n" "Modified: XXXX-XX-XX XX:XX:XX XXX\n" TEST_STREAM "${TEST_STREAM}")
90 file (WRITE ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_STREAM}")
91 endif (TEST_MASK_MOD)
92
93 if (TEST_MASK_ERROR)
94 if (NOT TEST_ERRREF)
95 file (READ ${TEST_FOLDER}/${TEST_OUTPUT} TEST_STREAM)
96 else (NOT TEST_ERRREF)
97 file (READ ${TEST_FOLDER}/${TEST_OUTPUT}.err TEST_STREAM)
98 endif (NOT TEST_ERRREF)
99 string (REGEX REPLACE "thread [0-9]*:" "thread (IDs):" TEST_STREAM "${TEST_STREAM}")
100 string (REGEX REPLACE ": ([^\n]*)[.]c " ": (file name) " TEST_STREAM "${TEST_STREAM}")
101 string (REGEX REPLACE " line [0-9]*" " line (number)" TEST_STREAM "${TEST_STREAM}")
102 string (REGEX REPLACE "v[1-9]*[.][0-9]*[.]" "version (number)." TEST_STREAM "${TEST_STREAM}")
103 string (REGEX REPLACE "[1-9]*[.][0-9]*[.][0-9]*[^)]*" "version (number)" TEST_STREAM "${TEST_STREAM}")
104 string (REGEX REPLACE "H5Eget_auto[1-2]*" "H5Eget_auto(1 or 2)" TEST_STREAM "${TEST_STREAM}")
105 string (REGEX REPLACE "H5Eset_auto[1-2]*" "H5Eset_auto(1 or 2)" TEST_STREAM "${TEST_STREAM}")
106 if (NOT TEST_ERRREF)
107 file (WRITE ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_STREAM}")
108 else (NOT TEST_ERRREF)
109 file (WRITE ${TEST_FOLDER}/${TEST_OUTPUT}.err "${TEST_STREAM}")
110 endif (NOT TEST_ERRREF)
111 endif (TEST_MASK_ERROR)
112
113 if (TEST_FILTER)
114 file (READ ${TEST_FOLDER}/${TEST_OUTPUT} TEST_STREAM)
115 string (REGEX REPLACE "${TEST_FILTER}" "" TEST_STREAM "${TEST_STREAM}")
116 file (WRITE ${TEST_FOLDER}/${TEST_OUTPUT} "${TEST_STREAM}")
117 endif (TEST_FILTER)
118
119 if (NOT TEST_SKIP_COMPARE)
120 if (WIN32 AND NOT MINGW)
121 file (READ ${TEST_FOLDER}/${TEST_REFERENCE} TEST_STREAM)
122 file (WRITE ${TEST_FOLDER}/${TEST_REFERENCE} "${TEST_STREAM}")
123 endif (WIN32 AND NOT MINGW)
124
125 # now compare the output with the reference
126 execute_process (
127 COMMAND ${CMAKE_COMMAND} -E compare_files ${TEST_FOLDER}/${TEST_OUTPUT} ${TEST_FOLDER}/${TEST_REFERENCE}
128 RESULT_VARIABLE TEST_RESULT
129 )
130 if (NOT ${TEST_RESULT} STREQUAL 0)
131 set (TEST_RESULT 0)
132 file (STRINGS ${TEST_FOLDER}/${TEST_OUTPUT} test_act)
133 list (LENGTH test_act len_act)
134 file (STRINGS ${TEST_FOLDER}/${TEST_REFERENCE} test_ref)
135 list (LENGTH test_ref len_ref)
136 if (NOT ${len_act} STREQUAL "0")
137 math (EXPR _FP_LEN "${len_ref} - 1")
138 foreach (line RANGE 0 ${_FP_LEN})
139 list (GET test_act ${line} str_act)
140 list (GET test_ref ${line} str_ref)
141 if (NOT "${str_act}" STREQUAL "${str_ref}")
142 if (NOT "${str_act}" STREQUAL "")
143 set (TEST_RESULT 1)
144 message ("line = ${line}\n***ACTUAL: ${str_act}\n****REFER: ${str_ref}\n")
145 endif (NOT "${str_act}" STREQUAL "")
146 endif (NOT "${str_act}" STREQUAL "${str_ref}")
147 endforeach (line RANGE 0 ${_FP_LEN})
148 endif (NOT ${len_act} STREQUAL "0")
149 if (NOT ${len_act} STREQUAL ${len_ref})
150 set (TEST_RESULT 1)
151 endif (NOT ${len_act} STREQUAL ${len_ref})
152 endif (NOT ${TEST_RESULT} STREQUAL 0)
153
154 message (STATUS "COMPARE Result: ${TEST_RESULT}")
155
156 # again, if return value is !=0 scream and shout
157 if (NOT ${TEST_RESULT} STREQUAL 0)
158 message (FATAL_ERROR "Failed: The output of ${TEST_OUTPUT} did not match ${TEST_REFERENCE}")
159 endif (NOT ${TEST_RESULT} STREQUAL 0)
160
161 if (TEST_ERRREF)
162 if (WIN32 AND NOT MINGW)
163 file (READ ${TEST_FOLDER}/${TEST_ERRREF} TEST_STREAM)
164 file (WRITE ${TEST_FOLDER}/${TEST_ERRREF} "${TEST_STREAM}")
165 endif (WIN32 AND NOT MINGW)
166
167 # now compare the error output with the error reference
168 execute_process (
169 COMMAND ${CMAKE_COMMAND} -E compare_files ${TEST_FOLDER}/${TEST_OUTPUT}.err ${TEST_FOLDER}/${TEST_ERRREF}
170 RESULT_VARIABLE TEST_RESULT
171 )
172 if (NOT ${TEST_RESULT} STREQUAL 0)
173 set (TEST_RESULT 0)
174 file (STRINGS ${TEST_FOLDER}/${TEST_OUTPUT}.err test_act)
175 list (LENGTH test_act len_act)
176 file (STRINGS ${TEST_FOLDER}/${TEST_ERRREF} test_ref)
177 list (LENGTH test_ref len_ref)
178 math (EXPR _FP_LEN "${len_ref} - 1")
179 if (NOT ${len_act} STREQUAL "0")
180 math (EXPR _FP_LEN "${len_ref} - 1")
181 foreach (line RANGE 0 ${_FP_LEN})
182 list (GET test_act ${line} str_act)
183 list (GET test_ref ${line} str_ref)
184 if (NOT "${str_act}" STREQUAL "${str_ref}")
185 if (NOT "${str_act}" STREQUAL "")
186 set (TEST_RESULT 1)
187 message ("line = ${line}\n***ACTUAL: ${str_act}\n****REFER: ${str_ref}\n")
188 endif (NOT "${str_act}" STREQUAL "")
189 endif (NOT "${str_act}" STREQUAL "${str_ref}")
190 endforeach (line RANGE 0 ${_FP_LEN})
191 endif (NOT ${len_act} STREQUAL "0")
192 if (NOT ${len_act} STREQUAL ${len_ref})
193 set (TEST_RESULT 1)
194 endif (NOT ${len_act} STREQUAL ${len_ref})
195 endif (NOT ${TEST_RESULT} STREQUAL 0)
196
197 message (STATUS "COMPARE Result: ${TEST_RESULT}")
198
199 # again, if return value is !=0 scream and shout
200 if (NOT ${TEST_RESULT} STREQUAL 0)
201 message (FATAL_ERROR "Failed: The error output of ${TEST_OUTPUT}.err did not match ${TEST_ERRREF}")
202 endif (NOT ${TEST_RESULT} STREQUAL 0)
203 endif (TEST_ERRREF)
204 endif (NOT TEST_SKIP_COMPARE)
205
206 # everything went fine...
207 message ("Passed: The output of ${TEST_PROGRAM} matches ${TEST_REFERENCE}")