Answer stability round 1: 1 🔴, 13 🟡 (1 misread); end a detail only at a ### <ID>. heading, print the whole help, refuse an empty bullet, an unreadable changelog and ### categories under Unreleased, take a heading saying unreleased as the section, and test one behaviour each; the README module map is left as it is, as the PR says
This commit is contained in:
+16
-9
@@ -5,13 +5,15 @@ from release_plan.todo_table import cells, DETAILS, items_table, Table
|
||||
from failure import Failure
|
||||
from release_plan.ranking import stated
|
||||
|
||||
# A line that would end the item's detail, or open another one, inside ## Details.
|
||||
HEADING = re.compile(r"^#{1,3} ", re.M)
|
||||
# A line that would end ## Details (##) or could open another item's detail (###).
|
||||
HEADING = re.compile(r"^#{2,3} ", re.M)
|
||||
UNRELEASED = "## Unreleased"
|
||||
# The changelog's section for news not yet released: ## Unreleased, or a release heading such as `## 0.6.0 (unreleased)`.
|
||||
UNRELEASED_SECTION = re.compile(r"^## .*unreleased.*\n+", re.M | re.I)
|
||||
|
||||
|
||||
def added(todo: str, named: dict[str, str], detail: str) -> tuple[int, str] | Failure:
|
||||
"""todo with an item filed under Next ID, and that ID; named holds its cells by column name, its Item cell a plain tagline."""
|
||||
"""todo with an item filed under Next ID, and that ID; named holds its cells by column name, its Item cell the tagline, bold or not."""
|
||||
next_id = stated(todo, "Next ID")
|
||||
if isinstance(next_id, Failure):
|
||||
return next_id
|
||||
@@ -21,7 +23,7 @@ def added(todo: str, named: dict[str, str], detail: str) -> tuple[int, str] | Fa
|
||||
if "\n" in tagline:
|
||||
return Failure("the tagline holds a line break; keep it on one line")
|
||||
if HEADING.search(detail):
|
||||
return Failure("the detail holds a markdown heading, which would split ## Details; drop the #s")
|
||||
return Failure("the detail holds a ## or ### heading, which would split ## Details; use #### or drop the #s")
|
||||
id_ = int(next_id)
|
||||
lines = re.sub(r"^`Next ID = [0-9]+`$", f"`Next ID = {id_ + 1}`", todo, count=1, flags=re.M).split("\n")
|
||||
table = held_table(lines)
|
||||
@@ -58,19 +60,24 @@ def finished(todo: str, id_: int) -> str | Failure:
|
||||
section = DETAILS.search(text)
|
||||
if section is None:
|
||||
return text
|
||||
body = re.sub(rf"^### {id_}\. .*?(?=^### |\Z)", "", section.group(1), count=1, flags=re.M | re.S)
|
||||
body = re.sub(rf"^### {id_}\. .*?(?=^### \d+\. |\Z)", "", section.group(1), count=1, flags=re.M | re.S)
|
||||
return text[:section.start(1)] + body + text[section.end(1):]
|
||||
|
||||
|
||||
def with_bullet(changelog: str, bullet: str) -> str | Failure:
|
||||
"""changelog with bullet first under ## Unreleased, which goes above the first release where missing; a new changelog where changelog is empty."""
|
||||
line = "- " + bullet.strip().removeprefix("- ")
|
||||
if "\n" in line:
|
||||
"""changelog with bullet first under its UNRELEASED_SECTION, adding ## Unreleased above the first release heading where it has none; a new changelog where changelog is empty. Failure where that section holds ### subsections, since the bullet belongs under one of them."""
|
||||
text = bullet.strip().removeprefix("- ").strip()
|
||||
if not text:
|
||||
return Failure("the changelog bullet is empty")
|
||||
if "\n" in text:
|
||||
return Failure("the changelog bullet holds a line break; keep it on one line")
|
||||
line = f"- {text}"
|
||||
if not changelog.strip():
|
||||
return f"# Changelog\n\n{UNRELEASED}\n\n{line}\n"
|
||||
if section := re.search(rf"^{UNRELEASED}\n+", changelog, re.M):
|
||||
if section := UNRELEASED_SECTION.search(changelog):
|
||||
rest = changelog[section.end():]
|
||||
if re.search(r"^### ", re.split(r"^## ", rest, maxsplit=1, flags=re.M)[0], re.M):
|
||||
return Failure(f"CHANGELOG.md's {section.group().strip()} holds ### subsections; add the bullet under the right one by hand, then finish with --no-changelog")
|
||||
return changelog[:section.end()] + line + "\n" + ("" if rest.startswith("- ") or not rest else "\n") + rest
|
||||
if release := re.search(r"^## ", changelog, re.M):
|
||||
return changelog[:release.start()] + f"{UNRELEASED}\n\n{line}\n\n" + changelog[release.start():]
|
||||
|
||||
+39
-20
@@ -4,19 +4,23 @@ usage: todo-item add --release <version> --l <L> --e <E> --i <I> --s <S> [--move
|
||||
[--blocked-by <IDs>] [--exempt <reason>] <tagline> [<<'EOF' detail EOF]
|
||||
todo-item finish <ID> [--changelog <bullet> | --no-changelog]
|
||||
|
||||
Works on the todo.md in the current folder, and the CHANGELOG.md and docs/decisions.md beside it.
|
||||
Works on the todo.md in the current folder and the CHANGELOG.md beside it, and reads the README.md
|
||||
and docs/decisions.md beside todo.md.
|
||||
The cells take the format rank_todo.py --help states; the letter flags take upper case too, as --L.
|
||||
|
||||
add takes Next ID as the item's ID and raises Next ID by one. It writes the row, with the tagline
|
||||
in bold and each | in it escaped, and the detail it reads from stdin, if any, under ## Details.
|
||||
The detail may hold no ## or ### heading.
|
||||
It prints `Filed item <ID>.` It writes nothing where ranking fails or scores the item under the bar.
|
||||
|
||||
finish removes the item's row and its detail. With --changelog it adds the bullet first under
|
||||
CHANGELOG.md's ## Unreleased, creating the section, or the file, where missing. Where CHANGELOG.md
|
||||
exists, finish needs --changelog or --no-changelog, so no bullet is left out by mistake. It prints
|
||||
`Finished item <ID>.` It writes nothing while docs/decisions.md cites the item.
|
||||
CHANGELOG.md's ## Unreleased, or a ## heading that says unreleased, creating the section, or the
|
||||
file, where missing. It refuses where that section holds ### subsections: add the bullet by hand.
|
||||
Where CHANGELOG.md exists, finish needs --changelog or --no-changelog, so no bullet is left out by
|
||||
mistake. It prints `Finished item <ID>.` It writes nothing while docs/decisions.md cites the item.
|
||||
|
||||
Exit 2 on bad arguments; exit 1, saying why, where it writes nothing.
|
||||
Exit 2 on bad arguments, and 1, saying why, on any other failure. A message opening `not filed:`
|
||||
or `not finished:` means it wrote nothing.
|
||||
"""
|
||||
import argparse
|
||||
import pathlib
|
||||
@@ -33,6 +37,9 @@ LETTERS = ("L", "E", "I", "S")
|
||||
|
||||
|
||||
def main(argv: list[str], folder: pathlib.Path = pathlib.Path(), stdin: typing.TextIO = sys.stdin) -> int:
|
||||
if argv[:1] in (["-h"], ["--help"]):
|
||||
print(__doc__.strip())
|
||||
return 0
|
||||
args = parser().parse_args(argv)
|
||||
done = add(args, folder, stdin) if args.command == "add" else finish(args, folder)
|
||||
if isinstance(done, Failure):
|
||||
@@ -43,7 +50,7 @@ def main(argv: list[str], folder: pathlib.Path = pathlib.Path(), stdin: typing.T
|
||||
|
||||
|
||||
def parser() -> argparse.ArgumentParser:
|
||||
parsed = argparse.ArgumentParser(prog="todo-item", description=__doc__.split("\n\n")[0])
|
||||
parsed = argparse.ArgumentParser(prog="todo-item")
|
||||
commands = parsed.add_subparsers(dest="command", required=True)
|
||||
adding = commands.add_parser("add")
|
||||
adding.add_argument("--release", required=True)
|
||||
@@ -67,6 +74,7 @@ def add(args: argparse.Namespace, folder: pathlib.Path, stdin: typing.TextIO) ->
|
||||
if isinstance(texts, Failure):
|
||||
return texts
|
||||
named = {"Release": args.release, BLOCKED_BY: args.blocked_by, "Exempt": args.exempt, "Item": args.tagline, MOVES: args.moves}
|
||||
# Claude Code's Bash tool gives a command /dev/null as stdin (readlink /proc/self/fd/0 shows it), so without a heredoc read() returns "" at once.
|
||||
filed = added(texts[0], named | {letter: getattr(args, letter) for letter in LETTERS}, "" if stdin.isatty() else stdin.read())
|
||||
if isinstance(filed, Failure):
|
||||
return Failure(f"not filed: {filed.reason}")
|
||||
@@ -84,8 +92,22 @@ def add(args: argparse.Namespace, folder: pathlib.Path, stdin: typing.TextIO) ->
|
||||
|
||||
def finish(args: argparse.Namespace, folder: pathlib.Path) -> str | Failure:
|
||||
todo, changelog = folder / "todo.md", folder / "CHANGELOG.md"
|
||||
texts = finish_texts(args, todo, changelog)
|
||||
if isinstance(texts, Failure):
|
||||
return Failure(f"not finished: {texts.reason}")
|
||||
ranked, news = texts
|
||||
if unwritten := ranking.write(todo, ranked.text):
|
||||
return Failure(f"not finished: {unwritten.reason}")
|
||||
if news is not None and (unwritten := ranking.write(changelog, news)):
|
||||
return Failure(f"{unwritten.reason}; todo.md no longer holds item {args.id}, so add its bullet by hand")
|
||||
report(todo, ranked)
|
||||
return f"Finished item {args.id}."
|
||||
|
||||
|
||||
def finish_texts(args: argparse.Namespace, todo: pathlib.Path, changelog: pathlib.Path) -> tuple[ranking.Ranked, str | None] | Failure:
|
||||
"""What finish writes: todo ranked without item args.id, and changelog's new text, None where finish leaves it as is."""
|
||||
if changelog.exists() and args.changelog is None and not args.no_changelog:
|
||||
return Failure("CHANGELOG.md exists beside todo.md; pass --changelog <bullet>, or --no-changelog for an item consumers don't notice")
|
||||
return Failure("CHANGELOG.md exists beside todo.md; pass --changelog <bullet>, or --no-changelog where the repo publishes nothing")
|
||||
texts = ranking.read_all(todo)
|
||||
if isinstance(texts, Failure):
|
||||
return texts
|
||||
@@ -95,21 +117,18 @@ def finish(args: argparse.Namespace, folder: pathlib.Path) -> str | Failure:
|
||||
ranked = ranking.rank(text, *texts[1:])
|
||||
if isinstance(ranked, Failure):
|
||||
return ranked
|
||||
news: str | None = None
|
||||
if args.changelog is not None:
|
||||
bulleted = with_bullet(changelog.read_text(encoding="utf-8") if changelog.exists() else "", args.changelog)
|
||||
if isinstance(bulleted, Failure):
|
||||
return bulleted
|
||||
news = bulleted
|
||||
if unwritten := ranking.write(todo, ranked.text):
|
||||
return unwritten
|
||||
if news is not None and (unwritten := ranking.write(changelog, news)):
|
||||
return Failure(f"{unwritten.reason}; todo.md no longer holds item {args.id}, so add its bullet by hand")
|
||||
report(todo, ranked)
|
||||
return f"Finished item {args.id}."
|
||||
if args.changelog is None:
|
||||
return ranked, None
|
||||
try:
|
||||
news = with_bullet(changelog.read_text(encoding="utf-8") if changelog.exists() else "", args.changelog)
|
||||
except OSError as failure:
|
||||
return Failure(f"{changelog}: {failure.strerror}")
|
||||
except UnicodeDecodeError:
|
||||
return Failure(f"{changelog}: not UTF-8")
|
||||
return news if isinstance(news, Failure) else (ranked, news)
|
||||
|
||||
|
||||
def report(todo: pathlib.Path, ranked: ranking.Ranked) -> None:
|
||||
"""Print what ranking says beside the ranked file, as rank_todo.py does."""
|
||||
"""Print ranking's notes and under-bar lines on stderr, each prefixed with todo.md's path, as rank_todo.py does."""
|
||||
for line in ranked.notes + ranked.under_bar:
|
||||
print(f"{todo}: {line}", file=sys.stderr)
|
||||
|
||||
+204
-49
@@ -8,12 +8,21 @@ import unittest
|
||||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
|
||||
|
||||
from release_plan.test_ranking import DECISIONS, README, TODO
|
||||
from todo_item import main
|
||||
from todo_item import edits, main
|
||||
from failure import Failure
|
||||
from release_plan import ranking
|
||||
|
||||
ADD = ["add", "--release", "0.1.0", "--l", "1", "--e", "1", "--i", "1", "--s", "1", "--moves", "1 by 9", "Count the `a | b` runs."]
|
||||
ROW = "| 6 | 0.1.0 | | | **Count the `a \\| b` runs.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 |"
|
||||
CHANGELOG = "# Changelog\n\n## Unreleased\n\n- Older news.\n\n## 0.1.0\n\n- First.\n"
|
||||
# ## Details of the ranked fixture, without the trailing ## Notes.
|
||||
DETAILS = "## Details\n\n### 3. Make it fast, `a | b`.\n\nFast detail.\n\n### 1. Ship the later thing.\n\nLater detail.\n\n"
|
||||
|
||||
|
||||
class Terminal(io.StringIO):
|
||||
"""A stdin that reads as a terminal, so add must not read it."""
|
||||
|
||||
def isatty(self):
|
||||
return True
|
||||
|
||||
|
||||
class TodoItem(unittest.TestCase):
|
||||
@@ -22,6 +31,7 @@ class TodoItem(unittest.TestCase):
|
||||
self.addCleanup(temp.cleanup)
|
||||
self.folder = pathlib.Path(temp.name)
|
||||
self.todo = self.folder / "todo.md"
|
||||
self.changelog = self.folder / "CHANGELOG.md"
|
||||
(self.folder / "README.md").write_text(README)
|
||||
(self.folder / "docs").mkdir()
|
||||
(self.folder / "docs" / "decisions.md").write_text(DECISIONS)
|
||||
@@ -29,87 +39,232 @@ class TodoItem(unittest.TestCase):
|
||||
assert not isinstance(ranked, ranking.Failure)
|
||||
self.todo.write_text(ranked.text)
|
||||
|
||||
def run_main(self, *argv, stdin=""):
|
||||
def run_main(self, *argv, stdin: str | io.StringIO = ""):
|
||||
with contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()) as err:
|
||||
code = main.main(list(argv), self.folder, io.StringIO(stdin))
|
||||
code = main.main(list(argv), self.folder, io.StringIO(stdin) if isinstance(stdin, str) else stdin)
|
||||
return code, out.getvalue(), err.getvalue()
|
||||
|
||||
def exit_code(self, *argv):
|
||||
with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit) as raised:
|
||||
main.main(list(argv), self.folder, io.StringIO())
|
||||
return raised.exception.code
|
||||
|
||||
def assert_ranked(self):
|
||||
self.assertEqual(ranking.check_failures(self.todo), [])
|
||||
|
||||
def details(self):
|
||||
text = self.todo.read_text()
|
||||
return text[text.index("## Details"):text.index("## Notes")]
|
||||
|
||||
def lower_item_2_under_the_bar(self):
|
||||
self.todo.write_text(self.todo.read_text().replace("**Tighten the tests.** | 1 | 2 | 1 | 2 | 4 |", "**Tighten the tests.** | 1 | 2 | 1 | 2 | 9 |"))
|
||||
|
||||
|
||||
class Help(TodoItem):
|
||||
def test_prints_the_whole_docstring_and_exits_0(self):
|
||||
for flag in ("-h", "--help"):
|
||||
with self.subTest(flag):
|
||||
code, out, err = self.run_main(flag)
|
||||
self.assertEqual((code, out, err), (0, main.__doc__.strip() + "\n", ""))
|
||||
for fact in ("stdin", "--changelog", "--no-changelog", "Exit 2"):
|
||||
self.assertIn(fact, out)
|
||||
|
||||
|
||||
class BadArguments(TodoItem):
|
||||
def test_exit_2(self):
|
||||
for argv in ([], ADD[:1] + ADD[3:], ["finish"]):
|
||||
with self.subTest(argv):
|
||||
self.assertEqual(self.exit_code(*argv), 2)
|
||||
|
||||
|
||||
class Add(TodoItem):
|
||||
def test_takes_next_id_bumps_it_and_writes_the_row_and_detail_ranked(self):
|
||||
self.assertEqual(self.run_main(*ADD, stdin="What the tagline leaves out.\n"), (0, "Filed item 6.\n", ""))
|
||||
text = self.todo.read_text()
|
||||
self.assertIn("`Next ID = 7`", text)
|
||||
self.assertIn(ROW, text)
|
||||
self.assertIn("### 6. Count the `a | b` runs.\n\nWhat the tagline leaves out.\n", text)
|
||||
def test_takes_next_id_and_raises_it(self):
|
||||
self.assertEqual(self.run_main(*ADD)[:2], (0, "Filed item 6.\n"))
|
||||
self.assertIn("`Next ID = 7`", self.todo.read_text())
|
||||
|
||||
def test_writes_the_row_with_the_tagline_bold_and_its_pipes_escaped(self):
|
||||
self.run_main(*ADD)
|
||||
self.assertIn("| 6 | 0.1.0 | | | **Count the `a \\| b` runs.** | 1 | 1 | 1 | 1 | 1 | 1 by 9 |", self.todo.read_text())
|
||||
|
||||
def test_takes_a_bold_tagline_upper_case_letters_blockers_and_exempt(self):
|
||||
argv = ["add", "--release", "0.1.0", "--blocked-by", "2", "--exempt", "owner", "--L", "1", "--E", "1", "--I", "1", "--S", "1", "**Count the runs.**"]
|
||||
self.assertEqual(self.run_main(*argv)[0], 0)
|
||||
self.assertIn("| 6 | 0.1.0 | 2 | owner | **Count the runs.** | 1 | 1 | 1 | 1 | 1 | |", self.todo.read_text())
|
||||
|
||||
def test_writes_the_detail_from_stdin_into_the_one_details_section(self):
|
||||
self.run_main(*ADD, stdin="What the tagline leaves out.\n")
|
||||
self.assertEqual(self.details(), DETAILS.replace("### 3.", "### 6. Count the `a | b` runs.\n\nWhat the tagline leaves out.\n\n### 3."))
|
||||
|
||||
def test_leaves_the_file_ranked(self):
|
||||
self.run_main(*ADD, stdin="Detail.")
|
||||
self.assert_ranked()
|
||||
|
||||
def test_files_no_detail_from_empty_stdin_and_takes_upper_case_letters_blockers_and_exempt(self):
|
||||
argv = ["add", "--release", "0.1.0", "--blocked-by", "2", "--exempt", "owner", "--L", "1", "--E", "1", "--I", "1", "--S", "1", "**Count the runs.**"]
|
||||
self.assertEqual(self.run_main(*argv)[:2], (0, "Filed item 6.\n"))
|
||||
text = self.todo.read_text()
|
||||
self.assertIn("| 6 | 0.1.0 | 2 | owner | **Count the runs.** | 1 | 1 | 1 | 1 | 1 | |", text)
|
||||
self.assertNotIn("### 6.", text)
|
||||
self.assert_ranked()
|
||||
def test_files_no_detail_from_empty_stdin(self):
|
||||
self.assertEqual(self.run_main(*ADD)[0], 0)
|
||||
self.assertEqual(self.details(), DETAILS)
|
||||
|
||||
def test_reads_no_detail_from_a_terminal(self):
|
||||
self.assertEqual(self.run_main(*ADD, stdin=Terminal("Detail."))[0], 0)
|
||||
self.assertEqual(self.details(), DETAILS)
|
||||
|
||||
def test_creates_details_where_todo_has_none(self):
|
||||
self.todo.write_text(self.todo.read_text().split("## Details")[0].rstrip("\n") + "\n")
|
||||
self.assertEqual(self.run_main(*ADD, stdin="Detail.")[0], 0)
|
||||
self.assertTrue(self.todo.read_text().endswith("## Details\n\n### 6. Count the `a | b` runs.\n\nDetail.\n"))
|
||||
self.assertTrue(self.todo.read_text().endswith("| 1 by 9 | 228.4 |\n\n## Details\n\n### 6. Count the `a | b` runs.\n\nDetail.\n"))
|
||||
self.assert_ranked()
|
||||
|
||||
def test_writes_nothing_for_an_item_under_the_bar_or_one_ranking_rejects(self):
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main("add", "--release", "0.1.0", "--l", "1", "--e", "1", "--i", "1", "--s", "1", "Count the runs.")
|
||||
self.assertEqual((code, err), (1, "not filed: item 6 scores 0.0, under the bar 2; delete it, or mark it with a reason the bar does not apply\n"))
|
||||
code, _, err = self.run_main("add", "--release", "0.1.0", "--l", "9", "--e", "1", "--i", "1", "--s", "1", "--moves", "1 by 9", "Count the runs.")
|
||||
self.assertEqual((code, err), (1, "not filed: item 6's L '9' is not a whole number from 1 to 4\n"))
|
||||
code, _, err = self.run_main("add", "--release", "0.1.0", "--l", "1", "--e", "1", "--i", "1", "--s", "1", "--moves", "1 by 9", "Count\nthe runs.")
|
||||
self.assertEqual((code, err), (1, "not filed: the tagline holds a line break; keep it on one line\n"))
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
def test_takes_a_detail_holding_a_one_or_four_hash_line(self):
|
||||
self.assertEqual(self.run_main(*ADD, stdin="Run it so:\n\n# run as root\n\n#### Why\n\nBecause.")[0], 0)
|
||||
self.assertIn("### 6. Count the `a | b` runs.\n\nRun it so:\n\n# run as root\n\n#### Why\n\nBecause.\n", self.todo.read_text())
|
||||
|
||||
def test_reports_ranking_lines_on_stderr_prefixed_with_todo_md(self):
|
||||
self.lower_item_2_under_the_bar()
|
||||
_, _, err = self.run_main(*ADD)
|
||||
self.assertTrue(err.startswith(f"{self.todo}: item 2 "), err)
|
||||
|
||||
def test_writes_nothing_where_it_refuses(self):
|
||||
refused = {
|
||||
"under the bar": ADD[:-3] + ["Count the runs."],
|
||||
"a score ranking rejects": ADD[:4] + ["9"] + ADD[5:],
|
||||
"a tagline with a line break": ADD[:-1] + ["Count\nthe runs."],
|
||||
}
|
||||
for case, argv in refused.items():
|
||||
with self.subTest(case):
|
||||
before = self.todo.read_text()
|
||||
code, out, err = self.run_main(*argv)
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertTrue(err.startswith("not filed: "), err)
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
self.assertIn("item 6", self.run_main(*refused["under the bar"])[2])
|
||||
|
||||
def test_writes_nothing_for_a_detail_holding_a_two_or_three_hash_line(self):
|
||||
for detail in ("## Goals", "### Why", "### 2. Other"):
|
||||
with self.subTest(detail):
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main(*ADD, stdin=f"Detail.\n\n{detail}\n")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(err.startswith("not filed: "), err)
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
|
||||
|
||||
class Finish(TodoItem):
|
||||
def test_removes_the_row_and_detail_and_adds_the_bullet_first_under_unreleased(self):
|
||||
(self.folder / "CHANGELOG.md").write_text(CHANGELOG)
|
||||
self.assertEqual(self.run_main("finish", "3", "--changelog", "Runs are fast."), (0, "Finished item 3.\n", ""))
|
||||
text = self.todo.read_text()
|
||||
self.assertNotRegex(text, r"(?m)^\| 3 \|")
|
||||
self.assertNotIn("### 3.", text)
|
||||
self.assertIn("### 1. Ship the later thing.\n\nLater detail.\n", text)
|
||||
self.assert_ranked()
|
||||
self.assertEqual((self.folder / "CHANGELOG.md").read_text(), "# Changelog\n\n## Unreleased\n\n- Runs are fast.\n- Older news.\n\n## 0.1.0\n\n- First.\n")
|
||||
def test_removes_the_row(self):
|
||||
self.assertEqual(self.run_main("finish", "3", "--no-changelog"), (0, "Finished item 3.\n", ""))
|
||||
self.assertNotRegex(self.todo.read_text(), r"(?m)^\| 3 \|")
|
||||
|
||||
def test_creates_unreleased_or_the_changelog_where_missing(self):
|
||||
(self.folder / "CHANGELOG.md").write_text("# Changelog\n\n## 0.1.0\n\n- First.\n")
|
||||
def test_removes_the_detail_and_keeps_the_others(self):
|
||||
self.run_main("finish", "3", "--no-changelog")
|
||||
self.assertEqual(self.details(), "## Details\n\n### 1. Ship the later thing.\n\nLater detail.\n\n")
|
||||
|
||||
def test_removes_a_detail_holding_a_sub_heading_whole(self):
|
||||
self.todo.write_text(self.todo.read_text().replace("Fast detail.\n", "Fast detail.\n\n### Why\n\nFast tail.\n").replace("Later detail.\n", "Later detail.\n\n### Why\n\nLater tail.\n"))
|
||||
self.assertEqual(self.run_main("finish", "3", "--no-changelog")[0], 0)
|
||||
self.assertEqual(self.details(), "## Details\n\n### 1. Ship the later thing.\n\nLater detail.\n\n### Why\n\nLater tail.\n\n")
|
||||
|
||||
def test_leaves_the_file_ranked(self):
|
||||
self.run_main("finish", "3", "--no-changelog")
|
||||
self.assert_ranked()
|
||||
|
||||
def test_adds_the_bullet_first_under_unreleased(self):
|
||||
self.changelog.write_text(CHANGELOG)
|
||||
self.assertEqual(self.run_main("finish", "3", "--changelog", "Runs are fast."), (0, "Finished item 3.\n", ""))
|
||||
self.assertEqual(self.changelog.read_text(), "# Changelog\n\n## Unreleased\n\n- Runs are fast.\n- Older news.\n\n## 0.1.0\n\n- First.\n")
|
||||
|
||||
def test_creates_unreleased_above_the_first_release(self):
|
||||
self.changelog.write_text("# Changelog\n\n## 0.1.0\n\n- First.\n")
|
||||
self.assertEqual(self.run_main("finish", "3", "--changelog", "- Runs are fast.")[0], 0)
|
||||
self.assertEqual((self.folder / "CHANGELOG.md").read_text(), "# Changelog\n\n## Unreleased\n\n- Runs are fast.\n\n## 0.1.0\n\n- First.\n")
|
||||
(self.folder / "CHANGELOG.md").unlink()
|
||||
self.assertEqual(self.changelog.read_text(), "# Changelog\n\n## Unreleased\n\n- Runs are fast.\n\n## 0.1.0\n\n- First.\n")
|
||||
|
||||
def test_creates_the_changelog_where_missing(self):
|
||||
self.assertEqual(self.run_main("finish", "2", "--changelog", "Tests are tight.")[0], 0)
|
||||
self.assertEqual((self.folder / "CHANGELOG.md").read_text(), "# Changelog\n\n## Unreleased\n\n- Tests are tight.\n")
|
||||
self.assertEqual(self.changelog.read_text(), "# Changelog\n\n## Unreleased\n\n- Tests are tight.\n")
|
||||
|
||||
def test_needs_a_changelog_choice_where_a_changelog_exists(self):
|
||||
(self.folder / "CHANGELOG.md").write_text(CHANGELOG)
|
||||
self.changelog.write_text(CHANGELOG)
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main("finish", "2")
|
||||
self.assertEqual((code, err), (1, "CHANGELOG.md exists beside todo.md; pass --changelog <bullet>, or --no-changelog for an item consumers don't notice\n"))
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(err.startswith("not finished: "), err)
|
||||
for fact in ("CHANGELOG.md", "--changelog", "--no-changelog"):
|
||||
self.assertIn(fact, err)
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
self.assertEqual(self.run_main("finish", "2", "--no-changelog"), (0, "Finished item 2.\n", ""))
|
||||
self.assertEqual((self.folder / "CHANGELOG.md").read_text(), CHANGELOG)
|
||||
self.assertNotRegex(self.todo.read_text(), r"(?m)^\| 2 \|")
|
||||
|
||||
def test_leaves_the_changelog_as_is_with_no_changelog(self):
|
||||
self.changelog.write_text(CHANGELOG)
|
||||
self.assertEqual(self.run_main("finish", "2", "--no-changelog")[0], 0)
|
||||
self.assertEqual(self.changelog.read_text(), CHANGELOG)
|
||||
|
||||
def test_reports_ranking_lines_on_stderr_prefixed_with_todo_md(self):
|
||||
self.lower_item_2_under_the_bar()
|
||||
_, _, err = self.run_main("finish", "3", "--no-changelog")
|
||||
self.assertTrue(err.startswith(f"{self.todo}: item 2 "), err)
|
||||
|
||||
def test_writes_nothing_for_an_item_a_decision_cites(self):
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main("finish", "5", "--changelog", "Writes moved out.")
|
||||
self.assertEqual((code, err), (1, 'docs/decisions.md cites "`todo.md` item 5", which todo.md no longer holds; revise that decision to say what holds now\n'))
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(err.startswith("not finished: "), err)
|
||||
self.assertIn("item 5", err)
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
self.assertFalse((self.folder / "CHANGELOG.md").exists())
|
||||
self.assertFalse(self.changelog.exists())
|
||||
|
||||
def test_fails_on_an_item_the_table_does_not_hold(self):
|
||||
self.assertEqual(self.run_main("finish", "9", "--no-changelog"), (1, "", "todo.md holds no item 9\n"))
|
||||
code, out, err = self.run_main("finish", "9", "--no-changelog")
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertTrue(err.startswith("not finished: "), err)
|
||||
self.assertIn("9", err)
|
||||
|
||||
def test_fails_on_a_todo_md_without_an_items_table(self):
|
||||
self.todo.write_text("# todo\n")
|
||||
code, _, err = self.run_main("finish", "3", "--no-changelog")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertIn("no items table", err)
|
||||
self.assertEqual(self.todo.read_text(), "# todo\n")
|
||||
|
||||
def test_writes_neither_file_for_a_bullet_it_refuses(self):
|
||||
changelogs = {
|
||||
"an empty bullet": (CHANGELOG, ""),
|
||||
"a whitespace bullet": (CHANGELOG, " \t "),
|
||||
"a bullet with a line break": (CHANGELOG, "Runs\nare fast."),
|
||||
"an Unreleased section with ### subsections": ("# Changelog\n\n## Unreleased\n\n### Security\n\n- Older news.\n", "Runs are fast."),
|
||||
}
|
||||
for case, (changelog, bullet) in changelogs.items():
|
||||
with self.subTest(case):
|
||||
self.changelog.write_text(changelog)
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main("finish", "3", "--changelog", bullet)
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(err.startswith("not finished: "), err)
|
||||
self.assertEqual((self.todo.read_text(), self.changelog.read_text()), (before, changelog))
|
||||
|
||||
def test_fails_on_a_changelog_that_is_not_utf_8(self):
|
||||
self.changelog.write_bytes(b"# Changelog\n\n\xff\n")
|
||||
before = self.todo.read_text()
|
||||
code, _, err = self.run_main("finish", "3", "--changelog", "Runs are fast.")
|
||||
self.assertEqual(code, 1)
|
||||
self.assertTrue(err.startswith("not finished: "), err)
|
||||
self.assertIn(str(self.changelog), err)
|
||||
self.assertEqual(self.todo.read_text(), before)
|
||||
|
||||
|
||||
class WithBullet(unittest.TestCase):
|
||||
def test_keeps_a_blank_line_before_prose_under_unreleased(self):
|
||||
self.assertEqual(edits.with_bullet("# Changelog\n\n## Unreleased\n\nSome note.\n", "New."), "# Changelog\n\n## Unreleased\n\n- New.\n\nSome note.\n")
|
||||
|
||||
def test_appends_unreleased_to_a_changelog_without_a_release(self):
|
||||
self.assertEqual(edits.with_bullet("# Changelog\n", "New."), "# Changelog\n\n## Unreleased\n\n- New.\n")
|
||||
|
||||
def test_takes_a_release_heading_marked_unreleased_as_the_section(self):
|
||||
self.assertEqual(edits.with_bullet("# Changelog\n\n## 0.6.0 (Unreleased)\n\n- Old.\n\n## 0.5.0\n", "New."), "# Changelog\n\n## 0.6.0 (Unreleased)\n\n- New.\n- Old.\n\n## 0.5.0\n")
|
||||
|
||||
def test_refuses_an_unreleased_section_with_subsections_and_says_to_add_by_hand(self):
|
||||
refused = edits.with_bullet("# Changelog\n\n## Unreleased\n\n### Security\n\n- Old.\n\n## 0.5.0\n", "New.")
|
||||
self.assertIsInstance(refused, Failure)
|
||||
assert isinstance(refused, Failure)
|
||||
self.assertIn("by hand", refused.reason)
|
||||
|
||||
def test_takes_subsections_of_a_release_below_unreleased(self):
|
||||
self.assertEqual(edits.with_bullet("## Unreleased\n\n- Old.\n\n## 0.5.0\n\n### Security\n", "New."), "## Unreleased\n\n- New.\n- Old.\n\n## 0.5.0\n\n### Security\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user