onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 1 | # 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 |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 5 | # - TEST_TEMP_FILE: the temp file for the current test output used in COMPARE mode |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 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 | |
| 14 | string(REPLACE " " ";" COMMAND_LIST ${COMMAND}) |
| 15 | set(cmd COMMAND ${COMMAND_LIST} RESULT_VARIABLE CMD_RESULT) |
| 16 | if("${TEST_MODE}" STREQUAL "COLLECT") |
| 17 | list(APPEND cmd OUTPUT_FILE ${TEST_OUTPUT_FILE} ERROR_FILE ${TEST_OUTPUT_FILE}) |
| 18 | elseif("${TEST_MODE}" STREQUAL "COMPARE") |
| 19 | list(APPEND cmd OUTPUT_FILE ${TEST_TEMP_FILE} ERROR_FILE ${TEST_TEMP_FILE}) |
| 20 | endif() |
| 21 | |
| 22 | execute_process(${cmd}) |
| 23 | |
| 24 | # fix line endings |
| 25 | if("${TEST_MODE}" STREQUAL "COLLECT" AND NOT CMAKE_HOST_UNIX) |
| 26 | execute_process(COMMAND dos2unix ${TEST_OUTPUT_FILE}) |
| 27 | endif() |
| 28 | |
| 29 | if("${TEST_MODE}" STREQUAL "COMPARE") |
onqtam | 9f934f8 | 2016-08-02 12:42:19 +0300 | [diff] [blame] | 30 | # fix line endings |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 31 | if(NOT CMAKE_HOST_UNIX) |
onqtam | c64b948 | 2016-08-02 10:42:50 +0300 | [diff] [blame] | 32 | execute_process(COMMAND dos2unix ${TEST_TEMP_FILE}) |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 33 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 34 | |
Daan De Meyer | f972602 | 2019-10-10 22:16:49 +0200 | [diff] [blame^] | 35 | if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.14.0") |
| 36 | set(IGNORE_EOL --ignore-eol) |
| 37 | endif() |
| 38 | |
| 39 | execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files ${IGNORE_EOL} ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} RESULT_VARIABLE cmp_result) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 40 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 41 | if(cmp_result) |
onqtam | d25f18e | 2019-03-21 11:55:45 +0200 | [diff] [blame] | 42 | find_package(Git) |
| 43 | if(GIT_FOUND) |
| 44 | set(cmd ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE}) |
| 45 | execute_process(COMMAND ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} OUTPUT_VARIABLE DIFF) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 46 | message("${DIFF}") |
onqtam | d25f18e | 2019-03-21 11:55:45 +0200 | [diff] [blame] | 47 | endif() |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 48 | |
onqtam | d25f18e | 2019-03-21 11:55:45 +0200 | [diff] [blame] | 49 | # file(READ ${TEST_OUTPUT_FILE} orig) |
| 50 | # file(READ ${TEST_TEMP_FILE} temp) |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 51 | |
onqtam | d25f18e | 2019-03-21 11:55:45 +0200 | [diff] [blame] | 52 | # message("==========================================================================") |
| 53 | # message("== CONTENTS OF ${TEST_OUTPUT_FILE}") |
| 54 | # message("==========================================================================") |
| 55 | # message("${orig}") |
| 56 | # message("==========================================================================") |
| 57 | # message("== CONTENTS OF ${TEST_TEMP_FILE}") |
| 58 | # message("==========================================================================") |
| 59 | # message("${temp}") |
| 60 | # message("==========================================================================") |
| 61 | # message("== CONTENTS END") |
| 62 | # message("==========================================================================") |
Claus Klein | 6f1241c | 2019-08-11 15:35:44 +0200 | [diff] [blame] | 63 | |
onqtam | 4a65563 | 2016-05-26 14:20:52 +0300 | [diff] [blame] | 64 | set(CMD_RESULT "Output is different from reference file!") |
| 65 | endif() |
| 66 | endif() |
| 67 | |
| 68 | if(CMD_RESULT) |
| 69 | message(FATAL_ERROR "Running '${COMMAND}' ended with code '${CMD_RESULT}'") |
| 70 | endif() |