MatlabTestsRedirect.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # -Dmaut_BATCH_OPTION="-batch"
  17. # -P FindMatlab_TestsRedirect.cmake
  18. set(Matlab_UNIT_TESTS_CMD -nosplash -nodesktop -nodisplay ${Matlab_ADDITIONAL_STARTUP_OPTIONS})
  19. if(WIN32 AND maut_BATCH_OPTION STREQUAL "-r")
  20. list(APPEND Matlab_UNIT_TESTS_CMD -wait)
  21. endif()
  22. if(NOT test_timeout)
  23. set(test_timeout 180)
  24. endif()
  25. # If timeout is -1, then do not put a timeout on the execute_process
  26. if(test_timeout EQUAL -1)
  27. set(test_timeout "")
  28. else()
  29. set(test_timeout TIMEOUT ${test_timeout})
  30. endif()
  31. if(NOT cmd_to_run_before_test)
  32. set(cmd_to_run_before_test)
  33. endif()
  34. get_filename_component(unittest_file_directory "${unittest_file_to_run}" DIRECTORY)
  35. get_filename_component(unittest_file_to_run_name "${unittest_file_to_run}" NAME_WE)
  36. set(concat_string '${unittest_file_directory}')
  37. foreach(s IN LISTS additional_paths)
  38. if(NOT "${s}" STREQUAL "")
  39. string(APPEND concat_string ", '${s}'")
  40. endif()
  41. endforeach()
  42. if(custom_Matlab_test_command)
  43. set(unittest_to_run "${custom_Matlab_test_command}")
  44. else()
  45. set(unittest_to_run "runtests('${unittest_file_to_run_name}'), exit(max([ans(1,:).Failed]))")
  46. endif()
  47. if(no_unittest_framework)
  48. set(unittest_to_run "${unittest_file_to_run_name}")
  49. endif()
  50. set(command_to_run "try, ${unittest_to_run}, catch err, disp('An exception has been thrown during the execution'), disp(err), disp(err.stack), exit(1), end, exit(0)")
  51. set(Matlab_SCRIPT_TO_RUN
  52. "addpath(${concat_string}); ${cmd_to_run_before_test}; ${command_to_run}"
  53. )
  54. # if the working directory is not specified then default
  55. # to the output_directory because the log file will go there
  56. # if the working_directory is specified it will override the
  57. # output_directory
  58. if(NOT working_directory)
  59. set(working_directory "${output_directory}")
  60. endif()
  61. string(REPLACE "/" "_" log_file_name "${test_name}.log")
  62. set(Matlab_LOG_FILE "${working_directory}/${log_file_name}")
  63. set(devnull)
  64. if(UNIX)
  65. set(devnull INPUT_FILE /dev/null)
  66. elseif(WIN32)
  67. set(devnull INPUT_FILE NUL)
  68. endif()
  69. execute_process(
  70. # Do not use a full path to log file. Depend on the fact that the log file
  71. # is always going to go in the working_directory. This is because matlab
  72. # on unix is a shell script that does not handle spaces in the logfile path.
  73. COMMAND "${Matlab_PROGRAM}" ${Matlab_UNIT_TESTS_CMD} -logfile "${log_file_name}" "${maut_BATCH_OPTION}" "${Matlab_SCRIPT_TO_RUN}"
  74. RESULT_VARIABLE res
  75. ${test_timeout}
  76. OUTPUT_QUIET # we do not want the output twice
  77. WORKING_DIRECTORY "${working_directory}"
  78. ${devnull}
  79. )
  80. if(NOT EXISTS ${Matlab_LOG_FILE})
  81. message( FATAL_ERROR "[MATLAB] ERROR: cannot find the log file ${Matlab_LOG_FILE}")
  82. endif()
  83. # print the output in any case.
  84. file(READ ${Matlab_LOG_FILE} matlab_log_content)
  85. message("Matlab test ${name_of_the_test} output:\n${matlab_log_content}") # if we put FATAL_ERROR here, the file is indented.
  86. if(NOT (res EQUAL 0))
  87. message( FATAL_ERROR "[MATLAB] TEST FAILED Matlab returned ${res}" )
  88. endif()