diff --git a/CMake/ITKModuleTest.cmake b/CMake/ITKModuleTest.cmake index 9e957fe9a6c..7e8b98e1a64 100644 --- a/CMake/ITKModuleTest.cmake +++ b/CMake/ITKModuleTest.cmake @@ -337,3 +337,79 @@ function(itk_memcheck_ignore) ${ARGN} ) endfunction() + +#----------------------------------------------------------------------------- +# Option to automatically remove large test output files after completion. +# BigIO write-read tests can produce multi-gigabyte temporary files +# that exhaust disk space on CI runners and local builds. +option( + ITK_REMOVE_TEST_FILES_ON_SUCCESS + "Remove large test output files after test completion. Set OFF to retain files for debugging." + ON +) +mark_as_advanced(ITK_REMOVE_TEST_FILES_ON_SUCCESS) + +#----------------------------------------------------------------------------- +# itk_add_file_test_cleanup( [ ...]) +# +# Adds a cleanup test that removes the given output files after +# completes. Uses CTest fixtures: the main test is declared FIXTURES_SETUP +# and the companion _cleanup test is declared FIXTURES_CLEANUP. +# +# NOTE: Per CMake documentation, FIXTURES_CLEANUP tests execute +# unconditionally after their fixture — they run regardless of whether the +# SETUP test passed, failed, or was skipped. To retain output files for +# debugging after a failure, set ITK_REMOVE_TEST_FILES_ON_SUCCESS=OFF. +# +# NOTE: In a full unfiltered run all tests (including *_cleanup variants) +# are in the active set and fixture ordering is respected. When rerunning +# a single test with a strict filter (e.g. ctest -R "^$"), +# the *_cleanup test does not match; omit the anchors (ctest -R ) +# so both the main test and its cleanup are selected. +# +# When ITK_REMOVE_TEST_FILES_ON_SUCCESS is OFF, no cleanup test is added +# and the output files are retained for manual inspection. +# +function(itk_add_file_test_cleanup TEST_NAME) + if(NOT ITK_REMOVE_TEST_FILES_ON_SUCCESS) + return() + endif() + + set(FIXTURE_NAME "Cleanup_${TEST_NAME}") + + # Make the main test the SETUP of this fixture + set_tests_properties( + ${TEST_NAME} + PROPERTIES + FIXTURES_SETUP + ${FIXTURE_NAME} + ) + + # Build the removal command for all output files + set(RM_ARGS "") + foreach(OUTPUT_FILE ${ARGN}) + # For .mhd files, also remove the companion .raw/.zraw + get_filename_component(EXT "${OUTPUT_FILE}" LAST_EXT) + get_filename_component(BASE_NAME "${OUTPUT_FILE}" NAME_WLE) + get_filename_component(DIR "${OUTPUT_FILE}" DIRECTORY) + list(APPEND RM_ARGS "${OUTPUT_FILE}") + if("${EXT}" STREQUAL ".mhd") + list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.raw") + list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.zraw") + endif() + endforeach() + + add_test( + NAME ${TEST_NAME}_cleanup + COMMAND + ${CMAKE_COMMAND} -E rm -f ${RM_ARGS} + ) + set_tests_properties( + ${TEST_NAME}_cleanup + PROPERTIES + FIXTURES_CLEANUP + ${FIXTURE_NAME} + LABELS + "CLEANUP" + ) +endfunction() diff --git a/Modules/IO/ImageBase/test/CMakeLists.txt b/Modules/IO/ImageBase/test/CMakeLists.txt index 9a990b499de..bb9d2867113 100644 --- a/Modules/IO/ImageBase/test/CMakeLists.txt +++ b/Modules/IO/ImageBase/test/CMakeLists.txt @@ -52,12 +52,6 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 16) ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteConvertReadTest.mha 30000L ) - set_tests_properties( - itkLargeImageWriteConvertReadTest - PROPERTIES - RESOURCE_LOCK - MEMORY_SIZE - ) itk_add_test( NAME itkLargeImageWriteReadTest_2D @@ -67,12 +61,6 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 16) ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteReadTest_2D.mha 30000L ) - set_tests_properties( - itkLargeImageWriteReadTest_2D - PROPERTIES - RESOURCE_LOCK - MEMORY_SIZE - ) itk_add_test( NAME itkLargeImageWriteReadTest_3D @@ -83,11 +71,28 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 16) 30000L 4L ) + + # BigIO tests produce multi-gigabyte files; MEMORY_SIZE lock serializes them set_tests_properties( + itkLargeImageWriteConvertReadTest + itkLargeImageWriteReadTest_2D itkLargeImageWriteReadTest_3D PROPERTIES RESOURCE_LOCK MEMORY_SIZE + LABELS + "BigIO;RUNS_LONG" + ) + + # Clean up BigIO output files after completion + itk_add_file_test_cleanup( + itkLargeImageWriteConvertReadTest ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteConvertReadTest.mha + ) + itk_add_file_test_cleanup( + itkLargeImageWriteReadTest_2D ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteReadTest_2D.mha + ) + itk_add_file_test_cleanup( + itkLargeImageWriteReadTest_3D ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteReadTest_3D.mha ) endif() diff --git a/Modules/IO/Meta/test/CMakeLists.txt b/Modules/IO/Meta/test/CMakeLists.txt index 84d87ef5e43..fbab393885e 100644 --- a/Modules/IO/Meta/test/CMakeLists.txt +++ b/Modules/IO/Meta/test/CMakeLists.txt @@ -482,7 +482,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) 50000L ) - # Due to the large memory requirements this tests must be run one by one + # Due to the large memory requirements these tests must be run one by one set_tests_properties( itkLargeMetaImageWriteReadTest1 itkLargeMetaImageWriteReadTest2 @@ -519,6 +519,10 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) LABELS RUNS_LONG ) + # Clean up BigIO output files after completion + itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.mhd) + itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.mhd) + itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.mhd) endif() if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) @@ -532,7 +536,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) 70000L ) - # Due to the large memory requirements this tests must be run one by one + # Due to the large memory requirements this test must be run serially set_tests_properties( itkLargeMetaImageWriteReadTest4 PROPERTIES @@ -542,6 +546,8 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) MEMORY_SIZE COST 30 + RUN_SERIAL + True ) set_property( TEST @@ -551,12 +557,5 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) LABELS RUNS_LONG ) - set_property( - TEST - itkLargeMetaImageWriteReadTest4 - APPEND - PROPERTY - RUN_SERIAL - True - ) + itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.mhd) endif() diff --git a/Modules/IO/TIFF/test/CMakeLists.txt b/Modules/IO/TIFF/test/CMakeLists.txt index 1dd906b9508..a3b0766e5f6 100644 --- a/Modules/IO/TIFF/test/CMakeLists.txt +++ b/Modules/IO/TIFF/test/CMakeLists.txt @@ -486,7 +486,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) 50000L ) - # Due to the large memory requirements this tests must be run one by one + # Due to the large memory requirements these tests must be run one by one set_tests_properties( itkLargeTIFFImageWriteReadTest1 itkLargeTIFFImageWriteReadTest2 @@ -496,6 +496,8 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) BigIO RESOURCE_LOCK MEMORY_SIZE + RUN_SERIAL + True ) set_property( TEST @@ -505,14 +507,6 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) LABELS RUNS_LONG ) - set_property( - TEST - itkLargeTIFFImageWriteReadTest1 - APPEND - PROPERTY - RUN_SERIAL - True - ) set_property( TEST itkLargeTIFFImageWriteReadTest2 @@ -521,14 +515,6 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) LABELS RUNS_LONG ) - set_property( - TEST - itkLargeTIFFImageWriteReadTest2 - APPEND - PROPERTY - RUN_SERIAL - True - ) set_property( TEST itkLargeTIFFImageWriteReadTest3 @@ -537,14 +523,10 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5) LABELS RUNS_LONG ) - set_property( - TEST - itkLargeTIFFImageWriteReadTest3 - APPEND - PROPERTY - RUN_SERIAL - True - ) + # Clean up BigIO output files after completion + itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.tif) + itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.tif) + itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.tif) endif() if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) @@ -558,7 +540,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) 70000L ) - # Due to the large memory requirements this tests must lock the memory size resource + # Due to the large memory requirements this test must be run serially set_tests_properties( itkLargeTIFFImageWriteReadTest4 PROPERTIES @@ -568,6 +550,8 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) MEMORY_SIZE COST 30 + RUN_SERIAL + True ) set_property( TEST @@ -577,14 +561,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12) LABELS RUNS_LONG ) - set_property( - TEST - itkLargeTIFFImageWriteReadTest4 - APPEND - PROPERTY - RUN_SERIAL - True - ) + itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.tif) endif() # expand + RGB image