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