diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2021-11-21 09:07:00 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2021-11-21 09:07:00 -0500 |
commit | 1ac09127438adac06de02172e4fad34d7b096be3 (patch) | |
tree | 505823280140facfa2fe1ab81fcf3dd6493a983e | |
parent | 57768ee130fe2b0e7d1b27399ff2c73aa1cdbfe3 (diff) | |
download | python-coveragepy-git-1ac09127438adac06de02172e4fad34d7b096be3.tar.gz |
test(refactor): use pathlib for the pth files
-rw-r--r-- | tests/conftest.py | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index 6d69fb20..ab546280 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,12 +7,12 @@ Pytest auto configuration. This module is run automatically by pytest, to define and enable fixtures. """ -import glob import itertools import os import sys import sysconfig import warnings +from pathlib import Path import pytest @@ -153,23 +153,22 @@ def possible_pth_dirs(): """Produce a sequence of directories for trying to write .pth files.""" # First look through sys.path, and if we find a .pth file, then it's a good # place to put ours. - for pth_dir in sys.path: # pragma: part covered - pth_files = glob.glob(os.path.join(pth_dir, "*.pth")) + for pth_dir in map(Path, sys.path): # pragma: part covered + pth_files = list(pth_dir.glob("*.pth")) if pth_files: yield pth_dir # If we're still looking, then try the Python library directory. # https://github.com/nedbat/coveragepy/issues/339 - yield sysconfig.get_path("purelib") # pragma: cant happen + yield Path(sysconfig.get_path("purelib")) # pragma: cant happen def find_writable_pth_directory(): """Find a place to write a .pth file.""" for pth_dir in possible_pth_dirs(): # pragma: part covered - try_it = os.path.join(pth_dir, f"touch_{WORKER}.it") + try_it = pth_dir / f"touch_{WORKER}.it" try: - with open(try_it, "w") as f: - f.write("foo") + try_it.write_text("foo") except OSError: # pragma: cant happen continue @@ -184,10 +183,9 @@ def create_pth_file(): """Create a .pth file for measuring subprocess coverage.""" pth_dir = find_writable_pth_directory() assert pth_dir - pth_path = os.path.join(pth_dir, "subcover.pth") - if not os.path.exists(pth_path): - with open(pth_path, "w") as pth: - pth.write("import coverage; coverage.process_startup()\n") + pth_path = pth_dir / "subcover.pth" + if not pth_path.exists(): + pth_path.write_text("import coverage; coverage.process_startup()\n") yield |