From 3ac972a627e454f8a6360f610a29465ef90fe149 Mon Sep 17 00:00:00 2001 From: tyza66 <83948628+tyza66@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:56:32 +0800 Subject: [PATCH] fix(impact): let declared inputs select pytest tests A `diffci.json` declaration whose `tests` names a pytest file selected nothing, so the full-suite fallback it was meant to narrow stayed. `declaredTests` builds its candidates from `knownTestPaths`, which walks `profile.tests` - the test locations the JavaScript runners declare - and from the Go adapter's `goTestPackages`, which #83 added for the same reason. The pytest adapter reports its files on `profile.pythonTests`, which neither source reads, so a `tests` glob like `tests/test_prices.py` matched nothing and DiffCI reported the declaration as stale ("Declared input ... selects no tests"). The Python adapter's test files now join the candidates under the same condition the Go ones use: the adapter is present in the profile. Nothing is selected that the declaration did not name - the globs still filter the list. `tests/repo/python-declared-inputs.test.ts` is the Python counterpart of the Go testdata case in `go-runtime-inputs.test.ts`: a pytest file reads `tests/data/prices.csv` by name, a declaration maps the file to the test, and changing the data file must select that test with `DECLARED_INPUT` and no other test, without falling back. A second case keeps the old behaviour honest: with no declaration the same change still falls back to the full suite. The first case fails without the source change (0 selected tests) and passes with it. docs/using-diffci.md gains the Python example next to the JavaScript ones, and the new test file is in release-files.json, which audit:boundary enforces. Verified with npm run typecheck, npm run build and npm run audit:boundary; `npm test` gives 1056 passes and the same 8 Rust/Cargo failures this machine has on unmodified main (link.exe "missing operand after '\377\376'"). Signed-off-by: tyza66 <83948628+tyza66@users.noreply.github.com> --- docs/using-diffci.md | 10 +++ release-files.json | 1 + src/repo/impact.ts | 10 ++- tests/repo/python-declared-inputs.test.ts | 90 +++++++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 tests/repo/python-declared-inputs.test.ts diff --git a/docs/using-diffci.md b/docs/using-diffci.md index 4d9792c..bc681f2 100644 --- a/docs/using-diffci.md +++ b/docs/using-diffci.md @@ -462,6 +462,16 @@ Some files reach tests without an import: fixtures loaded by name, `.env.test`, } ``` +The same works for a Python repository where a test loads data by name, as `open("tests/data/prices.csv")` does, which the import graph cannot see: + +```json +{ + "inputs": [ + { "files": ["tests/data/**"], "tests": ["tests/test_prices.py"] } + ] +} +``` + When a changed file matches `files`, every test matching `tests` is selected, with the reason `DECLARED_INPUT`. Declared tests are always added to what the dependency graph selects. For an unknown file, an unread asset or fixture, code under `ops/` outside the graph, a deleted file, and a database or infrastructure definition, a declaration that selects at least one test also replaces the full-suite fallback. Some changes still run the full suite whatever `inputs` says, because they change how every test runs: test-runner and tool configuration, `package.json` and workspace manifests, lockfiles and CI workflows. So does a declaration whose `tests` match no test file, and so does any edit to `diffci.json` itself, so a change cannot narrow its own selection. diff --git a/release-files.json b/release-files.json index dd6d7e1..b6b6ced 100644 --- a/release-files.json +++ b/release-files.json @@ -276,6 +276,7 @@ "tests/repo/maven-failsafe-names.test.ts", "tests/repo/path-literals.test.ts", "tests/repo/maven-pom-comments.test.ts", + "tests/repo/python-declared-inputs.test.ts", "tests/repo/graph.project-references.test.ts", "tests/repo/graph.test.ts", "tests/repo/impact.test.ts", diff --git a/src/repo/impact.ts b/src/repo/impact.ts index 34a7322..2a0dd4e 100644 --- a/src/repo/impact.ts +++ b/src/repo/impact.ts @@ -385,7 +385,15 @@ export class ImpactAnalyzer { const goTestCandidates = profile.adapters?.some(adapter => adapter.id === "go") ? Object.keys(profile.goTestPackages ?? {}).filter(test => test.endsWith("_test.go")) : []; - return [...this.knownTestPaths(profile, graph), ...goTestCandidates].filter((test) => globs.some((glob) => matchesTestGlob(test, glob))); + // Python has the same shape of gap as Go, which #83 fixed there: the pytest adapter reports its + // test files on `profile.pythonTests`, and `knownTestPaths` only walks `profile.tests`, which the + // JavaScript runners fill. Without these candidates a declaration naming a `test_*.py` file is + // always stale, and the fallback it was meant to narrow stays (2026-11-18, #162). + const pythonTestCandidates = profile.adapters?.some(adapter => adapter.id === "python") + ? profile.pythonTests ?? [] + : []; + return [...this.knownTestPaths(profile, graph), ...goTestCandidates, ...pythonTestCandidates] + .filter((test) => globs.some((glob) => matchesTestGlob(test, glob))); } /** diff --git a/tests/repo/python-declared-inputs.test.ts b/tests/repo/python-declared-inputs.test.ts new file mode 100644 index 0000000..01a1ca7 --- /dev/null +++ b/tests/repo/python-declared-inputs.test.ts @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: MIT +import { strict as assert } from "node:assert"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { describe, it } from "node:test"; + +import { computeAnalysis } from "../../src/git/git-diff.js"; +import type { ChangedFile, GitDelta } from "../../src/git/types.js"; +import { pythonInterpreter } from "../../src/repo/adapters/python.js"; +import { buildDependencyGraph } from "../../src/repo/graph.js"; +import { ImpactAnalyzer } from "../../src/repo/impact.js"; +import type { RepositoryProfile } from "../../src/repo/types.js"; + +const interpreter = pythonInterpreter(process.cwd()); + +// A pytest file that reads a data file by name is invisible to the import graph: nothing imports +// `tests/data/prices.csv`, so without a declaration the change falls back to the full suite. This is +// the Python counterpart of the Go testdata case in go-runtime-inputs.test.ts (issue #162). +// +// Declaring this used to select nothing: `declaredTests` looked for candidates in `profile.tests`, +// which only the JavaScript runners fill, so a `tests` glob naming a `test_*.py` file matched +// nothing and the declaration was reported as stale - the fallback it was meant to narrow stayed. +describe("declared inputs for pytest", { skip: interpreter ? false : "no Python 3 interpreter on PATH" }, () => { + function fixture(files: Record): string { + const root = mkdtempSync(join(tmpdir(), "diffci-python-inputs-")); + for (const [path, content] of Object.entries(files)) { + mkdirSync(dirname(join(root, path)), { recursive: true }); + writeFileSync(join(root, path), content); + } + return root; + } + + function delta(files: ChangedFile[]): GitDelta { + return { + baseSha: "base", headSha: "head", files, directories: [], + summary: { total: files.length, added: 0, modified: files.length, deleted: 0, renamed: 0, copied: 0, unmerged: 0, unknown: 0 }, + analysis: computeAnalysis(files), + }; + } + + function analyze(root: string, graph: Awaited>, path: string) { + const files = [{ path, changeType: "modified" as const }]; + return new ImpactAnalyzer().analyze(delta(files), graph, graph.profile as RepositoryProfile, { + repositoryFiles: new Set(graph.graph.nodes.map((node) => node.path)), + }); + } + + const files = { + "pyproject.toml": "[project]\nname = \"shop\"\n[tool.pytest.ini_options]\ntestpaths = [\"tests\"]\n", + "src/shop/__init__.py": "", + "src/shop/pricing.py": "def total(items):\n return sum(items)\n", + "tests/data/prices.csv": "sku,price\napple,1\n", + "tests/test_prices.py": "import csv\nfrom pathlib import Path\n\ndef test_prices():\n rows = list(csv.DictReader(Path(__file__).with_name(\"data\").joinpath(\"prices.csv\").open()))\n assert rows[0][\"sku\"] == \"apple\"\n", + "tests/test_pricing.py": "from shop.pricing import total\n\ndef test_total():\n assert total([1, 2]) == 3\n", + }; + + it("selects the test a declaration names when the data file it reads changes", async () => { + const root = fixture({ + ...files, + "diffci.json": JSON.stringify({ inputs: [{ files: ["tests/data/**"], tests: ["tests/test_prices.py"] }] }), + }); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + assert.deepEqual(graph.adapterBlockers ?? [], [], JSON.stringify(graph.adapterBlockers)); + // The adapter did model both test files, so the declaration names a test DiffCI knows about. + assert.deepEqual(graph.profile.pythonTests, ["tests/test_prices.py", "tests/test_pricing.py"]); + const analysis = analyze(root, graph, "tests/data/prices.csv"); + const selected = analysis.affectedTests.filter((entry) => entry.path === "tests/test_prices.py"); + assert.equal(selected.length, 1, JSON.stringify({ tests: analysis.affectedTests, fallback: analysis.fallbackReasons })); + assert.ok(selected[0]!.reasons.includes("DECLARED_INPUT"), JSON.stringify(selected[0])); + // The declaration stands in for the full-suite fallback an unread data file would cause. + assert.equal(analysis.fallbackRequired, false, analysis.fallbackReasons.join("; ")); + assert.ok(!analysis.affectedTests.some((entry) => entry.path === "tests/test_pricing.py"), "the other test must stay out"); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + it("falls back for a data file nothing declares", async () => { + const root = fixture(files); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + const analysis = analyze(root, graph, "tests/data/prices.csv"); + assert.equal(analysis.fallbackRequired, true, "an unread data file must still run everything"); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); +});