|
| 1 | +"""Pytest plugin for slipcover with pytest-xdist support. |
| 2 | +
|
| 3 | +This plugin enables coverage collection when using pytest-xdist for parallel testing. |
| 4 | +It is automatically activated when SLIPCOVER_ENABLED environment variable is set |
| 5 | +(which is done by slipcover's __main__.py when running pytest). |
| 6 | +
|
| 7 | +The plugin coordinates coverage collection across xdist workers by: |
| 8 | +1. Creating a shared temp directory for coverage files (controller) |
| 9 | +2. Having each worker write its coverage to the shared directory |
| 10 | +3. Merging all worker coverage in the controller at session end |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import os |
| 15 | +import tempfile |
| 16 | +from pathlib import Path |
| 17 | +from typing import Optional |
| 18 | + |
| 19 | +import slipcover as sc |
| 20 | + |
| 21 | + |
| 22 | +# Global state for the plugin |
| 23 | +_slipcover_instance: Optional[sc.Slipcover] = None |
| 24 | +_file_matcher: Optional[sc.FileMatcher] = None |
| 25 | +_import_manager: Optional[sc.ImportManager] = None |
| 26 | +_coverage_dir: Optional[str] = None |
| 27 | + |
| 28 | + |
| 29 | +def _is_xdist_worker() -> bool: |
| 30 | + """Check if running as an xdist worker process.""" |
| 31 | + return "PYTEST_XDIST_WORKER" in os.environ |
| 32 | + |
| 33 | + |
| 34 | +def _get_worker_id() -> str: |
| 35 | + """Get the xdist worker ID (e.g., 'gw0', 'gw1'), or 'main' if not a worker.""" |
| 36 | + return os.environ.get("PYTEST_XDIST_WORKER", "main") |
| 37 | + |
| 38 | + |
| 39 | +def pytest_configure(config): |
| 40 | + """Initialize slipcover in xdist workers. |
| 41 | +
|
| 42 | + This hook runs in both the controller and worker processes. |
| 43 | + We only activate slipcover if SLIPCOVER_ENABLED is set AND we're in an xdist worker. |
| 44 | + The controller (main process) is already handled by __main__.py. |
| 45 | + """ |
| 46 | + global _slipcover_instance, _file_matcher, _import_manager, _coverage_dir |
| 47 | + |
| 48 | + # Only activate if SLIPCOVER_ENABLED is set (by __main__.py when running pytest) |
| 49 | + if not os.environ.get("SLIPCOVER_ENABLED"): |
| 50 | + return |
| 51 | + |
| 52 | + # Check if xdist is being used (look for -n option or xdist plugin config) |
| 53 | + # Note: PYTEST_XDIST_TESTRUNUID is set later, so we check numprocesses |
| 54 | + is_xdist = hasattr(config.option, 'numprocesses') and config.option.numprocesses |
| 55 | + |
| 56 | + # Controller creates shared coverage directory for workers to write to |
| 57 | + # We detect controller as: xdist is being used AND we're not a worker |
| 58 | + if is_xdist and not _is_xdist_worker(): |
| 59 | + _coverage_dir = tempfile.mkdtemp(prefix="slipcover-xdist-") |
| 60 | + os.environ["SLIPCOVER_COVERAGE_DIR"] = _coverage_dir |
| 61 | + return # Controller's slipcover is already set up by __main__.py |
| 62 | + |
| 63 | + # Get coverage directory (workers inherit this from controller) |
| 64 | + _coverage_dir = os.environ.get("SLIPCOVER_COVERAGE_DIR") |
| 65 | + |
| 66 | + # Only set up slipcover in workers - controller is handled by __main__.py |
| 67 | + if not _is_xdist_worker(): |
| 68 | + return |
| 69 | + |
| 70 | + # Parse configuration from environment (set by __main__.py) |
| 71 | + branch = os.environ.get("SLIPCOVER_BRANCH") == "1" |
| 72 | + source = os.environ.get("SLIPCOVER_SOURCE") |
| 73 | + omit = os.environ.get("SLIPCOVER_OMIT") |
| 74 | + |
| 75 | + # Set up file matcher |
| 76 | + _file_matcher = sc.FileMatcher() |
| 77 | + if source: |
| 78 | + for s in source.split(","): |
| 79 | + s = s.strip() |
| 80 | + if s: |
| 81 | + _file_matcher.addSource(s) |
| 82 | + if omit: |
| 83 | + for o in omit.split(","): |
| 84 | + o = o.strip() |
| 85 | + if o: |
| 86 | + _file_matcher.addOmit(o) |
| 87 | + |
| 88 | + # Create Slipcover instance |
| 89 | + source_list = [s.strip() for s in source.split(",")] if source else None |
| 90 | + _slipcover_instance = sc.Slipcover(branch=branch, source=source_list) |
| 91 | + |
| 92 | + # Wrap pytest's assertion rewriter for instrumentation |
| 93 | + sc.wrap_pytest(_slipcover_instance, _file_matcher) |
| 94 | + |
| 95 | + # Start import instrumentation |
| 96 | + _import_manager = sc.ImportManager(_slipcover_instance, _file_matcher) |
| 97 | + _import_manager.__enter__() |
| 98 | + |
| 99 | + |
| 100 | +def pytest_unconfigure(config): |
| 101 | + """Clean up import manager on shutdown.""" |
| 102 | + global _import_manager |
| 103 | + if _import_manager: |
| 104 | + _import_manager.__exit__(None, None, None) |
| 105 | + _import_manager = None |
| 106 | + |
| 107 | + |
| 108 | +def pytest_sessionfinish(session, exitstatus): |
| 109 | + """Handle coverage collection at session end. |
| 110 | +
|
| 111 | + Workers: Write coverage to a file in the shared directory. |
| 112 | + Controller: Merge all worker coverage files into a single merged.json. |
| 113 | + """ |
| 114 | + global _slipcover_instance, _coverage_dir |
| 115 | + |
| 116 | + if not os.environ.get("SLIPCOVER_ENABLED"): |
| 117 | + return |
| 118 | + |
| 119 | + if _is_xdist_worker() and _slipcover_instance and _coverage_dir: |
| 120 | + # Worker: write coverage to shared directory |
| 121 | + coverage = _slipcover_instance.get_coverage() |
| 122 | + worker_id = _get_worker_id() |
| 123 | + cov_file = Path(_coverage_dir) / f"coverage-{worker_id}.json" |
| 124 | + try: |
| 125 | + with open(cov_file, "w") as f: |
| 126 | + json.dump(coverage, f) |
| 127 | + except Exception as e: |
| 128 | + import warnings |
| 129 | + warnings.warn(f"slipcover: failed to write worker coverage: {e}") |
| 130 | + |
| 131 | + elif _coverage_dir and not _is_xdist_worker(): |
| 132 | + # Controller: merge all worker coverage files |
| 133 | + # We know we're the controller if _coverage_dir is set and we're not a worker |
| 134 | + coverage_dir = Path(_coverage_dir) |
| 135 | + worker_files = list(coverage_dir.glob("coverage-gw*.json")) |
| 136 | + |
| 137 | + if worker_files: |
| 138 | + # Start with first worker's coverage |
| 139 | + merged = None |
| 140 | + for cov_file in worker_files: |
| 141 | + try: |
| 142 | + with open(cov_file) as f: |
| 143 | + worker_cov = json.load(f) |
| 144 | + if merged is None: |
| 145 | + merged = worker_cov |
| 146 | + else: |
| 147 | + sc.merge_coverage(merged, worker_cov) |
| 148 | + except Exception as e: |
| 149 | + import warnings |
| 150 | + warnings.warn(f"slipcover: error reading {cov_file}: {e}") |
| 151 | + |
| 152 | + if merged: |
| 153 | + # Write merged coverage for __main__.py to read |
| 154 | + merged_file = coverage_dir / "merged.json" |
| 155 | + try: |
| 156 | + with open(merged_file, "w") as f: |
| 157 | + json.dump(merged, f) |
| 158 | + except Exception as e: |
| 159 | + import warnings |
| 160 | + warnings.warn(f"slipcover: failed to write merged coverage: {e}") |
0 commit comments