Skip to content

Commit c7a97e2

Browse files
committed
STY: Prefer using pytest temporary path fixtures in tests
Prefer using the `pytest` `tmp_path` fixture in tests over the standard library `tempfile.TemporaryDirectory()`. In the previous implementation, the context was ensuring that the memmap files did not live past the context, failing to check anything that the `trx` function at issue would do in terms of the file survival. Since `close_or_delete_mmap` does not actually delete the file, and `test_close_or_delete_mmap_with_mmap_attr` actually checks that the memmap has been closed, this patch set checks that the file is still present after calling the function; `pytest` takes care of deleting the file once the testing function has finished its execution. Left behind in commit 25cc109.
1 parent 0b27b2b commit c7a97e2

1 file changed

Lines changed: 23 additions & 29 deletions

File tree

‎trx/tests/test_utils.py‎

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,30 @@ class Origin:
4747
TRACKVIS = None
4848

4949

50-
def test_close_or_delete_mmap_np_memmap():
50+
def test_close_or_delete_mmap_np_memmap(tmp_path):
5151
"""Test close_or_delete_mmap with a numpy.memmap."""
52-
with tempfile.TemporaryDirectory() as tmpdir:
53-
tmp_name = os.path.join(tmpdir, "test.mmap")
54-
mmap_arr = np.memmap(tmp_name, dtype="float32", mode="w+", shape=(10,))
55-
close_or_delete_mmap(mmap_arr)
56-
assert mmap_arr._mmap.closed
57-
assert not os.path.exists(tmp_name)
52+
tmp_name = tmp_path / "test.mmap"
53+
mmap_arr = np.memmap(tmp_name, dtype="float32", mode="w+", shape=(10,))
54+
close_or_delete_mmap(mmap_arr)
55+
assert tmp_name.exists()
5856

5957

60-
def test_close_or_delete_mmap_array_sequence():
58+
def test_close_or_delete_mmap_array_sequence(tmp_path):
6159
"""Test close_or_delete_mmap with an ArraySequence."""
62-
with tempfile.TemporaryDirectory() as tmpdir:
63-
tmp1_name = os.path.join(tmpdir, "test1.mmap")
64-
tmp2_name = os.path.join(tmpdir, "test2.mmap")
65-
data = np.memmap(tmp1_name, dtype="float32", mode="w+", shape=(10, 3))
66-
offsets = np.memmap(tmp2_name, dtype="uint32", mode="w+", shape=(5,))
60+
tmp1_name = tmp_path / "test1.mmap"
61+
tmp2_name = tmp_path / "test2.mmap"
62+
data = np.memmap(tmp1_name, dtype="float32", mode="w+", shape=(10, 3))
63+
offsets = np.memmap(tmp2_name, dtype="uint32", mode="w+", shape=(5,))
6764

68-
seq = ArraySequence()
69-
seq._data = data
70-
seq._offsets = offsets
71-
seq._lengths = np.array([2, 2, 2, 2, 2], dtype="uint32")
65+
seq = ArraySequence()
66+
seq._data = data
67+
seq._offsets = offsets
68+
seq._lengths = np.array([2, 2, 2, 2, 2], dtype="uint32")
7269

73-
close_or_delete_mmap(seq)
74-
assert seq._data._mmap.closed
75-
assert seq._offsets._mmap.closed
70+
close_or_delete_mmap(seq)
7671

77-
assert not os.path.exists(tmp1_name)
78-
assert not os.path.exists(tmp2_name)
72+
assert tmp1_name.exists()
73+
assert tmp2_name.exists()
7974

8075

8176
def test_close_or_delete_mmap_with_mmap_attr():
@@ -190,14 +185,13 @@ def test_get_reference_info_wrapper_nifti_header(nifti_ref):
190185
assert np.array_equal(dimensions, [10, 20, 30])
191186

192187

193-
def test_get_reference_info_wrapper_nifti_file(nifti_ref):
188+
def test_get_reference_info_wrapper_nifti_file(tmp_path, nifti_ref):
194189
"""Test get_reference_info_wrapper with a Nifti filename."""
195-
with tempfile.TemporaryDirectory() as tmp_dir:
196-
path = os.path.join(tmp_dir, "test.nii.gz")
197-
nib.save(nifti_ref, path)
198-
affine, dimensions, voxel_sizes, voxel_order = get_reference_info_wrapper(path)
199-
assert np.allclose(affine, nifti_ref.affine)
200-
assert np.array_equal(dimensions, [10, 20, 30])
190+
path = os.path.join(tmp_path, "test.nii.gz")
191+
nib.save(nifti_ref, path)
192+
affine, dimensions, voxel_sizes, voxel_order = get_reference_info_wrapper(path)
193+
assert np.allclose(affine, nifti_ref.affine)
194+
assert np.array_equal(dimensions, [10, 20, 30])
201195

202196

203197
@patch("nibabel.streamlines.load")

0 commit comments

Comments
 (0)