MatlabTestsRedirect.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. # This is an undocumented internal helper for the FindMatlab
  4. # module ``matlab_add_unit_test`` command.
  5. # Usage: cmake
  6. # -Dtest_timeout=180
  7. # -Doutput_directory=
  8. # -Dadditional_paths=""
  9. # -Dno_unittest_framework=""
  10. # -DMatlab_PROGRAM=matlab_exe_location
  11. # -DMatlab_ADDITIONAL_STARTUP_OPTIONS=""
  12. # -Dtest_name=name_of_the_test
  13. # -Dcustom_Matlab_test_command=""
  14. # -Dcmd_to_run_before_test=""
  15. # -Dunittest_file_to_run
  16. # -P FindMatlab_TestsRedirect.cmake
  17. set(Matlab_UNIT_TESTS_CMD -nosplash -nodesktop -nodisplay ${Matlab_ADDITIONAL_STARTUP_OPTIONS})
  18. if(WIN32)
  19. set(Matlab_UNIT_TESTS_CMD ${Matlab_UNIT_TESTS_CMD} -wait)
  20. endif()
  21. if(NOT test_timeout)
  22. set(test_timeout 180)
  23. endif()
  24. # If timeout is -1, then do not put a timeout on the execute_process
  25. if(test_timeout EQUAL -1)
  26. set(test_timeout "")
  27. else()
  28. set(test_timeout TIMEOUT ${test_timeout})
  29. endif()
  30. if(NOT cmd_to_run_before_test)
  31. set(cmd_to_run_before_test)
  32. endif()
  33. get_filename_component(unittest_file_directory "${unittest_file_to_run}" DIRECTORY)
  34. get_filename_component(unittest_file_to_run_name "${unittest_file_to_run}" NAME_WE)
  35. set(concat_string '${unittest_file_directory}')
  36. foreach(s IN LISTS additional_paths)
  37. if(NOT "${s}" STREQUAL "")
  38. string(APPEND concat_string ", '${s}'")
  39. endif()
  40. endforeach()
  41. if(custom_Matlab_test_command)
  42. set(unittest_to_run "${custom_Matlab_test_command}")
  43. else()
  44. set(unittest_to_run "runtests('${unittest_file_to_run_name}'), exit(max([ans(1,:).Failed]))")
  45. endif()
  46. if(no_unittest_framework)
  47. set(unittest_to_run "try, ${unittest_file_to_run_name}, catch err, disp('An exception has been thrown during the execution'), disp(err), disp(err.stack), exit(1), end, exit(0)")
  48. endif()
  49. set(Matlab_SCRIPT_TO_RUN
  50. "addpath(${concat_string}); ${cmd_to_run_before_test}; ${unittest_to_run}"
  51. )
  52. # if the working directory is not specified then default
  53. # to the output_directory because the log file will go there
  54. # if the working_directory is specified it will override the
  55. # output_directory
  56. if(NOT working_directory)
  57. set(working_directory "${output_directory}")
  58. endif()
  59. string(REPLACE "/" "_" log_file_name "${test_name}.log")
  60. set(Matlab_LOG_FILE "${working_directory}/${log_file_name}")
  61. set(devnull)
  62. if(UNIX)
  63. set(devnull INPUT_FILE /dev/null)
  64. elseif(WIN32)
  65. set(devnull INPUT_FILE NUL)
  66. endif()
  67. execute_process(
  68. # Do not use a full path to log file. Depend on the fact that the log file
  69. # is always going to go in the working_directory. This is because matlab
  70. # on unix is a shell script that does not handle spaces in the logfile path.
  71. COMMAND "${Matlab_PROGRAM}" ${Matlab_UNIT_TESTS_CMD} -logfile "${log_file_name}" -r "${Matlab_SCRIPT_TO_RUN}"
  72. RESULT_VARIABLE res
  73. ${test_timeout}
  74. OUTPUT_QUIET # we do not want the output twice
  75. WORKING_DIRECTORY "${working_directory}"
  76. ${devnull}
  77. )
  78. if(NOT EXISTS ${Matlab_LOG_FILE})
  79. message( FATAL_ERROR "[MATLAB] ERROR: cannot find the log file ${Matlab_LOG_FILE}")
  80. endif()
  81. # print the output in any case.
  82. file(READ ${Matlab_LOG_FILE} matlab_log_content)
  83. message("Matlab test ${name_of_the_test} output:\n${matlab_log_content}") # if we put FATAL_ERROR here, the file is indented.
  84. if(NOT (res EQUAL 0))
  85. message( FATAL_ERROR "[MATLAB] TEST FAILED Matlab returned ${res}" )
  86. endif()