blob: 5dce94b65dbc52d6c3569e218352ee35f8ce97ec [file] [log] [blame]
onqtam4a655632016-05-26 14:20:52 +03001# Arguments:
2# - COMMAND: the command to run with all it's arguments
3# - TEST_MODE: NORMAL/VALGRIND/COLLECT/COMPARE
4# - TEST_OUTPUT_FILE: the file to/from which to write/read the output of the test
5# - TEST_TEMP_FILE: the temp file for the current test output used in COMPARE mode
6# To run something through this script use cmake like this:
7# cmake -DCOMMAND=path/to/my.exe -arg1 -arg2 -DTEST_MODE=VALGRIND -P path/to/exec_test.cmake
8
9#message("COMMAND: ${COMMAND}")
10#message("TEST_MODE: ${TEST_MODE}")
11#message("TEST_OUTPUT_FILE: ${TEST_OUTPUT_FILE}")
12#message("TEST_TEMP_FILE: ${TEST_TEMP_FILE}")
13
14string(REPLACE " " ";" COMMAND_LIST ${COMMAND})
15set(cmd COMMAND ${COMMAND_LIST} RESULT_VARIABLE CMD_RESULT)
16if("${TEST_MODE}" STREQUAL "COLLECT")
17 list(APPEND cmd OUTPUT_FILE ${TEST_OUTPUT_FILE} ERROR_FILE ${TEST_OUTPUT_FILE})
18elseif("${TEST_MODE}" STREQUAL "COMPARE")
19 list(APPEND cmd OUTPUT_FILE ${TEST_TEMP_FILE} ERROR_FILE ${TEST_TEMP_FILE})
20endif()
21
22execute_process(${cmd})
23
24# fix line endings
25if("${TEST_MODE}" STREQUAL "COLLECT" AND NOT CMAKE_HOST_UNIX)
26 execute_process(COMMAND dos2unix ${TEST_OUTPUT_FILE})
27endif()
28
29if("${TEST_MODE}" STREQUAL "COMPARE")
onqtam9f934f82016-08-02 12:42:19 +030030 # fix line endings
onqtam4a655632016-05-26 14:20:52 +030031 if(NOT CMAKE_HOST_UNIX)
onqtamc64b9482016-08-02 10:42:50 +030032 execute_process(COMMAND dos2unix ${TEST_TEMP_FILE})
onqtam4a655632016-05-26 14:20:52 +030033 endif()
34
35 execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} RESULT_VARIABLE cmp_result)
36
37 if(cmp_result)
onqtamd25f18e2019-03-21 11:55:45 +020038 find_package(Git)
39 if(GIT_FOUND)
40 set(cmd ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE})
41 execute_process(COMMAND ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} OUTPUT_VARIABLE DIFF)
42 MESSAGE("${DIFF}")
43 endif()
onqtam4a655632016-05-26 14:20:52 +030044
onqtamd25f18e2019-03-21 11:55:45 +020045 # file(READ ${TEST_OUTPUT_FILE} orig)
46 # file(READ ${TEST_TEMP_FILE} temp)
47
48 # message("==========================================================================")
49 # message("== CONTENTS OF ${TEST_OUTPUT_FILE}")
50 # message("==========================================================================")
51 # message("${orig}")
52 # message("==========================================================================")
53 # message("== CONTENTS OF ${TEST_TEMP_FILE}")
54 # message("==========================================================================")
55 # message("${temp}")
56 # message("==========================================================================")
57 # message("== CONTENTS END")
58 # message("==========================================================================")
59
onqtam4a655632016-05-26 14:20:52 +030060 set(CMD_RESULT "Output is different from reference file!")
61 endif()
62endif()
63
64if(CMD_RESULT)
65 message(FATAL_ERROR "Running '${COMMAND}' ended with code '${CMD_RESULT}'")
66endif()