summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-11-21 10:44:28 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-11-21 11:10:21 -0500
commitdd8de3b0081190839dadfb83f038b701808594ec (patch)
treed81905c190411d9774357e7cf3376b456c83ca58
parenteaf4a2be6de1a063e6c89daedee5f77177729da9 (diff)
downloadpython-coveragepy-git-dd8de3b0081190839dadfb83f038b701808594ec.tar.gz
test(fix): remove the subcover.pth file when the main session ends
Leaving the pth file meant that the next igor run would start coverage, which meant igor couldn't delete the C extension file on Windows.
-rw-r--r--tests/conftest.py36
1 files changed, 20 insertions, 16 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index ab546280..ca94e5d2 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -90,6 +90,7 @@ def reset_filesdotpy_globals():
set_relative_directory()
yield
+WORKER = os.environ.get("PYTEST_XDIST_WORKER", "none")
TRACK_TESTS = False
TEST_TXT = "/tmp/tests.txt"
@@ -100,8 +101,25 @@ def pytest_sessionstart():
with open(TEST_TXT, "w") as testtxt:
print("Starting:", file=testtxt)
+ # Create a .pth file for measuring subprocess coverage.
+ if WORKER == "none":
+ pth_dir = find_writable_pth_directory()
+ assert pth_dir
+ pth_file = pth_dir / "subcover.pth"
+ if not pth_file.exists():
+ pth_file.write_text("import coverage; coverage.process_startup()\n")
+ # pth_path is deleted by pytest_sessionfinish below.
+
+
+def pytest_sessionfinish():
+ """Hook the end of a test session, to clean up."""
+ # This is called by each of the workers and by the main process.
+ if WORKER == "none":
+ for pth_dir in possible_pth_dirs(): # pragma: part covered
+ pth_file = pth_dir / "subcover.pth"
+ if pth_file.exists():
+ pth_file.unlink()
-WORKER = os.environ.get("PYTEST_XDIST_WORKER", "only")
def write_test_name(prefix):
"""For tracking where and when tests are running."""
@@ -136,6 +154,7 @@ def interleaved(firsts, rest, n):
yield alist.pop()
num -= 1
+
def pytest_collection_modifyitems(items):
"""Re-order the collected tests."""
# Trick the xdist scheduler to put all of the VirtualenvTest tests on the
@@ -176,18 +195,3 @@ def find_writable_pth_directory():
return pth_dir
return None # pragma: cant happen
-
-
-@pytest.fixture(scope="session", autouse=True)
-def create_pth_file():
- """Create a .pth file for measuring subprocess coverage."""
- pth_dir = find_writable_pth_directory()
- assert pth_dir
- pth_path = pth_dir / "subcover.pth"
- if not pth_path.exists():
- pth_path.write_text("import coverage; coverage.process_startup()\n")
-
- yield
-
- # We leave the pth file in place. This seems not-great, but deleting it
- # seems to always cause problems among the test workers.