From c93b8fd065a27f3fb68e776f370c26b50e1bf894 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:37:07 +0530 Subject: [PATCH] test(python): show that declared inputs select pytest tests, and document it A pytest fixture with a data file read by name and an inputs entry mapping it to its test: changing the file selects that test with reason DECLARED_INPUT, and without the entry it runs the full suite. The "Declare inputs the graph can't see" section gains a pytest example. Fixes #162 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> --- docs/using-diffci.md | 10 ++++ release-files.json | 1 + tests/repo/python-declared-inputs.test.ts | 68 +++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/repo/python-declared-inputs.test.ts diff --git a/docs/using-diffci.md b/docs/using-diffci.md index 4d9792c..6805067 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 pytest. A test that opens `tests/data/prices.csv` by name has no import edge to it, so a change to the CSV runs the full suite until it is declared: + +```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 68b13bd..1bed27d 100644 --- a/release-files.json +++ b/release-files.json @@ -415,6 +415,7 @@ "src/repo/route-visits.ts", "tests/repo/route-visits.test.ts", "tests/repo/python-adapter.test.ts", + "tests/repo/python-declared-inputs.test.ts", "packages/diffci-cli/src/client/coverage-map.ts", "packages/diffci-cli/tests/client/coverage-map.test.ts", "packages/diffci-cli/src/client/agent-stop.ts", diff --git a/tests/repo/python-declared-inputs.test.ts b/tests/repo/python-declared-inputs.test.ts new file mode 100644 index 0000000..c71cc9a --- /dev/null +++ b/tests/repo/python-declared-inputs.test.ts @@ -0,0 +1,68 @@ +// 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 { planSelectiveTestCommands } from "../../src/planner/test-command.js"; +import { pythonInterpreter } from "../../src/repo/adapters/python.js"; +import { buildDependencyGraph } from "../../src/repo/graph.js"; +import { ImpactAnalyzer } from "../../src/repo/impact.js"; + +const pythonAvailable = pythonInterpreter(process.cwd()) !== undefined; + +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), + }; +} + +// A pytest test that loads a data file by name, which the import graph can't see (#162). +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": "item,price\napple,1\n", + "tests/test_prices.py": "import csv\n\ndef test_prices():\n with open(\"tests/data/prices.csv\") as handle:\n assert list(csv.DictReader(handle))\n", + "tests/test_pricing.py": "from shop.pricing import total\n\ndef test_total():\n assert total([1, 2]) == 3\n", +}; + +describe("declared inputs in a pytest repository (#162)", { skip: pythonAvailable ? false : "no Python 3 interpreter on PATH" }, () => { + it("selects the test a declared data file maps to, with reason DECLARED_INPUT", 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)); + const impact = new ImpactAnalyzer().analyze(delta([{ path: "tests/data/prices.csv", changeType: "modified" }]), graph, graph.profile, { repositoryFiles: new Set(Object.keys(files)) }); + assert.equal(impact.fallbackRequired, false, impact.fallbackReasons.join("; ")); + assert.deepEqual(impact.affectedTests.map((test) => test.path), ["tests/test_prices.py"]); + assert.ok(impact.affectedTests[0]!.reasons.includes("DECLARED_INPUT"), JSON.stringify(impact.affectedTests)); + const plan = planSelectiveTestCommands(graph.profile, impact.affectedTests.map((test) => test.path)); + assert.deepEqual(plan.commands[0]?.args.slice(-3), ["-m", "pytest", "tests/test_prices.py"]); + } finally { rmSync(root, { recursive: true, force: true }); } + }); + + it("runs the full suite for the same change without the declaration", async () => { + const root = fixture(files); + try { + const graph = await buildDependencyGraph({ repoPath: root }); + const impact = new ImpactAnalyzer().analyze(delta([{ path: "tests/data/prices.csv", changeType: "modified" }]), graph, graph.profile, { repositoryFiles: new Set(Object.keys(files)) }); + assert.equal(impact.fallbackRequired, true); + assert.ok(impact.fallbackReasons.some((reason) => reason.includes("tests/data/prices.csv")), impact.fallbackReasons.join("; ")); + } finally { rmSync(root, { recursive: true, force: true }); } + }); +});