Computer Assited Medical Intervention Tool Kit  version 4.1
CamiTKTestActionStateMachine.h
Go to the documentation of this file.
1 
34 #-- declare outputfiles
35 set(CAMITK_TEST_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/command) # which command is run to test the exectable
36 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
37 set(CAMITK_TEST_COMMAND_OUTPUT_FILE ${CAMITK_TEST_OUTPUT_DIR}/command-output) # the output of the tested command goes in this files
38 set(CAMITK_TEST_EXPECTED_FILE_COMMAND_FILE ${CAMITK_TEST_OUTPUT_DIR}/test) # which command is run to diff the files
39 set(CAMITK_TEST_EXPECTED_FILE_OUTPUT_FILE ${CAMITK_TEST_OUTPUT_DIR}/test-output) # output of the diff cmake command
40 set(CAMITK_TEST_EXPECTED_FILE_RESULT_FILE ${CAMITK_TEST_OUTPUT_DIR}/test-result) # exit result (0=success, 1=failure)
41 
42 # clean/remove all output files and previous test attempt
43 unset(PREVIOUS_ATTEMPTS)
44 file(GLOB ALL_OUTPUT_FILES ${CAMITK_TEST_OUTPUT_DIR}/*)
45 foreach(POTENTIAL_DIR ${ALL_OUTPUT_FILES})
46  # only remove subdirectories (as they are previous attempts of running the test and should be asm output directory)
47  if(IS_DIRECTORY ${POTENTIAL_DIR})
48  list(APPEND PREVIOUS_ATTEMPTS ${POTENTIAL_DIR})
49  endif()
50 endforeach()
51 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})
52 
53 #-- First run the executable
54 # Build the command file (so the user can retrieve/review it later)
55 file(WRITE ${CAMITK_TEST_COMMAND_FILE} "${CAMITK_TEST_COMMAND} ${CAMITK_TEST_COMMAND_ARG}")
56 
57 # expands all arguments
58 string(REPLACE " " "" CAMITK_TEST_COMMAND_ARG_LIST ${CAMITK_TEST_COMMAND_ARG})
59 
60 # execute the command and write the output to the command-output file, and the result to the command-result file
61 execute_process(
62  COMMAND ${CAMITK_TEST_COMMAND} ${CAMITK_TEST_COMMAND_ARG_LIST}
63  RESULT_VARIABLE CAMITK_TEST_COMMAND_RESULT
64  OUTPUT_VARIABLE CAMITK_TEST_COMMAND_OUTPUT
65  WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
66 )
67 file(WRITE ${CAMITK_TEST_COMMAND_RESULT_FILE} ${CAMITK_TEST_COMMAND_RESULT})
68 file(WRITE ${CAMITK_TEST_COMMAND_OUTPUT_FILE} ${CAMITK_TEST_COMMAND_OUTPUT})
69 
70 # Transform back the "::" separated list of expected output to a classic "" separated string
71 string(REPLACE "::" "" CAMITK_TEST_EXPECTED_FILES_LIST ${CAMITK_TEST_EXPECTED_FILES})
72 
73 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")
74 set(CAMITK_TEST_EXPECTED_FILE_RESULT 0)
75 set(CAMITK_TEST_EXPECTED_FILE_COMMAND)
76 set(CAMITK_TEST_FAILED_FILE)
77 
78 #-- Then compare each expected output
79 foreach(EXPECTED_OUTPUT_FILE ${CAMITK_TEST_EXPECTED_FILES_LIST})
80  # find the same file in the asm output directory (*T*)
81  file(GLOB_RECURSE OUTPUT_FILE "${CAMITK_TEST_OUTPUT_DIR}/*T*/${EXPECTED_OUTPUT_FILE}")
82 
83  # compare the files
84  execute_process(
85  COMMAND ${CMAKE_COMMAND} -E compare_files ${OUTPUT_FILE} ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE}
86  RESULT_VARIABLE CAMITK_TEST_EXPECTED_FILE_RESULT_SINGLE
87  OUTPUT_VARIABLE CAMITK_TEST_EXPECTED_FILE_OUTPUT_SINGLE
88  OUTPUT_QUIET
89  ERROR_QUIET
90  )
91 
92  # update the output variables (command, output and result)
93  set(CAMITK_TEST_EXPECTED_FILE_OUTPUT
94  "${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")
95  set(CAMITK_TEST_EXPECTED_FILE_COMMAND
96  "${CAMITK_TEST_EXPECTED_FILE_COMMAND}${CMAKE_COMMAND} -E compare_files ${OUTPUT_FILE} ${CAMITK_TEST_OUTPUT_DIR}/${EXPECTED_OUTPUT_FILE}\n")
97  if(CAMITK_TEST_EXPECTED_FILE_RESULT_SINGLE)
98  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" )
99  set(CAMITK_TEST_EXPECTED_FILE_RESULT 1)
100  set(CAMITK_TEST_FAILED_FILE "${CAMITK_TEST_FAILED_FILE}${EXPECTED_OUTPUT_FILE}\n")
101  else()
102  set(CAMITK_TEST_EXPECTED_FILE_OUTPUT "${CAMITK_TEST_EXPECTED_FILE_OUTPUT}Result:\n${CAMITK_TEST_NAME}: comparing ${EXPECTED_OUTPUT_FILE} success!\n\n" )
103  endif()
104 endforeach()
105 
106 #-- write everything to the test-command, test-output and test-result files
107 # These information detail the results for an easier diagnostic by a human
108 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_COMMAND_FILE} ${CAMITK_TEST_EXPECTED_FILE_COMMAND})
109 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_OUTPUT_FILE} ${CAMITK_TEST_EXPECTED_FILE_OUTPUT})
110 file(WRITE ${CAMITK_TEST_EXPECTED_FILE_RESULT_FILE} ${CAMITK_TEST_EXPECTED_FILE_RESULT})
111 
112 #-- check result
113 if( CAMITK_TEST_EXPECTED_FILE_RESULT )
114  message("[FAIL]")
115  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}" )
116 else()
117  message("[OK]")
118 endif()
test result unset(PREVIOUS_ATTEMPTS) file(GLOB ALL_OUTPUT_FILES $
Definition: CamiTKTestActionStateMachine.h:43