diff --git a/bin/plan/release.py b/bin/plan/release.py index b75b91fa..062246bb 100644 --- a/bin/plan/release.py +++ b/bin/plan/release.py @@ -10,8 +10,9 @@ VERSION = r"[0-9]+\.[0-9]+\.[0-9]+" DATE_DIGITS = (4, 2, 2) DATED = re.compile(rf"({VERSION}) - " + "-".join(f"[0-9]{{{digits}}}" for digits in DATE_DIGITS)) TAG = re.compile(rf"v({VERSION})") -# The heading of CHANGELOG.md's section for news not yet released: ## Unreleased, or a heading that says unreleased, such as `## 0.6.0 (unreleased)`, as a project's own changelog may head it. -UNRELEASED_HEADING = re.compile(r"^## .*unreleased.*$", re.M | re.I) +# The title of CHANGELOG.md's section for news not yet released: Unreleased, or a title that says unreleased, such as `0.6.0 (unreleased)`, as a project's own changelog may head it; and its ## heading. +UNRELEASED_TITLE = r"(?i:.*unreleased.*)" +UNRELEASED_HEADING = re.compile(rf"^## {UNRELEASED_TITLE}$", re.M) def release_of(version: str) -> Release: diff --git a/bin/reading_cost/test_reading_cost.py b/bin/reading_cost/test_reading_cost.py index a58e1985..34eef95f 100644 --- a/bin/reading_cost/test_reading_cost.py +++ b/bin/reading_cost/test_reading_cost.py @@ -238,7 +238,7 @@ class Located(unittest.TestCase): class Update(unittest.TestCase): def test_fails_where_the_record_refuses_the_write(self): class Unwritable(MemoryStore): - def write(self, text: str, over: str | None = None) -> Held | Failure: + def write(self, text: str, over: str | None) -> Held | Failure: return Failure("the wiki is down") archive = pathlib.Path(self.enterContext(tempfile.TemporaryDirectory())) diff --git a/bin/record_store/confluence_page.py b/bin/record_store/confluence_page.py index e984abbf..2ce88460 100644 --- a/bin/record_store/confluence_page.py +++ b/bin/record_store/confluence_page.py @@ -81,7 +81,7 @@ class ConfluencePage: return Failure(f"cannot convert {self.label} to markdown: {texts.reason}") return texts[0] - def write(self, text: str, over: str | None = None) -> Held | Failure: + def write(self, text: str, over: str | None) -> Held | Failure: """Replaces the page's text with text, as the version after over, the one held() gave. Where over is None, it replaces the page as it stands.""" seen = self.seen_of(over) if isinstance(seen, Failure): diff --git a/bin/record_store/contract.py b/bin/record_store/contract.py index 3c912913..88085894 100644 --- a/bin/record_store/contract.py +++ b/bin/record_store/contract.py @@ -21,12 +21,12 @@ class RecordStoreContract: def test_reads_back_what_it_wrote_to_the_character(self) -> None: store = self.new_store() text = "# Decisions\n\n- Å, 🔴 and a | pipe\n\n\n" - self.assertEqual(store.write(text), store.held()) + self.assertEqual(store.write(text, None), store.held()) self.assertEqual(store.read(), text) def test_a_write_replaces_the_whole_record(self) -> None: store = self.new_store() - first = store.write("first\nsecond\n") + first = store.write("first\nsecond\n", None) assert isinstance(first, Held), first # A write over the version the last one left replaces the record, as a later save does. self.assertIsInstance(store.write("third\n", first.version), Held) @@ -34,7 +34,7 @@ class RecordStoreContract: def test_holds_its_text_with_the_version_a_write_over_it_names(self) -> None: store = self.new_store() - store.write("text\n") + store.write("text\n", None) held = store.held() assert isinstance(held, Held), held self.assertEqual(held.text, "text\n") diff --git a/bin/record_store/fakes.py b/bin/record_store/fakes.py index 96a345df..6398535b 100644 --- a/bin/record_store/fakes.py +++ b/bin/record_store/fakes.py @@ -19,7 +19,7 @@ class MemoryStore: def held(self) -> Held | Failure: return Failure(self.fails) if self.fails else Held(self.text) - def write(self, text: str, over: str | None = None) -> Held | Failure: + def write(self, text: str, over: str | None) -> Held | Failure: if self.fails: return Failure(self.fails) self.text = text diff --git a/bin/record_store/jira_description.py b/bin/record_store/jira_description.py index a9902f54..3f8580d9 100644 --- a/bin/record_store/jira_description.py +++ b/bin/record_store/jira_description.py @@ -54,5 +54,5 @@ class EpicDescription: text = self.read() return text if isinstance(text, Failure) else Held(text) - def write(self, text: str, over: str | None = None) -> Held | Failure: + def write(self, text: str, over: str | None) -> Held | Failure: return Failure(READ_ONLY) diff --git a/bin/record_store/stores.py b/bin/record_store/stores.py index 20a4adc7..671a24ec 100644 --- a/bin/record_store/stores.py +++ b/bin/record_store/stores.py @@ -31,8 +31,8 @@ class RecordStore(typing.Protocol): def held(self) -> Held | Failure: """The record as it stands, with the version a write over it names.""" - def write(self, text: str, over: str | None = None) -> Held | Failure: - """Replaces the record's whole text, creating the record where it doesn't exist yet, and gives the record as the write left it. over is the version held() gave: where someone saved the record since, the write refuses. None replaces the record as it stands.""" + def write(self, text: str, over: str | None) -> Held | Failure: + """Replaces the record's whole text, creating the record where it doesn't exist yet, and gives the record as the write left it. over is the version held() gave: where someone saved the record since, the write refuses. None replaces the record as it stands, so a caller passes it only where it read no version.""" @dataclasses.dataclass(frozen=True) @@ -59,7 +59,7 @@ class FileStore: except OSError as failure: return Failure(f"cannot read {self.path}: {failure.strerror}") - def write(self, text: str, over: str | None = None) -> Held | Failure: + def write(self, text: str, over: str | None) -> Held | Failure: try: self.path.parent.mkdir(parents=True, exist_ok=True) except OSError as failure: @@ -108,7 +108,7 @@ def local_file(record: RecordStore, copy: pathlib.Path) -> pathlib.Path | Failur if isinstance(held, Failure): return held if held.text: - written = FileStore(copy, None).write(held.text) + written = FileStore(copy, None).write(held.text, None) if isinstance(written, Failure): return written else: @@ -145,7 +145,7 @@ def version_file(copy: pathlib.Path) -> pathlib.Path: def kept(copy: pathlib.Path, version: str | None) -> Failure | None: """Keeps version beside copy, or removes the file that kept an earlier one where version is None.""" if version is not None: - written = FileStore(version_file(copy), None).write(version) + written = FileStore(version_file(copy), None).write(version, None) return written if isinstance(written, Failure) else None try: version_file(copy).unlink(missing_ok=True) diff --git a/bin/record_store/test_confluence_page.py b/bin/record_store/test_confluence_page.py index 68ef65ea..4a2bfb62 100644 --- a/bin/record_store/test_confluence_page.py +++ b/bin/record_store/test_confluence_page.py @@ -141,7 +141,7 @@ class ConfluencePageTest(ConfluenceCase): def test_a_write_with_no_read_replaces_the_page_as_it_stands(self) -> None: self.confluence.teammate_saves("earlier\n") - self.assertIsInstance(self.new_store().write("replaced\n"), Held) + self.assertIsInstance(self.new_store().write("replaced\n", None), Held) self.assertEqual(self.confluence.calls[0], ("GET", URL, AUTHORIZATION, None)) self.assertEqual(self.new_store().read(), "replaced\n") @@ -168,7 +168,7 @@ class ConfluencePageTest(ConfluenceCase): self.confluence.teammate_saves("text\n") store = ConfluencePage(PAGE, self.env, self.confluence.http, lambda _: Failure("the shim is down"), lambda _: Failure("the shim is down")) self.assertEqual(store.read(), Failure(f"cannot convert {PAGE.spelling} to markdown: the shim is down")) - self.assertEqual(store.write("text\n"), Failure(f"cannot convert the text for {PAGE.spelling} to ADF: the shim is down")) + self.assertEqual(store.write("text\n", None), Failure(f"cannot convert the text for {PAGE.spelling} to ADF: the shim is down")) def test_fails_naming_the_page_where_adf_codec_refuses_its_document(self) -> None: self.confluence.teammate_saves("text\n") @@ -178,7 +178,7 @@ class ConfluencePageTest(ConfluenceCase): def test_a_write_confluence_refuses_otherwise_fails_naming_the_page_and_not_a_teammate(self) -> None: answers = iter([{"title": "Decisions", "version": {"number": 3}}, ErrorAnswer("PUT /wiki/api/v2/pages/2293761 answered 403: Forbidden", 403)]) store = ConfluencePage(PAGE, self.env, lambda *_: next(answers), to_markdown, to_adf) - self.assertEqual(store.write("text\n"), Failure(f"cannot write {PAGE.spelling}: PUT /wiki/api/v2/pages/2293761 answered 403: Forbidden")) + self.assertEqual(store.write("text\n", None), Failure(f"cannot write {PAGE.spelling}: PUT /wiki/api/v2/pages/2293761 answered 403: Forbidden")) def test_fails_on_a_version_it_cannot_read_and_writes_nothing(self) -> None: written = self.new_store().write("text\n", "{}") diff --git a/bin/record_store/test_jira_description.py b/bin/record_store/test_jira_description.py index 07bf1292..67bd40db 100644 --- a/bin/record_store/test_jira_description.py +++ b/bin/record_store/test_jira_description.py @@ -79,7 +79,7 @@ class EpicDescriptionTest(unittest.TestCase): self.assertEqual(self.calls, []) def test_writing_fails_since_the_store_only_reads(self) -> None: - found = self.store().write("## Goals\n") + found = self.store().write("## Goals\n", None) assert isinstance(found, Failure), found self.assertIn("Jira", found.reason) self.assertEqual(self.calls, []) diff --git a/bin/record_store/test_stores.py b/bin/record_store/test_stores.py index 9bcc448e..e804bf4b 100644 --- a/bin/record_store/test_stores.py +++ b/bin/record_store/test_stores.py @@ -57,19 +57,19 @@ class FileStore(unittest.TestCase): def test_a_write_that_fails_is_a_failure_naming_it(self): (self.folder / "docs").write_text("a file where the folder goes") path = self.folder / "docs" / "decisions.md" - written = stores.FileStore(path, None).write("text\n") + written = stores.FileStore(path, None).write("text\n", None) self.assertIsInstance(written, Failure) self.assertIn(f"cannot write {path}: ", written.reason) def test_a_write_makes_each_folder_the_file_lacks(self): path = self.folder / "records" / "acme" / "decisions.md" - self.assertEqual(stores.FileStore(path, None).write("text\n"), Held("text\n")) + self.assertEqual(stores.FileStore(path, None).write("text\n", None), Held("text\n")) self.assertEqual(path.read_text(), "text\n") def test_a_write_onto_a_folder_is_a_failure_naming_it(self): path = self.folder / "decisions.md" path.mkdir() - written = stores.FileStore(path, None).write("text\n") + written = stores.FileStore(path, None).write("text\n", None) self.assertIsInstance(written, Failure) self.assertIn(f"cannot write {path}: ", written.reason) @@ -113,7 +113,7 @@ class LocalFile(unittest.TestCase): def test_a_file_record_is_its_own_local_file_and_no_copy_is_made(self): record = stores.FileStore(self.folder / "decisions.md", None) - record.write("kept\n") + record.write("kept\n", None) self.assertEqual(stores.local_file(record, self.folder / "copies" / "decisions.md"), self.folder / "decisions.md") self.assertIsNone(stores.stored(record, self.folder / "copies" / "decisions.md")) self.assertEqual((record.read(), (self.folder / "copies").exists()), ("kept\n", False)) diff --git a/bin/release_plan/release_standing.py b/bin/release_plan/release_standing.py index ffd0d233..6a31e4dc 100644 --- a/bin/release_plan/release_standing.py +++ b/bin/release_plan/release_standing.py @@ -1,15 +1,14 @@ """How the plan stands to the last release cut: the next release, whether work has landed towards it, and whether it is to be planned, reviewed or cut; and, from that standing, the item the next chunk takes first, with what the default branch holds for the pick.""" import dataclasses -import re from git_cli.git_repo import commits_since, file_at, git, Git -from plan.release import dated, dated_heading_pattern, newest_tag, parsed, Release, tagged, UNRELEASED_HEADING +from plan.release import dated, dated_heading_pattern, newest_tag, parsed, Release, tagged, UNRELEASED_TITLE from failure import Failure from plan.todo_table import Item, items, next_item, renames from plan_store.plans import Plan from plan_store import stores as plan_stores, plans from project_config.settings import ProjectConfig -from text_files.markdown_text import sections +from text_files.markdown_text import section, sections from record_store import stores from project_config import templates @@ -259,12 +258,9 @@ def unreleased_changes(cut: LastCut) -> int | Failure: def unreleased_entries(changelog: str) -> int | None: - """How many top-level `- ` entries CHANGELOG.md holds under its unreleased section, as UNRELEASED_HEADING heads it; None without that section.""" - heading = UNRELEASED_HEADING.search(changelog) - if heading is None: - return None - body = re.split(r"^## ", changelog[heading.end():], maxsplit=1, flags=re.M)[0] - return sum(line.startswith("- ") for line in body.splitlines()) + """How many top-level `- ` entries CHANGELOG.md holds under its unreleased section, whose heading's title UNRELEASED_TITLE matches; None without that section.""" + body = section(changelog, UNRELEASED_TITLE) + return None if body is None else sum(line.startswith("- ") for line in body) def named_releases(todos: list[str] | Failure) -> set[Release] | Failure: diff --git a/bin/release_plan/test_release_standing.py b/bin/release_plan/test_release_standing.py index c30dfaf1..6287210c 100644 --- a/bin/release_plan/test_release_standing.py +++ b/bin/release_plan/test_release_standing.py @@ -352,6 +352,8 @@ class ChunkItem(unittest.TestCase): self.assertEqual(release_standing.unreleased_entries("# Changelog\n\n## Unreleased\n\n- One.\n - A nested line.\n- Two.\n\n## 0.1.0\n\n- Old.\n"), 2) self.assertEqual(release_standing.unreleased_entries("# Changelog\n\n## Unreleased\n\n## 0.1.0\n\n- Old.\n"), 0) self.assertIsNone(release_standing.unreleased_entries("# Changelog\n\n## 0.1.0\n\n- Old.\n")) + # A heading inside a code fence doesn't end the section. + self.assertEqual(release_standing.unreleased_entries("# Changelog\n\n## Unreleased\n\n- One.\n\n```\n## 0.1.0\n```\n\n- Two.\n\n## 0.1.0\n\n- Old.\n"), 2) # The section todo-item finish files a bullet under, as a project's own changelog may head it. self.assertEqual(release_standing.unreleased_entries("# Changelog\n\n## 0.6.0 (unreleased)\n\n- One.\n\n## 0.5.0\n\n- Old.\n"), 1)