Skip to content
4 changes: 2 additions & 2 deletions docs/TESTING.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
schema: spec-driven
created: 2026-11-11
skip_specs: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Design

## Decisions

- **The real home is the `HOME` that `isolate_home` replaces.** `isolate_home` keeps it in the stash of the config. The controller adds it to the `workerinput` of each xdist worker in `pytest_configure_node`, and a worker reads it from there, because the worker inherits the isolated `HOME` of the controller. A pytester run in the same process records the `HOME` that its test sets, so a test can put the real home of the inner run in its own temporary directory.
- **The guard keeps three roots writable under the real home:** the run's temporary directory, the checkout with its common git directory (`checkout_roots` of `tests/support/host_paths.py`), and the interpreter (`sys.prefix`, `sys.base_prefix`, `sys.exec_prefix`, `sys.base_exec_prefix` and `sys.pycache_prefix` when it is set). Python writes bytecode caches into the interpreter, and a GitHub-hosted runner has the checkout in its home. The workspace tripwire checks the checkout.
- **The guard checks only while a test of the run runs**, from the start of its setup to the end of its teardown (a `pytest_runtest_protocol` wrapper). The issue asks the floor to fail a test. Outside a test, pytest itself writes into the root directory of the run. For a run that pytester starts, that directory is the `HOME` that pytester sets, so it is the real home of the inner run. A first version that checked all the time refused, in each pytester run of `tests/invariants/test_hermetic_floor.py`, the bytecode of the inner test module (`__pycache__/test_inner.cpython-314-pytest-9.1.1.pyc`) and the cache of the inner run (`pytest-cache-files-*/README.md`).
- **`_run_with_floor` gives the inner run the real home `pytester.path / "real-home"`.** The guard tests of the floor put their files outside the inner run under `pytester.path`, which pytester makes the `HOME`. With a real home of its own, such a file lies outside both the inner run and its real home.
- **The hook calls no function that a test can replace.** It compares the written path, made absolute and normalized with no file system call (`os.path.normpath`, and `os.getcwd` as the module took it at import), with the given and the resolved form of each root. A first version resolved the written path with `os.path.realpath`, which calls `os.lstat`. Three worktree tests replace `os.lstat` with a function that removes a file, so the removal started the hook again until the recursion limit (`tests/git/test_repos_worktrees.py`, `tests/git/test_repos_worktrees_reclaim.py`). The hook does not follow a symbolic link in the written path.
- **The guard refuses the same three events as the config write guard:** an open for writing, a rename onto a path and a removal. A path relative to a directory descriptor is not checked, as in the other two guards.
- **`hold_to_the_run` reads the run that the guard records at `pytest_configure`.** No run in the process means no pytest run, so the helper refuses. The refusal is `pytest.fail`, which an `except Exception` in a calling script does not hide.
- **One table of audit events.** `tests/support/file_changes.py` holds the events and the function that gets the changed path from an event. It is in `tests/`, not in `src/`: the fail-to-pass replay runs this `tests/` on the base's source, which lacks a name that the change adds to `src/`.

## Rejected alternatives

- **The home from the user database (`pwd.getpwuid(os.getuid()).pw_dir`).** It gives the same home in each worker with no transfer. But a test cannot put the real home of an inner run in its temporary directory without a patch of the user database. Also, the issue defines the real home as the `HOME` before `isolate_home`.
- **The `HOME` of each worker before `isolate_home`.** A worker inherits the isolated `HOME` of the controller (`isolate_home` sets `os.environ["HOME"]` at `pytest_configure`, and xdist starts the workers at session start). That home is in the run, so the guard would keep nothing.
- **An environment variable that carries the real home to the workers.** Each child process of a test would inherit it, and a test that clears the environment would lose it.
- **`PYTEST_VERSION` as the sign of a pytest run.** pytest sets it in `os.environ`, so a child process of a test inherits it, also when no floor runs in that child. The new test runs the helper in such a child process.
- **A refusal when `tests.support` is imported outside pytest.** `devops ci` runs `tests.support.git_recording` and `tests.support.check_test_settings` from plain Python (`src/devops_cli/commands/ci.py`, the `git` and `test-settings` rows).
- **The root directory of the run (`config.rootpath`) as a fourth writable root.** pytester makes its folder both the root directory and the `HOME` of the inner run, so the guard would keep nothing in a pytester run.
- **A third copy of the audit event table in the new guard.** The config write guard and the spend ledger write guard each held a copy. One module now holds it.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Proposal: #4739

## Why

Issue #4739: a helper of `tests/support/` wrote into the real home of a developer. An agent called `link_at_pin` from plain Python, outside pytest. No conftest ran, so the floor did not isolate `HOME` or the user data root. The helper got each path from devops-cli, which put it under `~/.local/share/devops-cli/tools/`, and wrote the stub `#!/bin/sh\necho mytool\n` over 18 real tools. The helper has no guard. Inside pytest, the two write guards of the floor refuse only config files and spend ledgers, and the host path guard sees only `open()`. No guard refuses another write under the real home.

## What Changes

- The new floor module `tests/support/home_write_guard.py` adds a PEP 578 audit hook, in the form of the config write guard. While a test runs, in its setup, call and teardown, it fails the test when the test opens a file for writing, renames a file onto one or removes one under the real home and outside the run's temporary directory, the checkout and the interpreter. The failure names the test, and the test also fails at teardown when the code under test catches the failure. `_run_with_floor` in `tests/invariants/test_hermetic_floor.py` gives each inner pytester run a real home of its own.
- `isolate_home` in `tests/support/bootstrap.py` records the real home, the `HOME` that it replaces. An xdist worker inherits the isolated `HOME` of the controller, so the controller gives its real home to each worker through `workerinput`.
- `hold_to_the_run` in the new module refuses a path outside the temporary directory of the pytest run in this process. When no pytest run is active in the process, it refuses each path. `link_at_pin` calls it for each path before it writes, and the write function of the `global_config` fixture calls it before it writes the global config file.
- The new module `tests/support/file_changes.py` holds the audit events that change a file. The config write guard, the spend ledger write guard and the home write guard read them from this one module.
- `docs/TESTING.md` names the home write guard and the rule for a support helper.
- The change alters no behavior a consumer observes. It changes only the test floor, its tests and a document. So it has no spec delta and sets `skip_specs: true`.
- The change fits one pull request.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

None.

## Impact

- Tests: a test that writes under the real home fails at the write and names itself. A support helper that is called from plain Python stops before it writes and tells why.
- Files: `tests/support/home_write_guard.py` (new), `tests/support/file_changes.py` (new), `tests/support/bootstrap.py`, `tests/support/tools.py`, `tests/support/config.py`, `tests/support/config_write_guard.py`, `tests/support/spend_write_guard.py`, `tests/invariants/test_hermetic_floor.py`, `docs/TESTING.md`.
- No path under `src/` changes.

## Feasibility

- The cause is in the code at `origin/main` (`4b289804`). `link_at_pin` (`tests/support/tools.py`) gets each path from `installed_commands`, which puts it under `tool_dir`, which is `user_data_root() / "tools" / <name> / <version>` (`src/devops_cli/tools_lock/__init__.py`). With no `DEVOPS_CLI_USER_DATA_ROOT`, `user_data_root()` returns `CONST_USER_DATA_ROOT`, which is `Path.home() / ".local" / "share" / "devops-cli"` (`src/devops_cli/core/repo.py`, `src/devops_cli/config/constants.py`).
- The census of the support helpers that write to a path that devops-cli resolves: `git grep -nE "user_data_root\(|Path\.home\(|expanduser\(|installed_commands\(|CONST_CONFIG_PATH\.(write|parent\.mkdir)" -- tests/support` prints 3 lines in 2 members: `link_at_pin` in `tests/support/tools.py`, a plain function, and the write function of the `global_config` fixture in `tests/support/config.py`. Both call `hold_to_the_run`.
- The floor has no guard for these writes. `config_write_guard.py` and `spend_write_guard.py` refuse a write only when the file name is a config file or a spend ledger. The host path guard in `tests/conftest.py` replaces `builtins.open` and `io.open` only, so `os.open`, `os.replace` and `os.remove` pass it, and its failure in another thread fails no test.
- pytest 9.1.1 and pytest-xdist 3.8.0 are in the lock. `isolate_home` sets `os.environ["HOME"]` at `pytest_configure`, and xdist starts its workers later, at session start, so a worker inherits the isolated `HOME`. In `xdist/workermanage.py`, `WorkerController.setup` builds `workerinput` (line 308), calls `pytest_configure_node` (line 342) and then sends `workerinput` to the worker (line 349). So a hook of the controller can add the real home to `workerinput`.
- The uv interpreter of the devcontainer is under the real home: `sys.base_prefix` is `/home/vscode/.local/share/uv/python/cpython-3.14.10-linux-x86_64-gnu`. Python writes its bytecode cache there. On a GitHub-hosted runner, the checkout is under `/home/runner/work/`, in the home of the runner. So the guard keeps the checkout and the interpreter writable.
- The reproduction of the issue was not run by hand: a support helper is called only through pytest. The new test runs the same call in a child process whose `HOME` is in the test's temporary directory.

## Existing Tool Considered

- PEP 578 audit hooks (`sys.addaudithook`, in the standard library since Python 3.8) report each `open`, `os.rename` and `os.remove` with its path. The config write guard and the spend ledger write guard use them. The home write guard uses them too.
- pytest's `Stash` keeps the real home on the config of the run, and the `pytest_configure_node` hook of pytest-xdist with `workerinput` carries it to each worker.
- pyfakefs replaces the file system of a test with a fake. It does not refuse a real write, it would change every test of the suite, and it does not run outside pytest. No maintained pytest plugin fails a test on a write by its path.
- The code to build is small: about 120,000 tokens to build, about 5,000 tokens a year to keep and fix, and about 2 hours of elapsed time.
- The change deletes the two copies of the audit event table and of `_changed_path` in `tests/support/config_write_guard.py` and `tests/support/spend_write_guard.py`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Tasks

## 1. The floor fails a test that writes under the real home

Kind: behavior. Changes: `tests/invariants/test_hermetic_floor.py`, `tests/support/bootstrap.py`, `tests/support/home_write_guard.py`, `tests/support/file_changes.py`, `tests/support/config_write_guard.py`, `tests/support/spend_write_guard.py`.

- [x] 1.1 Write a failing test: in a pytester run whose real home is in the test's temporary directory, a test that opens a file for writing, opens a descriptor for writing, renames a file onto one or removes one under that home fails and is named, also when it catches the failure. A read under the home, and a write under `tmp_path`, under the isolated `HOME` and outside the home pass. No file under the home changes.
- Verification: `$ uv run devops ci test tests/invariants/test_hermetic_floor.py`
- [x] 1.2 Record the real home in `isolate_home` and give it to each xdist worker. Add the home write guard. Move the audit event table into `tests/support/file_changes.py` and read it in the three guards.
- Verification: `$ uv run devops ci test tests/invariants/test_hermetic_floor.py tests/config/test_isolation_tripwire.py`

- [x] 1.3 Write a failing test: in a pytester run, a test that replaces `os.lstat` with a function that removes a file passes, and the function runs once. Then compare the written path with no file system call in the hook.
- Verification: `$ uv run devops ci test tests/invariants/test_hermetic_floor.py tests/git/test_repos_worktrees.py tests/git/test_repos_worktrees_reclaim.py`

## 2. A support helper writes only inside a pytest run

Kind: behavior. Changes: `tests/invariants/test_hermetic_floor.py`, `tests/support/home_write_guard.py`, `tests/support/tools.py`, `tests/support/config.py`.

- [x] 2.1 Write failing tests: `link_at_pin`, called from plain Python with `HOME` in the test's temporary directory, exits with the refusal and writes nothing. In a pytester run, `link_at_pin` with a user data root outside the run's temporary directory refuses and writes nothing.
- Verification: `$ uv run devops ci test tests/invariants/test_hermetic_floor.py`
- [x] 2.2 Add `hold_to_the_run`, and call it in `link_at_pin` for each path and in the write function of `global_config` before each write.
- Verification: `$ uv run devops ci test tests/invariants/test_hermetic_floor.py tests/commands/test_install_tools.py tests/commands/test_devcontainer_install_tools_dry_run.py tests/config/test_setting_sources.py`

## 3. Documents

Kind: documents. Changes: `docs/TESTING.md`.

- [x] 3.1 Name the home write guard and the rule for a support helper in the hermetic floor of `docs/TESTING.md`.
- Verification: `$ uv run devops ci docs`
Loading