diff --git a/docs/using-diffci.md b/docs/using-diffci.md index a1e06fc..d388168 100644 --- a/docs/using-diffci.md +++ b/docs/using-diffci.md @@ -693,7 +693,7 @@ _Module resource and dependency impact contributed by [@FredApps](https://github ### Python -These appear only in Python projects (a root `pyproject.toml`, `setup.py`, `setup.cfg`, `pytest.ini`, `.pytest.ini`, `tox.ini` or `conftest.py`). A changed project file such as `pyproject.toml` or `requirements.txt` falls back under `Configuration file(s) changed`. +These appear only in Python projects (a root `pyproject.toml`, `setup.py`, `setup.cfg`, `pytest.toml`, `.pytest.toml`, `pytest.ini`, `.pytest.ini`, `tox.ini` or `conftest.py`). A changed project file such as `pyproject.toml` or `requirements.txt` falls back under `Configuration file(s) changed`. | Reason | Example change | Narrow | | --- | --- | --- | @@ -702,6 +702,7 @@ These appear only in Python projects (a root `pyproject.toml`, `setup.py`, `setu | `Unresolved relative import in …: ……` | `from .gone import x` after deleting `gone.py` | No | | `Unresolved first-party Python import in …: …` | `import shop.generated` where `shop/generated.py` is created by a build step | No | | `pytest configuration DiffCI cannot read: …` | `python_files` set to something other than a string or list in `pyproject.toml` | Fix: use a literal string or list | +| `pytest configuration DiffCI cannot read: …` | A `pytest.toml` or `[tool.pytest]` key that isn't a list of strings, both `[tool.pytest]` and `[tool.pytest.ini_options]` in one `pyproject.toml`, or options above the `[pytest]` table in `pytest.toml`; a quoted or dotted key, or a table header with spaces or quotes inside the brackets; or a `pytest.toml`, `.pytest.toml` or `[tool.pytest]` next to another pytest config that pytest before 9 would read instead | Fix: use a list of strings, keep one table, and put options under `[pytest]` (pytest 9 rejects all three); write bare keys and `[pytest]` / `[tool.pytest]` headers; keep a single pytest config file | | `Python analysis found no Python files` | Project files with every `.py` file inside a virtual environment | No | _Reading pytest configuration the way pytest does was improved by [@sarankumar1325](https://github.com/sarankumar1325): `pytest.ini` wins even when empty, and `.pytest.ini` is read ([#314](https://github.com/DiffCI/core/pull/314)); `pyproject.toml` lists are read as TOML strings ([#315](https://github.com/DiffCI/core/pull/315)); `tox.ini` and `setup.cfg` settings are covered by tests ([#316](https://github.com/DiffCI/core/pull/316))._ diff --git a/release-files.json b/release-files.json index 9e4ad70..c0c14f6 100644 --- a/release-files.json +++ b/release-files.json @@ -284,6 +284,7 @@ "tests/repo/lockfile-free-dependencies.test.ts", "tests/repo/lockfiles.test.ts", "tests/repo/python-addopts-plugins.test.ts", + "tests/repo/python-pytest-toml.test.ts", "tests/repo/mocharc-config-fallback.test.ts", "tests/repo/python-testpaths-globs.test.ts", "tests/repo/orphan-tool-config.test.ts", diff --git a/src/git/git-diff.ts b/src/git/git-diff.ts index 64bc9b7..0d794f6 100644 --- a/src/git/git-diff.ts +++ b/src/git/git-diff.ts @@ -235,7 +235,7 @@ export const CONFIG_PATTERNS = [ // Python projects (2026-11-06): what pytest, the interpreter and the installed packages are. /^pyproject\.toml$/, /^setup\.(?:py|cfg)$/, - /^\.?pytest\.ini$/, + /^\.?pytest\.(?:ini|toml)$/, /^tox\.ini$/, /^noxfile\.py$/, /^requirements[^/]*\.(?:txt|in)$/, diff --git a/src/repo/adapters/python.ts b/src/repo/adapters/python.ts index ade16bf..2f1000b 100644 --- a/src/repo/adapters/python.ts +++ b/src/repo/adapters/python.ts @@ -65,7 +65,7 @@ export type PythonImport = [module: string, level: number, names: string[]]; export interface PythonFileImports { imports?: PythonImport[]; dynamic?: boolean; error?: string } const EXCLUDED_DIRS = new Set([".venv", "venv", "env", ".env", ".tox", ".nox", "site-packages", "__pycache__", ".eggs", ".mypy_cache", ".pytest_cache", ".ruff_cache"]); -const PROJECT_FILES = ["pyproject.toml", "setup.py", "setup.cfg", "pytest.ini", ".pytest.ini", "tox.ini", "conftest.py"]; +const PROJECT_FILES = ["pyproject.toml", "setup.py", "setup.cfg", "pytest.toml", ".pytest.toml", "pytest.ini", ".pytest.ini", "tox.ini", "conftest.py"]; export function isPythonSource(path: string): boolean { return path.endsWith(".py") && !path.split("/").some((part) => EXCLUDED_DIRS.has(part) || part.endsWith(".egg-info")); @@ -163,35 +163,107 @@ export function readPytestSettings(read: (path: string) => string | undefined): if (plugins.length) settings.plugins = plugins; } else settings[key] = values; }; - // pytest's search order. `pytest.ini` and `.pytest.ini` match even when empty or without a `[pytest]` - // section, and then pytest uses its defaults; the other files only match with their section. The first - // match wins and options are never merged. - for (const [file, header] of [["pytest.ini", "pytest"], [".pytest.ini", "pytest"], ["pyproject.toml", "tool.pytest.ini_options"], ["tox.ini", "pytest"], ["setup.cfg", "tool:pytest"]] as const) { + const escape = (text: string) => text.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&"); + const isToml = (file: string) => file.endsWith(".toml"); + // A table header may be indented and followed by a comment (`[pytest] # selection`). TOML also allows + // spaces inside the brackets and quoted keys (`[ pytest ]`, `[tool."pytest"]`), which this reader doesn't + // match: a header that only matches with spaces and quotes removed makes the file unreadable instead. + const section = (file: string, text: string, header: string): string | undefined => { + const comment = isToml(file) ? "#" : "#;"; + return new RegExp(`^[ \\t]*\\[${escape(header)}\\][ \\t]*(?:[${comment}].*)?$([\\s\\S]*?)(?=^[ \\t]*\\[|(?![\\s\\S]))`, "m").exec(text)?.[1]; + }; + const looseHeader = (file: string, text: string, header: string): boolean => + isToml(file) && [...text.matchAll(/^[ \t]*\[([^\]\n]*)\]/gm)].some((match) => + match[1]!.replace(/[\s"']/g, "") === header && section(file, match[0]!.trim(), header) === undefined); + const hasEntries = (body: string) => /^[ \t]*[^\s#]/m.test(body); + const keyAt = (key: string) => `^[ \\t]*${key}[ \\t]*=`; + // A quoted or dotted key (`"testpaths" = …`, `ini_options.testpaths = …`) can't be told apart from the + // keys selection reads, so a TOML table with one is unreadable. + const unclassified = (body: string) => /^[ \t]*(?:"[^"\n]*"|'[^'\n]*'|[A-Za-z0-9_-]+)[ \t]*\./m.test(body) || + /^[ \t]*(?:"[^"\n]*"|'[^'\n]*')[ \t]*=/m.test(body); + // pytest 9's native TOML mode (`pytest.toml`, `[tool.pytest]`): every key selection reads is a list of + // strings, and each `addopts` item is one argument. Any other type is an error in pytest, so it can't be read. + const readNative = (file: string, body: string) => { + if (unclassified(body)) { settings.unreadable = `${file} key DiffCI can't classify`; return settings; } + for (const key of keys) { + const array = new RegExp(`${keyAt(key)}[ \\t]*(?=\\[)`, "m").exec(body); + if (array) { + const values = readTomlStringArray(body, array.index + array[0].length); + if (values) apply(key, values); + else settings.unreadable = `${file} ${key}`; + } else if (new RegExp(keyAt(key), "m").test(body)) settings.unreadable = `${file} ${key}`; + } + return settings; + }; + const order = [["pytest.toml", "pytest"], [".pytest.toml", "pytest"], ["pytest.ini", "pytest"], [".pytest.ini", "pytest"], ["pyproject.toml", "tool.pytest.ini_options"], ["tox.ini", "pytest"], ["setup.cfg", "tool:pytest"]] as const; + // pytest before 9 ignores `pytest.toml`, `.pytest.toml` and `[tool.pytest]`, and uses the first of the + // other files that matches. When a native config wins and such a file also exists, the answer depends on + // the installed pytest, so it can't be read. + const olderPytestFile = (from: number): string | undefined => { + for (const [file, header] of order.slice(from)) { + const text = read(file); + if (text === undefined || file === ".pytest.toml") continue; + if (file === "pytest.ini" || file === ".pytest.ini" || section(file, text, header) !== undefined) return file; + } + return undefined; + }; + const native = (file: string, body: string | undefined, index: number) => { + const older = olderPytestFile(index); + if (older) { settings.unreadable = `${file} is ignored before pytest 9, which reads ${older}`; return settings; } + return body === undefined ? settings : readNative(file, body); + }; + // pytest's search order. `pytest.toml`, `.pytest.toml`, `pytest.ini` and `.pytest.ini` match even when + // empty or without their `[pytest]` table, and then pytest uses its defaults; the other files only match + // with their section. The first match wins and options are never merged. + for (const [index, [file, header]] of order.entries()) { const text = read(file); if (text === undefined) continue; - const section = new RegExp(`^\\[${header.replace(/[.:]/g, "\\$&")}\\]\\s*$([\\s\\S]*?)(?=^\\[|(?![\\s\\S]))`, "m").exec(text); - if (!section) { + if (looseHeader(file, text, "pytest") || looseHeader(file, text, "tool.pytest") || looseHeader(file, text, "tool.pytest.ini_options")) { + settings.unreadable = `${file} pytest table header DiffCI can't read`; + return settings; + } + if (file === "pytest.toml" || file === ".pytest.toml") { + const body = section(file, text, "pytest"); + // pytest rejects options written above any table. + if (body === undefined && hasEntries(text.split(/^[ \t]*\[/m, 1)[0]!)) { settings.unreadable = `${file} options outside [pytest]`; return settings; } + return native(file, body, index + 1); + } + if (file === "pyproject.toml") { + // `[tool.pytest]` holds native TOML values, `[tool.pytest.ini_options]` INI-style ones. pytest refuses a + // file that sets both, and an `ini_options` key inside `[tool.pytest]` is a form this reader doesn't parse. + const nativeBody = section(file, text, "tool.pytest"); + const ini = section(file, text, header); + if (nativeBody !== undefined && new RegExp(`^[ \\t]*ini_options[ \\t]*[.=]`, "m").test(nativeBody)) { settings.unreadable = `${file} [tool.pytest] ini_options`; return settings; } + if (nativeBody !== undefined && hasEntries(nativeBody)) { + if (ini !== undefined && hasEntries(ini)) { settings.unreadable = `${file} [tool.pytest] and [tool.pytest.ini_options]`; return settings; } + // An empty [tool.pytest.ini_options] beside it is what pytest 8 reads. + if (ini !== undefined) { settings.unreadable = `${file} [tool.pytest] is ignored before pytest 9, which reads [tool.pytest.ini_options]`; return settings; } + return native(file, nativeBody, index + 1); + } + } + const body = section(file, text, header); + if (body === undefined) { if (file === "pytest.ini" || file === ".pytest.ini") return settings; continue; } - const body = section[1]!; + if (file === "pyproject.toml" && unclassified(body)) { settings.unreadable = `${file} key DiffCI can't classify`; return settings; } for (const key of keys) { if (file === "pyproject.toml") { - const match = new RegExp(`^${key}\\s*=\\s*("[^"]*"|'[^']*')`, "m").exec(body); - const array = new RegExp(`^${key}\\s*=\\s*(?=\\[)`, "m").exec(body); + const match = new RegExp(`${keyAt(key)}[ \\t]*("[^"]*"|'[^']*')`, "m").exec(body); + const array = new RegExp(`${keyAt(key)}[ \\t]*(?=\\[)`, "m").exec(body); // A multi-line string would otherwise match as an empty one. Its contents are read as one string; // a basic string's line-ending backslash only joins lines. An unterminated one cannot be read. - const multiline = new RegExp(`^${key}\\s*=\\s*("""|'{3})([\\s\\S]*?)\\1`, "m").exec(body); + const multiline = new RegExp(`${keyAt(key)}[ \\t]*("""|'{3})([\\s\\S]*?)\\1`, "m").exec(body); if (multiline) apply(key, splitList(multiline[1] === '"""' ? multiline[2]!.replace(/\\\r?\n/g, " ") : multiline[2]!)); - else if (new RegExp(`^${key}\\s*=\\s*(?:"""|'{3})`, "m").test(body)) settings.unreadable = `${file} ${key}`; + else if (new RegExp(`${keyAt(key)}[ \\t]*(?:"""|'{3})`, "m").test(body)) settings.unreadable = `${file} ${key}`; else if (array) { const values = readTomlStringArray(body, array.index + array[0].length); if (values) apply(key, key === "addopts" ? values.flatMap(splitList) : values); else settings.unreadable = `${file} ${key}`; } else if (match) apply(key, splitList(match[1]!)); - else if (new RegExp(`^${key}\\s*=`, "m").test(body)) settings.unreadable = `${file} ${key}`; + else if (new RegExp(keyAt(key), "m").test(body)) settings.unreadable = `${file} ${key}`; } else { - // INI values continue on indented lines. + // INI values continue on indented lines, so a key starts at the beginning of a line. const match = new RegExp(`^${key}\\s*=\\s*(.*(?:\\r?\\n[ \\t]+.*)*)`, "m").exec(body); if (match) apply(key, splitList(match[1]!)); } diff --git a/tests/repo/python-pytest-toml.test.ts b/tests/repo/python-pytest-toml.test.ts new file mode 100644 index 0000000..7ba6e19 --- /dev/null +++ b/tests/repo/python-pytest-toml.test.ts @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: MIT +import { strict as assert } from "node:assert"; +import { describe, it } from "node:test"; + +import { isPytestFile, pythonAdapter, readPytestSettings } from "../../src/repo/adapters/python.js"; +import { computeAnalysis } from "../../src/git/git-diff.js"; + +const defaults = { pythonFiles: ["test_*.py", "*_test.py"], testpaths: [], pythonpath: [] }; +const settingsOf = (files: Record) => readPytestSettings((path) => files[path]); + +describe("pytest 9 pytest.toml and [tool.pytest] (#160)", () => { + it("reads the [pytest] table of pytest.toml and .pytest.toml in native TOML mode", () => { + const toml = settingsOf({ "pytest.toml": "[pytest]\npython_files = [\"check_*.py\"]\ntestpaths = [\"tests\"]\npythonpath = [\"src\"]\naddopts = [\"-p\", \"myplugin\", \"-ptools.fixtures\", \"-p\", \"no:cacheprovider\"]\n" }); + assert.deepEqual(toml, { pythonFiles: ["check_*.py"], testpaths: ["tests"], pythonpath: ["src"], plugins: ["myplugin", "tools.fixtures"] }); + assert.ok(isPytestFile("tests/check_a.py", toml)); + assert.ok(!isPytestFile("tests/test_a.py", toml)); + assert.deepEqual(settingsOf({ ".pytest.toml": "[pytest]\ntestpaths = [\"integration\"]\n" }).testpaths, ["integration"]); + }); + + it("puts pytest.toml before .pytest.toml, and both before [tool.pytest] in pyproject.toml", () => { + const rest = { + "pyproject.toml": "[tool.pytest]\ntestpaths = [\"pyproject\"]\n", + "tox.ini": "[tox]\nenvlist = py\n", + "setup.cfg": "[metadata]\nname = x\n", + }; + assert.deepEqual(settingsOf({ "pytest.toml": "[pytest]\ntestpaths = [\"a\"]\n", ".pytest.toml": "[pytest]\ntestpaths = [\"b\"]\n", ...rest }).testpaths, ["a"]); + assert.deepEqual(settingsOf({ ".pytest.toml": "[pytest]\ntestpaths = [\"b\"]\n", ...rest }).testpaths, ["b"]); + }); + + it("stops at a pytest.toml that is empty or has no [pytest] table", () => { + const pyproject = "[tool.pytest]\npython_files = [\"check_*.py\"]\n"; + assert.deepEqual(settingsOf({ "pytest.toml": "", "pyproject.toml": pyproject }), defaults); + assert.deepEqual(settingsOf({ ".pytest.toml": "# configured elsewhere\n[tool.other]\nx = 1\n", "pyproject.toml": pyproject }), defaults); + }); + + it("can't read options written above the [pytest] table, which pytest rejects", () => { + assert.equal(settingsOf({ "pytest.toml": "testpaths = [\"tests\"]\n" }).unreadable, "pytest.toml options outside [pytest]"); + }); + + it("reads [tool.pytest] in pyproject.toml without mistaking it for [tool.pytest.ini_options]", () => { + const native = settingsOf({ "pyproject.toml": "[project]\nname = \"x\"\n[tool.pytest]\npython_files = [\"check_*.py\"]\n[tool.ruff]\nline-length = 100\n" }); + assert.deepEqual(native, { ...defaults, pythonFiles: ["check_*.py"] }); + const ini = settingsOf({ "pyproject.toml": "[tool.pytest.ini_options]\npython_files = \"check_*.py\"\n" }); + assert.deepEqual(ini.pythonFiles, ["check_*.py"]); + }); + + it("can't read a native value that isn't a list of strings", () => { + assert.equal(settingsOf({ "pyproject.toml": "[tool.pytest]\ntestpaths = \"tests\"\n" }).unreadable, "pyproject.toml testpaths"); + assert.equal(settingsOf({ "pytest.toml": "[pytest]\npython_files = [1]\n" }).unreadable, "pytest.toml python_files"); + assert.equal(settingsOf({ "pytest.toml": "[pytest]\naddopts = \"-p myplugin\"\n" }).unreadable, "pytest.toml addopts"); + }); + + it("can't read a pyproject.toml with both tables, which pytest refuses", () => { + const both = settingsOf({ "pyproject.toml": "[tool.pytest]\ntestpaths = [\"a\"]\n[tool.pytest.ini_options]\ntestpaths = [\"b\"]\n" }); + assert.equal(both.unreadable, "pyproject.toml [tool.pytest] and [tool.pytest.ini_options]"); + assert.equal(settingsOf({ "pyproject.toml": "[tool.pytest]\nini_options.testpaths = [\"b\"]\n" }).unreadable, "pyproject.toml [tool.pytest] ini_options"); + }); + + it("uses whichever pyproject.toml table has options when the other is empty", () => { + // pytest 9 reads [tool.pytest] here, but pytest 8 reads the empty [tool.pytest.ini_options]. + assert.equal(settingsOf({ "pyproject.toml": "[tool.pytest]\ntestpaths = [\"a\"]\n[tool.pytest.ini_options]\n" }).unreadable, + "pyproject.toml [tool.pytest] is ignored before pytest 9, which reads [tool.pytest.ini_options]"); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest]\n# nothing yet\n[tool.pytest.ini_options]\ntestpaths = \"b\"\n" }).testpaths, ["b"]); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest]\n", "tox.ini": "[pytest]\ntestpaths = tox\n" }).testpaths, ["tox"]); + }); + + it("detects pytest.toml projects and falls back when pytest.toml or .pytest.toml changes", () => { + assert.ok(pythonAdapter.detect({ files: ["pytest.toml", "test_a.py"] } as never)); + assert.ok(pythonAdapter.detect({ files: [".pytest.toml", "test_a.py"] } as never)); + for (const path of ["pytest.toml", ".pytest.toml"]) { + assert.equal(computeAnalysis([{ path, changeType: "modified" }]).configChanged, true, path); + } + assert.equal(computeAnalysis([{ path: "docs/pytest.toml", changeType: "modified" }]).configChanged, false); + }); + + it("reads indented keys in every TOML pytest table", () => { + assert.deepEqual(settingsOf({ "pytest.toml": "[pytest]\n python_files = ['check_*.py']\n\ttestpaths = [\"tests\"]\n" }), + { ...defaults, pythonFiles: ["check_*.py"], testpaths: ["tests"] }); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest]\n python_files = [\"check_*.py\"]\n" }).pythonFiles, ["check_*.py"]); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest.ini_options]\n python_files = \"check_*.py\"\n testpaths = [\"t\"]\n" }), + { ...defaults, pythonFiles: ["check_*.py"], testpaths: ["t"] }); + assert.equal(settingsOf({ "pytest.toml": "[pytest]\n testpaths = \"tests\"\n" }).unreadable, "pytest.toml testpaths"); + }); + + it("can't read a TOML pytest table with a quoted or dotted key", () => { + for (const files of >>[ + { "pytest.toml": "[pytest]\n\"testpaths\" = [\"tests\"]\n" }, + { "pytest.toml": "[pytest]\n 'python_files' = ['check_*.py']\n" }, + { "pyproject.toml": "[tool.pytest.ini_options]\n\"python_files\" = \"check_*.py\"\n" }, + { "pyproject.toml": "[tool.pytest]\noptions.testpaths = [\"tests\"]\n" }, + ]) assert.match(settingsOf(files).unreadable ?? "", /key DiffCI can't classify$/, JSON.stringify(files)); + // A quoted string inside an array is a value, not a key. + assert.deepEqual(settingsOf({ "pytest.toml": "[pytest]\ntestpaths = [\n \"a\",\n \"b=c\",\n]\n" }).testpaths, ["a", "b=c"]); + }); + + it("reads a pytest table header followed by a comment", () => { + assert.deepEqual(settingsOf({ "pytest.toml": "[pytest] # selection\npython_files = [\"check_*.py\"]\n" }).pythonFiles, ["check_*.py"]); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest] # native\ntestpaths = [\"a\"]\n" }).testpaths, ["a"]); + assert.deepEqual(settingsOf({ "pyproject.toml": "[tool.pytest.ini_options] # ini\ntestpaths = \"b\"\n" }).testpaths, ["b"]); + assert.deepEqual(settingsOf({ "tox.ini": "[pytest] ; for pytest\ntestpaths = tox\n" }).testpaths, ["tox"]); + assert.deepEqual(settingsOf({ "setup.cfg": "[tool:pytest] # for pytest\ntestpaths = cfg\n" }).testpaths, ["cfg"]); + }); + + it("can't read a TOML pytest table header written with spaces or quotes", () => { + for (const files of >>[ + { "pytest.toml": "[ pytest ]\ntestpaths = [\"tests\"]\n" }, + { "pyproject.toml": "[tool.\"pytest\"]\ntestpaths = [\"tests\"]\n" }, + { "pyproject.toml": "[tool . pytest . ini_options]\ntestpaths = \"tests\"\n" }, + ]) assert.match(settingsOf(files).unreadable ?? "", /pytest table header DiffCI can't read$/, JSON.stringify(files)); + }); + + it("can't read a native config when pytest before 9 would read another file", () => { + const toml = "[pytest]\ntestpaths = [\"a\"]\n"; + for (const [older, text] of [["pytest.ini", ""], [".pytest.ini", "[pytest]\ntestpaths = b\n"], ["tox.ini", "[pytest]\ntestpaths = b\n"], ["setup.cfg", "[tool:pytest]\n"]] as const) { + assert.equal(settingsOf({ "pytest.toml": toml, [older]: text }).unreadable, `pytest.toml is ignored before pytest 9, which reads ${older}`); + } + assert.equal(settingsOf({ ".pytest.toml": "", "pyproject.toml": "[tool.pytest.ini_options]\ntestpaths = \"b\"\n" }).unreadable, + ".pytest.toml is ignored before pytest 9, which reads pyproject.toml"); + assert.equal(settingsOf({ "pyproject.toml": "[tool.pytest]\ntestpaths = [\"a\"]\n", "setup.cfg": "[tool:pytest]\ntestpaths = b\n" }).unreadable, + "pyproject.toml is ignored before pytest 9, which reads setup.cfg"); + // Files pytest doesn't take its configuration from don't matter. + assert.deepEqual(settingsOf({ "pytest.toml": toml, "tox.ini": "[tox]\nenvlist = py\n", "setup.cfg": "[metadata]\nname = x\n" }).testpaths, ["a"]); + }); +});