Repository navigation
feat(python): read pytest.toml and the native [tool.pytest] table - #318
Conversation
pytest 9 looks for pytest.toml and .pytest.toml before pytest.ini, and reads a native [tool.pytest] table in pyproject.toml. Both are now read in pytest's order with native TOML types; a value pytest would reject, or a pyproject.toml with both [tool.pytest] and [tool.pytest.ini_options], sets unreadable. A change to either file falls back as a configuration change. Fixes DiffCI#160 Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0d11ab7439
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // 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) => { | ||
| for (const key of keys) { | ||
| const array = new RegExp(`^${key}\\s*=\\s*(?=\\[)`, "m").exec(body); |
There was a problem hiding this comment.
Match native TOML option keys using TOML syntax
For a valid TOML table with an indented key ([pytest] followed by python_files = ['check_*.py']), this regex does not match because it requires the bare key at column 0. The entry passes hasEntries but never sets unreadable, so readNative silently returns defaults and isPytestFile omits check_*.py; source changes can consequently select no tests that pytest actually collects. Parse the table with TOML or mark unrecognized entries unreadable.
Useful? React with 👍 / 👎.
| const body = section(text, "pytest"); | ||
| if (body !== undefined) return readNative(file, body); |
There was a problem hiding this comment.
Accept comments on native TOML table headers
For a valid pytest.toml or .pytest.toml header such as [pytest] # test selection, section() does not match because it requires only whitespace after ]. The top-level check then sees no options and this branch returns default settings; if the table defines custom python_files or testpaths, DiffCI can omit those tests and pass a source change without selecting them. Accept trailing TOML comments or fall back when a populated native table cannot be read.
Useful? React with 👍 / 👎.
| if (file === "pytest.toml" || file === ".pytest.toml") { | ||
| const body = section(text, "pytest"); | ||
| if (body !== undefined) return readNative(file, body); |
There was a problem hiding this comment.
Respect the pytest version when choosing loaders
If a repository runs pytest older than the 9.2 loaders implemented here, pytest ignores pytest.toml and [tool.pytest], but this adapter always gives them precedence (even an empty pytest.toml stops the search). When a legacy pytest.ini defines custom test patterns, DiffCI can instead build its graph from defaults and skip tests the runner actually collects. Gate these loaders on the repository's pytest version or fall back when it is unknown.
Useful? React with 👍 / 👎.
…eader CodeQL flagged the moved header escape as incomplete (it escaped only . and :). The headers are constants, but escape them fully so the helper is safe. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
|
Thanks for the quick CodeQL fix. Codex's three P1s on 7d23b20 reproduce. The first two also affect
Please add a test for each. Happy to merge once they fail closed. |
Indented keys and a comment after a table header are valid TOML (and a comment after an INI header is valid for pytest), so read them. A quoted or dotted key, or a header with spaces or quotes inside the brackets, can't be matched reliably, so mark the config unreadable. pytest before 9 ignores pytest.toml, .pytest.toml and [tool.pytest]; when one of those wins while a file older pytest would read also exists, the selection depends on the installed pytest, so mark it unreadable too. Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
Fixes #160
Changed behavior
readPytestSettingsfollows pytest 9.2'sCONFIG_LOADERS_BY_NAMEorder:pytest.toml,.pytest.toml,pytest.ini,.pytest.ini,pyproject.toml,tox.ini,setup.cfg.pytest.toml/.pytest.toml: settings are read from the[pytest]table in native TOML mode. Likepytest.ini, the file wins even when it is empty or has no[pytest]table, and the defaults apply. Options written above any table make pytest exit withpytest configuration must be under a [pytest] table, so they setunreadable.[tool.pytest]inpyproject.toml: it is read in native TOML mode. The header patterns are exact, so[tool.pytest]and[tool.pytest.ini_options]never match each other.Cannot use both [tool.pytest] … and [tool.pytest.ini_options], sounreadableis set._config_from_tool_pytestdoes.[tool.pytest]with noini_optionsdoesn't match, so the search continues totox.ini.ini_optionskey written inside[tool.pytest]is a form this reader doesn't parse, so it setsunreadable.python_files,testpaths,pythonpathandaddoptsmust be lists of strings, read with pyproject.toml: a comment or]inside a pytest list drops entries #158'sreadTomlStringArray. Eachaddoptsitem is one argument and isn't split again, matching_getini_toml. Any other type is aTypeErrorin pytest, so it setsunreadable.pytest.tomland.pytest.tomlare added toPROJECT_FILESand to the Python entries ofCONFIG_PATTERNS(/^\.?pytest\.(?:ini|toml)$/). A change to either falls back as a configuration change, the same waypytest.inidoes.docs/using-diffci.mdlists the new files and the newcannot readcauses.Validation
tests/repo/python-pytest-toml.test.tsis new. It covers:[pytest]table, including-pplugins from a nativeaddoptslist,pytest.toml, or one without[pytest], stopping the search,[tool.pytest]vs[tool.pytest.ini_options](exact matching, both set, one empty, anini_optionskey),pytest --co:pytest.tomlbeatspyproject.toml; top-level options in.pytest.toml, both tables set, and a stringpython_filesin[tool.pytest]all error out; an empty[tool.pytest]falls through toini_options.unreadablecases, the empty-table checks, or theCONFIG_PATTERNSchange, makes a test fail.npm test: 1297 pass, 0 fail.npm run typecheck,npm run buildandnpm run audit:boundarypass; the new test file is inrelease-files.json.