|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import os |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from _pytest.config import Config |
| 9 | +from _pytest.config.argparsing import Parser |
| 10 | + |
| 11 | +PROJECT_ROOT = Path(__file__).resolve().parents[1] |
| 12 | +SRC_PATH = PROJECT_ROOT / "src" |
| 13 | +if SRC_PATH.is_dir(): |
| 14 | + src_str = str(SRC_PATH) |
| 15 | + if src_str not in sys.path: |
| 16 | + sys.path.insert(0, src_str) |
| 17 | + |
| 18 | + existing = os.environ.get("PYTHONPATH") |
| 19 | + if existing: |
| 20 | + if src_str not in existing.split(os.pathsep): |
| 21 | + os.environ["PYTHONPATH"] = os.pathsep.join([src_str, existing]) |
| 22 | + else: |
| 23 | + os.environ["PYTHONPATH"] = src_str |
| 24 | + |
| 25 | +_HAS_PYTEST_COV = importlib.util.find_spec("pytest_cov") is not None |
| 26 | + |
| 27 | + |
| 28 | +def pytest_addoption(parser: Parser) -> None: |
| 29 | + """Register no-op coverage flags when pytest-cov is unavailable.""" |
| 30 | + |
| 31 | + if _HAS_PYTEST_COV: |
| 32 | + return |
| 33 | + |
| 34 | + group = parser.getgroup("cov (missing plugin)") |
| 35 | + group.addoption( |
| 36 | + "--cov", |
| 37 | + action="append", |
| 38 | + dest="cov", |
| 39 | + default=[], |
| 40 | + metavar="PATH", |
| 41 | + help="coverage reporting requires pytest-cov; option accepted but ignored.", |
| 42 | + ) |
| 43 | + group.addoption( |
| 44 | + "--cov-report", |
| 45 | + action="append", |
| 46 | + dest="cov_report", |
| 47 | + default=[], |
| 48 | + help="coverage reporting requires pytest-cov; option accepted but ignored.", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def pytest_configure(config: Config) -> None: |
| 53 | + if _HAS_PYTEST_COV: |
| 54 | + return |
| 55 | + |
| 56 | + if getattr(config.option, "cov", None) or getattr(config.option, "cov_report", None): |
| 57 | + config.issue_config_time_warning( |
| 58 | + "pytest-cov is not installed; coverage options are ignored.", |
| 59 | + stacklevel=2, |
| 60 | + ) |
0 commit comments