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:
+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)
|
||||
|
||||
Reference in New Issue
Block a user