exec_test.cmake 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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
  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. #message("COMMAND: ${COMMAND}")
  9. #message("TEST_MODE: ${TEST_MODE}")
  10. #message("TEST_OUTPUT_FILE: ${TEST_OUTPUT_FILE}")
  11. #message("TEST_TEMP_FILE: ${TEST_TEMP_FILE}")
  12. string(REPLACE " " ";" COMMAND_LIST ${COMMAND})
  13. set(cmd COMMAND ${COMMAND_LIST} RESULT_VARIABLE CMD_RESULT)
  14. if("${TEST_MODE}" STREQUAL "COLLECT")
  15. list(APPEND cmd OUTPUT_FILE ${TEST_OUTPUT_FILE} ERROR_FILE ${TEST_OUTPUT_FILE})
  16. elseif("${TEST_MODE}" STREQUAL "COMPARE")
  17. list(APPEND cmd OUTPUT_FILE ${TEST_TEMP_FILE} ERROR_FILE ${TEST_TEMP_FILE})
  18. endif()
  19. execute_process(${cmd})
  20. # fix line endings
  21. if("${TEST_MODE}" STREQUAL "COLLECT" AND NOT CMAKE_HOST_UNIX)
  22. execute_process(COMMAND dos2unix ${TEST_OUTPUT_FILE})
  23. endif()
  24. if("${TEST_MODE}" STREQUAL "COMPARE")
  25. # fix line endings
  26. if(NOT CMAKE_HOST_UNIX)
  27. execute_process(COMMAND dos2unix ${TEST_TEMP_FILE})
  28. endif()
  29. if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.14.0")
  30. set(IGNORE_EOL --ignore-eol)
  31. endif()
  32. execute_process(COMMAND ${CMAKE_COMMAND} -E compare_files ${IGNORE_EOL} ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} RESULT_VARIABLE cmp_result)
  33. if(cmp_result)
  34. find_package(Git)
  35. if(GIT_FOUND)
  36. set(cmd ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE})
  37. execute_process(COMMAND ${GIT_EXECUTABLE} diff --no-index ${TEST_OUTPUT_FILE} ${TEST_TEMP_FILE} OUTPUT_VARIABLE DIFF)
  38. message("${DIFF}")
  39. endif()
  40. # file(READ ${TEST_OUTPUT_FILE} orig)
  41. # file(READ ${TEST_TEMP_FILE} temp)
  42. # message("==========================================================================")
  43. # message("== CONTENTS OF ${TEST_OUTPUT_FILE}")
  44. # message("==========================================================================")
  45. # message("${orig}")
  46. # message("==========================================================================")
  47. # message("== CONTENTS OF ${TEST_TEMP_FILE}")
  48. # message("==========================================================================")
  49. # message("${temp}")
  50. # message("==========================================================================")
  51. # message("== CONTENTS END")
  52. # message("==========================================================================")
  53. set(CMD_RESULT "Output is different from reference file!")
  54. endif()
  55. endif()
  56. if(CMD_RESULT)
  57. message(FATAL_ERROR "Running '${COMMAND}' ended with code '${CMD_RESULT}'")
  58. endif()