Answer stability round 1: 0 🔴, 17 🟡 (0 misread); read line ends as before, drop a stale copy, say why a read failed, and test each failure the round named

This commit is contained in:
2026-11-14 00:25:16 +01:00
parent d15380249d
commit b2142ee620
14 changed files with 151 additions and 43 deletions
+1 -11
View File
@@ -1,7 +1,7 @@
"""What every record backend must hold, as tests. A backend's test case inherits RecordStoreContract and unittest.TestCase, and builds its store in new_store."""
import typing
from record_store.stores import held_at, RecordStore
from record_store.stores import RecordStore
class RecordStoreContract:
@@ -30,13 +30,3 @@ class RecordStoreContract:
def test_names_itself_for_a_message(self) -> None:
self.assertTrue(self.new_store().label.strip())
def test_reads_as_it_stands_at_any_revision_where_no_commit_holds_it(self) -> None:
store = self.new_store()
store.write("as it stands\n")
if store.tracked is None:
self.assertEqual(held_at("no-such-revision", store, "/nowhere", run=refuse), "as it stands\n")
def refuse(*args: str, **options: object) -> str:
raise AssertionError(f"git {' '.join(args)} ran for a record no commit holds")
+14 -8
View File
@@ -44,18 +44,19 @@ class FileStore:
def read(self) -> str | Failure:
try:
# Decoded from bytes, so the text keeps the line ends the file holds.
return self.path.read_bytes().decode("utf-8")
return self.path.read_text(encoding="utf-8")
except FileNotFoundError:
return ""
except (OSError, UnicodeDecodeError) as failure:
return Failure(f"cannot read {self.path}: {failure}")
except UnicodeDecodeError as failure:
return Failure(f"cannot read {self.path}: not UTF-8 on line {failure.object[: failure.start].count(b'\n') + 1}")
except OSError as failure:
return Failure(f"cannot read {self.path}: {failure.strerror}")
def write(self, text: str) -> Failure | None:
try:
self.path.parent.mkdir(parents=True, exist_ok=True)
except OSError as failure:
return Failure(f"cannot write {self.path}: {failure}")
return Failure(f"cannot write {self.path}: {failure.strerror}")
unwritten = replace_file(self.path, text.encode("utf-8"))
return Failure(f"cannot write {self.path}: {unwritten.reason}") if unwritten else None
@@ -79,8 +80,13 @@ def local_file(record: RecordStore, copy: pathlib.Path) -> pathlib.Path | Failur
text = record.read()
if isinstance(text, Failure):
return text
if text and (unwritten := FileStore(copy, None).write(text)):
return unwritten
if text:
return FileStore(copy, None).write(text) or copy
try:
# An earlier copy would read as the record's text, and `stored` would put it back.
copy.unlink(missing_ok=True)
except OSError as failure:
return Failure(f"cannot remove {copy}: {failure.strerror}")
return copy
@@ -91,4 +97,4 @@ def stored(record: RecordStore, copy: pathlib.Path) -> Failure | None:
text = FileStore(copy, None).read()
if isinstance(text, Failure):
return text
return record.write(text) if text else Failure(f"{copy} is missing or empty, so nothing is stored in {record.label}; `record open` writes the file to edit")
return record.write(text) if text else Failure(f"{copy} is missing or empty, so nothing is stored in {record.label}")
+29 -17
View File
@@ -1,5 +1,4 @@
import pathlib
import re
import subprocess
import sys
import tempfile
@@ -41,10 +40,16 @@ class FileStore(unittest.TestCase):
def test_a_file_that_is_not_utf8_is_a_failure_naming_it(self):
path = self.folder / "decisions.md"
path.write_bytes(b"\xff\xfe")
path.write_bytes(b"# Decisions\n\xff\xfe")
self.assertEqual(stores.FileStore(path, None).read(), Failure(f"cannot read {path}: not UTF-8 on line 2"))
def test_a_folder_where_the_file_goes_is_a_failure_naming_it_once(self):
path = self.folder / "decisions.md"
path.mkdir()
read = stores.FileStore(path, None).read()
self.assertIsInstance(read, Failure)
self.assertIn(f"cannot read {path}: ", read.reason)
self.assertTrue(read.reason.startswith(f"cannot read {path}: "))
self.assertEqual(read.reason.count(str(path)), 1)
def test_a_write_that_fails_is_a_failure_naming_it(self):
(self.folder / "docs").write_text("a file where the folder goes")
@@ -65,11 +70,10 @@ class FileStore(unittest.TestCase):
self.assertIsInstance(written, Failure)
self.assertIn(f"cannot write {path}: ", written.reason)
def test_a_write_keeps_the_text_as_given_not_the_platforms_line_ends(self):
path = self.folder / "decisions.md"
stores.FileStore(path, None).write("one\r\ntwo\n")
self.assertEqual(path.read_bytes(), b"one\r\ntwo\n")
self.assertEqual(stores.FileStore(path, None).read(), "one\r\ntwo\n")
def test_a_file_with_windows_line_ends_reads_with_plain_ones(self):
path = self.folder / "CHANGELOG.md"
path.write_bytes(b"# Changelog\r\n\r\n## Unreleased\r\n")
self.assertEqual(stores.FileStore(path, None).read(), "# Changelog\n\n## Unreleased\n")
class HeldAt(unittest.TestCase):
@@ -94,7 +98,10 @@ class HeldAt(unittest.TestCase):
def test_a_record_outside_the_checkout_reads_as_it_stands(self):
store = stores.FileStore(self.top / "CHANGELOG.md", None)
self.assertEqual(stores.held_at("main", store, str(self.top)), "as it stands\n")
self.assertEqual(stores.held_at("no-such-revision", store, "/nowhere", run=self.refuse), "as it stands\n")
def refuse(self, *args: str, **options: object) -> str:
self.fail(f"git {' '.join(args)} ran for a record no commit holds")
class LocalFile(unittest.TestCase):
@@ -120,6 +127,19 @@ class LocalFile(unittest.TestCase):
copy = self.folder / "decisions.md"
self.assertEqual((stores.local_file(MemoryStore(), copy), copy.exists()), (copy, False))
def test_a_record_that_is_empty_now_loses_the_copy_made_earlier(self):
record, copy = MemoryStore("# Decisions\n"), self.folder / "decisions.md"
stores.local_file(record, copy)
record.text = ""
self.assertEqual((stores.local_file(record, copy), copy.exists()), (copy, False))
def test_a_copy_that_cannot_be_written_is_a_failure_naming_it(self):
(self.folder / "copies").write_text("a file where the folder goes")
copy = self.folder / "copies" / "decisions.md"
opened = stores.local_file(MemoryStore("# Decisions\n"), copy)
self.assertIsInstance(opened, Failure)
self.assertIn(f"cannot write {copy}: ", opened.reason)
def test_storing_a_missing_copy_fails_and_leaves_the_record(self):
record = MemoryStore("kept\n")
self.assertIn("is missing or empty", stores.stored(record, self.folder / "decisions.md").reason)
@@ -151,13 +171,5 @@ class Store(unittest.TestCase):
self.assertEqual(str(refused.exception), "plan is not a record; the records are decisions, score-anchors, comprehension-history, reading-cost, changelog")
class OneWayIn(unittest.TestCase):
def test_no_tool_reads_a_records_place_from_the_config_itself(self):
bin_folder = pathlib.Path(__file__).resolve().parents[1]
reach = re.compile(r"""(?:paths\[|\.place\(|\.tracked\()["'](?:%s)["']""" % "|".join(stores.RECORDS))
found = [f"{path.relative_to(bin_folder)}:{number}" for path in sorted(bin_folder.rglob("*.py")) if "test_" not in path.name and path.parent.name != "record_store" for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1) if reach.search(line)]
self.assertEqual(found, [], "read and write a record through record_store.stores.store")
if __name__ == "__main__":
unittest.main()