Read and write each record through the store its config names, and open one for agents with project-record
CI / tests (pull_request) Successful in 2m14s
CI / tests (pull_request) Successful in 2m14s
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
"""Open one of a project's records as a file to read and edit, and save the edit.
|
||||
|
||||
usage: project-record open <record>
|
||||
project-record save <record>
|
||||
|
||||
Run inside a checkout, or a worktree of one. <record> is one of decisions, score-anchors,
|
||||
comprehension-history, reading-cost and changelog, each kept where the project's config names it.
|
||||
|
||||
open prints the path of a file holding the record's text, and nothing else. Read and edit that
|
||||
file. Where the record is kept as a file, the path is that file. Where it is kept elsewhere, the
|
||||
path is a copy under ~/.local/state/project-record/<key>/, written afresh with the record as it
|
||||
stands, so open it again before each edit. A record that doesn't exist yet has no file at the path:
|
||||
create it there.
|
||||
|
||||
save stores the file's text in the record, and prints one line saying so. Where the record is kept
|
||||
as a file there is nothing left to store, and it says that. Run it after every edit, so the same
|
||||
steps work wherever a project keeps the record.
|
||||
|
||||
Exit 2 on bad arguments, and 1, saying why, on any other failure.
|
||||
"""
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from failure import Failure
|
||||
from record_store.stores import FileStore, local_file, RECORDS, RecordStore, store, stored
|
||||
from project_config.main import located
|
||||
|
||||
# Three readers, seated as the human and as the agents that follow the rules, picked the spelling from five candidates: git's verbs, such as checkout and commit, or pull and push, read as acting on the repo.
|
||||
COMMANDS = ("open", "save")
|
||||
# Where the copy of a record kept outside files goes, under the user's home, in a folder per project.
|
||||
COPIES = ".local/state/project-record"
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
if argv[:1] in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
if len(argv) != 2 or argv[0] not in COMMANDS or argv[1] not in RECORDS:
|
||||
print(f"usage: project-record {'|'.join(COMMANDS)} <record>, where <record> is one of {', '.join(RECORDS)}", file=sys.stderr)
|
||||
return 2
|
||||
home = pathlib.Path.home()
|
||||
config = located(None, home)
|
||||
done = config if isinstance(config, Failure) else act(argv[0], store(config, argv[1]), home / COPIES / config.key / f"{argv[1]}.md")
|
||||
if isinstance(done, Failure):
|
||||
print(f"project-record: {done.reason}", file=sys.stderr)
|
||||
return 1
|
||||
print(done)
|
||||
return 0
|
||||
|
||||
|
||||
def act(command: str, record: RecordStore, copy: pathlib.Path) -> str | Failure:
|
||||
"""What command, one of COMMANDS, prints for record, whose copy goes at copy where it needs one."""
|
||||
if command == "open":
|
||||
opened = local_file(record, copy)
|
||||
return opened if isinstance(opened, Failure) else str(opened)
|
||||
if unstored := stored(record, copy):
|
||||
return unstored
|
||||
return f"{record.path} is the record itself, so nothing is left to store." if isinstance(record, FileStore) else f"Stored {copy} in {record.label}."
|
||||
@@ -0,0 +1,71 @@
|
||||
import os
|
||||
import pathlib
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
|
||||
|
||||
from project_config.test_project_config import configured
|
||||
from failure import Failure
|
||||
from project_record import main
|
||||
from record_store.fakes import MemoryStore
|
||||
|
||||
TOOL = pathlib.Path(__file__).resolve().parents[1] / "project-record"
|
||||
|
||||
|
||||
class Act(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.copy = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory()), "acme", "decisions.md")
|
||||
|
||||
def test_open_prints_only_the_path_of_a_copy_holding_the_record(self):
|
||||
self.assertEqual(main.act("open", MemoryStore("# Decisions\n"), self.copy), str(self.copy))
|
||||
self.assertEqual(self.copy.read_text(), "# Decisions\n")
|
||||
|
||||
def test_save_stores_the_edited_copy_and_says_where(self):
|
||||
record = MemoryStore("# Decisions\n", label="the wiki page Decisions")
|
||||
main.act("open", record, self.copy)
|
||||
self.copy.write_text("# Decisions\n\n- New\n")
|
||||
self.assertEqual(main.act("save", record, self.copy), f"Stored {self.copy} in the wiki page Decisions.")
|
||||
self.assertEqual(record.text, "# Decisions\n\n- New\n")
|
||||
|
||||
def test_a_record_that_fails_is_a_failure(self):
|
||||
self.assertEqual(main.act("open", MemoryStore(fails="the wiki is down"), self.copy), Failure("the wiki is down"))
|
||||
|
||||
|
||||
class BlackBox(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.top = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())).resolve()
|
||||
configured(self, self.top)
|
||||
|
||||
def run_tool(self, *args: str) -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.run([sys.executable, str(TOOL), *args], capture_output=True, cwd=self.top, env=dict(os.environ), text=True)
|
||||
|
||||
def test_a_file_record_opens_as_its_own_file_and_save_has_nothing_to_store(self):
|
||||
opened = self.run_tool("open", "decisions")
|
||||
self.assertEqual((opened.returncode, opened.stdout, opened.stderr), (0, f"{self.top}/docs/decisions.md\n", ""))
|
||||
saved = self.run_tool("save", "decisions")
|
||||
self.assertEqual((saved.returncode, saved.stdout, saved.stderr), (0, f"{self.top}/docs/decisions.md is the record itself, so nothing is left to store.\n", ""))
|
||||
self.assertFalse((pathlib.Path(os.environ["HOME"]) / ".local").exists(), "a file record gets no copy")
|
||||
|
||||
def test_exits_2_with_the_usage_on_an_unknown_command_or_record(self):
|
||||
for args in ([], ["open"], ["open", "plan"], ["close", "decisions"], ["open", "decisions", "changelog"]):
|
||||
ran = self.run_tool(*args)
|
||||
self.assertEqual((ran.returncode, ran.stdout), (2, ""), args)
|
||||
self.assertIn("usage: project-record open|save <record>, where <record> is one of decisions, score-anchors, comprehension-history, reading-cost, changelog", ran.stderr)
|
||||
|
||||
def test_exits_1_saying_why_where_no_config_names_the_checkout(self):
|
||||
(pathlib.Path(os.environ["HOME"]) / ".claude-projects" / self.top.name / "config.toml").unlink()
|
||||
ran = self.run_tool("open", "decisions")
|
||||
self.assertEqual((ran.returncode, ran.stdout), (1, ""))
|
||||
self.assertIn("no config names the checkout", ran.stderr)
|
||||
|
||||
def test_help_prints_the_usage(self):
|
||||
ran = self.run_tool("--help")
|
||||
self.assertEqual(ran.returncode, 0)
|
||||
self.assertIn("usage: project-record open <record>", ran.stdout)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user