Skip to content

Commit

Permalink
Add support for testing Python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
daljit46 committed Jul 25, 2023
1 parent a3108e8 commit 9da6c50
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 116 deletions.
58 changes: 58 additions & 0 deletions cmake/BashTests.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# A function that adds a bash test for each line in a given file
function(add_bash_tests)
set(singleValueArgs FILE_PATH PREFIX WORKING_DIRECTORY)
set(multiValueArgs EXEC_DIRECTORIES)
cmake_parse_arguments(
ARG
""
"${singleValueArgs}"
"${multiValueArgs}"
${ARGN}
)

set(file_path ${ARG_FILE_PATH})
set(prefix ${ARG_PREFIX})
set(working_directory ${ARG_WORKING_DIRECTORY})
set(exec_directories ${ARG_EXEC_DIRECTORIES})

get_filename_component(file_name ${file_path} NAME_WE)

# Add a custom target for IDEs to pickup the test script
add_custom_target(test_${file_name} SOURCES ${file_path})

# Add test that cleans up temporary files
add_test(
NAME ${prefix}_${file_name}_cleanup
COMMAND ${BASH} -c "rm -rf ${DATA_DIR}/tmp* ${DATA_DIR}/*-tmp-*"
WORKING_DIRECTORY ${DATA_DIR}
)
set_tests_properties(${prefix}_${file_name}_cleanup PROPERTIES FIXTURES_SETUP ${file_name}_cleanup)

file(STRINGS ${file_path} FILE_CONTENTS)
list(LENGTH FILE_CONTENTS FILE_CONTENTS_LENGTH)
math(EXPR MAX_LINE_NUM "${FILE_CONTENTS_LENGTH} - 1")


set(EXEC_DIR_PATHS "${exec_directories}")
string(REPLACE ";" ":" EXEC_DIR_PATHS "${EXEC_DIR_PATHS}")

foreach(line_num RANGE ${MAX_LINE_NUM})
list(GET FILE_CONTENTS ${line_num} line)

set(test_name ${file_name})
if(${FILE_CONTENTS_LENGTH} GREATER 1)
math(EXPR suffix "${line_num} + 1")
set(test_name "${file_name}_${suffix}")
endif()

add_test(
NAME ${prefix}_${test_name}
COMMAND ${BASH} -c "export PATH=${EXEC_DIR_PATHS}:$PATH;${line}"
WORKING_DIRECTORY ${working_directory}
)
set_tests_properties(${prefix}_${test_name}
PROPERTIES FIXTURES_REQUIRED ${file_name}_cleanup
)
message(VERBOSE "Add bash tests commands for ${file_name}: ${line}")
endforeach()
endfunction()
1 change: 1 addition & 0 deletions testing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_subdirectory(binaries)
add_subdirectory(lib)
add_subdirectory(scripts)
add_subdirectory(tools)
add_subdirectory(unit_tests)
204 changes: 88 additions & 116 deletions testing/binaries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,123 +2,95 @@ find_program(BASH bash)

set(DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)

# Add test that cleans up temporary files
add_test(
NAME bin_cleanup
COMMAND ${BASH} -c "rm -rf ${DATA_DIR}/tmp* ${DATA_DIR}/*-tmp-*"
WORKING_DIRECTORY ${DATA_DIR}
)
set_tests_properties(bin_cleanup PROPERTIES FIXTURES_SETUP cleanup)

function(add_binary_tests file_path)
get_filename_component(file_name ${file_path} NAME_WE)
include(BashTests)
function(add_bash_binary_tests file_path)
set(EXEC_DIRS ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND EXEC_DIRS ${PROJECT_BINARY_DIR}/bin)
list(APPEND EXEC_DIRS ${PROJECT_BINARY_DIR}/testing/tools)

# Add a custom target for IDEs to pickup the test script
add_custom_target(test_${file_name} SOURCES ${file_path})

file(STRINGS ${file_path} FILE_CONTENTS)
list(LENGTH FILE_CONTENTS FILE_CONTENTS_LENGTH)
math(EXPR MAX_LINE_NUM "${FILE_CONTENTS_LENGTH} - 1")

foreach(line_num RANGE ${MAX_LINE_NUM})
list(GET FILE_CONTENTS ${line_num} line)

set(test_name ${file_name})
if(${FILE_CONTENTS_LENGTH} GREATER 1)
math(EXPR suffix "${line_num} + 1")
set(test_name "${file_name}_${suffix}")
endif()

set(EXEC_DIR1 ${CMAKE_CURRENT_BINARY_DIR})
set(EXEC_DIR2 ${PROJECT_BINARY_DIR}/bin)
set(EXEC_DIR3 ${PROJECT_BINARY_DIR}/testing/tools)

add_test(
NAME bin_${test_name}
COMMAND ${BASH} -c "export PATH=${EXEC_DIR1}:${EXEC_DIR2}:${EXEC_DIR3}:$PATH;${line}"
WORKING_DIRECTORY ${DATA_DIR}
)
set_tests_properties(bin_${test_name}
PROPERTIES FIXTURES_REQUIRED cleanup
)
message(VERBOSE "Add binary test command for ${file_name}: ${line}")
endforeach()
add_bash_tests(
FILE_PATH "${file_path}"
PREFIX "bin"
WORKING_DIRECTORY ${DATA_DIR}
EXEC_DIRECTORIES "${EXEC_DIRS}"
)
endfunction()

add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5tt2gmwmi)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5tt2vis)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5ttedit)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/amp2sh)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/connectomeedit)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dirgen)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2adc)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2fod)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2tensor)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwidenoise)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwiextract)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2peaks)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2sh)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2tsf)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2voxel)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcfestats)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelconnectivity)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcorrespondence)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcrop)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelfilter)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelreorient)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fod2dec)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fod2fixel)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/label2colour)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/label2mesh)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/labelconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/maskfilter)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mesh2voxel)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/meshconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcalc)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcat)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrclusterstats)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcolour)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrdegibbs)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrfilter)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrgrid)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrhistmatch)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrhistogram)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrmath)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrregister)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrstats)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrthreshold)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrtransform)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mtnormalise)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/peaks2amp)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/peaks2fixel)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2amp)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2peaks)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2power)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2response)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/shbasis)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/shconv)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tck2connectome)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckedit)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckgen)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckmap)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckresample)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksample)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksift)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksift2)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcktransform)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tensor2metric)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformcalc)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformcompose)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformconvert)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfdivide)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfmult)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfsmooth)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfthreshold)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/vectorstats)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/voxel2fixel)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warp2metric)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warpcorrect)
add_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warpinit)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5tt2gmwmi)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5tt2vis)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5ttedit)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/amp2sh)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/connectomeedit)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dirgen)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2adc)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2fod)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2tensor)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwidenoise)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwiextract)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2peaks)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2sh)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2tsf)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixel2voxel)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcfestats)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelconnectivity)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcorrespondence)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelcrop)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelfilter)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fixelreorient)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fod2dec)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/fod2fixel)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/label2colour)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/label2mesh)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/labelconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/maskfilter)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mesh2voxel)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/meshconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcalc)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcat)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrclusterstats)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrcolour)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrdegibbs)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrfilter)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrgrid)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrhistmatch)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrhistogram)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrmath)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrregister)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrstats)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrthreshold)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mrtransform)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/mtnormalise)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/peaks2amp)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/peaks2fixel)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2amp)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2peaks)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2power)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/sh2response)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/shbasis)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/shconv)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tck2connectome)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckedit)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckgen)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckmap)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tckresample)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksample)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksift)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcksift2)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tcktransform)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tensor2metric)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformcalc)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformcompose)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/transformconvert)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfdivide)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfmult)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfsmooth)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/tsfthreshold)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/vectorstats)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/voxel2fixel)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warp2metric)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warpcorrect)
add_bash_binary_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/warpinit)
31 changes: 31 additions & 0 deletions testing/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
find_program(BASH bash)

set(DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/data)

include(BashTests)
function(add_bash_script_tests file_path)
set(EXEC_DIRS ${CMAKE_CURRENT_BINARY_DIR})
list(APPEND EXEC_DIRS ${PROJECT_BINARY_DIR}/bin)
list(APPEND EXEC_DIRS ${PROJECT_BINARY_DIR}/testing/tools)

add_bash_tests(
FILE_PATH "${file_path}"
PREFIX "script"
WORKING_DIRECTORY ${DATA_DIR}
EXEC_DIRECTORIES "${EXEC_DIRS}"
)
endfunction()

add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/5ttgen)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2mask)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwi2response)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwibiascorrect)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwicat)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwifslpreproc)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwigradcheck)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwinormalise)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/dwishellmath)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/for_each)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/labelsgmfix)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/population_template)
add_bash_script_tests(${CMAKE_CURRENT_SOURCE_DIR}/tests/responsemean)

0 comments on commit 9da6c50

Please sign in to comment.