170 lines
7.4 KiB
Python
170 lines
7.4 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
|
|
from grm import store # noqa: E402
|
|
|
|
|
|
class _Scratch(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = Path(tempfile.mkdtemp()).resolve()
|
|
self.addCleanup(shutil.rmtree, self.tmp, True)
|
|
self.scratch = self.tmp / "scratch"
|
|
env = mock.patch.dict(os.environ, {"SPIRE_SCRATCH": str(self.scratch)})
|
|
env.start()
|
|
self.addCleanup(env.stop)
|
|
|
|
def unset_scratch(self, **extra):
|
|
env = {k: v for k, v in os.environ.items() if k != "SPIRE_SCRATCH"}
|
|
patch = mock.patch.dict(os.environ, {**env, **extra}, clear=True)
|
|
patch.start()
|
|
self.addCleanup(patch.stop)
|
|
|
|
def chdir(self, d):
|
|
old = os.getcwd()
|
|
os.chdir(d)
|
|
self.addCleanup(os.chdir, old)
|
|
|
|
def repo(self, name, commit=False):
|
|
d = self.tmp / name
|
|
(d / "src").mkdir(parents=True)
|
|
subprocess.run(["git", "init", "-q"], cwd=d, check=True, capture_output=True)
|
|
if commit:
|
|
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q",
|
|
"--allow-empty", "-m", "x"], cwd=d, check=True, capture_output=True)
|
|
return d
|
|
|
|
def scratch_of(self, run):
|
|
return store.scratch_root(run).resolve()
|
|
|
|
|
|
class ResolverTest(_Scratch):
|
|
def test_env_wins(self):
|
|
"""(a) $SPIRE_SCRATCH names the scratch root."""
|
|
run = self.tmp / "logs" / "2026-10-22-10-x" / "run.md"
|
|
self.assertEqual(store.state_dir(run), self.scratch / "2026-10-22-10-x" / "gremlins")
|
|
|
|
def test_git_toplevel(self):
|
|
"""(b) without $SPIRE_SCRATCH, the run log's checkout holds the scratch root (cwd: same repo)."""
|
|
self.unset_scratch()
|
|
repo = self.repo("repo")
|
|
self.chdir(repo / "src")
|
|
run = repo / "logs" / "2026-10-22-10-x" / "run.md"
|
|
run.parent.mkdir(parents=True)
|
|
self.assertEqual(store.state_dir(run).resolve(),
|
|
repo / ".spire" / "scratch" / "2026-10-22-10-x" / "gremlins")
|
|
|
|
def test_no_repo(self):
|
|
"""(c) outside a repo, or without a git binary: the run's own dir; slug is the stem."""
|
|
self.unset_scratch(GIT_CEILING_DIRECTORIES=str(self.tmp))
|
|
run = self.tmp / "x" / "notes.md"
|
|
run.parent.mkdir()
|
|
self.chdir(run.parent)
|
|
want = self.tmp / "x" / ".spire" / "scratch" / "notes" / "gremlins"
|
|
self.assertEqual(store.state_dir(run), want)
|
|
with mock.patch.object(store.subprocess, "run", side_effect=FileNotFoundError("git")):
|
|
self.assertEqual(store.state_dir(run), want)
|
|
|
|
def test_worktree_same_repo(self):
|
|
"""(b2) cwd in a worktree of the run log's repo: the run log's checkout, as in (b)."""
|
|
self.unset_scratch()
|
|
main = self.repo("main", commit=True)
|
|
wt = self.tmp / "wt"
|
|
subprocess.run(["git", "worktree", "add", "-q", str(wt)], cwd=main, check=True,
|
|
capture_output=True)
|
|
self.chdir(wt)
|
|
self.assertEqual(self.scratch_of(main / "logs" / "a" / "run.md"), main / ".spire" / "scratch")
|
|
self.chdir(main / "src")
|
|
self.assertEqual(self.scratch_of(wt / "logs" / "a" / "run.md"), wt / ".spire" / "scratch")
|
|
|
|
def test_run_log_in_other_repo(self):
|
|
"""(b3) run log in a separate repo (external tickets folder): the cwd's checkout, also
|
|
before the run dir exists."""
|
|
self.unset_scratch()
|
|
code, tickets = self.repo("code"), self.repo("code-tickets")
|
|
self.chdir(code / "src")
|
|
run = tickets / "workpad" / "logs" / "2026-10-24-10-x" / "run.md"
|
|
self.assertEqual(self.scratch_of(run), code / ".spire" / "scratch")
|
|
run.parent.mkdir(parents=True)
|
|
self.assertEqual(self.scratch_of(run), code / ".spire" / "scratch")
|
|
|
|
def test_run_log_in_no_repo(self):
|
|
"""(b4) run log in no repo, cwd in one: the cwd's checkout. cwd in none: (b) or (c)."""
|
|
self.unset_scratch(GIT_CEILING_DIRECTORIES=str(self.tmp))
|
|
code = self.repo("code")
|
|
run = self.tmp / "tickets" / "workpad" / "logs" / "x" / "run.md"
|
|
run.parent.mkdir(parents=True)
|
|
self.chdir(code / "src")
|
|
self.assertEqual(self.scratch_of(run), code / ".spire" / "scratch")
|
|
self.chdir(self.tmp / "tickets")
|
|
self.assertEqual(self.scratch_of(run), run.parent / ".spire" / "scratch")
|
|
self.assertEqual(self.scratch_of(code / "logs" / "x" / "run.md"), code / ".spire" / "scratch")
|
|
|
|
def test_env_wins_over_other_repo(self):
|
|
"""(a2) $SPIRE_SCRATCH wins when the run log and the cwd are in different repos."""
|
|
code, tickets = self.repo("code"), self.repo("code-tickets")
|
|
self.chdir(code / "src")
|
|
self.assertEqual(store.scratch_root(tickets / "logs" / "x" / "run.md"), self.scratch)
|
|
|
|
def test_stray_sibling_ignored(self):
|
|
"""(d) a stray `<run>-gremlins/` sibling is just a dir: state is under the scratch root."""
|
|
run = self.tmp / "notes.md"
|
|
(self.tmp / "notes-gremlins").mkdir()
|
|
self.assertEqual(store.init_state(run), self.scratch / "notes" / "gremlins")
|
|
self.assertEqual((self.scratch / ".gitignore").read_text(encoding="utf-8"), "*\n")
|
|
self.assertEqual(list((self.tmp / "notes-gremlins").iterdir()), [])
|
|
|
|
def test_gitignore_once(self):
|
|
"""(e) init writes `<scratch_root>/.gitignore` = `*` and never overwrites one."""
|
|
store.init_state(self.tmp / "a.md")
|
|
ignore = self.scratch / ".gitignore"
|
|
self.assertEqual(ignore.read_text(encoding="utf-8"), "*\n")
|
|
ignore.write_text("mine\n", encoding="utf-8")
|
|
store.init_state(self.tmp / "a.md")
|
|
store.init_state(self.tmp / "b.md")
|
|
self.assertEqual(ignore.read_text(encoding="utf-8"), "mine\n")
|
|
self.assertEqual(sorted(p.name for p in (self.scratch / "a" / "gremlins").iterdir()),
|
|
sorted(store.SUBDIRS))
|
|
|
|
def test_non_md_refused(self):
|
|
"""(f) a run that is not a .md file is refused, with the same error text."""
|
|
for f in (store.state_dir, store.init_state, store.run_slug, store.scratch_root):
|
|
with self.assertRaisesRegex(ValueError, "^--run must name the run log .md file: "):
|
|
f(self.tmp / "run.txt")
|
|
self.assertFalse(self.scratch.exists())
|
|
|
|
|
|
class LedgerTest(_Scratch):
|
|
def test_parallel_appends_give_whole_lines(self):
|
|
"""B3: two threads appending at once leave only whole, parsable lines."""
|
|
with tempfile.TemporaryDirectory() as d:
|
|
run = str(Path(d) / "run.md")
|
|
store.init_state(run)
|
|
|
|
def append(job):
|
|
for i in range(50):
|
|
store.ledger_append(run, {"job": job, "i": i, "pad": "x" * 500})
|
|
threads = [threading.Thread(target=append, args=(j,)) for j in ("a", "b")]
|
|
for t in threads:
|
|
t.start()
|
|
for t in threads:
|
|
t.join()
|
|
raw = (store.state_dir(run) / "ledger.jsonl").read_bytes().splitlines()
|
|
lines = store.ledger_lines(run)
|
|
self.assertEqual(len(raw), 100)
|
|
self.assertEqual(len(lines), 100)
|
|
self.assertEqual(sorted((x["job"], x["i"]) for x in lines),
|
|
sorted((j, i) for j in ("a", "b") for i in range(50)))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|