import contextlib import io import pathlib import re import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from failure import Failure import rank_todo from repo_checks.test_python_floor import ROOT import todo_table README = "# P\n\n## Goals\n\n1. **Achieve:** Fast.\n2. **Maintain:** Correct.\n 1. **Achieve:** Tested.\n\n## Audience\n\nMe.\n" DECISIONS = "## A choice\n\nStands until `todo.md` item 5 ends it.\n" TODO = """# todo ## Scoring `Score = 100 * V / 2**S` `Bar = 2` `Next ID = 6` ## Items | ID | Release | Blocked by | Exempt | Item | R | S | C | Moves | Score | |---|---|---|---|---|---|---|---|---|---| | 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 1 by 9 | 0 | | 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1 | 0 | | 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 2.1 by 8, 1 by 6 | 0 | | 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 2.1 by 1 | 0 | | 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 2.1 by 1 | 0 | ## Details ### 1. stale heading Later detail. ### 3. Make it fast. Fast detail. ## Notes Kept as written. """ OLD_SHAPE = """# todo ## Scoring `Score = -R - S/4 + 2*A + 2*G*W` `Bar = 9` `Next ID = 6` | Goal | W | |---|---| | 1 | 1.00 | | 2 | 0.67 | | 2.1 | 0.33 | ## Items | ID | Release | Blocked by | Exempt | Item | R | S | C | A | G | Goals | Score | |---|---|---|---|---|---|---|---|---|---|---|---| | 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 0 | | 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 5 | 5 | 2.1 | 0 | | 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 6 | 8 | 2.1, 1 | 0 | | 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 1 | 1 | 2.1 | 0 | | 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 4 | 5 | 2.1 | 0 | ## Details ### 1. stale heading Later detail. ### 3. Make it fast. Fast detail. ## Notes Kept as written. """ class Rank(unittest.TestCase): def ranked(self, todo=TODO, readme=README, decisions=DECISIONS): return rank_todo.rank(todo, readme, decisions) def test_scores_and_sorts_by_release_then_security_then_score_and_orders_the_details_to_match(self): ranked = self.ranked() self.assertIn("""| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 2.1 by 1 | 8.3 | | 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 2.1 by 8, 1 by 6 | 2.6 | | 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1 | 2.1 | | 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 2.1 by 1 | 1.0 | | 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 1 by 9 | 300.0 | """, ranked.text) self.assertTrue(ranked.text.endswith("## Details\n\n### 3. Make it fast, `a | b`.\n\nFast detail.\n\n### 1. Ship the later thing.\n\nLater detail.\n\n## Notes\n\nKept as written.\n")) self.assertEqual(ranked.under_bar, []) self.assertEqual(ranked.notes, []) self.assertEqual(self.ranked(ranked.text), ranked) def test_weighs_each_achieve_goal_by_its_sum_reciprocal_rank(self): readme = "## Goals\n\n1. **Achieve:** A.\n 1. **Maintain:** B.\n 2. **Achieve:** C.\n2. **Achieve:** D.\n" self.assertEqual({goal: round(weight, 4) for goal, weight in rank_todo.achieve_weights(rank_todo.readme_goals(readme)).items()}, {"1": 0.5455, "1.2": 0.2727, "2": 0.1818}) todo = TODO.replace("2.1 by 8, 1 by 6", "1.2 by 2, 2 by 3").replace("| 2.1 by 1 |", "| 1 by 1 |") ranked = self.ranked(todo, readme) self.assertIn("| **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 1.2 by 2, 2 by 3 | 0.4 |", ranked.text) def test_scores_an_item_that_moves_no_goal_zero(self): ranked = self.ranked(TODO.replace("| 2 | 4 | 3 | 2.1 by 1 |", "| 2 | 4 | 3 | |")) self.assertIn("| **Tighten the tests.** | 2 | 4 | 3 | | 0.0 |", ranked.text) self.assertEqual(ranked.under_bar, ["item 2 scores 0.0, under the bar 2; delete it, or mark it with a reason the bar does not apply"]) def test_holds_each_item_a_decision_cites_and_says_how_to_settle_a_finished_one(self): self.assertIsInstance(self.ranked(decisions=DECISIONS + "Until `todo.md` item 4 lands.\n"), rank_todo.Ranked) gone = self.ranked(TODO.replace("| 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 2.1 by 1 | 0 |\n", "")) self.assertEqual(gone.reason, "docs/decisions.md cites \"`todo.md` item 5\", which todo.md no longer holds; revise that decision to say what holds now") def test_names_each_item_under_the_bar_that_is_not_exempt(self): ranked = self.ranked(TODO.replace("`Bar = 2`", "`Bar = 2.5`")) self.assertEqual(ranked.under_bar, ["item 2 scores 2.1, under the bar 2.5; delete it, or mark it with a reason the bar does not apply"]) self.assertEqual(self.ranked(TODO.replace("`Bar = 2`", "`Bar = 2.1`")).under_bar, []) blocking = self.ranked(TODO.replace("`Bar = 2`", "`Bar = 2.5`").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 2.1, under the bar 2.5; 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, []) self.assertEqual(self.ranked(ranked.text.replace("| 2 | 0.1.0 | | |", "| 2 | 0.1.0 | | owner |")).under_bar, []) def test_reads_goals_and_sub_goals_in_reading_order(self): self.assertEqual(rank_todo.readme_goals(README), [("1", "Achieve"), ("2", "Maintain"), ("2.1", "Achieve")]) self.assertEqual(rank_todo.readme_goals(README.replace(" 1.", "\t1.")), [("1", "Achieve"), ("2", "Maintain"), ("2.1", "Achieve")]) self.assertEqual(rank_todo.readme_goals(README.replace("2. **Maintain:** Correct.", "```\n## Goals\n```\n2. Correct.")), [("1", "Achieve"), ("2", None), ("2.1", "Achieve")]) self.assertEqual(rank_todo.readme_goals(README.replace("2. **Maintain:**", "1. **Maintain:**")), Failure("README.md's ## Goals numbers 1, 1, 1.1; number each goal by its position")) self.assertEqual(rank_todo.readme_goals("## Goals\n\n1. **Achieve:** A.\n 1. **Achieve:** B.\n\t2. **Achieve:** C.\n"), [("1", "Achieve"), ("1.1", "Achieve"), ("1.2", "Achieve")]) def test_refuses_a_goal_it_cannot_weigh(self): cases = { (TODO.replace("2.1 by 8, 1 by 6", "2.1 by 8, 2 by 6"), README): "item 3 cites goal 2, a maintain goal, which vetoes and never scores; name only the achieve goals it moves", (TODO.replace("2.1 by 8, 1 by 6", "2.1 by 8, 3 by 6"), README): "item 3 cites goal 3, which README.md's ## Goals does not number", (TODO.replace("2.1 by 8, 1 by 6", "2.1 by 8, 2.1 by 6"), README): "item 3 cites goal 2.1 twice; name each goal once", (TODO, README.replace("**Maintain:** Correct", "Correct")): "README.md's goal 2 is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says, so rank_todo.py knows which goals score", } for (todo, readme), reason in cases.items(): with self.subTest(reason=reason): self.assertEqual(self.ranked(todo, readme), Failure(reason)) def test_scores_every_item_zero_where_readme_marks_no_goal_achieve(self): ranked = self.ranked(TODO.replace("2.1 by 8, 1 by 6", "").replace("| 2.1 by 1 |", "| |").replace("| 1 by 9 |", "| |"), README.replace("**Achieve:**", "**Maintain:**")) self.assertEqual([line.split(" scores ")[0] for line in ranked.under_bar], ["item 2", "item 3", "item 1"]) self.assertEqual(ranked.notes, ["README.md marks no goal **Achieve:**, so every item scores V 0"]) def test_accepts_a_move_of_ten_and_a_variable_base(self): ranked = self.ranked(TODO.replace("| 1 by 9 |", "| 1 by 10 |").replace("100 * V / 2**S", "V * S ** 2")) self.assertIn("| **Ship the later thing.** | 1 | 1 | 3 | 1 by 10 | 6.7 |", ranked.text) def test_asks_to_drop_a_goal_weights_table_from_a_table_with_moves(self): weighted = TODO.replace("## Items\n", "| Goal | W |\n|---|---|\n| 1 | 1.00 |\n\n## Items\n") self.assertEqual(self.ranked(weighted), Failure("todo.md holds a goal weights table beside a Moves column; drop the table, since rank_todo.py weighs each achieve goal by its rank in README.md")) def test_refuses_a_formula_that_is_more_than_arithmetic_over_the_scores(self): for formula in ("__import__('os')", "R.real", "X + 1", "C + 1", "A + V", "G + V", "W * V", "R ^ 2", "R +", "R / (S - 2)", "R\x00", "1" * 400, " + ".join(["R"] * 300), "-" * 100000 + "R", "10 ** 10 ** 10", "(-8) ** (1 / 3)", "0 ** -1"): with self.subTest(formula=formula): self.assertIsInstance(self.ranked(TODO.replace("100 * V / 2**S", formula)), Failure) self.assertIn("| 1 by 9 | 2.0 |", self.ranked(TODO.replace("100 * V / 2**S", "R + S / S")).text) self.assertEqual(self.ranked(TODO.replace("100 * V / 2**S", "1 ** (1 / (S - S))")), Failure("the formula gives item 1 no finite score")) self.assertEqual(self.ranked(TODO.replace("100 * V / 2**S", "(1 / (S - S)) ** 0")), Failure("the formula gives item 1 no finite score")) self.assertEqual(self.ranked(TODO.replace("100 * V / 2**S", "W")), Failure("the formula 'W' may hold only numbers, R, S, V, + - * / ** and parentheses")) self.assertEqual(self.ranked(TODO.replace("2.1 by 8, 1 by 6", "2.1: 8, 1: 6")), Failure("item 3's Moves '2.1: 8, 1: 6' is not empty or goals such as `1.1 by 8, 1.2 by 5`, each moved by a whole number from 1 to 10")) def test_refuses_a_file_off_the_format(self): row = "| 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1 | 0 |" broken = { row: row.replace("0.1.0", "0.1"), "| 2 | 4 | 3 | 2.1 by 1 |": "| 2 | 4 | 11 | 2.1 by 1 |", "Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1": "Tighten the tests.** | 2 | 4 | 3 | 2.1 by 11", "Move the writes out.** | 5 | 5 | 3 | 2.1 by 1": "Move the writes out.** | 5 | 5 | 3 | 2.1 by 0", "2.1 by 8, 1 by 6": "2.1: 8, 1: 6", "2.1 by 8, 1 by 6 ": "2.1 by 8,1 by 6 ", "| 3 | 8 | 3 | 2.1 by 8, 1 by 6 |": "| 3 | 8 | 3 | 2.1, 1 |", row + "\n": row.replace("4 | 3", "4 | ²") + "\n", "| 2.1 by 1 | 0 |\n\n## Details": "| 2.1 by 1 | 0 |\n\n| 9 | 0.1.0 | | | **Stray.** | 1 | 1 | 3 | 1 by 1 | 0 |\n\n## Details", "| 4 | 0.1.0 | | security |": "| 4 | 0.1.0 | | later |", "| 5 | 0.1.0 | | decision |": "| 5 | 0.1.0 | | |", "`Next ID = 6`": "`Next ID = 5`", "`Next ID = 6`\n": "`Next ID = 6.5`\n", "| 4 | 0.1.0 | | security | **Rotate": "| 4 | 0.1.0 | | decision | **Rotate", "Fast detail.": "```\n## Goals\n```", "### 3. Make it fast.": "### 7. Make it fast.", "`Bar = 2`": "`Bar = nan`", "## Items\n\n| ID | Release | Blocked by |": "## Plan\n\n| ID | Release |", } for old, new in broken.items(): with self.subTest(new=new): self.assertIn(old, TODO) self.assertIsInstance(self.ranked(TODO.replace(old, new)), Failure) class OldShape(unittest.TestCase): """A table with A, G and Goals, from before the Moves column, ranks by the formula and goal weights it states until its project reshapes it.""" def ranked(self, todo=OLD_SHAPE, readme=README): return rank_todo.rank(todo, readme, DECISIONS) def test_scores_by_the_stated_weights_and_says_to_reshape(self): ranked = self.ranked() self.assertIn("""| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 1 | 1 | 2.1 | -6.8 | | 3 | 0.1.0 | | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 6 | 8 | 2.1, 1 | 23.0 | | 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 5 | 5 | 2.1 | 10.3 | | 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 4 | 5 | 2.1 | 5.1 | | 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 9 | 9 | 1 | 34.8 | """, ranked.text) self.assertEqual(ranked.notes, [rank_todo.RESHAPE]) self.assertEqual(self.ranked(ranked.text), ranked) unmarked = "# P\n\n## Goals\n\n1. Fast.\n2. Correct.\n 1. Tested.\n" self.assertEqual(self.ranked(readme=unmarked), ranked) def test_prints_the_positional_weights_when_they_stop_listing_the_readmes_goals(self): failure = self.ranked(readme=README.replace(" 1. **Achieve:** Tested.", " 1. **Achieve:** Tested.\n3. Small.")) self.assertIn("| 1 | 1.00 |\n| 2 | 0.75 |\n| 2.1 | 0.50 |\n| 3 | 0.25 |", failure.reason) def test_refuses_an_item_citing_a_goal_the_weights_do_not_list(self): self.assertEqual(self.ranked(OLD_SHAPE.replace("| 2.1, 1 | 0 |", "| 2.1, 9 | 0 |")), Failure("item 3 cites goal 9, which the goal weights do not list")) def test_refuses_a_formula_over_v(self): self.assertEqual(self.ranked(OLD_SHAPE.replace("-R - S/4 + 2*A + 2*G*W", "V")), Failure("the formula 'V' may hold only numbers, A, G, R, S, W, + - * / ** and parentheses")) def test_asks_for_a_c_column_in_a_table_without_one(self): legacy = OLD_SHAPE.split("## Items")[0] + "## Items\n\n| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|\n| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 |\n" self.assertEqual(rank_todo.rank(legacy, README, "").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_a_table_with_both_moves_and_goals(self): both = OLD_SHAPE.replace("| Goals | Score |\n|---|", "| Goals | Moves | Score |\n|---|---|").replace(" | 0 |\n", " | 1 by 1 | 0 |\n") self.assertEqual(self.ranked(both), Failure("todo.md's items table has the column A, G, Goals, a column of the old shape, which Moves replaced; drop it")) def test_names_the_goals_column_a_table_misspells_by_its_shape(self): misspelt = TODO.replace("| C | Moves |", "| C | Move |") self.assertEqual(rank_todo.rank(misspelt, README, DECISIONS), Failure("todo.md's items table has no Moves column; rank_todo.py --help shows a valid one")) self.assertEqual(todo_table.items(misspelt), Failure("todo.md's items table has no Moves column; rank_todo.py --help shows a valid one")) self.assertEqual(self.ranked(OLD_SHAPE.replace("| G | Goals |", "| G | Goal |")), Failure("todo.md's items table has no Goals column; rank_todo.py --help shows a valid one")) class Blockers(unittest.TestCase): def ranked(self, todo): return rank_todo.rank(todo, README, DECISIONS) def test_ranks_each_blocker_just_above_the_first_item_it_blocks(self): ranked = self.ranked(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 5, 2 |")) self.assertIn("""| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 2.1 by 1 | 8.3 | | 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1 | 2.1 | | 5 | 0.1.0 | | decision | **Move the writes out.** | 5 | 5 | 3 | 2.1 by 1 | 1.0 | | 3 | 0.1.0 | 2, 5 | | **Make it fast, `a \\| b`.** | 3 | 8 | 3 | 2.1 by 8, 1 by 6 | 2.6 | | 1 | 0.2.0 | | | **Ship the later thing.** | 1 | 1 | 3 | 1 by 9 | 300.0 | """, 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)] self.assertEqual(rows, ["| 4", "| 2", "| 5", "| 3", "| 1"]) def test_drops_a_blocker_the_table_no_longer_holds_as_finished(self): finished = TODO.replace("| 4 | 0.1.0 | | security | **Rotate the leaked key.** | 9 | 2 | 3 | 2.1 by 1 | 0 |\n", "").replace("| 2 | 0.1.0 | |", "| 2 | 0.1.0 | 4 |") ranked = self.ranked(finished) self.assertIn("| 2 | 0.1.0 | | | **Tighten the tests.**", ranked.text) self.assertEqual(ranked.notes, ["item 2: dropped 4 from its Blocked by; the table no longer holds it"]) self.assertEqual(self.ranked(ranked.text).notes, []) def test_refuses_a_blocker_in_a_later_release_a_loop_and_an_id_past_next_id(self): cases = { "| 3 | 0.1.0 | 1 |": "item 3, in release 0.1.0, is blocked by item 1, in later release 0.2.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): self.assertEqual(self.ranked(TODO.replace("| 3 | 0.1.0 | |", cell)), Failure(reason)) looped = TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 |").replace("| 2 | 0.1.0 | |", "| 2 | 0.1.0 | 5 |").replace("| 5 | 0.1.0 | |", "| 5 | 0.1.0 | 3 |") self.assertEqual(self.ranked(looped), Failure("items 2, 3 and 5 block each other in a loop; drop one of the blockers")) self.assertEqual(self.ranked(TODO.replace("| 3 | 0.1.0 | |", "| 3 | 0.1.0 | 2 and 5 |")), Failure("item 3's Blocked by '2 and 5' is not empty or IDs separated by \", \"")) def test_adds_the_column_to_a_table_without_it(self): without = TODO.replace("| Blocked by ", "").replace("|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|") without = re.sub(r"\| (\d) \| (0\.[12]\.0) \| \| ", r"| \1 | \2 | ", without) self.assertEqual(self.ranked(without), self.ranked(TODO)) unscored = TODO.replace(" | Score |", " |").replace("|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|").replace(" | 0 |\n", " |\n") self.assertEqual(self.ranked(unscored), self.ranked(TODO)) def test_refuses_a_column_it_does_not_know_rather_than_drop_it_and_asks_for_c_beside_blocked_by(self): extra = TODO.replace("| Moves | Score |\n|---|", "| Moves | Score | Later |\n|---|---|").replace(" | 0 |\n", " | 0 | x |\n") self.assertEqual(self.ranked(extra), Failure("todo.md's items table has the column Later, which this rank_todo.py does not know; the columns it knows are ID, Release, Blocked by, Exempt, Item, R, S, C, Moves, Score. Rename it, or move this machine to the release that added it")) self.assertEqual([item.id for item in todo_table.items(extra)], [1, 2, 3, 4, 5]) without_c = TODO.replace("| R | S | C |", "| R | S |").replace("|---|---|---|---|---|---|---|---|---|---|", "|---|---|---|---|---|---|---|---|---|") without_c = re.sub(r"(\*\* \| \d+ \| \d+) \| \d+ \|", r"\1 |", without_c) 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): 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 by 1 | 0 |\n\n## Details", "| 2.1 by 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") table = TODO[TODO.index("| ID | Release |"):TODO.index("\n\n## Details")] + "\n" above = table + "\n" + TODO.replace(table, "") self.assertEqual(self.ranked(above).reason, "the items table sits under no ## heading; move it under ## Items") header = "| ID | Release | Blocked by | Exempt | Item | R | S | C | Moves | 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("| Moves | Score |\n|---|", "| Moves | 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 | Moves | 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): def run_main(self, *argv): with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said: code = rank_todo.main(list(argv)) return code, said.getvalue() def test_ranks_the_file_in_place_and_check_writes_nothing(self): with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") todo.write_text(TODO) pathlib.Path(temp, "README.md").write_text(README) pathlib.Path(temp, "docs").mkdir() pathlib.Path(temp, "docs", "decisions.md").write_text(DECISIONS) self.assertEqual(self.run_main("--check", str(todo))[0], 1) self.assertEqual(todo.read_text(), TODO) self.assertEqual(self.run_main(str(todo)), (0, "")) self.assertEqual(self.run_main("--check", str(todo)), (0, "")) pathlib.Path(temp, "README.md").write_bytes(b"\xff") self.assertEqual(self.run_main(str(todo)), (1, f"{temp}/README.md: not UTF-8\n")) pathlib.Path(temp, "README.md").unlink() self.assertEqual(self.run_main(str(todo)), (1, f"{temp}/README.md: not found; rank_todo.py weighs goals from the README.md beside todo.md\n")) def test_says_to_reshape_an_old_shape_table_but_passes_the_check(self): with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") todo.write_text(OLD_SHAPE) pathlib.Path(temp, "README.md").write_text(README) pathlib.Path(temp, "docs").mkdir() pathlib.Path(temp, "docs", "decisions.md").write_text(DECISIONS) self.assertEqual(self.run_main(str(todo)), (0, f"{todo}: {rank_todo.RESHAPE}\n")) self.assertEqual(self.run_main("--check", str(todo)), (0, "")) def test_shows_a_valid_file_in_help_and_names_arguments_it_cannot_read(self): self.assertEqual(self.run_main("--help")[0], 0) skeleton = "\n".join(line[4:] for line in rank_todo.__doc__.split("\n") if line.startswith(" ")) ranked = rank_todo.rank(skeleton, "## Goals\n\n1. **Achieve:** Work.\n", "") self.assertIsInstance(ranked, rank_todo.Ranked) self.assertEqual((ranked.under_bar, ranked.notes), ([], [])) self.assertEqual(self.run_main("--fix"), (1, "rank_todo.py: cannot read the arguments --fix; usage: rank_todo.py [--check] [todo.md]\n")) class ThisRepo(unittest.TestCase): def test_todo_md_is_ranked_with_nothing_under_the_bar(self): text = (ROOT / "todo.md").read_text(encoding="utf-8") ranked = rank_todo.rank(text, (ROOT / "README.md").read_text(encoding="utf-8"), (ROOT / "docs" / "decisions.md").read_text(encoding="utf-8")) self.assertEqual(ranked, rank_todo.Ranked(text, [], []))