Repository navigation
test: refuse a session that imports another checkout's apm_cli - #5809
Open
Daniel Meppiel (danielmeppiel) wants to merge 3 commits into
Open
Daniel Meppiel (danielmeppiel) wants to merge 3 commits into
Daniel Meppiel (danielmeppiel) wants to merge 3 commits into
Conversation
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>
Contributor
There was a problem hiding this comment.
🟢 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
A venv borrowed from another worktree carries an editable
apm_clithat points at that worktree'ssrc/. Tests then exercise that tree's code. Subprocesses that dropPYTHONPATH, such as the full-collection checks intest_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()andrequire_local_apm_cli(). Asrc/apm_clioutside this checkout raisespytest.UsageErrorthat names the foreign path and gives the recipe (uv sync --frozen --extra devin this checkout, then use its.venv). Installed builds (site-packages) and in-checkout copies (mutmut'smutants/src) are allowed. The check usesimportlib.util.find_specand imports nothing.tests/conftest.pycalls it at module load, before any conftest or test module importsapm_cli. That includes the subdirectory conftests that import it at top level.tests/unit/test_source_tree_guard.py:src/apm_clifirst onPYTHONPATH. 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
Testing
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 --changedpasses;ruff checkandruff format --check(0.15.12) are clean on the CI paths.src/apm_clifirst onPYTHONPATH, the session stops at conftest load with the hint.Changelog
Spec conformance (OpenAPM v0.1)
Agent assessment
PYTHONPATH(test_ci_topology.py:1273), and the borrowed venv'sapm_cli.__file__pointed at another worktree.