Files
claude-config/bin/score_outcomes/test_later_fixes.py
T

193 lines
9.3 KiB
Python

import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from score_outcomes.later_fixes import blamed, Landed, later_fixes, removed
from failure import Failure
from test_env import isolated_env
HEADER = "| ID | Release | Blocked by | Exempt | Item | L | E | I | R | S | Moves | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n"
ROWS = {
4: "| 4 | 0.1.0 | | | **Do four.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
5: "| 5 | 0.1.0 | | | **Do five.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
6: "| 6 | 0.1.0 | | defect | **Fix six.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
7: "| 7 | 0.1.0 | | security | **Close seven.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
8: "| 8 | 0.1.0 | | defect | **Fix eight.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
9: "| 9 | 0.1.0 | | defect | **Fix nine.** | 1 | 1 | 1 | 1 | 2 | 1.1 by 3 | 1.0 |",
}
def todo(*ids: int) -> str:
return "# todo\n\n## Items\n\n" + HEADER + "".join(f"{ROWS[id_]}\n" for id_ in ids)
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_hunk_line_inside_the_changed_text_as_text(self):
diff = "--- a/a.md\n+++ b/a.md\n@@ -3 +3 @@\n---- a/quoted\n+text\n"
self.assertEqual(removed(diff), {"a.md": [(3, 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 LaterFixes(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(4, 5, 6, 7, 8), "a.py": "1\n2\n3\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, at=None):
for name, text in files.items():
(self.checkout / name).write_text(text)
self.git("add", name)
dates = {"GIT_COMMITTER_DATE": at, "GIT_AUTHOR_DATE": at} if at else {}
self.env.update(dates)
self.git("commit", "-q", "-m", title)
for name in dates:
self.env.pop(name)
return self.git("rev-parse", "--short", "HEAD").strip()
def chunk(self, branch, *commits):
"""Merges a branch holding each (title, files) commit, and returns the merge commit's short hash."""
self.git("switch", "-q", "-c", branch)
for title, files in commits:
self.commit(title, files)
self.git("switch", "-q", "main")
self.git("merge", "-q", "--no-ff", "-m", f"Merge {branch}", branch)
return self.git("rev-parse", "--short", "HEAD").strip()
def fixes(self):
found = later_fixes("main", cwd=str(self.checkout))
self.assertNotIsInstance(found, Failure)
return {landed.label: landed.fixed_by for landed in found}
def test_counts_a_later_chunk_that_ships_a_defect_item_and_rewrites_a_line_an_earlier_chunk_added(self):
shipped = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
fixed = self.chunk("six", ("Fix six", {"b.py": "x\nY\n", "todo.md": todo(5, 7, 8)}))
found = self.fixes()
self.assertEqual(found[shipped], (fixed,))
self.assertEqual(found[fixed], ())
def test_counts_a_chunk_that_ships_a_security_item_as_a_fix(self):
shipped = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
fixed = self.chunk("seven", ("Close seven", {"b.py": "x\n", "todo.md": todo(5, 6, 8)}))
self.assertEqual(self.fixes()[shipped], (fixed,))
def test_counts_no_chunk_whose_shipped_items_are_neither_defect_nor_security(self):
shipped = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
self.chunk("five", ("Do five", {"b.py": "x\nY\n", "todo.md": todo(6, 7, 8)}))
self.assertEqual(self.fixes()[shipped], ())
def test_counts_no_chunk_that_only_edits_a_defect_items_row(self):
shipped = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
self.chunk("edit", ("Re-score six", {"b.py": "x\nY\n", "todo.md": todo(5, 6, 7, 8).replace("**Fix six.**", "**Fix six now.**")}))
self.assertEqual(self.fixes()[shipped], ())
def test_leaves_the_plans_row_out_so_a_fix_does_not_trace_to_the_chunk_that_filed_it(self):
filed = self.chunk("file", ("File item 9", {"c.py": "c\n", "todo.md": todo(4, 5, 6, 7, 8, 9)}))
self.chunk("nine", ("Fix nine", {"d.py": "d\n", "todo.md": todo(4, 5, 6, 7, 8)}))
self.assertEqual(self.fixes()[filed], ())
def test_counts_a_round_commit_that_answers_a_misread_red_finding_and_rewrites_an_earlier_chunks_line(self):
shipped = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
fixed = self.chunk("five", ("Do five", {"c.py": "c\n", "todo.md": todo(6, 7, 8)}), ("Answer stability round 1: 1 🔴, 0 🟡 (1 misread); keep y's caller", {"b.py": "x\nY\n"}))
self.assertEqual(self.fixes()[shipped], (fixed,))
def test_counts_no_round_commit_without_both_a_red_and_a_misread_finding(self):
shipped = self.chunk("four", ("Do four", {"b.py": "w\nx\ny\n", "todo.md": todo(5, 6, 7, 8)}))
self.chunk("five", ("Do five", {"c.py": "c\n", "todo.md": todo(6, 7, 8)}), ("Answer stability round 1: 1 🔴, 0 🟡 (0 misread); fix", {"b.py": "W\nx\ny\n"}), ("Answer prose round 1: 0 🔴, 1 🟡 (1 misread); fix", {"b.py": "W\nX\ny\n"}), ("Answer principles round 1: 1 🔴, 0 🟡; fix", {"b.py": "W\nX\nY\n"}))
self.assertEqual(self.fixes()[shipped], ())
def test_counts_no_fix_of_lines_its_own_chunk_wrote(self):
self.chunk("six", ("Fix six", {"b.py": "x\ny\n", "todo.md": todo(4, 5, 7, 8)}), ("Answer stability round 1: 1 🔴, 0 🟡 (1 misread); fix", {"b.py": "x\nY\n"}))
self.assertEqual(set(self.fixes().values()), {()})
def test_counts_a_fixing_chunk_once_against_each_chunk_it_fixes_newest_first(self):
first = self.chunk("four", ("Do four", {"b.py": "x\ny\n", "d.py": "d\n", "todo.md": todo(5, 6, 7, 8)}))
second = self.chunk("five", ("Do five", {"c.py": "z\n", "todo.md": todo(6, 7, 8)}))
fixed = self.chunk("six", ("Fix six", {"b.py": "X\ny\n", "todo.md": todo(7, 8)}), ("Answer stability round 1: 1 🔴, 0 🟡 (1 misread); fix", {"b.py": "X\nY\n", "c.py": "Z\n"}))
again = self.chunk("eight", ("Fix eight", {"b.py": "XX\nY\n", "d.py": "D\n", "todo.md": todo(7)}))
found = self.fixes()
self.assertEqual(found[first], (again, fixed))
self.assertEqual(found[second], (fixed,))
self.assertEqual(found[fixed], (again,))
def test_traces_a_line_to_the_rebased_run_that_added_it_and_labels_runs_as_score_outcomes_does(self):
first = self.commit("Do four", {"b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}, at="2026-10-13T09:00:00Z")
last = self.commit("Answer stability round 1: 0 🔴, 0 🟡 (0 misread)", {"c.py": "c\n"}, at="2026-10-13T09:00:00Z")
fixed = self.commit("Fix six", {"b.py": "x\nY\n", "todo.md": todo(5, 7, 8)}, at="2026-10-13T10:00:00Z")
self.assertEqual(self.fixes()[f"{first}^..{last}"], (fixed,))
def test_holds_each_chunks_commit_titles_and_landing_time(self):
self.git("switch", "-q", "-c", "four")
self.commit("Do four", {"b.py": "x\n", "todo.md": todo(5, 6, 7, 8)})
self.git("switch", "-q", "main")
self.env.update({"GIT_COMMITTER_DATE": "2026-10-14T10:00:00Z", "GIT_AUTHOR_DATE": "2026-10-14T10:00:00Z"})
self.git("merge", "-q", "--no-ff", "-m", "Merge four", "four")
merged = self.git("rev-parse", "--short", "HEAD").strip()
newest = later_fixes("main", cwd=str(self.checkout))[0]
self.assertEqual(newest, Landed(merged, "2026-10-14T10:00:00Z", frozenset({"Do four", "Merge four"}), ()))
def test_reads_paths_from_the_checkouts_top_when_run_in_a_folder_of_it(self):
(self.checkout / "sub").mkdir()
shipped = self.chunk("four", ("Do four", {"sub/b.py": "x\ny\n", "todo.md": todo(5, 6, 7, 8)}))
fixed = self.chunk("six", ("Fix six", {"sub/b.py": "x\nY\n", "todo.md": todo(5, 7, 8)}))
found = later_fixes("main", cwd=str(self.checkout / "sub"))
self.assertNotIsInstance(found, Failure)
self.assertEqual({landed.label: landed.fixed_by for landed in found}[shipped], (fixed,))
def test_fails_where_git_cannot_read_the_branch(self):
self.assertIsInstance(later_fixes("missing", cwd=str(self.checkout)), Failure)
if __name__ == "__main__":
unittest.main()