diff --git a/bin/rank_todo.py b/bin/rank_todo.py index 8622c29b..7df500b9 100755 --- a/bin/rank_todo.py +++ b/bin/rank_todo.py @@ -220,7 +220,7 @@ def under_the_bar(item: Item, value: float, bar: float, items: list[Item]) -> st def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure: - if "## Items" not in lines and items_table(lines) is None: + if "## Items" not in lines: return Failure("todo.md is not in the scored-table format; rank_todo.py --help shows a valid one") goals = readme_goals(readme) if isinstance(goals, Failure): @@ -253,8 +253,8 @@ def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], Table] | # Ranking writes COLUMNS, so it would drop a column it does not know. if unknown := [column for column in table.columns if column not in COLUMNS]: return Failure(f"todo.md's items table has the column {', '.join(unknown)}, which this rank_todo.py does not know; move this machine to the release that added it") - if beside_table(lines, table.header, table.start + len(table.rows)): - return Failure(f"## Items holds text beside its table; an order goes in the scores or the {BLOCKED_BY} column, and a note in the item's detail") + if misplaced := outside_items(lines, table): + return misplaced items = [] for row in table.rows: item = parse_item(row, scoring.weights, table.columns) @@ -272,12 +272,16 @@ def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], Table] | return items, table -def beside_table(lines: list[str], header: int, end: int) -> bool: - """Whether ## Items holds anything but its table, which starts at the header line and ends before end: the table's order is todo.md's only order.""" - heading = max((index for index in range(header) if lines[index].startswith("## ")), default=-1) - after = lines[end:] +def outside_items(lines: list[str], table: Table) -> Failure | None: + """Why the table does not stand alone under ## Items: the table's order is todo.md's only order.""" + heading = max((index for index in range(table.header) if lines[index].startswith("## ")), default=None) + if heading is None or lines[heading] != "## Items": + return Failure(f"the items table sits under {lines[heading] if heading is not None else 'no ## heading'}, not ## Items; move it under ## Items") + after = lines[table.start + len(table.rows):] section = after[:next((index for index, line in enumerate(after) if line.startswith("## ")), len(after))] - return any(line.strip() for line in lines[heading + 1:header] + section) + if any(line.strip() for line in lines[heading + 1:table.header] + section): + return Failure(f"## Items holds text beside its table; an order goes in the scores or the {BLOCKED_BY} column, and a note in the item's detail") + return None def blockers_first(scored: list[tuple[Item, float]]) -> list[tuple[Item, float]] | Failure: diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index c9cf12bb..987607c0 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -83,6 +83,7 @@ class Rank(unittest.TestCase): def test_names_each_item_under_the_bar_that_is_not_exempt(self): ranked = self.ranked(TODO.replace("`Bar = 9`", "`Bar = 11`")) self.assertEqual(ranked.under_bar, ["item 2 scores 10.3, under the bar 11; delete it, or mark it with a reason the bar does not apply"]) + self.assertEqual(self.ranked(TODO.replace("`Bar = 9`", "`Bar = 10.3`")).under_bar, []) blocking = self.ranked(TODO.replace("`Bar = 9`", "`Bar = 11`").replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 |").replace("| 5 | 0.1.0 | |", "| 5 | 0.1.0 | 3 |")) self.assertEqual(blocking.under_bar, ["item 2 scores 10.3, under the bar 11; delete it and the items it blocks, directly or through another, 3, 5, or mark it with a reason the bar does not apply"]) self.assertEqual(self.ranked(ranked.text.replace("| 2 | 0.1.0 | | |", "| 2 | 0.1.0 | | principle |")).under_bar, []) @@ -143,6 +144,10 @@ class Blockers(unittest.TestCase): """, ranked.text) self.assertEqual(self.ranked(ranked.text), ranked) + def test_ranks_an_items_blockers_in_score_order(self): + rows = [line.split(" | ")[0] for line in self.ranked(TODO.replace("| 4 | 0.1.0 | |", "| 4 | 0.1.0 | 2, 3 |")).text.splitlines() if re.match(r"\| \d \| 0\.\d\.0 \|", line)] + self.assertEqual(rows, ["| 3", "| 2", "| 4", "| 5", "| 1"]) + def test_ranks_a_blocker_above_its_blockers_blocker_and_takes_one_in_an_earlier_release_as_it_is(self): chained = TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5 |").replace("| 5 | 0.1.0 | |", "| 5 | 0.1.0 | 2 |").replace("| 1 | 0.2.0 | |", "| 1 | 0.2.0 | 3 |") rows = [line.split(" | ")[0] for line in self.ranked(chained).text.splitlines() if re.match(r"\| \d \| 0\.\d\.0 \|", line)] @@ -157,6 +162,7 @@ class Blockers(unittest.TestCase): "| 3 | 0.1.0 | 1 |": "item 3 is blocked by item 1, which release 0.2.0 ships after release 0.1.0; move item 1 to release 0.1.0 or earlier, or item 3 to release 0.2.0 or later", "| 3 | 0.1.0 | 3 |": "item 3 is blocked by itself; drop 3 from its Blocked by", "| 3 | 0.1.0 | 9 |": "item 3 is blocked by item 9, which is not under Next ID 6; name an item the table holds or held", + "| 3 | 0.1.0 | 6 |": "item 3 is blocked by item 6, which is not under Next ID 6; name an item the table holds or held", } for cell, reason in cases.items(): with self.subTest(cell=cell): @@ -179,10 +185,26 @@ class Blockers(unittest.TestCase): self.assertEqual(self.ranked(without_c).reason, "todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help") def test_refuses_text_under_items_beside_the_table(self): - for text in ("Release 0.1.0 takes item 5 first.\n\n", ""): - ordered = TODO.replace("## Items\n\n", f"## Items\n\n{text}") if text else TODO.replace("| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 |\n", "| 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 |\n\nTake item 5 first.\n") - with self.subTest(text=text): - self.assertEqual(self.ranked(ordered), Failure("## Items holds text beside its table; an order goes in the scores or the Blocked by column, and a note in the item's detail")) + last = TODO.split("\n## Details")[0] + "\n" + self.assertIsInstance(self.ranked(last), rank_todo.Ranked) + beside = Failure("## Items holds text beside its table; an order goes in the scores or the Blocked by column, and a note in the item's detail") + for ordered in (TODO.replace("## Items\n\n", "## Items\n\nRelease 0.1.0 takes item 5 first.\n\n"), TODO.replace("## Items\n\n", "## Items\nTake item 5 first.\n\n"), TODO.replace("| 2.1 | 0 |\n\n## Details", "| 2.1 | 0 |\n\nTake item 5 first.\n\n## Details"), last + "\nTake item 5 first.\n"): + with self.subTest(ordered=ordered[-60:]): + self.assertEqual(self.ranked(ordered), beside) + + def test_refuses_an_items_table_outside_items_a_second_one_and_a_repeated_column(self): + self.assertEqual(self.ranked(TODO.replace("## Items\n", "## Plan\n")).reason, "todo.md is not in the scored-table format; rank_todo.py --help shows a valid one") + outside = TODO.replace("## Items\n\n", "## Items\n\n## Rows\n\n") + self.assertEqual(self.ranked(outside).reason, "the items table sits under ## Rows, not ## Items; move it under ## Items") + header = "| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|---|---|\n" + self.assertEqual(todo_table.items(TODO + "\n" + header.replace("| Score |", "| Later |")), Failure("todo.md holds 2 tables whose header opens | ID | Release |; keep one items table")) + repeated = TODO.replace("| Goals | Score |\n|---|", "| Goals | Score | Exempt |\n|---|---|").replace(" | 0 |\n", " | 0 | x |\n") + self.assertEqual(todo_table.items(repeated), Failure("todo.md's items table names the column Exempt twice; keep one")) + + def test_reads_an_empty_table_and_fails_on_a_header_with_no_divider(self): + header = "| ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score |" + self.assertEqual(todo_table.items(f"## Items\n\n{header}\n|---|---|---|---|---|---|---|---|---|---|---|---|\n\n## Details\n"), []) + self.assertEqual(todo_table.items(f"## Items\n\n{header}"), Failure(f"the table under {header} has no |---| divider line")) class Main(unittest.TestCase): diff --git a/bin/todo_table.py b/bin/todo_table.py index 173955de..951ec405 100644 --- a/bin/todo_table.py +++ b/bin/todo_table.py @@ -72,6 +72,8 @@ def items_table(lines: list[str]) -> Table | None | Failure: table = table_rows(lines, headers[0]) if isinstance(table, Failure): return table + if repeated := sorted({column for column in table.columns if table.columns.count(column) > 1}): + return Failure(f"todo.md's items table names the column {', '.join(repeated)} twice; keep one") if missing := [column for column in NEEDED if column not in table.columns]: return Failure(f"todo.md's items table has no {', '.join(missing)} column; rank_todo.py --help shows a valid one") return table