Files
claude-config/bin/chunk_preflight/test_chunk_preflight.py
T

163 lines
6.9 KiB
Python

import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from chunk_preflight import facts
from test_env import isolated_env
from todo_table import items
BIN = pathlib.Path(__file__).resolve().parents[1]
README = "# Project\n\n## Goals\n\n1. **Achieve:** Work.\n\n## Audience\n\nMe.\n"
# Item 3 is the next release's first item that does not wait; 7 waits only on 3, 5 is a question, 8 waits on 5, 9 is in a later release.
TODO = """# todo
## Scoring
`Score = 100 * V / (2**S + 8)`
`Bar = 0.1`
`Next ID = 10`
## Items
| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 5 | 0.2.0 | | question | **Ask first.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 90.0 |
| 3 | 0.2.0 | | | **Do the next thing.** | 4 | 2 | 3 | 7 | 3 | 1 by 9 | 56.2 |
| 7 | 0.2.0 | 3 | | **Do the thing after.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 10.0 |
| 8 | 0.2.0 | 5 | | **Do the answered thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 1 | 10.0 |
| 9 | 0.3.0 | | | **Do a later thing.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 | 90.0 |
## Details
### 3. Do the next thing.
Change `src/app.py`'s `run` and the files under `src`, not `gone.py`.
"""
CHANGELOG = "# Changelog\n\n## Unreleased\n\n- One.\n - A nested line.\n- Two.\n\n## 0.1.0\n\n- Old.\n"
# Prints its arguments, then two open pull requests, as `tea pulls list --output tsv` does.
FAKE_TEA = "#!/bin/sh\necho \"tea $*\"\nprintf 'index\\ttitle\\thead\\tstate\\n12\\tWIP: the next thing\\titem-3\\topen\\n'\n"
class Repo:
"""An origin with a tagged release, a clone of it at <temp>/proj, and a fake tea on PATH."""
def __init__(self, test, todo=TODO):
temp = tempfile.TemporaryDirectory()
test.addCleanup(temp.cleanup)
self.temp = pathlib.Path(os.path.realpath(temp.name))
self.origin = self.temp / "origin.git"
self.root = self.temp / "proj"
fake = self.temp / "fake-bin"
fake.mkdir()
(fake / "tea").write_text(FAKE_TEA)
(fake / "tea").chmod(0o755)
(fake / "git").symlink_to(shutil.which("git"))
self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "GIT_CONFIG_NOSYSTEM": "1", "HOME": str(self.temp / "home"), "PATH": str(fake), "LOOP_CLAUDE_TRANSCRIPTS": str(self.temp / "archive")}
self.git(self.temp, "init", "--quiet", "--bare", "--initial-branch=main", str(self.origin))
self.git(self.temp, "clone", "--quiet", str(self.origin), str(self.root))
self.write("README.md", README)
self.write("src/app.py", "def run():\n\treturn 1\n\n")
self.commit("start")
self.git(self.root, "tag", "v0.1.0")
self.write("CHANGELOG.md", CHANGELOG)
if todo:
self.write("todo.md", todo)
self.commit("plan")
self.git(self.root, "push", "--quiet", "origin", "main", "v0.1.0")
def git(self, cwd, *args):
return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@example.com", *args], capture_output=True, check=True, cwd=cwd, env=self.env, text=True).stdout
def write(self, path, text):
(self.root / path).parent.mkdir(parents=True, exist_ok=True)
(self.root / path).write_text(text)
def commit(self, title):
self.git(self.root, "add", "-A")
self.git(self.root, "commit", "--quiet", "-m", title)
def preflight(self):
return subprocess.run([sys.executable, str(BIN / "chunk-preflight")], capture_output=True, cwd=self.root, env=self.env, text=True, timeout=120)
class ChunkPreflightTool(unittest.TestCase):
def test_makes_the_items_worktree_off_the_fetched_default_branch_and_states_the_chunks_facts(self):
repo = Repo(self)
done = repo.preflight()
self.assertEqual(done.returncode, 0, done.stderr)
worktree = repo.temp / "proj-item-3"
self.assertEqual(repo.git(worktree, "rev-parse", "--abbrev-ref", "HEAD").strip(), "item-3")
self.assertEqual(repo.git(worktree, "rev-parse", "HEAD"), repo.git(repo.root, "rev-parse", "origin/main"))
for fact in (
"Last version cut: v0.1.0. Unreleased: 2 entries under CHANGELOG.md → ## Unreleased.",
"Next release: 0.2.0. Its first item that does not wait: item 3, L 4, E + I 5.",
f"Worktree: {worktree}, on the new branch item-3, made off origin/main.",
"- item 7, L 1, E + I 2, S 1: **Do the thing after.**",
"- `src/app.py`: 3 lines",
"- `src`: 1 file",
"Change `src/app.py`'s `run` and the files under `src`, not `gone.py`.",
"tea pulls list --fields index,title,head,state --output tsv",
"12\tWIP: the next thing\titem-3\topen",
"`rank_todo.py --check todo.md` exited 0",
"`larv-review needs-scoring-run 3` exited 0",
"`reading-cost` exited 0",
"`larv-review places` exited 0",
):
self.assertIn(fact, done.stdout)
self.assertNotIn("item 5,", done.stdout)
self.assertNotIn("item 8,", done.stdout)
self.assertNotIn("item 9,", done.stdout)
self.assertNotIn("`gone.py`:", done.stdout)
self.assertIn(f"to {worktree}/docs/reading-cost.md", done.stdout)
def test_continues_the_items_worktree_and_else_its_pushed_branch(self):
repo = Repo(self)
repo.preflight()
worktree = repo.temp / "proj-item-3"
self.assertIn(f"Worktree: {worktree}, on the branch item-3, which it already held.", repo.preflight().stdout)
(worktree / "work.txt").write_text("work\n")
repo.git(worktree, "add", "work.txt")
repo.git(worktree, "commit", "--quiet", "-m", "work")
repo.git(worktree, "push", "--quiet", "origin", "item-3")
repo.git(repo.root, "worktree", "remove", "--force", str(worktree))
repo.git(repo.root, "branch", "--quiet", "-D", "item-3")
self.assertIn(f"Worktree: {worktree}, on the new branch item-3, made off origin/item-3.", repo.preflight().stdout)
self.assertTrue((worktree / "work.txt").exists())
def test_without_a_plan_states_the_release_facts_and_makes_no_worktree(self):
repo = Repo(self, todo="")
done = repo.preflight()
self.assertEqual(done.returncode, 0, done.stderr)
self.assertIn("Last version cut: v0.1.0.", done.stdout)
self.assertIn("No plan item: origin/main holds no todo.md items table, so no worktree was made.", done.stdout)
self.assertFalse((repo.temp / "proj-item-3").exists())
def test_exits_1_outside_a_checkout(self):
with tempfile.TemporaryDirectory() as temp:
done = subprocess.run([sys.executable, str(BIN / "chunk-preflight")], capture_output=True, cwd=temp, env={**isolated_env(), "GIT_CONFIG_NOSYSTEM": "1"}, text=True)
self.assertEqual(done.returncode, 1)
self.assertIn("git rev-parse --show-toplevel", done.stderr)
class Facts(unittest.TestCase):
def test_counts_the_top_level_entries_under_unreleased(self):
self.assertEqual(facts.unreleased_entries(CHANGELOG), 2)
self.assertEqual(facts.unreleased_entries("# Changelog\n\n## Unreleased\n\n## 0.1.0\n\n- Old.\n"), 0)
self.assertIsNone(facts.unreleased_entries("# Changelog\n\n## 0.1.0\n\n- Old.\n"))
def test_bundles_an_item_blocked_only_by_items_the_chunk_may_take(self):
found = items(TODO.replace("| 9 | 0.3.0 | |", "| 9 | 0.2.0 | 7 |"))
self.assertEqual([item.id for item in facts.bundle_candidates(found, found[1])], [7, 9])
if __name__ == "__main__":
unittest.main()