@@ -337,3 +337,79 @@ function(itk_memcheck_ignore)
337337 ${ARGN}
338338 )
339339endfunction ()
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 (BASE_NAME "${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} /${BASE_NAME} .raw" )
398+ list (APPEND RM_ARGS "${DIR} /${BASE_NAME} .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 ()
0 commit comments