Answer stability round 1: 0 🔴, 2 🟡 (0 misread); show reuses item_details and reads todo.md alone, and the --calls file is owner-only
This commit is contained in:
@@ -13,18 +13,20 @@ tool's name, then up to two words that read as subcommands, as in `tea pr create
|
||||
starts with a lowercase letter and holds only lowercase letters, digits and dashes; any other word,
|
||||
such as an option, a path or a number, ends the pattern.
|
||||
|
||||
With --calls <file>, it also writes the file, one JSON object per Bash call in the order the chunks and
|
||||
threads give them: `chunk` (the chunk's number in the window, from 1), `pattern`, `seconds`,
|
||||
`command_characters`, `result_characters` and `command`, the command's text. The file is
|
||||
overwritten, and written before the table is printed. Commands may carry a secret, so write the file outside any
|
||||
folder git tracks.
|
||||
|
||||
The table has one row per pattern, most calls first. Its columns are the chunks that ran the
|
||||
pattern, its calls, and the tokens of its commands and of their results. Tokens are estimated at
|
||||
four characters each. The last column sums the seconds from each call to its result.
|
||||
|
||||
Exit 1, saying why, on unknown arguments, a version no tag names, or when git cannot start or finds
|
||||
no checkout, or the archive cannot be read, or the --calls file cannot be written.
|
||||
With --calls <file>, it first writes the file, replacing any it holds, with one line per Bash call:
|
||||
a JSON object with `chunk` (the chunk's number in the window, from 1, in the order the archive lists
|
||||
the chunks), `pattern` (as above), `seconds` (as in the table), `command_characters`,
|
||||
`result_characters` and `command`, the command's text. Lines follow the chunks in that order, and each
|
||||
chunk's threads. The file is readable by its owner alone. Commands may carry a secret, so write it
|
||||
outside any folder git tracks. Where it cannot be written, no table is printed.
|
||||
|
||||
Exit 1, saying why, on unknown arguments or --calls without a file; on a version no tag names; when git
|
||||
cannot start or finds no checkout; when the archive cannot be read; and when the --calls file cannot
|
||||
be written.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
@@ -88,14 +90,24 @@ def report(version: str | None, calls_file: pathlib.Path | None) -> str | Failur
|
||||
return read
|
||||
threads.append(read)
|
||||
if calls_file is not None:
|
||||
try:
|
||||
calls_file.write_text("".join(json.dumps(each) + "\n" for each in records(threads)), encoding="utf-8")
|
||||
except OSError as failure:
|
||||
return Failure(f"{calls_file}: {failure.strerror}")
|
||||
saved = save_calls(calls_file, "".join(json.dumps(each) + "\n" for each in records(threads)))
|
||||
if saved:
|
||||
return saved
|
||||
named = f"v{version}" if version else "the release not yet cut"
|
||||
return f"Shell command patterns of {len(threads)} chunk{'' if len(threads) == 1 else 's'} in {named}:\n\n{table(tally(threads))}"
|
||||
|
||||
|
||||
def save_calls(path: pathlib.Path, text: str) -> Failure | None:
|
||||
"""Write text to path for its owner alone, since a command may carry a secret; path keeps no wider mode it already had."""
|
||||
try:
|
||||
with open(os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w", encoding="utf-8") as file:
|
||||
os.fchmod(file.fileno(), 0o600)
|
||||
file.write(text)
|
||||
except OSError as failure:
|
||||
return Failure(f"{path}: {failure.strerror}")
|
||||
return None
|
||||
|
||||
|
||||
def window(version: str | None, cwd: str) -> tuple[int | None, int | None] | Failure:
|
||||
"""The release's window as tag dates in seconds since the epoch: after the tag before it, up to its own; None where it is open."""
|
||||
listed = git("-c", "versionsort.suffix=-", "tag", "--list", "v*", "--sort=-v:refname", "--format=%(refname:short) %(creatordate:unix)", cwd=cwd)
|
||||
|
||||
@@ -129,6 +129,14 @@ class Main(unittest.TestCase):
|
||||
self.assertIn("| tea pr create | 1 | 1 |", ran.stdout)
|
||||
self.assertEqual([json.loads(line) for line in saved.read_text().splitlines()], [{"chunk": 1, "pattern": "tea pr create", "seconds": 0.0, "command_characters": 13, "result_characters": 2, "command": "tea pr create"}])
|
||||
|
||||
def test_makes_the_calls_file_readable_by_its_owner_alone_even_where_it_existed(self):
|
||||
saved = self.home / "calls.jsonl"
|
||||
saved.write_text("old")
|
||||
saved.chmod(0o644)
|
||||
self.assertEqual(self.run_tool("0.2.0", "--calls", str(saved)).returncode, 0)
|
||||
self.assertEqual(saved.stat().st_mode & 0o777, 0o600)
|
||||
self.assertNotIn("old", saved.read_text())
|
||||
|
||||
def test_takes_the_calls_flag_before_the_version_too(self):
|
||||
saved = self.home / "calls.jsonl"
|
||||
self.assertEqual(self.run_tool("--calls", str(saved), "0.2.0").returncode, 0)
|
||||
|
||||
+25
-19
@@ -1,7 +1,7 @@
|
||||
"""The text edits behind todo-item: filing an item in todo.md, removing a finished one, and adding a bullet to CHANGELOG.md. Each returns new text; ranking.rank then orders the table and details and writes the derived cells."""
|
||||
import re
|
||||
|
||||
from release_plan.todo_table import cells, DETAILS, items_table, Table
|
||||
from release_plan.todo_table import cells, DETAILS, item_details, items_table, Table
|
||||
from failure import Failure
|
||||
from release_plan.ranking import stated
|
||||
|
||||
@@ -47,15 +47,10 @@ def finished(todo: str, id_: int) -> str | Failure:
|
||||
table = held_table(lines)
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
for at, row in enumerate(table.rows, table.start):
|
||||
found = cells(row, len(table.columns))
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
if found[0] == str(id_):
|
||||
del lines[at]
|
||||
break
|
||||
else:
|
||||
return Failure(f"todo.md holds no item {id_}")
|
||||
at = row_index(table, id_)
|
||||
if isinstance(at, Failure):
|
||||
return at
|
||||
del lines[at]
|
||||
text = "\n".join(lines)
|
||||
section = DETAILS.search(text)
|
||||
if section is None:
|
||||
@@ -65,23 +60,34 @@ def finished(todo: str, id_: int) -> str | Failure:
|
||||
|
||||
|
||||
def shown(todo: str, id_: int) -> str | Failure:
|
||||
"""The table's header and divider, item id_'s row, and its detail whole, as `finished` takes it; the row alone where the item has no detail."""
|
||||
"""The table's header and divider, item id_'s row, then its detail with its heading, sub-headings included; the row alone where the item has no detail."""
|
||||
lines = todo.split("\n")
|
||||
table = held_table(lines)
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
for row in table.rows:
|
||||
at = row_index(table, id_)
|
||||
if isinstance(at, Failure):
|
||||
return at
|
||||
details = item_details(todo)
|
||||
if isinstance(details, Failure):
|
||||
return details
|
||||
shown_table = "\n".join([*lines[table.header:table.start], lines[at]])
|
||||
if id_ not in details:
|
||||
return shown_table
|
||||
heading = re.search(rf"^### {id_}\. .*$", todo, re.M)
|
||||
assert heading is not None # item_details found the detail by this heading
|
||||
return f"{shown_table}\n\n{heading.group()}\n\n{details[id_]}"
|
||||
|
||||
|
||||
def row_index(table: Table, id_: int) -> int | Failure:
|
||||
"""The index in the file's lines of item id_'s row."""
|
||||
for at, row in enumerate(table.rows, table.start):
|
||||
found = cells(row, len(table.columns))
|
||||
if isinstance(found, Failure):
|
||||
return found
|
||||
if found[0] == str(id_):
|
||||
break
|
||||
else:
|
||||
return Failure(f"todo.md holds no item {id_}")
|
||||
shown_table = "\n".join([*lines[table.header:table.start], row])
|
||||
section = DETAILS.search(todo)
|
||||
detail = re.search(rf"^### {id_}\. .*?(?=^### \d+\. |\Z)", section.group(1), re.M | re.S) if section else None
|
||||
return shown_table + (f"\n\n{detail.group().strip()}" if detail else "")
|
||||
return at
|
||||
return Failure(f"todo.md holds no item {id_}")
|
||||
|
||||
|
||||
def with_bullet(changelog: str, bullet: str) -> str | Failure:
|
||||
|
||||
+13
-7
@@ -1,12 +1,12 @@
|
||||
"""File an item in todo.md, or finish one, and rank the file as rank_todo.py does.
|
||||
"""File an item in todo.md or finish one, ranking the file as rank_todo.py does, or show one.
|
||||
|
||||
usage: todo-item add --release <version> --l <L> --e <E> --i <I> --s <S> [--moves <moves>]
|
||||
[--blocked-by <IDs>] [--exempt <reason>] <tagline> [<<'EOF' detail EOF]
|
||||
todo-item finish <ID> [--changelog <bullet> | --no-changelog]
|
||||
todo-item show <ID>
|
||||
|
||||
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.
|
||||
All three work on the todo.md in the current folder. add and finish also use the CHANGELOG.md beside it,
|
||||
and read 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
|
||||
@@ -20,8 +20,9 @@ file, where missing. It refuses where that section holds ### subsections: add th
|
||||
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.
|
||||
|
||||
show prints the items table's header, item <ID>'s row and its detail, from its ### heading to the next
|
||||
item's, sub-headings included. It writes nothing, and fails, saying so, where todo.md has no such row.
|
||||
show reads only todo.md. It prints the items table's header and divider, item <ID>'s row, and its detail
|
||||
from its ### heading to the next item's, sub-headings included. It fails, saying so, where todo.md has
|
||||
no row for <ID>.
|
||||
|
||||
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.
|
||||
@@ -78,8 +79,13 @@ def parser() -> argparse.ArgumentParser:
|
||||
|
||||
|
||||
def show(args: argparse.Namespace, folder: pathlib.Path) -> str | Failure:
|
||||
texts = plan_file.read_all(folder / "todo.md")
|
||||
return texts if isinstance(texts, Failure) else shown(texts[0], args.id)
|
||||
todo = folder / "todo.md"
|
||||
try:
|
||||
return shown(todo.read_text(encoding="utf-8"), args.id)
|
||||
except OSError as failure:
|
||||
return Failure(f"{todo}: {failure.strerror}")
|
||||
except UnicodeDecodeError:
|
||||
return Failure(f"{todo}: not UTF-8")
|
||||
|
||||
|
||||
def add(args: argparse.Namespace, folder: pathlib.Path, stdin: typing.TextIO) -> str | Failure:
|
||||
|
||||
@@ -280,6 +280,16 @@ class Show(TodoItem):
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertIn("todo.md", err)
|
||||
|
||||
def test_reads_todo_md_alone(self):
|
||||
(self.folder / "README.md").unlink()
|
||||
self.assertEqual(self.run_main("show", "3")[0], 0)
|
||||
|
||||
def test_fails_where_a_detail_holds_a_fenced_block(self):
|
||||
self.todo.write_text(self.todo.read_text().replace("Fast detail.\n", "Fast detail.\n\n```\n## Goals\n```\n"))
|
||||
code, out, err = self.run_main("show", "3")
|
||||
self.assertEqual((code, out), (1, ""))
|
||||
self.assertIn("fenced", err)
|
||||
|
||||
def test_takes_a_whole_number_as_the_id(self):
|
||||
self.assertEqual(self.exit_code("show", "three"), 2)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user