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
2 changes: 1 addition & 1 deletion docs/TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Every new test follows these rules. Some of them name a path, fixture or check t

- `devops ci test <paths>` runs only the tests a change reaches, and so does `devops test run --changed`, over the files changed since the branch left `main`, committed or not. A test module is reached when it is named for a changed module, at any depth, or imports it. A test module that only sits in the directory mirroring the module's package is not reached.
- `devops ci test` takes a directory too. A directory of `tests/` runs the test modules (`test_*.py`) inside it at any depth, and not its helper modules. A directory of `src/` selects tests for the Python modules inside it, as for a list of files. It refuses a path that does not exist, a directory that gives no module, and a directory outside `src/` and `tests/`, with exit status 1 and an error that names the path, whatever `--fallback` says. A file that exists and maps to no test, such as `README.md`, still exits 0 with a note, and a trigger file that is gone, such as a deleted `conftest.py`, still forces the full run (#2868).
- Given no `-n`, a narrowed `devops ci test` runs serially for up to `DEFAULT_CI_TEST_SERIAL_MAX_FILES` test modules, where starting xdist workers costs more than it saves, and on `DEFAULT_CI_TEST_NARROWED_WORKERS` workers above that; the full suite, and the fallback to it, run on `addopts`' `-n logical`. A direct `uv run pytest` with `-n logical` or `-n auto` waits for a gate slot in the `pytest_xdist_auto_num_workers` wrapper of the root conftest, and starts at most the CPUs of one slot (#4525). Any run with workers distributes its tests with `addopts`' `--dist worksteal`: a worker that runs out of tests takes half the tests another worker has not started, so a block of slow tests handed to one worker late in the run no longer keeps the others waiting for it. It runs pytest in a session of its own, so a timeout, Ctrl-C, SIGTERM or SIGHUP stops pytest and its workers with `uv`: a timeout with SIGTERM, a signal by first forwarding the SIGINT a terminal would have sent, so pytest runs its teardown, then SIGTERM and SIGKILL, which a second signal brings forward and cannot skip. The run takes over all three signals through `loop.add_signal_handler`, so each one wakes the run's event loop wherever it lands, only cancels the run, and raises no KeyboardInterrupt in the middle of the stop. A test that drives `devops ci test` doubles `asyncio.create_subprocess_exec` with `record_spawns` from `tests/support/session_spawns.py`, and asks for `refuse_nested_test_runs`, which fails any real `uv` or pytest it would start.
- Given no `-n`, a narrowed `devops ci test` collects the tests that it selects before it starts them. The collection command is `uv run pytest -n 0 --collect-only -q`, with the same target list and `-k` filter as the run. The run is serial, `-n 0`, for up to `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` (50) collected tests, where the start-up of xdist workers costs more than it saves. It runs on `DEFAULT_CI_TEST_NARROWED_WORKERS` workers above that. The `-k` filter changes the count, so a selection of 399 tests that `-k slow` narrows to 5 runs serially. A collection that exits 5, the exit pytest gives when no test matches, is a count of zero. That run is serial and prints no warning. A collection that fails or times out prints a warning that names the command and the reason, and the number of test modules then decides. Up to `DEFAULT_CI_TEST_SERIAL_MAX_FILES` (12) modules run serially, and more run on `DEFAULT_CI_TEST_NARROWED_WORKERS` workers, so a failed collection over 13 target files runs on 4 workers. A given `-n`, a run with no paths, a dry run and a `--repeat` run make no collection call and keep their worker choice. The full suite, and the fallback to it, run on `addopts`' `-n logical`. A direct `uv run pytest` with `-n logical` or `-n auto` waits for a gate slot in the `pytest_xdist_auto_num_workers` wrapper of the root conftest, and starts at most the CPUs of one slot (#4525). Any run with workers distributes its tests with `addopts`' `--dist worksteal`: a worker that runs out of tests takes half the tests another worker has not started, so a block of slow tests handed to one worker late in the run no longer keeps the others waiting for it. It runs pytest in a session of its own, so a timeout, Ctrl-C, SIGTERM or SIGHUP stops pytest and its workers with `uv`: a timeout with SIGTERM, a signal by first forwarding the SIGINT a terminal would have sent, so pytest runs its teardown, then SIGTERM and SIGKILL, which a second signal brings forward and cannot skip. The run takes over all three signals through `loop.add_signal_handler`, so each one wakes the run's event loop wherever it lands, only cancels the run, and raises no KeyboardInterrupt in the middle of the stop. A test that drives `devops ci test` doubles `asyncio.create_subprocess_exec` with `record_spawns` from `tests/support/session_spawns.py`, and asks for `refuse_nested_test_runs`, which fails any real `uv` or pytest it would start. The collection starts through that same edge, so a test that answers the run answers the collection too. A helper that reads the pytest run skips the child that holds `--collect-only`.
- A test module that replays a shared set computes the replay once in a module-scoped fixture and gives each case its own parametrized item, so a narrowed run stays inside the budget and xdist can distribute the cases.
- With a fresh coverage index (`devops ci coverage --build-index`), `devops ci test` selects the test modules that executed each changed module the index holds, in place of name-and-import matching. A module the index does not hold, a missing or stale index, and `devops test run --changed` select by name and import. So a test that should run for a module executes the module's code inside the test, not only on import, and names the module or imports it.
- A change that can alter tests that never import it runs the whole suite: `conftest.py`, `pyproject.toml`, `uv.lock`, `.python-version`, and a helper module under `tests/` that reaches every test, as the next rule says. `devops ci test`, and so the commit hook, runs the whole suite for any `conftest.py` it is given, at any depth, and under `--no-fallback` refuses any of these files instead. `devops test run --changed` narrows a conftest below `tests/`: one that only defines fixtures selects its own directory, and one that binds a `pytest_*` hook, which pytest calls for every test, selects the whole suite.
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/ci.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- **Start the collection through `_run_in_own_session`, the runner the run itself uses.**: One edge starts both children, the process runner seam answers both in the tests, and `tests/support/session_spawns.py:def record_spawns` doubles the same start.
- **Give the collection-failure case 13 target files, more than `DEFAULT_CI_TEST_SERIAL_MAX_FILES`.**: `src/devops_cli/commands/ci.py:1940:if len(targets) <= DEFAULT_CI_TEST_SERIAL_MAX_FILES:` gives `-n 0` at 12 files or fewer, so a 7-file case cannot tell the file-count fallback from a count of zero; at 13 files the fallback gives `-n 4` and the test asserts the rule that ran.
- **Treat pytest's exit code 5 as a count of zero and as no collection failure.**: A `-k` filter that matches no test is no failure of the collection, so the command runs `-n 0` with no warning; the fallback scenario names exit code 3, so no test reads two answers from one exit code.
- **Prove the fix with the selection of the issue, and read the new limit from the `defaults` module in the body of a test.**: `devops ci fail-to-pass` replays the changed tests on the base, and only a test that fails in its call phase proves the fix. A module-level import of `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` stops the collection of `tests/commands/test_test_selection.py` on the base, so no case of the module runs there. The 13-file cases get `-n 4` from the file count on the base too. The case of 7 test modules that collect 399 tests shows the defect, because the base gives it `-n 0`.
- **Skip a child that holds `--collect-only` in the test helpers that read the pytest run.**: `tests/commands/test_test_selection.py:70:def _pytest_runs(process_fake: ProcessFake) -> list[list[str]]:` and `tests/commands/test_ci.py:1947:runs = [call for call in process_fake.calls if "pytest" in call.argv]` take the first pytest child as the run, and the collection is now the first one.
- **Give `-n 2` to the tests that read the run's own child.**: A given `-n` makes no collection call, so the helpers `_run_recording_handlers` and `_devops_ci_test` and the timeout case keep the child they signal, time out and record.
- **Make no collection call in a dry run.**: A dry run renders the planned command and starts nothing, so a collection there would answer with a plan and no count.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@

Kind: behavior. Reads: `src/devops_cli/commands/ci.py`, `src/devops_cli/config/defaults.py`, `src/devops_cli/lang/en/messages.py`, `src/devops_cli/lang/en/help.py`, `src/devops_cli/roadmap/implement/implement_test_form.py`, `tests/commands/test_test_selection.py`, `tests/support/fakes/process.py`, `tests/support/session_spawns.py`. Changes: `src/devops_cli/commands/ci.py`, `src/devops_cli/config/defaults.py`, `src/devops_cli/lang/en/messages.py`, `src/devops_cli/lang/en/help.py`, `tests/commands/test_test_selection.py`.

- [ ] 1.1 Add the failing case `tests/commands/test_test_selection.py::test_a_selection_past_the_collected_test_limit_runs_on_the_capped_workers` and replace `tests/commands/test_test_selection.py::test_a_selection_past_the_serial_limit_runs_on_the_capped_workers` with it, because the file count no longer decides alone
- [x] 1.1 Add the failing case `tests/commands/test_test_selection.py::test_a_few_test_modules_that_collect_many_tests_run_on_the_capped_workers`, the selection of the issue: 7 test modules whose collection answers 399 test ids run on `DEFAULT_CI_TEST_NARROWED_WORKERS`, where the file count gives `-n 0`. Add the failing case `tests/commands/test_test_selection.py::test_a_selection_past_the_collected_test_limit_runs_on_the_capped_workers` and replace `tests/commands/test_test_selection.py::test_a_selection_past_the_serial_limit_runs_on_the_capped_workers` with it, because the file count no longer decides alone
- Verification: `$ uv run devops ci test tests/commands/test_test_selection.py` fails on the base
- [ ] 1.2 Write the failing cases first in `tests/commands/test_test_selection.py`: import `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` and `ProcessAnswer`, make `_pytest_runs` and `_started_runs` skip a child that holds `--collect-only`, and answer the collection with `process_fake.answer` and a matcher on `--collect-only` whose `ProcessAnswer` stdout holds one test id on each line. A collection of `DEFAULT_CI_TEST_SERIAL_MAX_TESTS + 1` ids runs on `DEFAULT_CI_TEST_NARROWED_WORKERS`; a collection of `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` ids runs serially; a collection that exits 5 runs serially and prints no warning; a collection that exits 3 over 13 target files prints the warning and runs on `DEFAULT_CI_TEST_NARROWED_WORKERS`, because the file-count rule gives 4 workers above 12 files while a count of zero gives `-n 0`; a `-k slow` selection passes `-k slow` to the collection and to the run; a given `-n` and a run with no paths make no collection call. Then add `DEFAULT_CI_TEST_SERIAL_MAX_TESTS: Final[int] = 50` beside the file-count constant with a comment that states the rule, add the collection-failure warning to the CI messages beside `repeat_seed_unavailable`, and rewrite the `-n` help text to say that the collected tests choose the workers. Add `_collection_cmd(targets, keyword)`, which returns `[*_build_test_cmd("0", False, keyword, False, None), "--collect-only", "-q", *targets]`, and `_collected_test_count(targets, keyword)`, which returns None in a dry run, starts the command with `asyncio.run(_run_in_own_session(cmd, _get_project_root(), DEFAULT_SUBPROCESS_TIMEOUT_SECONDS, True, None))`, returns 0 on exit 5, returns None with the warning on `subprocess.TimeoutExpired` or any other non-zero exit, re-raises `asyncio.CancelledError` as `typer.Exit(CONST_EXIT_CANCELLED)`, and otherwise returns `collected_items(stdout)`. Give `_narrowed_workers(targets, collected)` the collected count and keep the count of test modules for the None case
- [x] 1.2 Write the failing cases first in `tests/commands/test_test_selection.py`: import `ProcessAnswer` and the `devops_cli.config.defaults` module, and read `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` from that module in the body of the test, so that the module imports on the base and its cases fail there in their call phase, make `_pytest_runs` and `_started_runs` skip a child that holds `--collect-only`, and answer the collection with `process_fake.answer` and a matcher on `--collect-only` whose `ProcessAnswer` stdout holds one test id on each line. A collection of `DEFAULT_CI_TEST_SERIAL_MAX_TESTS + 1` ids runs on `DEFAULT_CI_TEST_NARROWED_WORKERS`; a collection of `DEFAULT_CI_TEST_SERIAL_MAX_TESTS` ids runs serially; a collection that exits 5 runs serially and prints no warning; a collection that exits 3 over 13 target files prints the warning and runs on `DEFAULT_CI_TEST_NARROWED_WORKERS`, because the file-count rule gives 4 workers above 12 files while a count of zero gives `-n 0`; a `-k slow` selection passes `-k slow` to the collection and to the run; a given `-n` and a run with no paths make no collection call. Then add `DEFAULT_CI_TEST_SERIAL_MAX_TESTS: Final[int] = 50` beside the file-count constant with a comment that states the rule, add the collection-failure warning to the CI messages beside `repeat_seed_unavailable`, and rewrite the `-n` help text to say that the collected tests choose the workers. Add `_collection_cmd(targets, keyword)`, which returns `[*_build_test_cmd("0", False, keyword, False, None), "--collect-only", "-q", *targets]`, and `_collected_test_count(targets, keyword)`, which returns None in a dry run, starts the command with `asyncio.run(_run_in_own_session(cmd, _get_project_root(), DEFAULT_SUBPROCESS_TIMEOUT_SECONDS, True, None))`, returns 0 on exit 5, returns None with the warning on `subprocess.TimeoutExpired` or any other non-zero exit, re-raises `asyncio.CancelledError` as `typer.Exit(CONST_EXIT_CANCELLED)`, and otherwise returns `collected_items(stdout)`. Give `_narrowed_workers(targets, collected)` the collected count and keep the count of test modules for the None case
- Verification: `$ uv run devops ci test tests/commands/test_test_selection.py` passes

## 2. Skip the collection in the tests that read the pytest run

Kind: other. Reads: `tests/commands/test_ci.py`, `tests/commands/test_ci_repeat.py`, `tests/support/session_spawns.py`, `src/devops_cli/commands/ci.py`. Changes: `tests/commands/test_ci.py`, `tests/commands/test_ci_repeat.py`.

- [ ] 2.1 In `tests/commands/test_ci.py`, update `test_staged_sources_select_the_worktrees_tests` so that its `runs` list drops a child that holds `--collect-only`, because the collection is now the first pytest child. Give `-n 2` to `test_a_test_run_past_its_timeout_fails_naming_its_command_and_timeout` and to the invocation that `_run_recording_handlers` makes, which serves `test_a_run_takes_over_ctrl_c_sigterm_and_sighup_and_hands_them_back`, `test_a_run_off_the_main_thread_sets_no_signal_handler` and `test_an_ignored_hangup_stays_ignored_through_a_run`. Add `-n 2` to the default argv of `_devops_ci_test`, which serves `test_a_signal_to_devops_ci_test_lets_pytest_tear_down_then_stops_its_group` and `test_a_second_signal_to_devops_ci_test_cannot_skip_the_sigkill`, so their fake `uv` is the child that waits for the signal. In `tests/commands/test_ci_repeat.py`, drop a call that holds `--collect-only` from what `_invoke` returns, so `test_the_gate_and_a_plain_run_stay_in_file_order` keeps the one run that it compares
- [x] 2.1 In `tests/commands/test_ci.py`, update `test_staged_sources_select_the_worktrees_tests` so that its `runs` list drops a child that holds `--collect-only`, because the collection is now the first pytest child. Give `-n 2` to `test_a_test_run_past_its_timeout_fails_naming_its_command_and_timeout` and to the invocation that `_run_recording_handlers` makes, which serves `test_a_run_takes_over_ctrl_c_sigterm_and_sighup_and_hands_them_back`, `test_a_run_off_the_main_thread_sets_no_signal_handler` and `test_an_ignored_hangup_stays_ignored_through_a_run`. Add `-n 2` to the default argv of `_devops_ci_test`, which serves `test_a_signal_to_devops_ci_test_lets_pytest_tear_down_then_stops_its_group` and `test_a_second_signal_to_devops_ci_test_cannot_skip_the_sigkill`, so their fake `uv` is the child that waits for the signal. In `tests/commands/test_ci_repeat.py`, drop a call that holds `--collect-only` from what `_invoke` returns, so `test_the_gate_and_a_plain_run_stay_in_file_order` keeps the one run that it compares
- Verification: `$ uv run devops ci test tests/commands/test_ci.py tests/commands/test_ci_repeat.py` passes

## 3. Describe the collected-test worker rule in the docs

Kind: other. Reads: `docs/TESTING.md`, `docs/commands/ci.md`, `src/devops_cli/lang/en/help.py`. Changes: `docs/TESTING.md`, `docs/commands/ci.md`.

- [ ] 3.1 Replace the file-count sentence of `docs/TESTING.md` with the collected-test rule: the collection command, the threshold of 50 collected tests, the `-k` filter, an empty collection, the fallback on a failure with its 13-file example, and the runs that make no collection call. State that a test that drives the command answers the collection through the process edge it already doubles. Then regenerate the command pages with `uv run devops docs generate`, which rewrites `docs/commands/ci.md` from the changed help text
- [x] 3.1 Replace the file-count sentence of `docs/TESTING.md` with the collected-test rule: the collection command, the threshold of 50 collected tests, the `-k` filter, an empty collection, the fallback on a failure with its 13-file example, and the runs that make no collection call. State that a test that drives the command answers the collection through the process edge it already doubles. Then regenerate the command pages with `uv run devops docs generate`, which rewrites `docs/commands/ci.md` from the changed help text
- Verification: `$ uv run devops ci docs` passes
- [ ] 3.2 Edit `docs/TESTING.md` and regenerate `docs/commands/ci.md` with `uv run devops docs generate`
- [x] 3.2 Edit `docs/TESTING.md` and regenerate `docs/commands/ci.md` with `uv run devops docs generate`
- Verification: `$ uv run devops ci docs` passes

## Workflow follow-up
Expand Down
Loading