Files

332 lines
18 KiB
Python

import functools
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.templates import BARE, Titles
from branch_chunks.fixes import blamed, fixing_changes, removed, removed_work, Traced, with_fixes
from branch_chunks.cut import Branch, chunk_log, Commit, cut_branch, landing, NOT_WORK, shipped, Span
from branch_chunks.read import branch_chunks, traced_to, unsupported
from project_config.doubles import configured
from failure import Failure
from git_cli.git_repo import git
from test_env import isolated_env
from plan.todo_table import Item
HEADER = "| ID | Release | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|\n"
WITHOUT_C = "| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|\n"
FIRST = "| 4 | 0.1.0 | | **Do it.** | 2 | 3 | 10 | 6 | 7 | 1 | 9.0 |"
SECOND = "| 5 | 0.1.0 | | **Then this.** | 4 | 6 | 8 | 6 | 7 | 1 | 9.0 |"
FIX = "| 6 | 0.1.0 | defect | **Fix it.** | 1 | 1 | 1 | 6 | 7 | 1 | 9.0 |"
def todo(*rows: str, header: str = HEADER) -> str:
return "# todo\n\n## Items\n\n" + header + "".join(f"{row}\n" for row in rows)
def item(id_: int, exempt: str = "") -> Item:
return Item(id_, (0, 1, 0), exempt, f"**Item {id_}.**", {}, (), ())
def span(label: str) -> Span:
return Span(f"{label}-before", f"{label}-tip", label, "merge", "2026-10-13T10:00:00Z", ())
def commit(hash_: str, committed: int, authored: int, *parents: str, title: str = "") -> Commit:
return Commit(hash_, hash_, committed, authored, parents, title, ())
def cut(*line: Commit) -> list[tuple[str, str, int, str]]:
"""Each chunk chunk_log cuts the first-parent line into: its oldest and newest commit, its commit time and how it landed."""
return [(run[-1].hash, run[0].hash, run[0].committed, landing(run)) for run in chunk_log(list(line))]
class Shipped(unittest.TestCase):
def test_ships_each_item_the_change_removed_with_the_scores_it_had_before(self):
self.assertEqual([found.id for found in shipped(todo(FIRST, SECOND), todo(SECOND))], [4])
self.assertEqual(shipped(todo(FIRST, SECOND), todo(SECOND))[0].scores, {"R": 2, "S": 3, "C": 10, "A": 6, "G": 7})
def test_lists_the_shipped_items_by_id(self):
self.assertEqual([found.id for found in shipped(todo(SECOND, FIRST), todo())], [4, 5])
def test_reads_a_table_from_before_c_joined_it(self):
older = "| 4 | 0.1.0 | | **A \\| B.** | 2 | 3 | 6 | 7 | 1 | 9.0 |"
self.assertEqual(shipped(todo(older, header=WITHOUT_C), todo(header=WITHOUT_C))[0].scores, {"R": 2, "S": 3, "A": 6, "G": 7})
def test_leaves_out_each_item_a_title_opening_drop_item_names(self):
third = "| 7 | 0.1.0 | | **And that.** | 4 | 6 | 8 | 6 | 7 | 1 | 9.0 |"
eighth = "| 8 | 0.1.0 | | **Or this.** | 4 | 6 | 8 | 6 | 7 | 1 | 9.0 |"
before = todo(FIRST, SECOND, third, eighth)
self.assertEqual([found.id for found in shipped(before, todo(), ("Build item 4", "Drop item 5: item 4 covers it"))], [4, 7, 8])
self.assertEqual([found.id for found in shipped(before, todo(), ("Drop items 5, 7 and 8: moot",))], [4])
self.assertEqual([found.id for found in shipped(before, todo(), ("Drop items 5 and 7: moot",))], [4, 8])
def test_ships_an_item_a_title_drops_only_mid_line_or_without_the_colon(self):
titles = ("Keep the steps and drop item 4, with its measurement", "Drop item 5 later", "Drop item 45: another")
self.assertEqual([found.id for found in shipped(todo(FIRST, SECOND), todo(), titles)], [4, 5])
def test_ships_nothing_where_either_plan_has_no_table_it_can_read(self):
self.assertEqual(shipped(todo(FIRST), "# todo\n\n- [ ] Do it.\n"), [])
self.assertEqual(shipped(todo(FIRST, "| x | 0.1.0 | | **Bad.** | 2 | 3 | 5 | 6 | 7 | 1 | 9.0 |"), todo()), [])
class CutBranch(unittest.TestCase):
def test_reads_a_title_that_holds_a_control_character(self):
found = cut_branch(f"\x00{'a' * 40} 100 100 \x1faaaaaaa\x1fTake \x1e and \x1f\n\nb.py\n", BARE)
self.assertNotIsInstance(found, Failure)
self.assertEqual([found.titles for found in found.chunks], [("Take \x1e and \x1f",)])
def test_reads_each_title_as_its_summary_in_the_projects_commit_title_template(self):
log = f"\x00{'a' * 40} 100 100 {'b' * 40}\x1faaaaaaa\x1fPAY-7 - Drop item 6: moot\n\nb.py\n\x00{'b' * 40} 90 90 \x1fbbbbbbb\x1fFrom before the template\n\nb.py\n"
found = cut_branch(log, Titles("PAY-{id} - {summary}", "{summary}"))
self.assertNotIsInstance(found, Failure)
self.assertEqual(sorted(title for chunk in found.chunks for title in chunk.titles), ["Drop item 6: moot", "From before the template"])
def test_fails_on_a_commit_it_cannot_read(self):
self.assertIsInstance(cut_branch(f"\x00{'a' * 40} 100\x1faaaaaaa\n", BARE), Failure)
def test_fails_on_a_log_that_holds_no_commit(self):
self.assertIsInstance(cut_branch("", BARE), Failure)
class ChunkLog(unittest.TestCase):
def test_a_merge_is_a_chunk_of_its_own_and_joins_no_run_below_it(self):
self.assertEqual(cut(commit("m", 100, 100, "a", "b"), commit("c", 100, 100, "a")), [("m", "m", 100, "merge"), ("c", "c", 100, "push")])
self.assertEqual(cut(commit("c", 100, 100, "m"), commit("m", 100, 100, "a", "b")), [("c", "c", 100, "push"), ("m", "m", 100, "merge")])
def test_commits_sharing_a_commit_time_form_one_run_labelled_rebase_when_any_differs(self):
self.assertEqual(cut(commit("c2", 200, 150, "c1"), commit("c1", 200, 100, "r")), [("c1", "c2", 200, "rebase")])
def test_a_run_of_commits_that_all_kept_their_author_time_is_labelled_push(self):
self.assertEqual(cut(commit("c2", 200, 200, "c1"), commit("c1", 200, 200, "r")), [("c1", "c2", 200, "push")])
def test_a_run_is_rebase_when_only_its_older_commit_was_replayed(self):
self.assertEqual(cut(commit("c2", 200, 200, "c1"), commit("c1", 200, 100, "r")), [("c1", "c2", 200, "rebase")])
def test_a_run_is_rebase_when_only_its_newer_commit_was_replayed(self):
self.assertEqual(cut(commit("c2", 200, 100, "c1"), commit("c1", 200, 200, "r")), [("c1", "c2", 200, "rebase")])
def test_a_commit_at_another_time_starts_a_new_chunk(self):
self.assertEqual(cut(commit("c2", 300, 300, "c1"), commit("c1", 200, 200, "r")), [("c2", "c2", 300, "push"), ("c1", "c1", 200, "push")])
class FixingChanges(unittest.TestCase):
def test_takes_the_whole_change_of_each_chunk_that_ships_a_defect_or_a_security_item(self):
branch = Branch({}, [span("d"), span("c"), span("b"), span("a")], {})
shipped = [(item(9, "security"),), (item(8),), (item(6, "defect"), item(7)), ()]
self.assertEqual(fixing_changes(branch, shipped), [(0, "d-before", "d-tip"), (2, "b-before", "b-tip")])
def test_takes_each_round_commit_whose_title_counts_both_a_red_and_a_misread_finding(self):
titles = {
"r1": "Answer stability round 1: 1 🔴, 0 🟡 (1 misread); keep y's caller",
"r2": "Answer stability round 2: 1 🔴, 0 🟡 (0 misread); fix",
"r3": "Answer prose round 1: 0 🔴, 1 🟡 (1 misread); fix",
"r4": "Answer principles round 1: 1 🔴, 0 🟡; fix",
"r5": "Do the work",
}
commits = {name: commit(name, 100, 100, f"{name}-parent", title=title) for name, title in titles.items()}
branch = Branch(commits, [span("b"), span("a")], {"r1": 1, "r2": 0, "r3": 0, "r4": 0, "r5": 0})
self.assertEqual(fixing_changes(branch, [(), ()]), [(1, "r1-parent", "r1")])
class WithFixes(unittest.TestCase):
def test_names_each_later_chunk_once_against_each_chunk_whose_commits_its_fixes_traced_to_newest_first(self):
branch = Branch({}, [span("d"), span("c"), span("b"), span("a")], {"a1": 3, "a2": 3, "b1": 2, "c1": 1, "d1": 0})
found = with_fixes(branch, [()] * 4, [Traced(1, {"a1", "a2"}), Traced(0, {"a1", "b1", "c1"}), Traced(1, {"a2"}), Traced(0, {"a2"})])
self.assertEqual([found.fixed_by for found in found], [(), ("d",), ("d",), ("d", "c")])
def test_counts_no_fix_of_its_own_chunks_commit_a_newer_chunks_or_one_no_chunk_brought(self):
branch = Branch({}, [span("b"), span("a")], {"b1": 0, "a1": 1})
self.assertEqual([found.fixed_by for found in with_fixes(branch, [(), ()], [Traced(1, {"a1", "b1", "elsewhere"})])], [(), ()])
def test_keeps_each_chunk_as_cut_with_the_items_it_shipped(self):
(found,) = with_fixes(Branch({}, [span("a")], {}), [(item(4),)], [])
self.assertEqual((found.before, found.tip, found.label, found.landing, found.merged, found.titles), ("a-before", "a-tip", "a", "merge", "2026-10-13T10:00:00Z", ()))
self.assertEqual([shipped.id for shipped in found.shipped], [4])
class Removed(unittest.TestCase):
def test_reads_the_old_lines_each_hunk_removes_or_rewrites_by_the_path_they_had(self):
diff = "\n".join([
"diff --git a/a.py b/a.py",
"--- a/a.py",
"+++ b/a.py",
"@@ -2 +2 @@ def f():",
"-x",
"+y",
"@@ -5,3 +4,0 @@",
"@@ -9,0 +8,2 @@",
"diff --git a/old.py b/new.py",
"--- a/old.py",
"+++ b/new.py",
"@@ -1,2 +1,2 @@",
"diff --git a/gone.py b/gone.py",
"--- a/gone.py",
"+++ /dev/null",
"@@ -1,4 +0,0 @@",
"diff --git a/born.py b/born.py",
"--- /dev/null",
"+++ b/born.py",
"@@ -0,0 +1,3 @@",
"diff --git \"a/tab\\there.py\" \"b/tab\\there.py\"",
"--- \"a/tab\\there.py\"",
"+++ \"b/tab\\there.py\"",
"@@ -1 +1 @@",
"diff --git a/my file.py b/my file.py",
"--- a/my file.py\t",
"+++ b/my file.py\t",
"@@ -7,2 +7 @@",
])
self.assertEqual(removed(diff), {"a.py": [(2, 1), (5, 3)], "old.py": [(1, 2)], "gone.py": [(1, 4)], "my file.py": [(7, 2)]})
def test_reads_a_changed_line_that_looks_like_a_header_as_text(self):
# The second removed line was `-- a/other` in the file.
diff = "--- a/q.sql\n+++ b/q.sql\n@@ -3,2 +3 @@\n-x\n--- a/other\n+z\n@@ -9 +8,0 @@\n-y\n"
self.assertEqual(removed(diff), {"q.sql": [(3, 2), (9, 1)]})
class RemovedWork(unittest.TestCase):
def test_leaves_out_the_plan_and_the_records(self):
diff = "".join(f"--- a/{path}\n+++ b/{path}\n@@ -2 +2 @@\n-x\n+y\n" for path in ("a.py", *NOT_WORK))
self.assertEqual(removed_work(diff), {"a.py": [(2, 1)]})
class Blamed(unittest.TestCase):
def test_reads_the_commit_of_each_line_from_porcelain_output(self):
first, second = "a" * 40, "b" * 40
porcelain = f"{first} 2 2 1\nauthor t\nfilename a.py\n\tx\n{second} 5 5 2\nauthor t\n\ty\n{second} 6 6\n\t{first} 1 1 1\n"
self.assertEqual(blamed(porcelain), {first, second})
class Unsupported(unittest.TestCase):
def test_says_which_files_the_config_puts_where_no_commits_are_read(self):
checkout = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())).resolve()
run = functools.partial(git, env=isolated_env())
self.assertIsNone(unsupported(str(checkout), checkout, run), "no git checkout, so no config names it")
folder = configured(self, checkout)
home = folder.parents[1]
self.assertIsNone(unsupported(str(checkout), home, run))
config = folder / "config.toml"
config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"').replace('"checkout:docs/reading-cost.md"', '"checkout:records/reading-cost.md"'))
self.assertEqual(unsupported(str(checkout), home, run), f"the project's config puts {folder}/todo.md, {checkout}/records/reading-cost.md elsewhere than todo.md, docs/comprehension-history.md, docs/reading-cost.md in the checkout, the only paths whose commits are read so far")
class TracedTo(unittest.TestCase):
DIFF = "--- a/a.py\n+++ b/a.py\n@@ -2 +2 @@\n-x\n+y\n--- a/todo.md\n+++ b/todo.md\n@@ -5 +4,0 @@\n-| 6 |\n"
def test_names_the_commits_blame_names_for_the_work_a_change_removes(self):
blames = {"a.py": f"{'a' * 40} 2 2 1\n\tx\n", "todo.md": f"{'b' * 40} 5 5 1\n\t| 6 |\n"}
self.assertEqual(traced_to("before", "tip", lambda *args, **options: self.DIFF if args[0] == "diff" else blames[args[-1]]), {"a" * 40})
def test_fails_where_git_cannot_blame(self):
self.assertEqual(traced_to("before", "tip", lambda *args, **options: self.DIFF if args[0] == "diff" else Failure("no blame")), Failure("no blame"))
class BranchChunks(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
self.checkout = pathlib.Path(os.path.realpath(temp.name))
self.env = {**isolated_env(), "GIT_CONFIG_GLOBAL": os.devnull, "HOME": str(self.checkout)}
self.git("init", "-q", "-b", "main")
self.commit("Start", {"todo.md": todo(FIRST, SECOND, FIX), "a.py": "1\n"})
def git(self, *args):
return subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *args], cwd=self.checkout, check=True, env=self.env, capture_output=True, text=True).stdout
def commit(self, title, files):
for name, text in files.items():
(self.checkout / name).write_bytes(text if isinstance(text, bytes) else text.encode())
self.git("add", name)
self.git("commit", "-q", "-m", title)
def merge(self, branch, title, files, at):
"""Merges a branch holding one commit, at the given time, and returns the merge commit's short hash."""
self.git("switch", "-q", "-c", branch)
self.commit(title, files)
self.git("switch", "-q", "main")
self.env.update({"GIT_COMMITTER_DATE": at, "GIT_AUTHOR_DATE": at})
self.git("merge", "-q", "--no-ff", "-m", f"Merge {branch}", branch)
for name in ("GIT_COMMITTER_DATE", "GIT_AUTHOR_DATE"):
self.env.pop(name)
return self.git("rev-parse", "--short", "HEAD").strip()
def read(self, run=None, titles=BARE):
"""branch_chunks over main, run by git_repo's git, or else by run, in the test's own environment."""
return branch_chunks("main", functools.partial(run or git, cwd=str(self.checkout), env=self.env), titles)
def fixing_history(self):
"""Merges seven chunks onto main, then checks out a branch off it. Returns each of main's chunks by label, with the chunks that fixed it.
The defect chunk six rewrites the lines around one that an unfixed chunk wrote, renames a file and rewrites a line of it, rewrites a line that is not UTF-8, and moves a gitlink. Chunk seven fixes nothing."""
start = self.git("rev-parse", "--short", "HEAD").strip()
one = self.merge("one", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-10T10:00:00Z")
two = self.merge("two", "Do five", {"b.py": "x\nn\ny\n", "todo.md": todo(FIX)}, "2026-10-11T10:00:00Z")
self.git("update-index", "--add", "--cacheinfo", f"160000,{'1' * 40},sub")
three = self.merge("three", "Add old.py and sub", {"old.py": "k1\nk2\nk3\nk4\nk5\n"}, "2026-10-12T10:00:00Z")
four = self.merge("four", "Rewrite k5", {"old.py": "k1\nk2\nk3\nk4\nr\n"}, "2026-10-13T10:00:00Z")
five = self.merge("five", "Add d.txt", {"d.txt": "café\n".encode("latin-1")}, "2026-10-14T10:00:00Z")
self.git("rm", "-q", "old.py")
self.git("update-index", "--cacheinfo", f"160000,{'2' * 40},sub")
six = self.merge("six", "Fix six", {"b.py": "X\nn\nY\n", "new.py": "k1\nk2\nk3\nk4\nR\n", "d.txt": "cafè\n".encode("latin-1"), "todo.md": todo()}, "2026-10-15T10:00:00Z")
seven = self.merge("seven", "Add c.py", {"c.py": "1\n"}, "2026-10-16T10:00:00Z")
self.git("switch", "-q", "-c", "side")
self.commit("Side", {"a.py": "2\n"})
return {start: (), one: (six,), two: (), three: (), four: (six,), five: (six,), six: (), seven: ()}
def test_traces_a_fix_across_files_renames_text_that_is_not_utf_8_and_gitlinks(self):
expected = self.fixing_history()
found = self.read()
self.assertNotIsInstance(found, Failure)
self.assertEqual({chunk.label: chunk.fixed_by for chunk in found}, expected)
def test_keeps_the_checkouts_diff_and_blame_config_out_of_the_trace(self):
expected = self.fixing_history()
for setting in ("diff.noprefix=true", "color.ui=always", "diff.external=false", "diff.interHunkContext=5", "diff.renames=false", "blame.ignoreRevsFile=missing", "diff.doubled.textconv=sed p"):
self.git("config", *setting.split("=", 1))
(self.checkout / ".git" / "info" / "attributes").write_text("*.py diff=doubled\n")
found = self.read()
self.assertNotIsInstance(found, Failure)
self.assertEqual({chunk.label: chunk.fixed_by for chunk in found}, expected)
def test_fails_where_git_cannot_blame_a_fixing_change(self):
self.merge("four", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-13T10:00:00Z")
self.merge("six", "Fix six", {"b.py": "x\nY\n", "todo.md": todo(SECOND)}, "2026-10-14T10:00:00Z")
self.assertEqual(self.read(lambda *args, **options: Failure("no blame") if args[0] == "blame" else git(*args, **options)), Failure("no blame"))
def test_reads_each_chunk_with_the_items_it_shipped_and_the_later_chunks_that_fixed_it(self):
shipped = self.merge("four", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-13T10:00:00Z")
fixed = self.merge("six", "Fix six", {"b.py": "x\nY\n", "todo.md": todo(SECOND)}, "2026-10-14T10:00:00Z")
found = self.read()
self.assertNotIsInstance(found, Failure)
self.assertEqual([(found.label, found.landing, found.merged, [item.id for item in found.shipped], found.fixed_by) for found in found[:2]], [(fixed, "merge", "2026-10-14T10:00:00Z", [6], ()), (shipped, "merge", "2026-10-13T10:00:00Z", [4], (fixed,))])
self.assertEqual(sorted(found[1].titles), ["Do four", "Merge four"])
self.assertEqual((found[1].tip, found[2].tip), (self.git("rev-parse", "HEAD^").strip(), self.git("rev-parse", "HEAD^^").strip()))
self.assertEqual((found[1].before, found[2].before), (found[2].tip, None))
def test_a_chunk_that_drops_an_item_by_a_commit_title_does_not_ship_it(self):
self.merge("four", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-13T10:00:00Z")
self.merge("six", "Drop item 6: item 4 fixed it", {"b.py": "x\nY\n", "todo.md": todo(SECOND)}, "2026-10-14T10:00:00Z")
found = self.read()
self.assertNotIsInstance(found, Failure)
self.assertEqual([([item.id for item in found.shipped], found.fixed_by) for found in found[:2]], [([], ()), ([4], ())])
def test_reads_a_drop_title_through_the_projects_commit_title(self):
self.merge("four", "Do four", {"b.py": "x\ny\n", "todo.md": todo(SECOND, FIX)}, "2026-10-13T10:00:00Z")
self.merge("six", "PAY-6 Drop item 6: item 4 fixed it", {"b.py": "x\nY\n", "todo.md": todo(SECOND)}, "2026-10-14T10:00:00Z")
found = self.read(titles=Titles("{summary}", "PAY-{id} {summary}"))
self.assertNotIsInstance(found, Failure)
self.assertEqual([[item.id for item in found.shipped] for found in found[:2]], [[], [4]])
def test_fails_where_git_cannot_read_the_branch(self):
self.assertEqual(branch_chunks("main", lambda *args: Failure("no log"), BARE), Failure("no log"))
if __name__ == "__main__":
unittest.main()