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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-21
skip_specs: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Design

## Decisions

- **Give `-n 0` in place of `-o addopts=`.** The empty `addopts` stopped the xdist workers of `-n logical`, and it also removed the other values of `addopts`. `-n 0` stops only the workers. pytest puts `addopts` before the command line, so the last `-n` wins.
- **Keep the replay in one process.** A replay runs few test files. An explicit `-n` count calls no `pytest_xdist_auto_num_workers`, so the replay takes no gate slot and does not wait for a `devops ci` run.
- **Give no `--no-cov`.** `addopts` holds no `--cov`, so coverage is off in the replay without it.

## Rejected alternatives

- **Change the test to read the configuration of the run.** The test checks the `addopts` of `pyproject.toml`, which is the configuration of the full gate. The replay, and not the test, changed that value. Another test that reads `addopts`, or that needs the plugins or the Hypothesis profile of `addopts`, would also fail.
- **Remove some values from the `addopts` string.** That repeats the parse of pytest, and it can remove a value that a test reads.
- **Keep `-n logical` in the replay.** The replay then takes a gate slot and starts a worker for each CPU, for a few test files.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Proposal: #5599

## Why

The replay of `devops ci fail-to-pass` starts pytest with `-o addopts=`, which sets the `addopts` of the tree to an empty value. `tests/commands/test_test_selection.py::test_the_full_gate_collects_every_test_module` reads `addopts` with `pytestconfig.getini`, so it fails in the replay on each base and on each head, but it passes in a normal run. A fix that changes `tests/commands/test_test_selection.py` gets a false failure on base, and when the head replay fails too, the check exits 1. The empty `addopts` also loads pytest-randomly and the logfire plugins, and removes the Hypothesis `ci` profile, so the replay is not a normal run. Closes #5599.

## What Changes

- The replay keeps the `addopts` of the tree, and gives `-n 0` in place of `-o addopts=`. pytest puts `addopts` before the command line, so `-n 0` overrides `-n logical`: the replay starts no xdist worker and takes no gate slot, as before.
- The other replay options do not change: `-p no:cacheprovider`, `--continue-on-collection-errors` and `--junitxml`.

## Capabilities

- `ci-fail-to-pass`: ADDED requirement 'The replay keeps the addopts of the tree'.

## Impact

A test that reads `addopts`, or that needs the plugins or the Hypothesis profile of `addopts`, passes in the replay as in a normal run. The base replay, the plain base replay and the head replay use the same options. No new option, failure class or setting.

## Feasibility

- pytest 9.2.0: `uv run python -m pytest -o addopts= -p no:cacheprovider tests/commands/test_test_selection.py::test_the_full_gate_collects_every_test_module` fails with `At index 1 diff: [] != ['-n', 'logical', ...]`, and the run prints `Using --randomly-seed=`. The same test with `-n 0` in place of `-o addopts=` passes.
- `uv run python -m pytest -n 0 -p no:cacheprovider --continue-on-collection-errors tests/commands/test_test_selection.py`: 105 passed.
- pytest-xdist 3.8.0 (`xdist/plugin.py`): distribution mode needs a `tx` value, and `-n 0` gives none. Only `-n auto` or `-n logical` calls `pytest_xdist_auto_num_workers`, which takes the gate slot of `tests/conftest.py`.
- `addopts` holds no `--cov`. Only the test step of `devops ci` gives `--cov=src`, so the replay needs no `--no-cov`.

## Existing Tool Considered

pytest and pytest-xdist cover the need: `-n 0` stops the workers and keeps `addopts`. The `[tool.mutmut]` table of `pyproject.toml` uses the same `-n 0` for the same reason. The change deletes the `-o addopts=` option of the replay and adds no code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## ADDED Requirements

### Requirement: The replay keeps the addopts of the tree
The pytest run of the replay SHALL keep the `addopts` of the tree that it runs in, and SHALL start no xdist worker. A test that reads `addopts` SHALL pass in the replay when it passes in a normal run.

#### Scenario: A test reads addopts in the replay
- **WHEN** the replay runs a test that reads `addopts` and the number of xdist workers, in a project whose `addopts` give `-n 2`
- **THEN** the test reads the `addopts` of the project and 0 workers, and the JUnit report of the replay gives it as passed
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Tasks

## 1. Behavior

- [x] 1.1 A test that reads `addopts` and the number of workers fails in the replay, which clears `addopts`
- Verification: `$ uv run devops ci test tests/ci/test_fail_to_pass.py`
- [x] 1.2 The replay gives `-n 0` in place of `-o addopts=`, so it keeps `addopts` and starts no worker
- Verification: `$ uv run devops ci test tests/ci/test_fail_to_pass.py`

## 2. Documents

- [x] 2.1 The docstring of `fail_to_pass.py` and the comment of `CONST_FAIL_TO_PASS_PYTEST_FLAGS` name `-n 0` and say that the replay keeps `addopts`
- Verification: `$ uv run devops ci docs`
7 changes: 7 additions & 0 deletions openspec/specs/ci-fail-to-pass/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,10 @@ The base, plain-base, head and gate-row replays SHALL use this report.
#### Scenario: A fix of a rule lock
- **WHEN** a fix changes only `src/devops_cli/review/rules/rules.lock`
- **THEN** the command exits 0 and starts no pytest

### Requirement: The replay keeps the addopts of the tree
The pytest run of the replay SHALL keep the `addopts` of the tree that it runs in, and SHALL start no xdist worker. A test that reads `addopts` SHALL pass in the replay when it passes in a normal run.

#### Scenario: A test reads addopts in the replay
- **WHEN** the replay runs a test that reads `addopts` and the number of xdist workers, in a project whose `addopts` give `-n 2`
- **THEN** the test reads the `addopts` of the project and 0 workers, and the JUnit report of the replay gives it as passed
17 changes: 9 additions & 8 deletions src/devops_cli/ci/fail_to_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
`devops ci fail-to-pass` resolves the merge base of `--base` and `--head`, lists added or
modified `tests/**/test_*.py` files and check modules of `tests/gate/<row>_checks/`, checks out
the base tree as a detached git worktree in a temporary directory (#2197) with the head's `tests/`
overlaid, and runs those test files with the current pytest (`-o addopts=`, `-p no:cacheprovider`,
overlaid, and runs those test files with the current pytest (`-n 0`, `-p no:cacheprovider`,
`--continue-on-collection-errors`, `--junitxml`), so a test file that cannot be collected on base
is reported as errored and the others still run. A test that fails on base proves a change of
code, so only a fix that changes source code (`is_source_code`: a `.py` file under `src/` outside
the knowledge base) or a gate check needs one; a fix that changes neither passes with no replay
(#6207). The workflow runs it only for a fix pull request (#1879), so the command reads no title,
label or body. A
replay whose pytest was interrupted, stopped with an internal or usage error, or wrote no complete
JUnit report could not run on base, and fails (#1669). A replay with a failure that the host path
is reported as errored and the others still run. The run keeps the `addopts` of the tree, so a
test that reads them passes as in a normal run, and `-n 0` stops only their xdist workers (#5599).
A test that fails on base proves a change of code, so only a fix that changes source code
(`is_source_code`: a `.py` file under `src/` outside the knowledge base) or a gate check needs
one; a fix that changes neither passes with no replay (#6207). The workflow runs it only for a
fix pull request (#1879), so the command reads no title, label or body. A replay whose pytest
was interrupted, stopped with an internal or usage error, or wrote no complete JUnit report
could not run on base, and fails (#1669). A replay with a failure that the host path
guard of the test harness raised, or with a test that the fix does not change and that fails or
errors on base, proves nothing about the fix and fails too (#3124).

Expand Down
8 changes: 6 additions & 2 deletions src/devops_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2608,9 +2608,13 @@ class WebhookExposure(StrEnum):
# ── Fail-to-Pass Test Replay (`devops ci fail-to-pass`, #844) ────────────────
# A changed test file that imports a module the fix adds cannot be collected on base; without
# `--continue-on-collection-errors` pytest then runs none of the other changed files (#861).
# The replay keeps the `addopts` of the tree, so a test runs with the plugins, the Hypothesis
# profile and the `addopts` value of a normal run, and a test that reads them passes (#5599).
# `-n 0` comes after `addopts` and overrides their `-n logical`: the replay starts no xdist
# worker, and so takes no gate slot.
CONST_FAIL_TO_PASS_PYTEST_FLAGS: Final[tuple[str, ...]] = (
"-o",
"addopts=",
"-n",
"0",
"-p",
"no:cacheprovider",
"--continue-on-collection-errors",
Expand Down
35 changes: 34 additions & 1 deletion tests/ci/test_fail_to_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,39 @@ def test_a_test_that_runs_git_ls_files_passes_on_base_in_the_replay(
assert (report.passed_count, report.failed_count, report.could_not_run) == (1, 0, None)


# A test that reads the pytest configuration of its project, as
# `tests/commands/test_test_selection.py::test_the_full_gate_collects_every_test_module` does.
_READS_ITS_CONFIGURATION = """\
def test_reads_its_configuration(pytestconfig):
assert (pytestconfig.getini("addopts"), pytestconfig.getoption("numprocesses")) == (
["-p", "xdist", "-n", "2", "-p", "no:randomly"],
0,
)
"""


def test_a_test_that_reads_addopts_passes_in_the_replay_and_starts_no_workers(
pytester: pytest.Pytester, monkeypatch: pytest.MonkeyPatch
) -> None:
"""The replay keeps the `addopts` of the project (#5599): a test that reads them passes in
the replay as in a normal run, so the replay gives no false failure on base. The replay
stops only the xdist workers, so it runs in one process and takes no gate slot."""
# The inner run loads no installed plugin: autoloading each plugin costs seconds.
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
pytester.makepyprojecttoml(
'[tool.pytest.ini_options]\naddopts = "-p xdist -n 2 -p no:randomly"\n'
)
pytester.makepyfile(test_reads=_READS_ITS_CONFIGURATION)
argv = pytest_replay_argv(pytester.path / "junit.xml", ["test_reads.py"])

pytester.runpytest(*argv[argv.index("pytest") + 1 :])

report = parse_junit_xml((pytester.path / "junit.xml").read_text(encoding="utf-8"))
assert [(case.test_id, case.status) for case in report] == [
("test_reads::test_reads_its_configuration", "passed")
]


def test_the_replay_leaves_no_worktree_behind(
tmp_path: Path, replay_state: _FailToPassState
) -> None:
Expand Down Expand Up @@ -700,7 +733,7 @@ def test_fail_to_pass_helpers_and_no_ci_stage(tmp_path: Path) -> None:
assert (
"fail-to-pass" in {spec.name for spec in get_check_specs()},
[(o.test_id, o.status) for o in outcomes],
("-o" in argv and "addopts=" in argv and "-p" in argv and "no:cacheprovider" in argv),
("-p" in argv and "no:cacheprovider" in argv),
"devops ci fail-to-pass" in testing_md,
"devops ci fail-to-pass" in generated_md,
) == (
Expand Down