import contextlib import io import pathlib import sys import tempfile import unittest sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent)) from project_config.test_project_config import configured from release_plan.test_ranking import DECISIONS, OLD_SHAPE, README, TODO from release_plan import goals_file, ranking import rank_todo 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") configured(self, pathlib.Path(temp)) self.enterContext(contextlib.chdir(temp)) 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 file the project's config names as goals-and-audience\n")) todo.unlink() self.assertEqual(self.run_main(str(todo)), (1, f"{todo}: not found; every project keeps a todo.md, and rank_todo.py --help shows one\n")) def test_ranks_the_plan_and_reads_the_decisions_where_the_config_puts_them(self): with tempfile.TemporaryDirectory() as temp: folder = configured(self, pathlib.Path(temp)) self.enterContext(contextlib.chdir(temp)) config = folder / "config.toml" config.write_text(config.read_text().replace('"checkout:todo.md"', '"folder:todo.md"').replace('"checkout:docs/decisions.md"', '"folder:decisions.md"')) pathlib.Path(temp, "README.md").write_text(README) pathlib.Path(temp, "todo.md").write_text("the project owners' own notes\n") (folder / "todo.md").write_text(TODO) self.assertIn("docs/decisions.md does not cite", self.run_main()[1]) (folder / "decisions.md").write_text(DECISIONS) self.assertEqual(self.run_main("--check")[0], 1, "the plan in the config's folder is not ranked yet") self.assertEqual(self.run_main(), (0, "")) self.assertEqual(self.run_main("--check"), (0, "")) self.assertEqual(pathlib.Path(temp, "todo.md").read_text(), "the project owners' own notes\n") (folder / "decisions.md").write_bytes(b"\xff") self.assertEqual(self.run_main(), (1, f"{folder}/decisions.md: not UTF-8\n")) def test_refuses_a_file_that_is_not_the_plan_the_config_names_and_a_checkout_no_config_names(self): with tempfile.TemporaryDirectory() as temp: folder = configured(self, pathlib.Path(temp)) self.enterContext(contextlib.chdir(temp)) pathlib.Path(temp, "todo.md").write_text(TODO) pathlib.Path(temp, "README.md").write_text(README) other = pathlib.Path(temp, "another-project.md") other.write_text(TODO) code, said = self.run_main(str(other)) self.assertEqual(code, 1) self.assertIn(f"{other} is not the plan", said) self.assertIn(str(pathlib.Path(temp, "todo.md").resolve()), said) self.assertEqual(other.read_text(), TODO, "a file ranked by another project's goals is not written") (folder / "config.toml").unlink() code, said = self.run_main() self.assertEqual(code, 1) self.assertIn("no config names the checkout", said) 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") configured(self, pathlib.Path(temp)) self.enterContext(contextlib.chdir(temp)) 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") configured(self, pathlib.Path(temp)) self.enterContext(contextlib.chdir(temp)) 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 (goals_file.GOAL_STEP, 0.212, 0.188, 0.082, 0.053): self.assertIn(str(figure), rank_todo.__doc__) self.assertIn(f"While it is under {goals_file.FLOOR}, or the history holds no scoring run, code health weighs {goals_file.UNDER_FLOOR} times its weight by position, and the other goals keep theirs.", " ".join(rank_todo.__doc__.split()))