From afe631efdc62cfdadb01094b587f2d8322543b94 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Mon, 21 Sep 2026 07:58:39 -0700 Subject: [PATCH] Collapse Whitespace Before Pinning a Sentence in the Docstring Tests Closes #1714. Four contract tests pin a sentence against the raw text of `pr_review.py`'s module docstring. That docstring is hand-wrapped near 100 columns, so adding a word earlier in a paragraph moves the wrap, splits a pinned sentence across two lines, and fails a test that makes no claim about the edit. `flowed_text` collapses whitespace runs before the match, so those four assertions pin the docstring's words rather than its line breaks. ## What the issue assumed, and what is actually exposed #1714 names three assertions reading `.github/copilot-instructions.md` and one shape in `spec/validate.py`. Neither is exposed. That runbook and `GOVERNANCE.md` are one logical paragraph per line, longest lines 989 and 3483 characters. A paragraph there is already a single line, so an edit cannot rewrap a neighboring one, and those assertions are left reading raw. Collapsing them would also have cost something: it joins paragraphs, headings and fenced blocks into one line, letting a needle match across a boundary it should not cross. `spec/validate.py`'s `\.github/skills/[A-Za-z0-9_-]+/SKILL\.md` carries no whitespace and its character class excludes whitespace, so no rewrap can split a match. Run over the runbook rewrapped at 40, 60, 80 and 100 columns it returned the identical result each time. Unchanged. ## The coverage marker reads raw, and that is its subject `read_coverage` runs once per line, so a marker split over two lines yields no coverage statement at all and the round reads as stating no coverage. Being one line is what that assertion is for, and it sits in a fenced block besides. The reason is now written beside it, because collapsing it looked like an improvement until the per-line reader was checked. ## Nothing enforces the docstring's wrap `pyproject.toml` sets `line-length = 100`, but the lint selection is ruff's default set plus `I`, which excludes E501, and the formatter does not reflow docstring prose. Measured: a 206-character docstring line passes `ruff check`, and `ruff format` changes only the placement of its closing quotes. The wrap is a hand convention, which is exactly why an editor rewraps it by hand and the raw pin breaks. ## Left for their own issues #1732 covers a regex pinning a phrase inside a `#` comment, where a rewrap interleaves the continuation's `#` and collapsing cannot help. Two further assertions pinning a whitespace-bearing needle against wrappable code rather than prose are recorded there too. Verified: 1641 tests pass, ruff format and check, mypy, prose_lint against the diff, the eol gate, and spec/validate.py all clean. --- scripts/tests/test_pr_review.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/scripts/tests/test_pr_review.py b/scripts/tests/test_pr_review.py index 9d027972d..0796b58d8 100755 --- a/scripts/tests/test_pr_review.py +++ b/scripts/tests/test_pr_review.py @@ -33,6 +33,19 @@ REPO = Path(__file__).resolve().parent.parent.parent RUNBOOK = REPO / ".github" / "copilot-instructions.md" GOVERNANCE = REPO / "GOVERNANCE.md" +CODE_REVIEW_SKILL = REPO / ".github" / "skills" / "code-review" / "SKILL.md" + + +def flowed_text(text: str) -> str: + """Collapse every whitespace run in `text` to one space, stripping the ends. + + A hard-wrapped body rewraps when a word is added earlier in the paragraph, so a sentence + pinned against its raw text fails on an edit making no claim about it. Collapsing pins the + words rather than the line breaks. An assertion whose subject is the layout itself reads + raw instead, since collapsing would let a differently wrapped or indented line match. + """ + return " ".join(text.split()) + HEAD = "a" * 40 OLD = "b" * 40 @@ -5491,18 +5504,19 @@ def test_gh_being_absent_does_not_raise(self) -> None: class TestContract(unittest.TestCase): def test_status_documents_its_no_review_success_case(self) -> None: - self.assertIn("Exit 0 = no Copilot review covers the head yet", pr_review.__doc__ or "") + doc = flowed_text(pr_review.__doc__ or "") + self.assertIn("Exit 0 = no Copilot review covers the head yet", doc) def test_status_documents_review_on_head_as_copilot_scoped(self) -> None: """`review_on_head=NO` alongside a genuine `other_reviewed` head is a scoping fact, not a coverage gap, the exact confusion that reached this docstring as a filed issue. """ - doc = pr_review.__doc__ or "" + doc = flowed_text(pr_review.__doc__ or "") self.assertIn('never "no review of any kind covers this head"', doc) self.assertIn("not a gap", doc) def test_status_documents_the_coderabbit_and_qodo_finding_fields(self) -> None: - doc = pr_review.__doc__ or "" + doc = flowed_text(pr_review.__doc__ or "") self.assertIn("cr_outside_diff=N (on_head=X earlier=Y)", doc) self.assertIn("qodo_open=N", doc) @@ -5518,11 +5532,10 @@ def test_the_runbook_forbids_suppressed_findings(self) -> None: def test_the_runbook_publishes_the_machine_readable_coverage_marker(self) -> None: """The instructed shape is stable while the parser retains legacy prose readers.""" - text = (REPO / ".github" / "skills" / "code-review" / "SKILL.md").read_text( - encoding="utf-8" - ) marker = "" - self.assertIn(marker, text) + # Raw, because `read_coverage` is called once per line. + # A marker split over two lines yields no statement, so one line is what is asserted. + self.assertIn(marker, CODE_REVIEW_SKILL.read_text(encoding="utf-8")) self.assertIsNotNone(pr_review.read_coverage(marker.replace("N", "1"))) def test_the_runbook_names_partial_coverage_as_a_state_that_blocks_a_merge(self) -> None: @@ -5668,7 +5681,7 @@ def test_the_documented_exit_codes_are_the_ones_the_code_returns(self) -> None: of it, which is the direction that costs a reader trust in the field. Raised in review here. This holds the docstring to naming the condition rather than only the shapes. """ - doc = pr_review.__doc__ or "" + doc = flowed_text(pr_review.__doc__ or "") self.assertIn("44 =", doc) self.assertIn("BLOCKED", doc) # Every shape the digest can print is named where the code can return 44 for it.