test_apply_plan_backs_up_the_original_bytes¶
Tests › tests/test_init.py
What this test is for
Why it exists
The single most consequential write in the tool, so it is tested directly rather than through the command. `os.replace` guarantees the target is never half-written; it guarantees NOTHING about the content being right, and the content has been through a full parse → mutate → re-serialise round-trip. A defect anywhere in that round-trip destroys the file perfectly atomically.
Given
target = tmp_path / REPO_CONFIG_NAMEoriginal = ( '{\n "mcpServers": {\n "entropic": {"command": "entropic-mcp"}\n },\n' ' "note": "hand written, irreplaceable"\n}\n' )target.write_text(original, encoding="utf-8")
When
apply_plan(plan_merge(target, _expected_entry()))backup = target.with_name(target.name + BACKUP_SUFFIX)
Then
- ✓ backup.is_file() holds
- ✓ backup.read_text(encoding="utf-8") equals original
- ✓ json.loads(target.read_text(encoding="utf-8"))[SERVERS_KEY][MCP_ENTRY_NAME] equals _expected_entry()
- ✓ target.with_name(target.name + ".clew.tmp").exists() is false
What it exercises
The parts of the system's flows this test ran, measured by per-test coverage: lit steps ran in this test, dimmed ones did not, and ▶ marks where the test called in. Each title opens the whole flow with these steps lit.
clew — `main` (clew/cli.py) · 4 of 400 steps
… 190 earlier steps
… 202 later steps
tests/test_init.py:493 · its file's page
What it checks (4)
assert backup.is_file(), "an existing target must be backed up before it is replaced"not attributedassert backup.read_text(encoding="utf-8") == original, "the backup must hold the ORIGINAL bytes"not attributedassert json.loads(target.read_text(encoding="utf-8"))[SERVERS_KEY][MCP_ENTRY_NAME] == ( _expected_entry() )observes_expected_entryassert not target.with_name(target.name + ".clew.tmp").exists(), ( "the atomic-swap temp file must not survive" )not attributed
Where the product reaches it
Call flow
What it reaches
Each row runs from this test, through the calls it makes, to the code a requirement names. If that code breaks, the test's checks are what should catch it. Reaching is not proving: a check only proves what it observes. No mutation records were given, so these rows say what the test REACHES.
Also reaches 2 requirements
Its run touches their code; that is reach, not proof.
- REQ-DDB-CLI-002 measured
- REQ-DDB-MCP-001 measured