157 lines
8.1 KiB
Python
157 lines
8.1 KiB
Python
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.doubles import configured
|
|
from plan.doubles import DECISIONS, OLD_SHAPE, README, TODO
|
|
from project_config.settings import DEFAULT_FLOOR
|
|
from plan import goals_file, ranking
|
|
import rank_todo
|
|
from todo_item.doubles import READABLE_TOUCHES, UNREADABLE_BULLETS
|
|
|
|
|
|
class Main(unittest.TestCase):
|
|
# The home of a test that writes no config.
|
|
home = pathlib.Path("/no-home")
|
|
|
|
def configured(self, checkout: str) -> pathlib.Path:
|
|
folder = configured(self, pathlib.Path(checkout))
|
|
self.home = folder.parents[1]
|
|
return folder
|
|
|
|
def run_main(self, *argv):
|
|
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()) as said:
|
|
code = rank_todo.main(list(argv), self.home)
|
|
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")
|
|
self.configured(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"cannot read {temp}/README.md: not UTF-8 on line 1\n"))
|
|
pathlib.Path(temp, "README.md").unlink()
|
|
self.assertEqual(self.run_main(str(todo)), (1, f"{temp}/README.md: missing or empty; rank_todo.py weighs goals from the goals 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 = self.configured(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"cannot read {folder}/decisions.md: not UTF-8 on line 1\n"))
|
|
|
|
def test_refuses_a_project_whose_plan_is_in_jira_and_writes_no_file(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
config = self.configured(temp) / "config.toml"
|
|
self.enterContext(contextlib.chdir(temp))
|
|
config.write_text(config.read_text().replace('"checkout:todo.md"', '"jira:https://acme.atlassian.net/browse/ABC-12"') + 'env-file = "~/acme.env"\njira-score-formula = "V"\njira-score-bar = 1\n')
|
|
todo = pathlib.Path(temp, "todo.md")
|
|
todo.write_text(TODO)
|
|
for args in ((), (str(todo),), ("--check", str(todo))):
|
|
with self.subTest(args=args):
|
|
self.assertEqual(self.run_main(*args), (1, "rank_todo.py ranks a todo.md, and this checkout's config keeps the plan in Jira, whose backlog ranks it: jira:https://acme.atlassian.net/browse/ABC-12\n"))
|
|
self.assertEqual(self.run_main("--check"), (0, ""))
|
|
self.assertEqual(todo.read_text(), TODO)
|
|
|
|
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 = self.configured(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")
|
|
self.configured(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_check_fails_on_each_item_whose_touches_list_it_cannot_read(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
todo = pathlib.Path(temp, "todo.md")
|
|
self.configured(temp)
|
|
self.enterContext(contextlib.chdir(temp))
|
|
pathlib.Path(temp, "README.md").write_text(README)
|
|
pathlib.Path(temp, "docs").mkdir()
|
|
pathlib.Path(temp, "docs", "decisions.md").write_text(DECISIONS)
|
|
for bullet in UNREADABLE_BULLETS:
|
|
with self.subTest(bullet):
|
|
# Item 1 sits in a later release than item 3: the check reads every item's list, not only the next release's.
|
|
todo.write_text(TODO.replace("Fast detail.", READABLE_TOUCHES).replace("Later detail.", f"Touches:\n\n{bullet}"))
|
|
self.assertEqual(self.run_main(str(todo))[0], 0)
|
|
code, said = self.run_main("--check", str(todo))
|
|
self.assertEqual(code, 1)
|
|
self.assertTrue(said.startswith(f"{todo}: item 1: "), said)
|
|
self.assertIn(repr(bullet), said)
|
|
self.assertEqual(said.count("\n"), 1)
|
|
|
|
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")
|
|
self.configured(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", "", "", DEFAULT_FLOOR)
|
|
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 the project's readability 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. The config sets the floor as `readability-floor`, {DEFAULT_FLOOR} where left out.", " ".join(rank_todo.__doc__.split()))
|