import contextlib import io import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) import rank_todo from loop_claude.failure import Failure from test_python_floor import ROOT README = "# P\n\n## Goals\n\n1. Fast.\n2. Correct.\n 1. Tested.\n\n## Audience\n\nMe.\n" DECISIONS = "## A choice\n\nStands until `todo.md` item 5 ends it.\n" TODO = """# 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 | Exempt | Item | R | S | A | G | Goals | Score | |---|---|---|---|---|---|---|---|---|---| | 1 | 0.2.0 | | **Ship the later thing.** | 1 | 1 | 9 | 9 | 1 | 0 | | 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 | | 3 | 0.1.0 | | **Make it fast, `a \\| b`.** | 3 | 8 | 6 | 8 | 2.1, 1 | 0 | | 4 | 0.1.0 | security | **Rotate the leaked key.** | 9 | 2 | 1 | 1 | 2.1 | 0 | | 5 | 0.1.0 | decision | **Move the writes out.** | 5 | 5 | 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 | 1 | 1 | 2.1 | -6.8 | | 3 | 0.1.0 | | **Make it fast, `a \\| b`.** | 3 | 8 | 6 | 8 | 2.1, 1 | 23.0 | | 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 10.3 | | 5 | 0.1.0 | decision | **Move the writes out.** | 5 | 5 | 4 | 5 | 2.1 | 5.1 | | 1 | 0.2.0 | | **Ship the later thing.** | 1 | 1 | 9 | 9 | 1 | 34.8 | """, 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(self.ranked(ranked.text), ranked) 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 | 4 | 5 | 2.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 = 9`", "`Bar = 11`")) self.assertEqual(ranked.under_bar, ["item 2 scores 10.3, under the bar 11; delete it and any item that needs it, 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, []) def test_reads_goals_and_sub_goals_in_reading_order(self): self.assertEqual(rank_todo.readme_goals(README), ["1", "2", "2.1"]) self.assertEqual(rank_todo.readme_goals(README.replace(" 1.", "\t1.")), ["1", "2", "2.1"]) self.assertEqual(rank_todo.readme_goals(README.replace("2. Correct.", "```\n## Goals\n```\n2. Correct.")), ["1", "2", "2.1"]) self.assertIsInstance(rank_todo.readme_goals(README.replace("2. Correct.", "1. Correct.")), Failure) def test_prints_the_positional_weights_when_they_stop_listing_the_readmes_goals(self): failure = self.ranked(readme=README.replace(" 1. Tested.", " 1. 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_a_formula_that_is_more_than_arithmetic_over_the_scores(self): for formula in ("__import__('os')", "R.real", "X + 1", "R ** 2", "R +", "R / (S - 2)", "R\x00", "1" * 400, " + ".join(["R"] * 300), "-" * 100000 + "R"): with self.subTest(formula=formula): self.assertIsInstance(self.ranked(TODO.replace("-R - S/4 + 2*A + 2*G*W", formula)), Failure) def test_refuses_a_file_off_the_format(self): row = "| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 |" broken = { row: row.replace("0.1.0", "0.1"), row + "\n": row.replace("5 | 5", "5 | ²") + "\n", "| 2.1 | 0 |\n\n## Details": "| 2.1 | 0 |\n\n| 9 | 0.1.0 | | **Stray.** | 1 | 1 | 1 | 1 | 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 = 9`": "`Bar = nan`", "## Items\n\n| ID | Release | Exempt |": "## Plan\n\n| ID | Release |", } for old, new in broken.items(): with self.subTest(new=new): self.assertIsInstance(self.ranked(TODO.replace(old, new)), Failure) 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_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. Work.\n", "") self.assertIsInstance(ranked, rank_todo.Ranked) self.assertEqual(ranked.under_bar, []) 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, []))