Skip to content

test: refuse a session that imports another checkout's apm_cli - #5809

Open
Daniel Meppiel (danielmeppiel) wants to merge 3 commits into
mainfrom
test/conftest-foreign-apm-cli-guard
Open

Daniel Meppiel (danielmeppiel) wants to merge 3 commits into
mainfrom
test/conftest-foreign-apm-cli-guard

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

A venv borrowed from another worktree carries an editable apm_cli that points at that worktree's src/. Tests then exercise that tree's code. Subprocesses that drop PYTHONPATH, such as the full-collection checks in test_ci_topology.py, import it too. Local results then silently disagree with CI: this happened three times on 2026-12-02, including a false XFAIL count.

Change (test-only):

  • tests/utils/source_tree_guard.py: foreign_source_root() and require_local_apm_cli(). A src/apm_cli outside this checkout raises pytest.UsageError that names the foreign path and gives the recipe (uv sync --frozen --extra dev in this checkout, then use its .venv). Installed builds (site-packages) and in-checkout copies (mutmut's mutants/src) are allowed. The check uses importlib.util.find_spec and imports nothing.
  • tests/conftest.py calls it at module load, before any conftest or test module imports apm_cli. That includes the subdirectory conftests that import it at top level.
  • tests/unit/test_source_tree_guard.py:
    • unit cases for the foreign, this-checkout, in-checkout-copy and installed-build origins;
    • a pytester case that runs an inner session in a subprocess with a foreign src/apm_cli first on PYTHONPATH. The session must fail with the hint and must not run any test.

Issue and approved scope

Issue: Fixes #5808

Human scope-approval comment: #5808 (comment)

Does this PR complete the issue, or what remains? It completes the issue. Done when: the pytester case fails with the hint, the full suite collects unchanged in CI, and QL signs.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally (with PYTHONPATH=<this checkout>/src):
    • pytest tests/unit/test_source_tree_guard.py tests/unit/test_real_home_guard.py -> 39 passed;
    • pytest tests/unit/test_source_tree_guard.py tests/quality/test_test_taxonomy.py tests/quality/test_promise_obligations.py -> 75 passed;
    • pytest --collect-only tests -> 57254/57489 collected (235 deselected), unchanged;
    • python -m tests.quality.taxonomy --changed passes;
    • ruff check and ruff format --check (0.15.12) are clean on the CI paths.
  • Manual negative check: with a foreign src/apm_cli first on PYTHONPATH, the session stops at conftest load with the hint.
  • Added tests for new functionality.

Changelog

  • One fragment per section
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

Agent assessment

  • Change confidence: high. The guard is small and read-only. Allowed cases (CI's editable install in the checkout, installed builds, mutmut copies) are unit-tested, and the full suite collects unchanged.
  • Diagnosis confidence: high. The full-collection subprocess strips PYTHONPATH (test_ci_topology.py:1273), and the borrowed venv's apm_cli.__file__ pointed at another worktree.
  • Evidence fit: strong. Three independent occurrences on 2026-12-02, and the ModuleNotFoundError reproduced locally.

A venv borrowed from another worktree runs that worktree's src/apm_cli, and subprocesses that drop PYTHONPATH import it too, so local results silently disagree with CI. tests/conftest.py now calls require_local_apm_cli before any conftest or test module imports apm_cli: a src/apm_cli outside this checkout fails the session with the uv sync recipe. Installed builds and in-checkout copies such as mutmut's mutants/src are allowed. A pytester case runs an inner session with a foreign apm_cli first on sys.path and checks the hint.

Fixes #5808

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 22:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the test-only guard against importing apm_cli from another checkout.

No open issues 🎉

What changed in this PR

Adds a test-only guard for #5808 so pytest stops early when apm_cli resolves to another checkout's source tree rather than silently testing the wrong code.

Changes:

  • Detects foreign source trees while allowing installed builds and in-checkout copies.
  • Runs the guard before test collection and provides an environment repair hint.
  • Adds path-classification tests and a subprocess rejection test.
File Description
tests/​utils/​source_tree_guard.py Detects foreign source imports and raises a usage error.
tests/​unit/​test_source_tree_guard.py Tests allowed origins and foreign-source session rejection.
tests/​conftest.py Invokes the guard during conftest loading.

Review effort: Balanced

Run the real tests/conftest.py in a subprocess with a foreign src/apm_cli first on PYTHONPATH; the session must fail with the hint. Replacing the require_local_apm_cli call in tests/conftest.py with pass turns this case red (QL C1 on #5809).

Refs #5808

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
The pure foreign_source_root cases move to a unit-marked tests/unit module, and the two session cases that spawn pytest (pytester and the real-conftest wiring proof) move to tests/integration/test_source_tree_guard_session.py, keeping the component mark and P-INV-I7 (QL on #5809: tm006 process spawn in tests/unit, AQ003 on return-value-only component cases).

Refs #5808

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Tests: a borrowed venv silently runs another worktree's apm_cli and produces false results

2 participants