Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions release-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
"tests/repo/mocharc-config-fallback.test.ts",
"tests/repo/orphan-tool-config.test.ts",
"tests/repo/removed-files.test.ts",
"tests/repo/python-test-file-readers.test.ts",
"tests/repo/repo-config.test.ts",
"tests/repo/runner-universe.test.ts",
"tests/repo/rust-adapter.test.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/repo/impact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,16 @@ export class ImpactAnalyzer {
}
const layout = this.layout;
const scripts = profile.packageJson.scripts;
// A .py file the Python adapter modelled is code like a .js file: pytest runs a test that reads the
// changed file without anything naming the test, so it must not be followed as a script.
const pythonSources = new Set(profile.pythonSources ?? []);
const resolution = resolveFileReferences(changedPath, {
files: this.sortedRepositoryFiles,
read: this.readRepositoryFile,
isTestFile: this.isTestFile,
isDocumentation: (path) => layout.isDocumentationPath(path) || isAgentConfigurationPath(path),
inGraph: (path) => hasNode(graph, path),
isCodeFile: isSourceFilePath,
isCodeFile: (path) => isSourceFilePath(path) || pythonSources.has(path),
workflowFeedsTests: (content, names) => workflowFeedsTests(content, names, scripts),
contentCleared: this.contentCleared,
implicitReadersUnresolvable,
Expand Down
60 changes: 60 additions & 0 deletions tests/repo/python-test-file-readers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
import { strict as assert } from "node:assert";
import { execSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { test } from "node:test";
import { analyzeGitDelta } from "../../src/git/git-diff.js";
import { repositoryFileReader } from "../../src/repo/file-references.js";
import { buildDependencyGraph } from "../../src/repo/graph.js";
import { ImpactAnalyzer } from "../../src/repo/impact.js";

// A pytest test that reads a data file was followed as a script, because .py is not a JavaScript source
// extension. Nothing names the test, so the change selected nothing and did not fall back.
function write(dir: string, files: Record<string, string>) {
for (const [path, content] of Object.entries(files)) { mkdirSync(dirname(join(dir, path)), { recursive: true }); writeFileSync(join(dir, path), content); }
}

async function change(files: Record<string, string>, path: string) {
const dir = join(tmpdir(), `diffci-py-readers-${randomUUID()}`);
mkdirSync(dir, { recursive: true });
try {
execSync("git init --quiet && git config user.email t@diffci.local && git config user.name t && git config core.autocrlf false", { cwd: dir });
write(dir, { "pyproject.toml": "[tool.pytest.ini_options]\ntestpaths = [\"tests\"]\n", "pkg/__init__.py": "", "tests/test_other.py": "def test_other():\n pass\n", ...files });
execSync("git add -A && git commit --quiet -m base", { cwd: dir });
const baseSha = execSync("git rev-parse HEAD", { cwd: dir, encoding: "utf8" }).trim();
write(dir, { [path]: "changed\n" });
execSync("git add -A && git commit --quiet -m head", { cwd: dir });
const headSha = execSync("git rev-parse HEAD", { cwd: dir, encoding: "utf8" }).trim();
const delta = await analyzeGitDelta({ repoPath: dir, baseSha, headSha });
assert.ok(delta.success);
const graph = await buildDependencyGraph({ repoPath: dir, excludeDirs: [".git"] });
const impact = new ImpactAnalyzer().analyze(delta.delta, graph, graph.profile, { repositoryFiles: delta.inventory?.files, readRepositoryFile: repositoryFileReader(dir) });
return { tests: impact.affectedTests.map((t) => t.path).sort(), fallback: impact.fallbackReasons };
} finally { rmSync(dir, { recursive: true, force: true }); }
}

test("a data file read by a pytest test selects that test", async () => {
const result = await change({
"tests/data/prices.csv": "a,b\n",
"tests/test_prices.py": "from pathlib import Path\n\ndef test_prices():\n assert (Path(__file__).parent / \"data\" / \"prices.csv\").read_text()\n",
}, "tests/data/prices.csv");
assert.deepEqual(result, { tests: ["tests/test_prices.py"], fallback: [] });
});

test("a data file read by a helper selects the tests that import it", async () => {
const result = await change({
"data/rates.json": "{}\n",
"pkg/rates.py": "import json\nfrom pathlib import Path\n\ndef load():\n return json.loads((Path(__file__).parent.parent / \"data\" / \"rates.json\").read_text())\n",
"tests/test_rates.py": "from pkg.rates import load\n\ndef test_rates():\n assert load() == {}\n",
}, "data/rates.json");
assert.deepEqual(result, { tests: ["tests/test_rates.py"], fallback: [] });
});

test("a data file nothing reads still falls back", async () => {
const result = await change({ "data/unused.json": "{}\n" }, "data/unused.json");
assert.deepEqual(result.tests, []);
assert.ok(result.fallback.length > 0);
});
Loading