Skip to content

test: enforce checkout imports across the suite - #557

Open
mangeshraut712 wants to merge 5 commits into
awslabs:devfrom
mangeshraut712:fix/441-test-checkout-guard
Open

mangeshraut712 wants to merge 5 commits into
awslabs:devfrom
mangeshraut712:fix/441-test-checkout-guard

Conversation

@mangeshraut712

@mangeshraut712 mangeshraut712 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Issue #, if available:

Fixes #441.

Description of changes:

The import-origin guard now lives in tests/conftest.py as a pytest_sessionstart hook that calls pytest.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. The tests/integrations/conftest.py copy is removed, and the tests and integrations READMEs are updated.

tests/test_checkout_guard.py adds 20 subprocess cases: another checkout and a similarly named sibling directory (prefix match), from tests/ and tests/integrations, with and without xfail; 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.

  • With the guard removed from tests/conftest.py: 16 failed, 4 passed.
  • With this change: 20 passed.
  • The real top-level export tests imported from a sibling checkout give a false green on dev (31 passed); with the guard the same run exits 4 before collection.
  • Non-integration suite at head fc7aa10: 4121 passed, 13 skipped, 1 xfailed (Python 3.12). A fresh uv sync --frozen environment imports this checkout without PYTHONPATH and passes the same suite, so CI's setup is unaffected.
  • Changed Python files pass Ruff and formatting; repository-wide Ruff has the same 17 pre-existing findings.

Because the hook runs before collection, --collect-only against 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.

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 vawsgit 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.

needs changes

One medium finding. A test marked xfail gets past the guard and passes against the wrong tree. Measured at head e6712d7.

imported = Path(inspect.getfile(stickler)).resolve()
if not imported.is_relative_to(CHECKOUT_SRC.resolve()):
pytest.fail(
f"stickler imported from {imported}, not this checkout ({CHECKOUT_SRC}). "
"Run uv sync --frozen in this checkout, or set PYTHONPATH to its src/ "
"directory. Install strands-agents-evals without the [stickler] extra.",
pytrace=False,
)

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's coverage 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_to exists on 3.10. "Fixes #441" is correct. No conflict with dev.

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-25 copies the whole root conftest into the fake checkout. Its integrations/conftest.py entry 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.
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

@vawsgit Fixed in 8107464. The guard now calls pytest.exit(reason=..., returncode=pytest.ExitCode.USAGE_ERROR). With PYTHONPATH pointed at a git archive of dev's src/, test_zero_config_evaluate_path exits 4 and prints the message once. Against this checkout, the file gives 15 passed, 1 xfailed. test_checkout_guard.py now expects exit 4, one guard message, and no passed or xfailed result. It also runs each wrong-tree case with a strict-xfail probe, and with the old pytest.fail guard those cases fail. tests/test_checkout_guard.py tests/integrations gives 657 passed, 1 skipped. Both READMEs now say the run stops instead of every test failing. Could you re-review?

@sromoam sromoam self-assigned this Nov 11, 2026

@sromoam sromoam 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.

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

  1. The guard crashes under pytest-xdist. tests/README.md lists pytest tests/ -n auto as a quick command. In that mode, a pytest.exit raised from a session fixture in a worker crashes the controller. The run ends with exit code 3 and an INTERNALERROR traceback, and the guard's message never appears. We reproduced this with xdist 3.8.0 and pytest 9.0.3, with stickler imported from another directory.

    The fix is small: do the same check in a pytest_sessionstart(session) hook in tests/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 needing pytest.exit as a special case.

  2. Handle a namespace-package stickler. If a stale stickler/ directory with no __init__.py comes first on sys.path, inspect.getfile(stickler) raises TypeError. 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.

  3. Cover the parallel mode in the harness (tests/test_checkout_guard.py). The harness sets PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 and copies no tests/__init__.py, so it never exercises xdist or the repo's real package layout. One case that runs with -p xdist -n 2 would have caught item 1.

Smaller things

  • _run_guard still copies integrations/conftest.py when 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!

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Thank you @sromoam for the detailed review! All points have been addressed in fc7aa10:

  1. pytest_sessionstart hook: Moved the import guard from the session fixture to a pytest_sessionstart(session) hook in tests/conftest.py. It executes once in the controller before any xdist workers start, exiting cleanly with code 4 under both serial execution and -n auto without worker internal errors or swallowing by xfail.
  2. Namespace-package support: Handled modules without __file__ (module_file = getattr(stickler, "__file__", None)), treating a missing origin as an invalid checkout.
  3. Parallel coverage in harness: Added test cases running under -p xdist -n 2 in tests/test_checkout_guard.py to ensure parallel execution regressions are caught.
  4. Cleanup: Removed the branch copying integrations/conftest.py.
  5. Parametrization & description: Reduced directory parametrization to top-level and one nested directory, halving subprocess runs, and updated the PR description accordingly.

Whenever you have a moment, please take a look!

@vawsgit vawsgit 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.

✅ 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 xfail test passed against the wrong tree. Fixed. The guard is now a pytest_sessionstart hook that calls pytest.exit(..., USAGE_ERROR). test_anls_secret_refusal.py::...::test_zero_config_evaluate_path against the wrong tree exits 4, prints the guard message, and reports no xfailed or passed line. Serial runs and -n 4 runs 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.0 in site-packages, pytest tests/integrations, pytest -n 2 and CI's coverage run -m pytest -v -s all exit 4 with the guard message.
  • The harness still copied integrations/conftest.py. Fixed. The harness now copies only tests/conftest.py.

@sromoam's review (on 8107464):

  • The guard crashed under xdist. Fixed. At 8107464, the wrong tree with -n 2 exits 3 with 35 INTERNALERROR lines, and the guard message never prints. At head, the same run exits 4 with 0 INTERNALERROR lines, and the guard message prints. A full-suite run with -n auto gives the same result. I reproduced the -n 2 result myself.
  • A namespace-package stickler became an ordinary setup error. Fixed. I tested an empty stickler/ directory with no __init__.py. With -n 2 the run exits 4 with "stickler imported from a package without file".
  • The harness never ran xdist. Fixed. With the 8107464 conftest restored, 6 harness tests fail. Four of them are the -p xdist -n 2 cases.
  • 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 behind dev, but none of those commits touch these files. I tested a local merge into dev. 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 4 and CI's coverage run -m pytest -v -s each gave 4138 passed, 3 skipped and 1 xfailed.

  • A copy of the strands-evals CI job. strands-agents-evals==1.6.0 gave 821 passed and 1 xfailed, and stickler imported from the checkout. Forcing stickler-eval==0.7.0 makes the guard stop the run, as intended.

  • Python 3.10 and 3.14. tests/test_checkout_guard.py tests/integrations gave 661 passed and 1 skipped on each version. On 3.14 the wrong tree with -n 2 exits 4.

  • Mutation tests on tests/conftest.py. The harness (20 tests) caught each break:

    Mutation Harness failures
    Guard disabled 16
    pytest.fail instead of pytest.exit 16
    __file__ is None treated as OK 4
    inspect.getfile instead of the getattr fallback 4
    String startswith instead of is_relative_to 4
    .resolve() dropped 1

    Moving the hook to pytest_configure causes 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-only is not blocked, but at head --collect-only against 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, and is_relative_to compares case-sensitively, but APFS ignores case. After cd ~/documents/code/github/..., PYTHONPATH=$PWD/src names 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.samefile against the parent directories would fix this if you want it.

I did not test Windows.

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Merged current dev, including the 1.0.2 cut, in 332cdde. This PR has no changelog bullets, so CHANGELOG.md matches dev: Unreleased is empty and the 1.0.2 notes stay under ## [1.0.2]. Locally, tests/test_checkout_guard.py passed (20 tests) and pytest tests/ --ignore=tests/integrations passed (4946 passed, 12 skipped, 1 xfailed). Ruff is clean on the PR files.

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.

Fail the whole test suite when stickler is not imported from this checkout

3 participants