Skip to content

Commit c676e1d

Browse files
hjmjohnsonblowekampdzenanz
committed
COMP: Auto-remove transient single-owner test outputs in itk_add_test
Wire transient-output cleanup into every test whose ${ITK_TEST_OUTPUT_DIR}/ ${TEMP} outputs are owned by a single test. Each such test lists its outputs with an ITK_REMOVE_TEMPORARY_TEST_FILES <file>... argument to itk_add_test, which registers a FIXTURES_CLEANUP companion that removes them. Gated by the ITK_REMOVE_TEMPORARY_TEST_FILES option (ON by default); set OFF to retain outputs for debugging. The argument keeps each test's cleanup file list co-located with the itk_add_test call that produces them, rather than a separate itk_add_file_test_cleanup() companion call. It is the only parsed keyword, so it must appear last. The gersemi stub declares it so call sites format correctly. Detached-header outputs (.mhd, .nhdr) also remove their companion raw data files, and the cleanup command uses rm -rf so directory outputs (DICOM series, FileTools dirs) are removed as well as plain files. 1355 tests instrumented across 126 test/CMakeLists.txt. 42 multi-consumer shared outputs are excluded so the file persists for the consuming test. Co-authored-by: Bradley Lowekamp <blowekamp@mail.nih.gov> Co-authored-by: Dženan Zukić <dzenan.zukic@kitware.com>
1 parent a449d57 commit c676e1d

128 files changed

Lines changed: 3089 additions & 34 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMake/ITKModuleTest.cmake

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,29 @@ endfunction()
101101
# to the value of its containing module.
102102
#
103103
function(itk_add_test)
104+
# Optional ITK_REMOVE_TEMPORARY_TEST_FILES <file>... lists the transient
105+
# outputs to remove after the test. It is the only parsed keyword, so it
106+
# must appear LAST: every argument following it is consumed as a file to
107+
# remove. Arguments preceding it form the add_test specification.
108+
cmake_parse_arguments(_iat "" "" "ITK_REMOVE_TEMPORARY_TEST_FILES" ${ARGN})
109+
set(_iat_args ${_iat_UNPARSED_ARGUMENTS})
110+
104111
# Add tests with data in the ITKData group.
105112
ExternalData_Add_Test(
106113
ITKData
107-
${ARGN}
114+
${_iat_args}
108115
)
109116

110-
if("NAME" STREQUAL "${ARGV0}")
111-
set(_iat_testname ${ARGV1})
117+
list(GET _iat_args 0 _iat_arg0)
118+
if("NAME" STREQUAL "${_iat_arg0}")
119+
list(GET _iat_args 1 _iat_testname)
112120
else()
113-
set(_iat_testname ${ARGV0})
121+
set(_iat_testname ${_iat_arg0})
114122
endif()
115123

116124
if(itk-module)
117125
set(_label ${itk-module})
118-
if(TARGET ${itk-module}-all AND "${ARGN}" MATCHES "DATA{")
126+
if(TARGET ${itk-module}-all AND "${_iat_args}" MATCHES "DATA{")
119127
add_dependencies(${itk-module}-all ITKData)
120128
endif()
121129
else()
@@ -129,6 +137,13 @@ function(itk_add_test)
129137
LABELS
130138
${_label}
131139
)
140+
141+
if(_iat_ITK_REMOVE_TEMPORARY_TEST_FILES)
142+
itk_add_file_test_cleanup(
143+
${_iat_testname}
144+
${_iat_ITK_REMOVE_TEMPORARY_TEST_FILES}
145+
)
146+
endif()
132147
endfunction()
133148

134149
#-----------------------------------------------------------------------------
@@ -343,15 +358,19 @@ function(itk_memcheck_ignore)
343358
endfunction()
344359

345360
#-----------------------------------------------------------------------------
346-
# Option to automatically remove large test output files after completion.
347-
# BigIO write-read tests can produce multi-gigabyte temporary files
348-
# that exhaust disk space on CI runners and local builds.
361+
# Removing transient test outputs is the default behavior: each wired test
362+
# gets a companion cleanup test that deletes its ${ITK_TEST_OUTPUT_DIR}
363+
# outputs, which otherwise accumulate (BigIO write-read tests alone produce
364+
# multi-gigabyte files that exhaust disk on CI runners and local builds).
365+
# Cleanup runs unconditionally after the test (pass, fail, or skip); a
366+
# developer who needs to inspect outputs must opt out by configuring with
367+
# -DITK_REMOVE_TEMPORARY_TEST_FILES=OFF, which retains all test outputs.
349368
option(
350-
ITK_REMOVE_TEST_FILES_ON_SUCCESS
351-
"Remove large test output files after test completion. Set OFF to retain files for debugging."
369+
ITK_REMOVE_TEMPORARY_TEST_FILES
370+
"Remove transient test outputs after each test (default ON; removed even on failure). Set OFF to retain all outputs for debugging."
352371
ON
353372
)
354-
mark_as_advanced(ITK_REMOVE_TEST_FILES_ON_SUCCESS)
373+
mark_as_advanced(ITK_REMOVE_TEMPORARY_TEST_FILES)
355374

356375
#-----------------------------------------------------------------------------
357376
# itk_add_file_test_cleanup(<test_name> <output_file> [<output_file2> ...])
@@ -363,19 +382,19 @@ mark_as_advanced(ITK_REMOVE_TEST_FILES_ON_SUCCESS)
363382
# NOTE: Per CMake documentation, FIXTURES_CLEANUP tests execute
364383
# unconditionally after their fixture — they run regardless of whether the
365384
# SETUP test passed, failed, or was skipped. To retain output files for
366-
# debugging after a failure, set ITK_REMOVE_TEST_FILES_ON_SUCCESS=OFF.
385+
# debugging after a failure, set ITK_REMOVE_TEMPORARY_TEST_FILES=OFF.
367386
#
368387
# NOTE: In a full unfiltered run all tests (including *_cleanup variants)
369388
# are in the active set and fixture ordering is respected. When rerunning
370389
# a single test with a strict filter (e.g. ctest -R "^<test_name>$"),
371390
# the *_cleanup test does not match; omit the anchors (ctest -R <test_name>)
372391
# so both the main test and its cleanup are selected.
373392
#
374-
# When ITK_REMOVE_TEST_FILES_ON_SUCCESS is OFF, no cleanup test is added
393+
# When ITK_REMOVE_TEMPORARY_TEST_FILES is OFF, no cleanup test is added
375394
# and the output files are retained for manual inspection.
376395
#
377396
function(itk_add_file_test_cleanup TEST_NAME)
378-
if(NOT ITK_REMOVE_TEST_FILES_ON_SUCCESS)
397+
if(NOT ITK_REMOVE_TEMPORARY_TEST_FILES)
379398
return()
380399
endif()
381400

@@ -392,21 +411,24 @@ function(itk_add_file_test_cleanup TEST_NAME)
392411
# Build the removal command for all output files
393412
set(RM_ARGS "")
394413
foreach(OUTPUT_FILE ${ARGN})
395-
# For .mhd files, also remove the companion .raw/.zraw
414+
# Detached-header formats write a companion raw data file; remove it too.
396415
get_filename_component(EXT "${OUTPUT_FILE}" LAST_EXT)
397416
get_filename_component(BASE_NAME "${OUTPUT_FILE}" NAME_WLE)
398417
get_filename_component(DIR "${OUTPUT_FILE}" DIRECTORY)
399418
list(APPEND RM_ARGS "${OUTPUT_FILE}")
400419
if("${EXT}" STREQUAL ".mhd")
401420
list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.raw")
402421
list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.zraw")
422+
elseif("${EXT}" STREQUAL ".nhdr")
423+
list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.raw")
424+
list(APPEND RM_ARGS "${DIR}/${BASE_NAME}.raw.gz")
403425
endif()
404426
endforeach()
405427

406428
add_test(
407429
NAME ${TEST_NAME}_cleanup
408430
COMMAND
409-
${CMAKE_COMMAND} -E rm -f ${RM_ARGS}
431+
${CMAKE_COMMAND} -E rm -rf ${RM_ARGS}
410432
)
411433
set_tests_properties(
412434
${TEST_NAME}_cleanup

CMake/stubs/itk_add_test.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function(itk_add_test)
1212
ARG
1313
"COMMAND_EXPAND_LISTS"
1414
"NAME;WORKING_DIRECTORY"
15-
"COMMAND;CONFIGURATIONS"
15+
"COMMAND;CONFIGURATIONS;ITK_REMOVE_TEMPORARY_TEST_FILES"
1616
)
1717

1818
# This is a stub function to assist code formatting

Modules/Bridge/VtkGlue/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ if(NOT VTK_RENDERING_BACKEND STREQUAL "None")
8282
QuickViewTest
8383
DATA{${ITK_DATA_ROOT}/Input/peppers.png}
8484
${ITK_TEST_OUTPUT_DIR}
85+
ITK_REMOVE_TEMPORARY_TEST_FILES
86+
${ITK_TEST_OUTPUT_DIR}/QuickViewTest0.png
8587
)
8688
set_property(
8789
TEST

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ itk_add_test(
172172
${TEMP}/itkColorTableTest1.txt
173173
itkColorTableTest
174174
8
175+
ITK_REMOVE_TEMPORARY_TEST_FILES
176+
${TEMP}/itkColorTableTest1.txt
175177
)
176178
set_tests_properties(
177179
itkColorTableTest1
@@ -187,6 +189,8 @@ itk_add_test(
187189
${TEMP}/itkColorTableTest2.txt
188190
itkColorTableTest
189191
16
192+
ITK_REMOVE_TEMPORARY_TEST_FILES
193+
${TEMP}/itkColorTableTest2.txt
190194
)
191195
set_tests_properties(
192196
itkColorTableTest2
@@ -208,6 +212,8 @@ itk_add_test(
208212
ITKCommon1TestDriver
209213
itkMultipleLogOutputTest
210214
${TEMP}/test_multi.txt
215+
ITK_REMOVE_TEMPORARY_TEST_FILES
216+
${TEMP}/test_multi.txt
211217
)
212218
itk_add_test(
213219
NAME itkFixedArrayTest2
@@ -256,6 +262,8 @@ itk_add_test(
256262
DATA{${ITK_DATA_ROOT}/Input/sf4.png}
257263
0
258264
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionHorizTest.tiff
265+
ITK_REMOVE_TEMPORARY_TEST_FILES
266+
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionHorizTest.tiff
259267
)
260268
itk_add_test(
261269
NAME itkSobelOperatorImageFilterHorizTest
@@ -268,6 +276,8 @@ itk_add_test(
268276
DATA{${ITK_DATA_ROOT}/Input/sf4.png}
269277
0
270278
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageFilterHorizTest.tiff
279+
ITK_REMOVE_TEMPORARY_TEST_FILES
280+
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageFilterHorizTest.tiff
271281
)
272282
itk_add_test(
273283
NAME itkSobelOperatorImageConvolutionVertTest
@@ -280,6 +290,8 @@ itk_add_test(
280290
DATA{${ITK_DATA_ROOT}/Input/sf4.png}
281291
1
282292
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionVertTest.tiff
293+
ITK_REMOVE_TEMPORARY_TEST_FILES
294+
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionVertTest.tiff
283295
)
284296

285297
# A simplified data test for debugging sobel operator failures
@@ -294,6 +306,8 @@ itk_add_test(
294306
DATA{${ITK_DATA_ROOT}/Input/circle.png}
295307
1
296308
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionVertCircleTest.tiff
309+
ITK_REMOVE_TEMPORARY_TEST_FILES
310+
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageConvolutionVertCircleTest.tiff
297311
)
298312

299313
itk_add_test(
@@ -307,6 +321,8 @@ itk_add_test(
307321
DATA{${ITK_DATA_ROOT}/Input/sf4.png}
308322
1
309323
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageFilterVertTest.tiff
324+
ITK_REMOVE_TEMPORARY_TEST_FILES
325+
${ITK_TEST_OUTPUT_DIR}/itkSobelOperatorImageFilterVertTest.tiff
310326
)
311327

312328
itk_add_test(
@@ -316,6 +332,8 @@ itk_add_test(
316332
--redirectOutput
317333
${TEMP}/itkConstNeighborhoodIteratorTest.txt
318334
itkConstNeighborhoodIteratorTest
335+
ITK_REMOVE_TEMPORARY_TEST_FILES
336+
${TEMP}/itkConstNeighborhoodIteratorTest.txt
319337
)
320338
set_tests_properties(
321339
itkConstNeighborhoodIteratorTest
@@ -331,6 +349,8 @@ itk_add_test(
331349
--redirectOutput
332350
${TEMP}/itkShapedNeighborhoodIteratorTest.txt
333351
itkShapedNeighborhoodIteratorTest
352+
ITK_REMOVE_TEMPORARY_TEST_FILES
353+
${TEMP}/itkShapedNeighborhoodIteratorTest.txt
334354
)
335355
set_tests_properties(
336356
itkShapedNeighborhoodIteratorTest
@@ -431,6 +451,8 @@ itk_add_test(
431451
itkImageDuplicatorTest2
432452
DATA{${ITK_DATA_ROOT}/Input/vol-raw-big.nrrd}
433453
${ITK_TEST_OUTPUT_DIR}/vol-raw-big-dup2.nrrd
454+
ITK_REMOVE_TEMPORARY_TEST_FILES
455+
${ITK_TEST_OUTPUT_DIR}/vol-raw-big-dup2.nrrd
434456
)
435457
itk_add_test(
436458
NAME itkImageIteratorsForwardBackwardTest
@@ -456,6 +478,8 @@ itk_add_test(
456478
ITKCommon1TestDriver
457479
itkImageRandomIteratorTest2
458480
${ITK_TEST_OUTPUT_DIR}/itkImageRandomIteratorTest2Output.mha
481+
ITK_REMOVE_TEMPORARY_TEST_FILES
482+
${ITK_TEST_OUTPUT_DIR}/itkImageRandomIteratorTest2Output.mha
459483
)
460484
itk_add_test(
461485
NAME itkImageSliceIteratorTest
@@ -488,27 +512,35 @@ itk_add_test(
488512
ITKCommon1TestDriver
489513
itkLoggerTest
490514
${TEMP}/test_logger.txt
515+
ITK_REMOVE_TEMPORARY_TEST_FILES
516+
${TEMP}/test_logger.txt
491517
)
492518
itk_add_test(
493519
NAME itkLoggerOutputTest
494520
COMMAND
495521
ITKCommon2TestDriver
496522
itkLoggerOutputTest
497523
${TEMP}/test_loggerOutput.txt
524+
ITK_REMOVE_TEMPORARY_TEST_FILES
525+
${TEMP}/test_loggerOutput.txt
498526
)
499527
itk_add_test(
500528
NAME itkLoggerManagerTest
501529
COMMAND
502530
ITKCommon2TestDriver
503531
itkLoggerManagerTest
504532
${TEMP}/test_LoggerManager.txt
533+
ITK_REMOVE_TEMPORARY_TEST_FILES
534+
${TEMP}/test_LoggerManager.txt
505535
)
506536
itk_add_test(
507537
NAME itkLoggerThreadWrapperTest
508538
COMMAND
509539
ITKCommon2TestDriver
510540
itkLoggerThreadWrapperTest
511541
${TEMP}/test_LoggerThreadWrapper.txt
542+
ITK_REMOVE_TEMPORARY_TEST_FILES
543+
${TEMP}/test_LoggerThreadWrapper.txt
512544
)
513545
itk_add_test(
514546
NAME itkMatrixTest
@@ -561,6 +593,8 @@ itk_add_test(
561593
${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest1.png
562594
itkPointSetToImageFilterTest1
563595
${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest1.png
596+
ITK_REMOVE_TEMPORARY_TEST_FILES
597+
${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest1.png
564598
)
565599
itk_add_test(
566600
NAME itkPointSetToImageFilterTest2
@@ -572,6 +606,8 @@ itk_add_test(
572606
itkPointSetToImageFilterTest2
573607
DATA{${ITK_DATA_ROOT}/Input/VascularTreePointSet.txt}
574608
${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest2.mha
609+
ITK_REMOVE_TEMPORARY_TEST_FILES
610+
${ITK_TEST_OUTPUT_DIR}/itkPointSetToImageFilterTest2.mha
575611
)
576612
itk_add_test(
577613
NAME itkDataObjectTest
@@ -977,6 +1013,8 @@ itk_add_test(
9771013
--redirectOutput
9781014
${TEMP}/itkSliceIteratorTest.txt
9791015
itkSliceIteratorTest
1016+
ITK_REMOVE_TEMPORARY_TEST_FILES
1017+
${TEMP}/itkSliceIteratorTest.txt
9801018
)
9811019
set_tests_properties(
9821020
itkSliceIteratorTest
@@ -996,13 +1034,17 @@ itk_add_test(
9961034
ITKCommon2TestDriver
9971035
itkStdStreamLogOutputTest
9981036
${TEMP}/testStreamLogOutput.txt
1037+
ITK_REMOVE_TEMPORARY_TEST_FILES
1038+
${TEMP}/testStreamLogOutput.txt
9991039
)
10001040
itk_add_test(
10011041
NAME itkThreadLoggerTest
10021042
COMMAND
10031043
ITKCommon2TestDriver
10041044
itkThreadLoggerTest
10051045
${TEMP}/test_threadLogger.txt
1046+
ITK_REMOVE_TEMPORARY_TEST_FILES
1047+
${TEMP}/test_threadLogger.txt
10061048
)
10071049
itk_add_test(
10081050
NAME itkThreadDefsTest
@@ -1022,13 +1064,17 @@ itk_add_test(
10221064
ITKCommon1TestDriver
10231065
itkSymmetricSecondRankTensorImageReadTest
10241066
${ITK_TEST_OUTPUT_DIR}/testSymmetricTensor.nrrd
1067+
ITK_REMOVE_TEMPORARY_TEST_FILES
1068+
${ITK_TEST_OUTPUT_DIR}/testSymmetricTensor.nrrd
10251069
)
10261070
itk_add_test(
10271071
NAME itkSymmetricSecondRankTensorImageWriteReadTest
10281072
COMMAND
10291073
ITKCommon1TestDriver
10301074
itkSymmetricSecondRankTensorImageWriteReadTest
10311075
${ITK_TEST_OUTPUT_DIR}/testSymmetricTensorWriteRead.mha
1076+
ITK_REMOVE_TEMPORARY_TEST_FILES
1077+
${ITK_TEST_OUTPUT_DIR}/testSymmetricTensorWriteRead.mha
10321078
)
10331079
itk_add_test(
10341080
NAME itkSymmetricSecondRankTensorTest
@@ -1071,6 +1117,8 @@ itk_add_test(
10711117
DATA{${ITK_DATA_ROOT}/Input/CellsFluorescence1.png}
10721118
${ITK_TEST_OUTPUT_DIR}/itkStreamingImageFilterTest3_1.png
10731119
10
1120+
ITK_REMOVE_TEMPORARY_TEST_FILES
1121+
${ITK_TEST_OUTPUT_DIR}/itkStreamingImageFilterTest3_1.png
10741122
)
10751123
itk_add_test(
10761124
NAME itkStreamingImageFilterTest3_2
@@ -1083,6 +1131,8 @@ itk_add_test(
10831131
DATA{${ITK_DATA_ROOT}/Input/CellsFluorescence1.png}
10841132
${ITK_TEST_OUTPUT_DIR}/itkStreamingImageFilterTest3_2.png
10851133
1000
1134+
ITK_REMOVE_TEMPORARY_TEST_FILES
1135+
${ITK_TEST_OUTPUT_DIR}/itkStreamingImageFilterTest3_2.png
10861136
)
10871137
itk_add_test(
10881138
NAME itkVariableLengthVectorTest
@@ -1285,6 +1335,8 @@ itk_add_test(
12851335
ITKCommon2TestDriver
12861336
itkXMLFileOutputWindowTest
12871337
${ITK_TEST_OUTPUT_DIR}/itkXMLFileOutputWindowTest.xml
1338+
ITK_REMOVE_TEMPORARY_TEST_FILES
1339+
${ITK_TEST_OUTPUT_DIR}/itkXMLFileOutputWindowTest.xml
12881340
)
12891341

12901342
itk_add_test(
@@ -1295,6 +1347,8 @@ itk_add_test(
12951347
${ITK_TEST_OUTPUT_DIR}/itkXMLFilterWatcherTest.txt
12961348
itkXMLFilterWatcherTest
12971349
DATA{${ITK_DATA_ROOT}/Input/cthead1.png}
1350+
ITK_REMOVE_TEMPORARY_TEST_FILES
1351+
${ITK_TEST_OUTPUT_DIR}/itkXMLFilterWatcherTest.txt
12981352
)
12991353

13001354
if(ITK_BUILD_SHARED_LIBS AND ITK_DYNAMIC_LOADING)

Modules/Core/ImageAdaptors/test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ itk_add_test(
2929
itkVectorImageTest
3030
${ITK_TEST_OUTPUT_DIR}/VectorImage.nrrd
3131
${ITK_TEST_OUTPUT_DIR}/VectorImage.mhd
32+
ITK_REMOVE_TEMPORARY_TEST_FILES
33+
${ITK_TEST_OUTPUT_DIR}/VectorImage.nrrd
34+
${ITK_TEST_OUTPUT_DIR}/VectorImage.mhd
3235
)
3336
itk_add_test(
3437
NAME itkComplexConjugateImageAdaptorTest

0 commit comments

Comments
 (0)