Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce non-determinism in tests #1347

Merged
merged 6 commits into from
Dec 5, 2024

Conversation

drammock
Copy link
Member

@drammock drammock commented Dec 4, 2024

PR Description

closes #1346

Merge checklist

Maintainer, please confirm the following before merging.
If applicable:

  • All comments are resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • New contributors have been added to CITATION.cff
  • PR description includes phrase "closes <#issue-number>"

@drammock drammock changed the title don't delete finecal/crosstalk files from session-scoped test dataset reduce non-determinism in tests Dec 4, 2024
Copy link

codecov bot commented Dec 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.42%. Comparing base (6a659c9) to head (9e4f1d4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1347   +/-   ##
=======================================
  Coverage   97.42%   97.42%           
=======================================
  Files          40       40           
  Lines        8883     8893   +10     
=======================================
+ Hits         8654     8664   +10     
  Misses        229      229           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

shutil.copytree(return_bids_test_dir, bids_root)

# without providing all the entities, ambiguous when trying
# to use fpath
# without providing all the entities, ambiguous when trying to use fpath
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated change, just a reflow

Comment on lines +266 to +269
def test_rm(return_bids_test_dir, capsys, tmp_path):
"""Test BIDSPath's rm method to remove files."""
# for some reason, mne's logger can't be captured by caplog....
bids_root = str(tmp_path_factory.mktemp("test_rm") / "mnebids_utils_test_bids_ds")
bids_root = tmp_path / "mnebids_utils_test_bids_ds"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to use tmp_path_factory here, as it is session-scoped and we don't need this extra dir to persist beyond the life of this one test.

Comment on lines -1386 to +1393
# should then return None.
# Move the fine-calibration file. BIDSPath.meg_calibration_fpath should then be None
bids_path_ = _bids_path.copy().update(subject="01", root=bids_root)
Path(bids_path_.meg_calibration_fpath).unlink()
src = Path(bids_path_.meg_calibration_fpath)
src.rename(tmp_path / src.name)
assert bids_path_.meg_calibration_fpath is None
# restore the file
(tmp_path / src.name).rename(src)
assert bids_path_.meg_calibration_fpath is not None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this essentially just changes a Path(...).unlink() to a Path(...).rename(...), then moves the file back once we're done testing that its absence yields the desired result.

This is important because the file in question is in a session-scoped dataset fixture, so simply deleting the file means that other tests that expect it to be there will fail if they are executed after this test

Comment on lines -1418 to +1428
# return None.
# Move the crosstalk file. BIDSPath.meg_crosstalk_fpath should then be None.
bids_path = _bids_path.copy().update(subject="01", root=bids_root)
Path(bids_path.meg_crosstalk_fpath).unlink()
src = Path(bids_path.meg_crosstalk_fpath)
src.rename(tmp_path / src.name)
assert bids_path.meg_crosstalk_fpath is None
# restore the file
(tmp_path / src.name).rename(src)
assert bids_path.meg_crosstalk_fpath is not None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same change as for the fine-calibration file above: move,test,move-back instead of unlink.

Copy link
Member

@sappelhoff sappelhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot for digging into this @drammock. Please ping me once you are done working on it.

@drammock
Copy link
Member Author

drammock commented Dec 5, 2024

thanks a lot for digging into this @drammock. Please ping me once you are done working on it.

I'm done. Only failing CI is validator-main (expected to fail IIUC) and I took it out of draft mode already forgot to take it out of draft mode when I self-reviewed, oops. Anyway, I no longer see the prior errors locally when running tests on this branch, so I'm hopeful that I caught everything.

In the long run, we should consider a fixture to automatically check the state of the test dir before & after each test, to ensure no files are changed. Not sure if this could be built into return_bids_test_dir fixture or would need to be a separate thing... @larsoner IIRC MNE has such a setup for unclosed pyvista windows? That could be a good starting point.

@drammock drammock marked this pull request as ready for review December 5, 2024 16:13
@sappelhoff sappelhoff merged commit 70cd53e into mne-tools:main Dec 5, 2024
22 of 23 checks passed
@sappelhoff
Copy link
Member

Thanks!

@drammock drammock deleted the fix-nondeterministic-tests branch December 10, 2024 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

non-determinism in tests
2 participants