refactor(file-ops): fold simplify-pass findings

- write_file: encode content once, share bytes between bytes_written and
  the sha256 verification (drops a second full-content encode per write)
- patch_parser: replace the except-TypeError retry around
  write_file(pre_content=...) with signature-based feature detection so a
  TypeError raised inside a capable implementation propagates instead of
  triggering a duplicate write; tests for both duck-typing contracts
- tests: real-ops V4A BOM round-trip + _file_has_bom disk-probe guard
  (the teknium1-review regression previously only covered by a fake)
- comment: document dirs_created's long-standing "parent ensured" meaning
This commit is contained in:
kshitijk4poor
2026-08-03 16:47:54 +05:30
committed by kshitij
parent fcae5ad49a
commit 8c19e29259
4 changed files with 126 additions and 14 deletions
+56
View File
@@ -692,6 +692,62 @@ class _DictFileOps:
return SimpleNamespace(error=None)
class TestDuckTypedWriteFileCompat:
"""V4A UPDATE must work with basic write_file(path, content) impls.
apply_v4a_operations is duck-typed (file_ops: Any); external callers may
only implement the two-argument contract. The signature-based feature
detection must route them to the 2-arg call — and must NOT swallow a
TypeError raised INSIDE a pre_content-capable write_file (which would
trigger a duplicate write).
"""
PATCH = (
"*** Begin Patch\n"
"*** Update File: f.py\n"
"@@\n"
"-x = 1\n"
"+x = 2\n"
"*** End Patch"
)
def test_two_arg_write_file_still_supported(self):
calls = []
class BasicOps(_DictFileOps):
def write_file(self, path, content): # no pre_content
calls.append(path)
self.files[path] = content
return SimpleNamespace(error=None)
ops, err = parse_v4a_patch(self.PATCH)
assert err is None
fo = BasicOps({"f.py": "x = 1\n"})
result = apply_v4a_operations(ops, fo)
assert result.success is True, getattr(result, "error", None)
assert fo.files["f.py"] == "x = 2\n"
assert calls == ["f.py"] # exactly one write, no double invocation
def test_internal_typeerror_not_silently_retried(self):
# A TypeError raised INSIDE a pre_content-capable write_file must not
# trigger a second 2-arg write. (The op-loop's blanket except turns it
# into a failed result — the key contract is: ONE call, error surfaced.)
calls = []
class ExplodingOps(_DictFileOps):
def write_file(self, path, content, pre_content=None):
calls.append(path)
raise TypeError("bug inside a pre_content-capable impl")
ops, err = parse_v4a_patch(self.PATCH)
assert err is None
fo = ExplodingOps({"f.py": "x = 1\n"})
result = apply_v4a_operations(ops, fo)
assert result.success is False
assert "bug inside" in result.error
assert calls == ["f.py"] # not silently retried with 2 args
class TestMoveThenUpdateSameFile:
"""A rename-then-edit patch must validate and apply (was rejected).