Skip to content

test(cli): no test module reads a path from its own file or imports a test module, checked in the full gate - #1711

Merged
dan-petty merged 1 commit into
release/v0.2.37from
test/1312-test-path-ban
Oct 20, 2026
Merged

dan-petty merged 1 commit into
release/v0.2.37from
test/1312-test-path-ban

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

The last of #1312's five pull requests. No test module reads a path from its own file or imports another test module, and a ban that runs in the full gate keeps it that way. This closes #1312.

The last sites.

  • The final eight Path(__file__) lines, in six test modules, read tests.support.paths.
  • No module keeps a root constant or an alias of one. tests/test_github_session.py uses REPO_ROOT. tests/test_roadmap_github_store.py imports FIXTURES and names its own directory ROADMAP_FIXTURES.

The ban: tests/test_test_module_independence.py.

  • What it reads: it parses every Python file under tests/ once, except tests/fixtures/, conftests and helper modules included.
  • What it reports, as path:line:
    • each bare __file__ outside tests/support/;
    • each import of a test module or a conftest, through absolute or relative imports, import, from X import n and as aliases;
    • each relative import past the top-level package.
  • How it decides: pytest's own python_files patterns say what a test module is. A relative import resolves against the importing module's package. from X import n counts as X.n only when X is a package or X/n.py exists.
  • What passes: a module's __file__ attribute, devops-cli modules named test_*, and functions imported from a plain support module.
  • The seeded tree: a tree under tmp_path pins each reported and each allowed form, including plants in a conftest and a helper. The banned forms are spelled in parts, so a text search over tests/ finds none.

Full gate only (the owner's decision on #1312).

  • Cost: parsing the 526 files takes about 1 s idle, and the ban about 1.8 s.
  • How it stays out of narrowed runs: it imports nothing from devops-cli or tests/ and spells no devops-cli module's name, so the selector maps no change to it. The plan's location, tests/test_architectural_invariants.py, is selected by 31 source modules.
  • Two tests in tests/test_test_selection.py hold this:
    • No narrowed selection reaches it, short of the whole suite. They check this over every source module, over the helper modules that don't reach the whole suite, and over each conftest, in a repository whose src/ is this one's.
    • The full gate collects it. The test step's pytest arguments and addopts each equal an exact list, and testpaths and python_files their exact values. No conftest or support module binds collect_ignore, collect_ignore_glob or pytest_ignore_collect. So any path, ignore, deselect, keyword or marker filter added to the gate fails it.

Move proof. In a scratch worktree, three modules were git mv'd into package directories: tests/test_release.py, tests/test_roadmap_dry_runs.py and tests/test_security_kubelinter.py. With no other edit, the suite passed and collected the same IDs once each ID's directory part was dropped.

Records.

  • docs/TESTING.md describes the check as existing.
  • changelog.d/1312.md is the item's one fragment, for its five pull requests.
  • The task file records this PR and PR 2's outcome against its own parent, and reconciles every criterion of the plan.
  • The plan's "devops ci test tests/support/paths.py --no-fallback exits 1" is recorded as superseded by PR 3's precise rule, with its reason.
  • rg -n 'tests\.conftest' tests prints one line, a subprocess source string in tests/test_openblas_worker_threads.py (test(cli): tests wait for a background thread with one generous bound, not a 1-second deadline, so they pass under load #1664), which the ban rightly ignores.

Verification

  • Tests first.
    • Real-tree ban: at the base it named exactly the 8 sites.
    • Seeded-tree test: each checker mutant fails it with a specific diff:
      • absolute imports only;
      • no X.n check;
      • X.n always;
      • no tests/support/ exemption;
      • no tests/fixtures/ exclusion;
      • test modules only;
      • the test_ prefix only;
      • Path(...) only;
      • no relative-import resolution.
    • Narrowed-run guard: it fails for the ban moved into test_architectural_invariants.py, into a directory beside a fixtures-only conftest, importing tests.support.paths, or with devops_cli in its docstring.
    • Full-gate guard: it fails for --deselect, --ignore or -k in the gate, --ignore in addopts, and collect_ignore in a conftest.
  • Review. An adversarial review, with each finding checked by a skeptic, drove the whole-value full-gate check, the conftest and helper plants, the X.n rule, the relative-import report, and PR 2's outcome record.
  • Isolation. The full suite passed under a guard that refuses gh children: 10,986 passed, 1 skipped and 8 xfailed. The only ID difference from the base is the 4 new tests.
  • Gate. uv run devops ci passed (pre-push hook).

Closes #1312

Fail-to-pass: not applicable: changes only the test harness

… test module, checked in the full gate

The last eight Path(__file__) lines, in six test modules, read tests.support.paths, and no module keeps a root constant of its own or an alias of one. Four modules drop theirs and import REPO_ROOT, tests/test_roadmap_config.py imports it in place of its inline root, and tests/test_roadmap_github_store.py imports FIXTURES and names its own directory ROADMAP_FIXTURES.

tests/test_test_module_independence.py parses every Python file under tests/ but tests/fixtures/ once, conftests and helper modules included, and names, as path:line, each bare __file__ outside tests/support/ and each import of a test module or a conftest. pytest's own python_files patterns say what a test module is, a relative import is resolved against the importing module's package and one past the top-level package is reported, and from X import n is checked as X and, when X is a package or X/n.py exists, as X.n. So a module's __file__ attribute, imports of devops-cli modules named test_* and a function imported from a plain support module pass. A seeded tree under tmp_path pins each reported and each allowed form, and spells the banned forms in parts, so a text search over tests/ finds none of them.

The ban runs only in the full gate, as the owner decided on the issue: parsing the 526 Python files under tests/ takes about 1 s idle and the ban 1.8 s. It lives in a module of its own that imports nothing from devops-cli and nothing under tests/, spells no devops-cli module's dotted name and is named for none, so the selector maps no change to it; the plan's place, tests/test_architectural_invariants.py, is selected by 31 source modules. Two tests in tests/test_test_selection.py hold this. In a repository whose src/ is this one's and whose tests/ holds the ban beside the suite's helper modules and conftest, no selection short of the whole suite reaches it, over every source module, over the helper modules that do not reach the whole suite, or over any conftest, which may narrow to its directory. And the full gate collects it: the test step's pytest arguments and pyproject.toml's addopts each equal an exact list, testpaths and python_files their exact values, and no conftest or support module binds collect_ignore, collect_ignore_glob or pytest_ignore_collect, so any path, ignore, deselect, keyword or marker filter added fails the test.

A move proof in a scratch worktree moved tests/test_release.py, tests/test_roadmap_dry_runs.py and tests/test_security_kubelinter.py into package directories with git mv: with no other edit the suite passed and collected the same IDs once each ID's directory was dropped. docs/TESTING.md describes the check as existing, changelog.d/1312.md is the item's one fragment, for its five pull requests, and the task file records this pull request, PR 2's outcome against its own parent, and every criterion of the plan.

Closes #1312
@dan-petty
dan-petty merged commit 0d27928 into release/v0.2.37 Oct 20, 2026
15 checks passed
@dan-petty
dan-petty deleted the test/1312-test-path-ban branch October 20, 2026 02:35
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.

1 participant