Repository navigation
test: enforce checkout imports across the suite - #557
mangeshraut712 wants to merge 5 commits into
Conversation
Address awslabs#441 by extending the existing session-scoped import guard to all tests. Add subprocess regressions for wrong worktrees, sibling source paths and valid symlinks. AI-assisted implementation with independent review. Full suite passes in both the existing environment and a new frozen isolated environment on Python 3.12.15.
vawsgit
left a comment
There was a problem hiding this comment.
needs changes
One medium finding. A test marked xfail gets past the guard and passes against the wrong tree. Measured at head e6712d7.
Lines 19 to 26 in e6712d7
The guard calls pytest.fail inside a fixture. pytest records a fixture failure in an xfail test (with no raises=) as "xfailed". So tests/comparators/test_anls_secret_refusal.py::TestTheModelLevelReproduction::test_zero_config_evaluate_path (xfail(strict=True)) passes against another tree. Its reason says it should flip to XPASS once the masking fix merges, which is exactly the measurement this guard exists to protect.
With PYTHONPATH pointed at a git archive of origin/dev's src/, running that one test gives:
tests/comparators/test_anls_secret_refusal.py x [100%]
============================== 1 xfailed in 0.03s ==============================
The exit code is 0, and the guard message never prints. The full suite still fails (4128 errors), so the false pass needs a single-test or -k run. That is how someone checks one xfail. It also makes "for every test" in tests/README.md and "Fails every test in the suite" in tests/integrations/README.md untrue.
Fix: use pytest.exit(reason=..., returncode=pytest.ExitCode.USAGE_ERROR) instead of pytest.fail(...). Against the wrong tree that gives exit 4, the message prints once, and no tests run. Against the right tree, the file gives 15 passed, 1 xfailed. Update tests/test_checkout_guard.py:81-82, which expects TESTS_FAILED and "1 error".
Verified and cleared
- The new tests give 10 passed. "On dev ce6c100, 4 failed, 6 passed" reproduces.
- Full suite with the llm and bert extras,
-n 4: 4128 passed, 3 skipped, 1 xfailed. CI'scoverage run -m pytest -v -s tests/test_checkout_guard.py tests/integrations: 651 passed, 1 skipped. - Mutations are caught: guard disabled, string-prefix match instead of
is_relative_to,.resolve()dropped, not autouse,parents[2]. Path.is_relative_toexists on 3.10. "Fixes #441" is correct. No conflict withdev.
Non-blocking (low)
- When collection fails (for example against v0.7.0), the run stops with an ImportError before the guard message prints. It still fails loudly.
tests/test_checkout_guard.py:20-25copies the whole root conftest into the fake checkout. Itsintegrations/conftest.pyentry is dead at head.
pytest records a fixture failure inside an xfail test (no raises=) as "xfailed", so running a single xfail test against another tree exited 0 and never printed the guard message. The guard now calls pytest.exit with ExitCode.USAGE_ERROR, which stops the session before any test runs and prints the message once. The isolated guard runs now also cover a strict xfail probe, and expect exit code 4, a single guard message, and no passed or xfailed result. The test READMEs describe the new behaviour.
|
@vawsgit Fixed in |
sromoam
left a comment
There was a problem hiding this comment.
Thank you, @mangeshraut712! Guarding against a wrong-checkout import is exactly what this suite needs, and the test harness around it is careful work. One blocking problem, and a few smaller ones.
Needs changes
-
The guard crashes under pytest-xdist.
tests/README.mdlistspytest tests/ -n autoas a quick command. In that mode, apytest.exitraised from a session fixture in a worker crashes the controller. The run ends with exit code 3 and anINTERNALERRORtraceback, and the guard's message never appears. We reproduced this with xdist 3.8.0 and pytest 9.0.3, withsticklerimported from another directory.The fix is small: do the same check in a
pytest_sessionstart(session)hook intests/conftest.py, instead of an autouse session fixture. That hook runs once, in the controller, before any worker or test starts. We tried it: it exits 4 with the message both serially and with-n 4, and it also closes the xfail hole without needingpytest.exitas a special case. -
Handle a namespace-package
stickler. If a stalestickler/directory with no__init__.pycomes first onsys.path,inspect.getfile(stickler)raisesTypeError. The guard then becomes an ordinary setup error, and xfail tests are reported as "xfailed" again. Treat a module with no__file__as the wrong checkout and give the same message. -
Cover the parallel mode in the harness (
tests/test_checkout_guard.py). The harness setsPYTEST_DISABLE_PLUGIN_AUTOLOAD=1and copies notests/__init__.py, so it never exercises xdist or the repo's real package layout. One case that runs with-p xdist -n 2would have caught item 1.
Smaller things
_run_guardstill copiesintegrations/conftest.pywhen it exists, but this PR deletes that file, so the branch can go.- With one guard in
tests/conftest.py, the three-directory parametrization isn't needed. One nested directory plus the top level covers the same behaviour with about half the subprocess runs. The PR description's "ten subprocess cases" also needs updating.
A suggestion (not blocking): adding pythonpath = ["src"] under [tool.pytest.ini_options] would make pytest import this checkout in the first place, with the guard as a backstop. It's worth a separate discussion, so feel free to leave it out of this PR.
Thanks again, this is a really useful safeguard!
|
Thank you @sromoam for the detailed review! All points have been addressed in
Whenever you have a moment, please take a look! |
vawsgit
left a comment
There was a problem hiding this comment.
✅ LGTM!
I reviewed head fc7aa101 and found no medium or high findings. Every blocking finding from my review and from @sromoam's review is fixed. I measured each fix.
Before merging: CI has never run on this PR. All nine workflow runs on this branch are action_required. They are fork workflows that wait for a maintainer to approve them. A maintainer must approve the runs and see them pass. The local runs below use CI's commands, but they do not replace CI.
All runs used Python 3.12, pytest 9.0.3 and xdist 3.8.0, after uv sync --extra llm --extra bert --frozen. Each run printed inspect.getfile(stickler). "Wrong tree" means PYTHONPATH pointed at a git archive of dev's src/.
Earlier findings
My review (on e6712d7):
- An
xfailtest passed against the wrong tree. Fixed. The guard is now apytest_sessionstarthook that callspytest.exit(..., USAGE_ERROR).test_anls_secret_refusal.py::...::test_zero_config_evaluate_pathagainst the wrong tree exits 4, prints the guard message, and reports noxfailedorpassedline. Serial runs and-n 4runs behave the same. - Collection failed against 0.7.0 before the guard message printed. Fixed. The hook now runs before collection. With
stickler-eval==0.7.0in site-packages,pytest tests/integrations,pytest -n 2and CI'scoverage run -m pytest -v -sall exit 4 with the guard message. - The harness still copied
integrations/conftest.py. Fixed. The harness now copies onlytests/conftest.py.
@sromoam's review (on 8107464):
- The guard crashed under xdist. Fixed. At
8107464, the wrong tree with-n 2exits 3 with 35INTERNALERRORlines, and the guard message never prints. At head, the same run exits 4 with 0INTERNALERRORlines, and the guard message prints. A full-suite run with-n autogives the same result. I reproduced the-n 2result myself. - A namespace-package
sticklerbecame an ordinary setup error. Fixed. I tested an emptystickler/directory with no__init__.py. With-n 2the run exits 4 with "stickler imported from a package without file". - The harness never ran xdist. Fixed. With the
8107464conftest restored, 6 harness tests fail. Four of them are the-p xdist -n 2cases. - The optional suggestions are done, except the PR description update (see the non-blocking notes below). The two-directory parametrization is done. The copy branch is removed. The optional
pythonpath = ["src"]was not adopted.
What I checked
-
Diff against current
dev. The PR changes 5 files and no production code, dependencies or workflows. The branch is 56 commits behinddev, but none of those commits touch these files. I tested a local merge intodev. With the right tree and-n 4, 4201 passed, 3 skipped and 1 xfailed. With the wrong tree, the run exits 4. -
Full suite at head, right tree.
pytest -n 4and CI'scoverage run -m pytest -v -seach gave 4138 passed, 3 skipped and 1 xfailed. -
A copy of the strands-evals CI job.
strands-agents-evals==1.6.0gave 821 passed and 1 xfailed, and stickler imported from the checkout. Forcingstickler-eval==0.7.0makes the guard stop the run, as intended. -
Python 3.10 and 3.14.
tests/test_checkout_guard.py tests/integrationsgave 661 passed and 1 skipped on each version. On 3.14 the wrong tree with-n 2exits 4. -
Mutation tests on
tests/conftest.py. The harness (20 tests) caught each break:Mutation Harness failures Guard disabled 16 pytest.failinstead ofpytest.exit16 __file__ is Nonetreated as OK4 inspect.getfileinstead of thegetattrfallback4 String startswithinstead ofis_relative_to4 .resolve()dropped1 Moving the hook to
pytest_configurecauses no failures. That is expected, because that hook also runs once in the controller before workers start.
Non-blocking notes (low, optional)
- The PR description is out of date. It still says "ten subprocess cases", "10 passed" and "4 failed, 6 passed", but the harness now has 20 cases. It also says
--collect-onlyis not blocked, but at head--collect-onlyagainst the wrong tree exits 4. - The guard rejects the right checkout when the path is typed in the wrong case on macOS (
tests/conftest.py:22).Path.resolve()keeps the typed case, andis_relative_tocompares case-sensitively, but APFS ignores case. Aftercd ~/documents/code/github/...,PYTHONPATH=$PWD/srcnames the right checkout and the run still exits 4. The guard only blocks the run and never reports a wrong number, and CI is unaffected.os.path.samefileagainst the parent directories would fix this if you want it.
I did not test Windows.
|
Merged current |
Issue #, if available:
Fixes #441.
Description of changes:
The import-origin guard now lives in
tests/conftest.pyas apytest_sessionstarthook that callspytest.exit(..., USAGE_ERROR), so it runs once before collection and before xdist workers start, and it covers the whole suite instead of only the Strands integrations. The error reports the imported and expected paths and explains how to select this checkout. Thetests/integrations/conftest.pycopy is removed, and the tests and integrations READMEs are updated.tests/test_checkout_guard.pyadds 20 subprocess cases: another checkout and a similarly named sibling directory (prefix match), fromtests/andtests/integrations, with and withoutxfail; another checkout and a namespace package without__file__, serially and with-p xdist -n 2; and the correct checkout from both directories, a symlink to it, and a parallel run.tests/conftest.py: 16 failed, 4 passed.uv sync --frozenenvironment imports this checkout withoutPYTHONPATHand passes the same suite, so CI's setup is unaffected.Because the hook runs before collection,
--collect-onlyagainst the wrong checkout also exits 4. No production code or dependency changes.AI-assisted implementation, testing and review.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.