|
| 1 | +"""Structural boundary between unified pytest and the copied CMP E2E suite. |
| 2 | +
|
| 3 | +The root ``pyproject.toml`` loads this plugin explicitly. Additive command-line |
| 4 | +and ``PYTEST_ADDOPTS`` options, including ``--confcutdir`` and ``--rootdir``, remain protected. |
| 5 | +Replacing the configured ``addopts`` or explicitly disabling this plugin is an |
| 6 | +operator decision outside this boundary. An arbitrary in-process plugin can |
| 7 | +also intentionally unregister or corrupt other plugins, so that is not claimed |
| 8 | +as a supported threat boundary. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import posixpath |
| 14 | +from collections.abc import Generator, Sequence |
| 15 | +from dataclasses import dataclass |
| 16 | +from pathlib import Path, PurePosixPath |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +REPOSITORY_ROOT = Path(__file__).resolve().parents[1] |
| 21 | +UNIFIED_PYTEST_CONFIG = REPOSITORY_ROOT / "pyproject.toml" |
| 22 | +UNIFIED_COPIED_E2E_DENIAL = ( |
| 23 | + "copied CMP E2E tests cannot run under the unified root pytest configuration; " |
| 24 | + "use the standalone e2e/pytest.ini configuration" |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@dataclass(frozen=True, slots=True) |
| 29 | +class _ItemProvenance: |
| 30 | + node_path: str | None |
| 31 | + source_paths: tuple[str, ...] |
| 32 | + |
| 33 | + |
| 34 | +_ORIGINAL_PROVENANCE = pytest.StashKey[_ItemProvenance]() |
| 35 | + |
| 36 | + |
| 37 | +@pytest.hookimpl(wrapper=True, tryfirst=True) |
| 38 | +def pytest_itemcollected(item: pytest.Item) -> Generator[None]: |
| 39 | + """Snapshot provenance before ordinary item-collected hooks can mutate it.""" |
| 40 | + item.stash[_ORIGINAL_PROVENANCE] = _capture_provenance(item) |
| 41 | + yield |
| 42 | + |
| 43 | + |
| 44 | +@pytest.hookimpl(trylast=True) |
| 45 | +def pytest_collection_finish(session: pytest.Session) -> None: |
| 46 | + _reject_selected_copied_e2e(session.config, session.items) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.hookimpl(tryfirst=True) |
| 50 | +def pytest_runtest_protocol(item: pytest.Item, nextitem: pytest.Item | None) -> None: |
| 51 | + del nextitem |
| 52 | + _reject_selected_copied_e2e(item.config, (item,)) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.hookimpl(tryfirst=True) |
| 56 | +def pytest_runtest_setup(item: pytest.Item) -> None: |
| 57 | + _reject_selected_copied_e2e(item.config, (item,)) |
| 58 | + |
| 59 | + |
| 60 | +def _reject_selected_copied_e2e( |
| 61 | + config: pytest.Config, |
| 62 | + items: Sequence[pytest.Item], |
| 63 | +) -> None: |
| 64 | + if not _uses_unified_config(config): |
| 65 | + return |
| 66 | + if any(_is_copied_e2e_item(item) for item in items): |
| 67 | + raise pytest.UsageError(UNIFIED_COPIED_E2E_DENIAL) |
| 68 | + |
| 69 | + |
| 70 | +def _uses_unified_config(config: pytest.Config) -> bool: |
| 71 | + inipath = config.inipath |
| 72 | + return inipath is not None and Path(inipath).resolve() == UNIFIED_PYTEST_CONFIG |
| 73 | + |
| 74 | + |
| 75 | +def _is_copied_e2e_item(item: pytest.Item) -> bool: |
| 76 | + original = item.stash.get(_ORIGINAL_PROVENANCE, None) |
| 77 | + current = _capture_provenance(item) |
| 78 | + provenances = (current,) if original is None else (original, current) |
| 79 | + return any(_is_copied_e2e_provenance(provenance) for provenance in provenances) |
| 80 | + |
| 81 | + |
| 82 | +def _capture_provenance(item: pytest.Item) -> _ItemProvenance: |
| 83 | + paths: list[str] = [] |
| 84 | + for collector in item.listchain(): |
| 85 | + _append_resolved_path(paths, getattr(collector, "path", None)) |
| 86 | + _append_resolved_path(paths, getattr(item, "path", None)) |
| 87 | + module = getattr(item, "module", None) |
| 88 | + _append_resolved_path(paths, getattr(module, "__file__", None)) |
| 89 | + return _ItemProvenance( |
| 90 | + node_path=_normalized_node_path(item.nodeid), |
| 91 | + source_paths=tuple(dict.fromkeys(paths)), |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def _append_resolved_path(paths: list[str], value: object) -> None: |
| 96 | + if value is None: |
| 97 | + return |
| 98 | + try: |
| 99 | + paths.append(Path(str(value)).resolve().as_posix()) |
| 100 | + except (OSError, RuntimeError, ValueError): |
| 101 | + return |
| 102 | + |
| 103 | + |
| 104 | +def _normalized_node_path(nodeid: str) -> str | None: |
| 105 | + raw_path = nodeid.partition("::")[0].replace("\\", "/") |
| 106 | + normalized = posixpath.normpath(raw_path) |
| 107 | + path = PurePosixPath(normalized) |
| 108 | + if normalized in {"", "."} or path.is_absolute() or ".." in path.parts: |
| 109 | + return None |
| 110 | + return path.as_posix() |
| 111 | + |
| 112 | + |
| 113 | +def _is_copied_e2e_provenance(provenance: _ItemProvenance) -> bool: |
| 114 | + if provenance.node_path == "e2e" or (provenance.node_path or "").startswith("e2e/"): |
| 115 | + return True |
| 116 | + for source in provenance.source_paths: |
| 117 | + try: |
| 118 | + relative = Path(source).relative_to(REPOSITORY_ROOT) |
| 119 | + except ValueError: |
| 120 | + continue |
| 121 | + if relative.parts[:1] == ("e2e",): |
| 122 | + return True |
| 123 | + return False |
0 commit comments