Skip to content

Test suite leaks temp files: make_nifti_image never removes the directory it creates #9041

Description

@aymuos15

Describe the problem

make_nifti_image in tests/test_utils.py creates a NIfTI file inside a fresh tempfile.mkdtemp() directory when no dir= is given, but returns only the file path. Its docstring assigns cleanup to the caller:

Create a temporary nifti image on the disk and return the image name.
User is responsible for deleting the temporary file when done with it.

11 of the 15 call sites across 9 test files discard the returned path entirely. The other four, in tests/data/test_nifti_rw.py and tests/integration/test_integration_sliding_window.py, do try to honour the contract, but they os.remove the file and never remove the directory holding it. Running tests/data/test_nifti_rw.py alone leaves 65 empty directories behind.

So every call site leaks a directory. The helper creates two things and hands back one, and both callers who tried to clean up got it wrong the same way.

tests/data/test_make_nifti.py:30 additionally calls tempfile.mkdtemp() inside a loop at module scope, leaking three more directories at import time, before any test runs.

To reproduce

before=$(ls /tmp | wc -l)
python -m pytest tests/data/test_nifti_rw.py tests/data/test_make_nifti.py \
                 tests/transforms/inverse/test_invert.py \
                 tests/transforms/utility/test_splitdimd.py -q
after=$(ls /tmp | wc -l); echo $((after - before))

77 entries left behind: a mix of empty directories (callers that removed the file but not the directory) and populated ones (callers that removed nothing).

Expected behavior

A test run leaves no new entries under the system temp directory.

Context

Surfaced by #9040 (comment), where fixing it was out of scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions