Skip to content

Commit 5f4d4dd

Browse files
committed
ENH: Add itk_add_file_test_cleanup for automatic test output cleanup
Add itk_add_file_test_cleanup() to ITKModuleTest.cmake. The function uses CTest FIXTURES_SETUP/FIXTURES_CLEANUP to register a companion <test_name>_cleanup test that removes specified output files after the main test completes. Controlled by the ITK_REMOVE_TEST_FILES_ON_SUCCESS option (default ON). When OFF, no cleanup test is registered and files are retained for manual debugging. For .mhd output files the companion .raw/.zraw sidecar is automatically included in the removal list. Apply to all BigIO large-image tests in IO/ImageBase, IO/Meta, and IO/TIFF where multi-gigabyte outputs would otherwise accumulate and exhaust disk space on CI runners and local builds. The function name was chosen to be IO-format-agnostic (suggested by blowekamp during review of an earlier draft).
1 parent fee34e2 commit 5f4d4dd

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-11
lines changed

CMake/ITKModuleTest.cmake

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,79 @@ function(itk_memcheck_ignore)
337337
${ARGN}
338338
)
339339
endfunction()
340+
341+
#-----------------------------------------------------------------------------
342+
# Option to automatically remove large test output files after completion.
343+
# BigIO write-read tests can produce multi-gigabyte temporary files
344+
# that exhaust disk space on CI runners and local builds.
345+
option(
346+
ITK_REMOVE_TEST_FILES_ON_SUCCESS
347+
"Remove large test output files after test completion. Set OFF to retain files for debugging."
348+
ON
349+
)
350+
mark_as_advanced(ITK_REMOVE_TEST_FILES_ON_SUCCESS)
351+
352+
#-----------------------------------------------------------------------------
353+
# itk_add_file_test_cleanup(<test_name> <output_file> [<output_file2> ...])
354+
#
355+
# Adds a cleanup test that removes the given output files after <test_name>
356+
# completes. Uses CTest fixtures: the main test is declared FIXTURES_SETUP
357+
# and the companion <test_name>_cleanup test is declared FIXTURES_CLEANUP.
358+
#
359+
# NOTE: Per CMake documentation, FIXTURES_CLEANUP tests execute
360+
# unconditionally after their fixture — they run regardless of whether the
361+
# SETUP test passed, failed, or was skipped. To retain output files for
362+
# debugging after a failure, set ITK_REMOVE_TEST_FILES_ON_SUCCESS=OFF.
363+
#
364+
# NOTE: In a full unfiltered run all tests (including *_cleanup variants)
365+
# are in the active set and fixture ordering is respected. When rerunning
366+
# a single test with a strict filter (e.g. ctest -R "^<test_name>$"),
367+
# the *_cleanup test does not match; omit the anchors (ctest -R <test_name>)
368+
# so both the main test and its cleanup are selected.
369+
#
370+
# When ITK_REMOVE_TEST_FILES_ON_SUCCESS is OFF, no cleanup test is added
371+
# and the output files are retained for manual inspection.
372+
#
373+
function(itk_add_file_test_cleanup TEST_NAME)
374+
if(NOT ITK_REMOVE_TEST_FILES_ON_SUCCESS)
375+
return()
376+
endif()
377+
378+
set(FIXTURE_NAME "Cleanup_${TEST_NAME}")
379+
380+
# Make the main test the SETUP of this fixture
381+
set_tests_properties(
382+
${TEST_NAME}
383+
PROPERTIES
384+
FIXTURES_SETUP
385+
${FIXTURE_NAME}
386+
)
387+
388+
# Build the removal command for all output files
389+
set(RM_ARGS "")
390+
foreach(OUTPUT_FILE ${ARGN})
391+
# For .mhd files, also remove the companion .raw/.zraw
392+
get_filename_component(EXT "${OUTPUT_FILE}" LAST_EXT)
393+
get_filename_component(NAME_WLE "${OUTPUT_FILE}" NAME_WLE)
394+
get_filename_component(DIR "${OUTPUT_FILE}" DIRECTORY)
395+
list(APPEND RM_ARGS "${OUTPUT_FILE}")
396+
if("${EXT}" STREQUAL ".mhd")
397+
list(APPEND RM_ARGS "${DIR}/${NAME_WLE}.raw")
398+
list(APPEND RM_ARGS "${DIR}/${NAME_WLE}.zraw")
399+
endif()
400+
endforeach()
401+
402+
add_test(
403+
NAME ${TEST_NAME}_cleanup
404+
COMMAND
405+
${CMAKE_COMMAND} -E rm -f ${RM_ARGS}
406+
)
407+
set_tests_properties(
408+
${TEST_NAME}_cleanup
409+
PROPERTIES
410+
FIXTURES_CLEANUP
411+
${FIXTURE_NAME}
412+
LABELS
413+
"CLEANUP"
414+
)
415+
endfunction()

Modules/IO/ImageBase/test/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 16)
8585
)
8686

8787
# Clean up BigIO output files after successful completion
88-
itk_add_bigio_test_cleanup(
88+
itk_add_file_test_cleanup(
8989
itkLargeImageWriteConvertReadTest ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteConvertReadTest.mha
9090
)
91-
itk_add_bigio_test_cleanup(
91+
itk_add_file_test_cleanup(
9292
itkLargeImageWriteReadTest_2D ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteReadTest_2D.mha
9393
)
94-
itk_add_bigio_test_cleanup(
94+
itk_add_file_test_cleanup(
9595
itkLargeImageWriteReadTest_3D ${ITK_TEST_OUTPUT_DIR}/itkLargeImageWriteReadTest_3D.mha
9696
)
9797
endif()

Modules/IO/Meta/test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,9 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5)
520520
RUNS_LONG
521521
)
522522
# Clean up BigIO output files after successful completion
523-
itk_add_bigio_test_cleanup(itkLargeMetaImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.mhd)
524-
itk_add_bigio_test_cleanup(itkLargeMetaImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.mhd)
525-
itk_add_bigio_test_cleanup(itkLargeMetaImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.mhd)
523+
itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.mhd)
524+
itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.mhd)
525+
itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.mhd)
526526
endif()
527527

528528
if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12)
@@ -557,5 +557,5 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12)
557557
LABELS
558558
RUNS_LONG
559559
)
560-
itk_add_bigio_test_cleanup(itkLargeMetaImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.mhd)
560+
itk_add_file_test_cleanup(itkLargeMetaImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.mhd)
561561
endif()

Modules/IO/TIFF/test/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,9 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 5)
524524
RUNS_LONG
525525
)
526526
# Clean up BigIO output files after successful completion
527-
itk_add_bigio_test_cleanup(itkLargeTIFFImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.tif)
528-
itk_add_bigio_test_cleanup(itkLargeTIFFImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.tif)
529-
itk_add_bigio_test_cleanup(itkLargeTIFFImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.tif)
527+
itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest1 ${ITK_TEST_OUTPUT_DIR}/LargeImage01.tif)
528+
itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest2 ${ITK_TEST_OUTPUT_DIR}/LargeImage02.tif)
529+
itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest3 ${ITK_TEST_OUTPUT_DIR}/LargeImage03.tif)
530530
endif()
531531

532532
if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12)
@@ -561,7 +561,7 @@ if("${ITK_COMPUTER_MEMORY_SIZE}" GREATER 12)
561561
LABELS
562562
RUNS_LONG
563563
)
564-
itk_add_bigio_test_cleanup(itkLargeTIFFImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.tif)
564+
itk_add_file_test_cleanup(itkLargeTIFFImageWriteReadTest4 ${ITK_TEST_OUTPUT_DIR}/LargeImage04.tif)
565565
endif()
566566

567567
# expand + RGB image

0 commit comments

Comments
 (0)