Computer Assisted Medical Intervention Tool Kit  version 5.2
CamiTKTestActionStateMachine.h
Go to the documentation of this file.
1 #-- declare outputfiles
8 set(CAMITK_TEST_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/command) # which command is run to test the exectable
9 set(CAMITK_TEST_COMMAND_RESULT_FILE ${CAMITK_TEST_OUTPUT_DIR}/command-result) # the exit result (0=success, 1=failure) of the tested command goes in this file
10 set(CAMITK_TEST_COMMAND_OUTPUT_FILE ${CAMITK_TEST_OUTPUT_DIR}/command-output) # the output of the tested command goes in this files
11 set(CAMITK_TEST_EXPECTED_FILE_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/test) # which command is run to diff the files
12 set(CAMITK_TEST_EXPECTED_FILE_OUTPUT_FILE ${CAMITK_TEST_OUTPUT_DIR}/test-output) # output of the diff cmake command
13 set(CAMITK_TEST_EXPECTED_FILE_RESULT_FILE ${CAMITK_TEST_OUTPUT_DIR}/test-result) # exit result (0=success, 1=failure)
14 
15 # clean/remove all output files and previous test attempt
16 unset(PREVIOUS_ATTEMPTS)
17 file(GLOB ALL_OUTPUT_FILES ${CAMITK_TEST_OUTPUT_DIR}/*)
18 foreach(POTENTIAL_DIR ${ALL_OUTPUT_FILES})
19  # only remove subdirectories (as they are previous attempts of running the test and should be asm output directory)
20  if(IS_DIRECTORY ${POTENTIAL_DIR})
21  list(APPEND PREVIOUS_ATTEMPTS ${POTENTIAL_DIR})
22  endif()
23 endforeach()
24 file(REMOVE_RECURSE ${CAMITK_TEST_COMMAND_FILE} ${CAMITK_TEST_COMMAND_RESULT_FILE} ${CAMITK_TEST_COMMAND_OUTPUT_FILE} ${CAMITK_TEST_EXPECTED_FILE_COMMAND_FILE} ${CAMITK_TEST_EXPECTED_FILE_OUTPUT_FILE} ${CAMITK_TEST_EXPECTED_FILE_RESULT_FILE} ${PREVIOUS_ATTEMPTS})
25 
26 #-- First run the executable
27 # Build the command file (so the user can retrieve/review it later)
28 file(WRITE ${CAMITK_TEST_COMMAND_FILE} "${CAMITK_TEST_COMMAND} ${CAMITK_TEST_COMMAND_ARG}")
29 
30 # expands all arguments
31 string(REPLACE " " "" CAMITK_TEST_COMMAND_ARG_LIST ${CAMITK_TEST_COMMAND_ARG})
32 
33 # execute the command and write the output to the command-output file, and the result to the command-result file
34 execute_process(
35  COMMAND ${CAMITK_TEST_COMMAND} ${CAMITK_TEST_COMMAND_ARG_LIST}
36  RESULT_VARIABLE CAMITK_TEST_COMMAND_RESULT
37  OUTPUT_VARIABLE CAMITK_TEST_COMMAND_OUTPUT
38  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
39 )
40 file(WRITE ${CAMITK_TEST_COMMAND_RESULT_FILE} ${CAMITK_TEST_COMMAND_RESULT})
41 file(WRITE ${CAMITK_TEST_COMMAND_OUTPUT_FILE} ${CAMITK_TEST_COMMAND_OUTPUT})
42 
43 # Transform back the "::" separated list of expected output to a classic "" separated string
44 string(REPLACE "::" "" CAMITK_TEST_EXPECTED_FILES_LIST ${CAMITK_TEST_EXPECTED_FILES})
45 
46 set(CAMITK_TEST_EXPECTED_FILE_OUTPUT "Action state machine regression test called with:\n - CAMITK_TEST_NAME=${CAMITK_TEST_NAME}\n - CAMITK_TEST_COMMAND=${CMAKE_BINARY_DIR}/bin/camitk-actionstatemachine\n - CAMITK_TEST_COMMAND_ARG=${CAMITK_TEST_COMMAND_ARG}\n - CAMITK_TEST_EXPECTED_FILES=${CAMITK_TEST_EXPECTED_FILES_LIST}\n - CAMITK_TEST_OUTPUT_DIR=${CAMITK_TEST_OUTPUT_DIR}\n")
47 set(CAMITK_TEST_EXPECTED_FILE_RESULT 0)
48 set(CAMITK_TEST_EXPECTED_FILE_COMMAND)
49 set(CAMITK_TEST_FAILED_FILE)
50 
51 #-- Then compare each expected output
52 foreach(EXPECTED_OUTPUT_FILE ${CAMITK_TEST_EXPECTED_FILES_LIST})
53  # find the same file in the asm output directory (*T*)
54  file(GLOB_RECURSE OUTPUT_FILE "${CAMITK_TEST_OUTPUT_DIR}/*T*/${EXPECTED_OUTPUT_FILE}")
55 
56  # compare the files
57  execute_process(
58  COMMAND ${CMAKE_COMMAND} -E compare_files ${OUTPUT_FILE} ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE}
59  RESULT_VARIABLE CAMITK_TEST_EXPECTED_FILE_RESULT_SINGLE
60  OUTPUT_VARIABLE CAMITK_TEST_EXPECTED_FILE_OUTPUT_SINGLE
61  OUTPUT_QUIET
62  ERROR_QUIET
63  )
64 
65  # update the output variables (command, output and result)
66  set(CAMITK_TEST_EXPECTED_FILE_OUTPUT
67  "${CAMITK_TEST_EXPECTED_FILE_OUTPUT}=================================================\nComparing ${OUTPUT_FILE} with ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE}\nOutput:\n${CAMITK_TEST_EXPECTED_FILE_OUTPUT_SINGLE}\n")
68  set(CAMITK_TEST_EXPECTED_FILE_COMMAND
69  "${CAMITK_TEST_EXPECTED_FILE_COMMAND}${CMAKE_COMMAND} -E compare_files ${OUTPUT_FILE} ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE}\n")
70  if(CAMITK_TEST_EXPECTED_FILE_RESULT_SINGLE)
71  set(CAMITK_TEST_EXPECTED_FILE_OUTPUT "${CAMITK_TEST_EXPECTED_FILE_OUTPUT}Result:\n${CAMITK_TEST_NAME}: comparing ${EXPECTED_OUTPUT_FILE} failed:\nOutput file ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE} is not the same as ${OUTPUT_FILE}\n\n" )
72  set(CAMITK_TEST_EXPECTED_FILE_RESULT 1)
73  set(CAMITK_TEST_FAILED_FILE "${CAMITK_TEST_FAILED_FILE}${EXPECTED_OUTPUT_FILE}\n")
74  else()
75  set(CAMITK_TEST_EXPECTED_FILE_OUTPUT "${CAMITK_TEST_EXPECTED_FILE_OUTPUT}Result:\n${CAMITK_TEST_NAME}: comparing ${EXPECTED_OUTPUT_FILE} success!\n\n" )
76  endif()
77 endforeach()
78 
79 #-- write everything to the test-command, test-output and test-result files
80 # These information detail the results for an easier diagnostic by a human
81 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_COMMAND_FILE} ${CAMITK_TEST_EXPECTED_FILE_COMMAND})
82 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_OUTPUT_FILE} ${CAMITK_TEST_EXPECTED_FILE_OUTPUT})
83 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_RESULT_FILE} ${CAMITK_TEST_EXPECTED_FILE_RESULT})
84 
85 #-- check result
86 if( CAMITK_TEST_EXPECTED_FILE_RESULT )
87  message(STATUS "[FAIL]")
88  message(FATAL_ERROR "${CAMITK_TEST_NAME}: (one or more) output file do(es) not match the corresponding expected file.\n${CAMITK_TEST_EXPECTED_FILE_OUTPUT}" )
89 else()
90  message(STATUS "[OK]")
91 endif()
test result unset(PREVIOUS_ATTEMPTS) file(GLOB ALL_OUTPUT_FILES $
Definition: CamiTKTestActionStateMachine.h:16
set(CAMITK_TEST_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/command) set(CAMITK_TEST_COMMAND_RESULT_FILE $
This CMake script run the action state machine and compare the output files to the expected files The...
Definition: CamiTKTestActionStateMachine.h:8