Skip to content

Commit 70cd53e

Browse files
authored
reduce non-determinism in tests (#1347)
* don't delete finecal/crosstalk files from session-scoped test dataset * clean up temporary duplicate coordsystem file after test * don't use tmp_path_factory in non-fixture * random comment reflow * more cleanup of files created by tests in the session-scoped dataset * changelog
1 parent 6a659c9 commit 70cd53e

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

doc/whats_new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ The following authors contributed for the first time. Thank you so much! 🤩
2222
The following authors had contributed before. Thank you for sticking around! 🤘
2323

2424
* `Stefan Appelhoff`_
25+
* `Daniel McCloy`_
2526

2627
Detailed list of changes
2728
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -50,7 +51,7 @@ Detailed list of changes
5051
⚕️ Code health
5152
^^^^^^^^^^^^^^
5253

53-
- Nothing yet
54+
- Tests that were adding or deleting files to/from a session-scoped dataset now properly clean up after themselves, by `Daniel McCloy`_ (:gh:`1347`)
5455

5556
:doc:`Find out what was new in previous releases <whats_new_previous_releases>`
5657

mne_bids/tests/test_path.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,13 @@ def test_make_folders(tmp_path):
263263

264264

265265
@testing.requires_testing_data
266-
def test_rm(return_bids_test_dir, capsys, tmp_path_factory):
266+
def test_rm(return_bids_test_dir, capsys, tmp_path):
267267
"""Test BIDSPath's rm method to remove files."""
268268
# for some reason, mne's logger can't be captured by caplog....
269-
bids_root = str(tmp_path_factory.mktemp("test_rm") / "mnebids_utils_test_bids_ds")
269+
bids_root = tmp_path / "mnebids_utils_test_bids_ds"
270270
shutil.copytree(return_bids_test_dir, bids_root)
271271

272-
# without providing all the entities, ambiguous when trying
273-
# to use fpath
272+
# without providing all the entities, ambiguous when trying to use fpath
274273
bids_path = BIDSPath(
275274
subject=subject_id,
276275
session=session_id,
@@ -494,13 +493,13 @@ def test_find_matching_sidecar(return_bids_test_dir, tmp_path):
494493
expected_file = op.join("sub-01", "ses-01", "meg", "sub-01_ses-01_coordsystem.json")
495494
assert str(sidecar_fname).endswith(expected_file)
496495

497-
# Find multiple sidecars, tied in score, triggering an error
496+
# create a duplicate sidecar, which will be tied in match score, triggering an error
497+
dupe = Path(str(sidecar_fname).replace("coordsystem.json", "2coordsystem.json"))
498+
dupe.touch()
498499
with pytest.raises(RuntimeError, match="Expected to find a single"):
499-
open(
500-
str(sidecar_fname).replace("coordsystem.json", "2coordsystem.json"), "w"
501-
).close()
502500
print_dir_tree(bids_root)
503501
bids_path.find_matching_sidecar(suffix="coordsystem", extension=".json")
502+
dupe.unlink() # clean up extra file
504503

505504
# Find nothing and raise.
506505
with pytest.raises(RuntimeError, match="Did not find any"):
@@ -1038,6 +1037,7 @@ def test_match(return_bids_test_dir):
10381037

10391038
assert bids_path_01.match(check=True) == []
10401039
assert bids_path_01.match(check=False)[0].fpath.name == "sub-01_foo.eeg"
1040+
bids_path_01.fpath.unlink() # clean up created file
10411041

10421042

10431043
@testing.requires_testing_data
@@ -1119,6 +1119,7 @@ def test_find_matching_paths(return_bids_test_dir):
11191119
check=False,
11201120
)
11211121
assert paths_match == paths_find
1122+
bids_path_01.fpath.unlink() # clean up created file
11221123

11231124

11241125
@pytest.mark.filterwarnings(warning_str["meas_date_set_to_none"])
@@ -1358,7 +1359,7 @@ def test_bids_path_label_vs_index_entity():
13581359

13591360

13601361
@testing.requires_testing_data
1361-
def test_meg_calibration_fpath(return_bids_test_dir):
1362+
def test_meg_calibration_fpath(return_bids_test_dir, tmp_path):
13621363
"""Test BIDSPath.meg_calibration_fpath."""
13631364
bids_root = return_bids_test_dir
13641365

@@ -1382,15 +1383,18 @@ def test_meg_calibration_fpath(return_bids_test_dir):
13821383
with pytest.raises(ValueError, match="Can only find .* for MEG"):
13831384
bids_path_.meg_calibration_fpath
13841385

1385-
# Delete the fine-calibration file. BIDSPath.meg_calibration_fpath
1386-
# should then return None.
1386+
# Move the fine-calibration file. BIDSPath.meg_calibration_fpath should then be None
13871387
bids_path_ = _bids_path.copy().update(subject="01", root=bids_root)
1388-
Path(bids_path_.meg_calibration_fpath).unlink()
1388+
src = Path(bids_path_.meg_calibration_fpath)
1389+
src.rename(tmp_path / src.name)
13891390
assert bids_path_.meg_calibration_fpath is None
1391+
# restore the file
1392+
(tmp_path / src.name).rename(src)
1393+
assert bids_path_.meg_calibration_fpath is not None
13901394

13911395

13921396
@testing.requires_testing_data
1393-
def test_meg_crosstalk_fpath(return_bids_test_dir):
1397+
def test_meg_crosstalk_fpath(return_bids_test_dir, tmp_path):
13941398
"""Test BIDSPath.meg_crosstalk_fpath."""
13951399
bids_root = return_bids_test_dir
13961400

@@ -1414,11 +1418,14 @@ def test_meg_crosstalk_fpath(return_bids_test_dir):
14141418
with pytest.raises(ValueError, match="Can only find .* for MEG"):
14151419
bids_path.meg_crosstalk_fpath
14161420

1417-
# Delete the crosstalk file. BIDSPath.meg_crosstalk_fpath should then
1418-
# return None.
1421+
# Move the crosstalk file. BIDSPath.meg_crosstalk_fpath should then be None.
14191422
bids_path = _bids_path.copy().update(subject="01", root=bids_root)
1420-
Path(bids_path.meg_crosstalk_fpath).unlink()
1423+
src = Path(bids_path.meg_crosstalk_fpath)
1424+
src.rename(tmp_path / src.name)
14211425
assert bids_path.meg_crosstalk_fpath is None
1426+
# restore the file
1427+
(tmp_path / src.name).rename(src)
1428+
assert bids_path.meg_crosstalk_fpath is not None
14221429

14231430

14241431
@testing.requires_testing_data

0 commit comments

Comments
 (0)