import contextlib import io import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from release_plan.test_ranking import DECISIONS, OLD_SHAPE, README, TODO import rank_todo from release_plan import ranking 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_writes_the_ranked_file_yet_fails_on_an_item_under_the_bar(self): under = "item 2 scores 3.1, under the bar 4; delete it, or mark it with a reason the bar does not apply" with tempfile.TemporaryDirectory() as temp: todo = pathlib.Path(temp, "todo.md") todo.write_text(TODO.replace("`Bar = 2`", "`Bar = 4`")) 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)), (1, f"{todo}: {under}\n")) self.assertEqual(self.run_main("--check", str(todo)), (1, f"{todo}: {under}\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}: {ranking.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 = ranking.rank(skeleton, "## Goals\n\n1. **Achieve:** Work.\n", "", "") self.assertIsInstance(ranked, ranking.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")) def test_states_the_goal_weights_the_ranking_computes(self): for figure in (ranking.GOAL_STEP, 0.212, 0.188, 0.082, 0.053): self.assertIn(str(figure), rank_todo.__doc__) self.assertIn(f"While it is under {ranking.FLOOR}, or the history holds no scoring run, code health weighs {ranking.UNDER_FLOOR} times its weight by position, and the other goals keep theirs.", " ".join(rank_todo.__doc__.split()))