-
Notifications
You must be signed in to change notification settings - Fork 374
Update tests to align with mutation testing runs #1361
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
base: forks/osaka
Are you sure you want to change the base?
Changes from 4 commits
6a9c5a6
4997870
512e23a
5c159ab
9109bf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,6 +162,7 @@ test = [ | |
| "filelock>=3.15.1,<4", | ||
| "requests", | ||
| "requests-cache>=1.2.1,<2", | ||
| "portalocker", | ||
| ] | ||
|
|
||
| lint = [ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| from typing import Final, Optional, Set | ||
|
|
||
| import git | ||
| import portalocker | ||
| import pytest | ||
| import requests_cache | ||
| from _pytest.config import Config | ||
| from _pytest.config.argparsing import Parser | ||
|
|
@@ -214,19 +216,8 @@ def __exit__( | |
| fixture_lock = StashKey[Optional[FileLock]]() | ||
|
|
||
|
|
||
| def pytest_sessionstart(session: Session) -> None: # noqa: U100 | ||
| if get_xdist_worker_id(session) != "master": | ||
| return | ||
|
|
||
| lock_path = session.config.rootpath.joinpath("tests/fixtures/.lock") | ||
| stash = session.stash | ||
| lock_file = FileLock(str(lock_path), timeout=0) | ||
| lock_file.acquire() | ||
|
|
||
| assert fixture_lock not in stash | ||
| stash[fixture_lock] = lock_file | ||
|
|
||
| with _FixturesDownloader(session.config.rootpath) as downloader: | ||
| def download_fixtures(root: Path) -> None: | ||
| with _FixturesDownloader(root) as downloader: | ||
| for _, props in TEST_FIXTURES.items(): | ||
| fixture_path = props["fixture_path"] | ||
|
|
||
|
|
@@ -243,15 +234,70 @@ def pytest_sessionstart(session: Session) -> None: # noqa: U100 | |
| ) | ||
|
|
||
|
|
||
| def pytest_sessionstart(session: Session) -> None: # noqa: U100 | ||
| lock_path = session.config.rootpath.joinpath("tests/fixtures/.lock") | ||
|
|
||
| # use portalocker for mutmut runs | ||
| if os.environ.get("MUTANT_UNDER_TEST"): | ||
| shared_lock = portalocker.Lock(lock_path, flags=portalocker.LOCK_SH) | ||
| shared_lock.acquire() | ||
|
||
| session.stash['mutmut_shared_lock'] = shared_lock | ||
|
|
||
| all_fixtures_ready = all( | ||
| os.path.exists(props["fixture_path"]) | ||
| for props in TEST_FIXTURES.values() | ||
| ) | ||
| if not all_fixtures_ready: | ||
| shared_lock.release() | ||
| with portalocker.Lock(lock_path, flags=portalocker.LOCK_EX): | ||
| all_fixtures_ready = all( | ||
| os.path.exists(props["fixture_path"]) | ||
| for props in TEST_FIXTURES.values() | ||
| ) | ||
| if not all_fixtures_ready: | ||
| download_fixtures(session.config.rootpath) | ||
| shared_lock.acquire() | ||
|
||
| session.stash['mutmut_shared_lock'] = shared_lock | ||
| return | ||
|
|
||
| if get_xdist_worker_id(session) != "master": | ||
| return | ||
|
|
||
| stash = session.stash | ||
| lock_file = FileLock(str(lock_path), timeout=0) | ||
| lock_file.acquire() | ||
|
|
||
| assert fixture_lock not in stash | ||
| stash[fixture_lock] = lock_file | ||
|
|
||
| download_fixtures(session.config.rootpath) | ||
|
|
||
|
|
||
| def pytest_sessionfinish( | ||
| session: Session, exitstatus: int # noqa: U100 | ||
| ) -> None: | ||
| del exitstatus | ||
|
|
||
| if os.environ.get("MUTANT_UNDER_TEST"): | ||
| shared_lock = session.stash.get('mutmut_shared_lock', None) | ||
| if shared_lock: | ||
| shared_lock.release() | ||
| return | ||
|
|
||
| if get_xdist_worker_id(session) != "master": | ||
| return | ||
|
|
||
| lock_file = session.stash[fixture_lock] | ||
| session.stash[fixture_lock] = None | ||
| if fixture_lock in session.stash: | ||
| lock_file = session.stash[fixture_lock] | ||
| session.stash[fixture_lock] = None | ||
|
|
||
| assert lock_file is not None | ||
| lock_file.release() | ||
|
|
||
|
|
||
| assert lock_file is not None | ||
| lock_file.release() | ||
| # This is required explicitly becuase when the source does not have any | ||
| # mutable code, mutmut does not run the forced fail condition. | ||
| @pytest.fixture(autouse=True) | ||
| def mutmut_forced_fail() -> None: | ||
| if os.environ.get("MUTANT_UNDER_TEST") == "fail": | ||
| pytest.fail("Forced fail for mutmut sanity check") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use the same locking code for both mutmut and non-mutmut runs. The more fine-grained locking isn't going to hurt our normal tests at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tried doing a unified locking - but somehow mutmut tests unusually exits from the point the fixtures are deleted 🤔
can investigate into this more if we want to unify them!