2016-09-27 22:01:08 +03:00
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
2015-02-12 19:13:31 +03:00
# This is an undocumented internal helper for the FindMatlab
# module ``matlab_add_unit_test`` command.
# Usage: cmake
# -Dtest_timeout=180
# -Doutput_directory=
# -Dadditional_paths=""
# -Dno_unittest_framework=""
# -DMatlab_PROGRAM=matlab_exe_location
2016-10-28 20:40:58 +03:00
# -DMatlab_ADDITIONAL_STARTUP_OPTIONS=""
2015-02-12 19:13:31 +03:00
# -Dtest_name=name_of_the_test
2016-08-30 22:02:35 +03:00
# -Dcustom_Matlab_test_command=""
2015-02-12 19:13:31 +03:00
# -Dcmd_to_run_before_test=""
# -Dunittest_file_to_run
# -P FindMatlab_TestsRedirect.cmake
2016-10-28 20:40:58 +03:00
set ( Matlab_UNIT_TESTS_CMD -nosplash -nodesktop -nodisplay ${ Matlab_ADDITIONAL_STARTUP_OPTIONS } )
2015-02-12 19:13:31 +03:00
if ( WIN32 )
set ( Matlab_UNIT_TESTS_CMD ${ Matlab_UNIT_TESTS_CMD } -wait )
endif ( )
if ( NOT test_timeout )
set ( test_timeout 180 )
endif ( )
2016-08-30 22:02:35 +03:00
# If timeout is -1, then do not put a timeout on the execute_process
if ( test_timeout EQUAL -1 )
set ( test_timeout "" )
else ( )
set ( test_timeout TIMEOUT ${ test_timeout } )
endif ( )
2015-02-12 19:13:31 +03:00
if ( NOT cmd_to_run_before_test )
set ( cmd_to_run_before_test )
endif ( )
get_filename_component ( unittest_file_directory "${unittest_file_to_run}" DIRECTORY )
get_filename_component ( unittest_file_to_run_name "${unittest_file_to_run}" NAME_WE )
set ( concat_string ' ${ unittest_file_directory } ' )
foreach ( s IN LISTS additional_paths )
if ( NOT "${s}" STREQUAL "" )
2016-07-28 01:41:13 +03:00
string ( APPEND concat_string ", '${s}'" )
2015-02-12 19:13:31 +03:00
endif ( )
endforeach ( )
2016-08-30 22:02:35 +03:00
if ( custom_Matlab_test_command )
set ( unittest_to_run "${custom_Matlab_test_command}" )
else ( )
set ( unittest_to_run "runtests('${unittest_file_to_run_name}'), exit(max([ans(1,:).Failed]))" )
endif ( )
2015-02-12 19:13:31 +03:00
if ( no_unittest_framework )
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)" )
endif ( )
set ( Matlab_SCRIPT_TO_RUN
2016-08-30 22:02:35 +03:00
" addpath ( ${ concat_string } ) ; $ { c m d _ t o _ r u n _ b e f o r e _ t e s t } ; $ { u n i t t e s t _ t o _ r u n } "
2015-02-12 19:13:31 +03:00
)
2016-08-30 22:02:35 +03:00
# if the working directory is not specified then default
# to the output_directory because the log file will go there
# if the working_directory is specified it will override the
# output_directory
if ( NOT working_directory )
set ( working_directory "${output_directory}" )
endif ( )
2015-02-12 19:13:31 +03:00
2016-08-30 22:02:35 +03:00
string ( REPLACE "/" "_" log_file_name "${test_name}.log" )
set ( Matlab_LOG_FILE "${working_directory}/${log_file_name}" )
2015-02-12 19:13:31 +03:00
set ( devnull )
if ( UNIX )
set ( devnull INPUT_FILE /dev/null )
elseif ( WIN32 )
set ( devnull INPUT_FILE NUL )
endif ( )
execute_process (
2016-08-30 22:02:35 +03:00
# Do not use a full path to log file. Depend on the fact that the log file
# is always going to go in the working_directory. This is because matlab
# on unix is a shell script that does not handle spaces in the logfile path.
C O M M A N D " $ { M a t l a b _ P R O G R A M } " $ { M a t l a b _ U N I T _ T E S T S _ C M D } - l o g f i l e " $ { l o g _ f i l e _ n a m e } " - r " $ { M a t l a b _ S C R I P T _ T O _ R U N } "
2015-02-12 19:13:31 +03:00
R E S U L T _ V A R I A B L E r e s
2016-08-30 22:02:35 +03:00
$ { t e s t _ t i m e o u t }
2015-02-12 19:13:31 +03:00
O U T P U T _ Q U I E T # we do not want the output twice
2016-08-30 22:02:35 +03:00
W O R K I N G _ D I R E C T O R Y " $ { w o r k i n g _ d i r e c t o r y } "
2015-02-12 19:13:31 +03:00
$ { d e v n u l l }
)
if ( NOT EXISTS ${ Matlab_LOG_FILE } )
message ( FATAL_ERROR "[MATLAB] ERROR: cannot find the log file ${Matlab_LOG_FILE}" )
endif ( )
# print the output in any case.
file ( READ ${ Matlab_LOG_FILE } matlab_log_content )
message ( "Matlab test ${name_of_the_test} output:\n${matlab_log_content}" ) # if we put FATAL_ERROR here, the file is indented.
if ( NOT ( res EQUAL 0 ) )
2016-08-30 22:02:35 +03:00
message ( FATAL_ERROR "[MATLAB] TEST FAILED Matlab returned ${res}" )
2015-02-12 19:13:31 +03:00
endif ( )