Let the OS name a README.md read failure and reject a repeated premises heading

This commit is contained in:
2026-09-25 08:26:17 +02:00
parent 17dd447056
commit e74b52a640
3 changed files with 42 additions and 35 deletions
+13 -10
View File
@@ -87,26 +87,29 @@ def sections(text):
for line in text.splitlines():
fenced ^= line.startswith("```")
if not fenced and line.startswith("## "):
current = line.strip()[3:]
found[current] = []
current = []
found.setdefault(line.strip()[3:], []).append(current)
elif not fenced and line.startswith("# "):
current = None
elif current and line.strip():
found[current].append(line)
return {name: "\n".join(body) for name, body in found.items()}
elif current is not None and line.strip():
current.append(line)
return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}
def read_premises():
try:
found = sections(open("README.md", encoding="utf-8").read())
except OSError:
sys.exit(f"no README.md in {os.getcwd()} - no premises.")
except OSError as failure:
sys.exit(f"README.md in {os.getcwd()}: {failure.strerror} - no premises.")
for name in ("Goals", "Audience"):
if name not in found:
bodies = found.get(name, [])
if not bodies:
sys.exit(f"README.md has no ## {name} heading - no premises.")
if not found[name]:
if len(bodies) > 1:
sys.exit(f"README.md has ## {name} twice - no premises.")
if not bodies[0]:
sys.exit(f"README.md has nothing under ## {name} - no premises.")
return f"## Goals\n{found['Goals']}\n\n## Audience\n{found['Audience']}"
return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"
def project():
+28 -25
View File
@@ -1,3 +1,4 @@
import contextlib
import importlib.machinery
import importlib.util
import json
@@ -26,35 +27,37 @@ def human(content):
class Sections(unittest.TestCase):
def test_keeps_every_headed_section_with_its_body(self):
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n# Appendix\nnot audience\n"
self.assertEqual(loop.sections(text), {"Goals": "1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority", "Empty": "", "Audience": "me"})
def test_keeps_every_headed_section_with_its_bodies(self):
text = "# Title\n\nintro\n\n## Goals\n\n1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority\n\n## Empty\n\n## Audience\nme\n## Audience\nagain\n# Appendix\nnot audience\n"
self.assertEqual(loop.sections(text), {"Goals": ["1. one\n```sh\n# not a heading\n```\n### Later\n2. two\n#1 priority"], "Empty": [""], "Audience": ["me", "again"]})
class ReadPremises(unittest.TestCase):
def exit_message(self):
with self.assertRaises(SystemExit) as stopped:
loop.read_premises()
self.assertTrue(stopped.exception.code.endswith("no premises."))
return stopped.exception.code
def test_names_what_is_missing(self):
cases = [
(None, "no README.md in"),
("# T\n## Audience\nme\n", "no ## Goals heading"),
("# T\n## Goals\n\n## Audience\nme\n", "nothing under ## Goals"),
("# T\n## Goals\n1. g\n", "no ## Audience heading"),
("# T\n## Goals\n1. g\n## Audience\n", "nothing under ## Audience"),
]
with tempfile.TemporaryDirectory() as root:
previous = os.getcwd()
os.chdir(root)
try:
for text, missing in cases:
if text is not None:
open("README.md", "w").write(text)
with self.assertRaises(SystemExit) as stopped:
loop.read_premises()
self.assertIn(missing, stopped.exception.code)
self.assertTrue(stopped.exception.code.endswith("no premises."))
open("README.md", "w").write("# T\n## Goals\n1. g\n## Audience\nme\n")
self.assertEqual(loop.read_premises(), "## Goals\n1. g\n\n## Audience\nme")
finally:
os.chdir(previous)
texts = {
"# T\n## Audience\nme\n": "no ## Goals heading",
"# T\n## Goals\n\n## Audience\nme\n": "nothing under ## Goals",
"# T\n## Goals\n1. g\n": "no ## Audience heading",
"# T\n## Goals\n1. g\n## Audience\n": "nothing under ## Audience",
"# T\n## Goals\n1. g\n## Audience\nme\n## Goals\n": "## Goals twice",
}
with tempfile.TemporaryDirectory() as root, contextlib.chdir(root):
self.assertIn(f"README.md in {os.getcwd()}: No such file or directory", self.exit_message())
os.mkdir("README.md")
self.assertIn("Is a directory", self.exit_message())
os.rmdir("README.md")
for text, missing in texts.items():
with self.subTest(missing=missing):
open("README.md", "w").write(text)
self.assertIn(missing, self.exit_message())
open("README.md", "w").write("# T\n## Goals\n1. g\n## Audience\nme\n")
self.assertEqual(loop.read_premises(), "## Goals\n1. g\n\n## Audience\nme")
class Render(unittest.TestCase):
+1
View File
@@ -8,3 +8,4 @@ Ordered by how many seats of the 2026-09-24 scoring panel (`docs/decisions.md`)
- Derive `project()` from the checkout root, today `os.getcwd()`, so `loop-claude` run from a subdirectory joins the root's session.
- Settle whether the "Expect the codebase to get smaller" bullet under `CLAUDE.md` → "One spelling per goal" names an action, and cut it if it is a forecast.
- Re-run the four-seat scoring panel and record the score in `docs/decisions.md`; the target is every dimension at 7.0 or above.
- Exit `loop-claude` with a message, today a traceback, when `README.md` is not UTF-8.