diff --git a/scripts/pr_check.py b/scripts/pr_check.py index 3a526b4a9d..9c1cfbf396 100644 --- a/scripts/pr_check.py +++ b/scripts/pr_check.py @@ -66,21 +66,24 @@ def builds_runtime(path): return path.startswith("crates/xcl/data/") or path in RUNTIME_BUILD +def section_texts(body, section): + """What is written under each `**section**` heading of `body`, up to the next heading. A + heading is a line of its own. Comments are taken out first, so a heading or a line inside + one is not there; an unclosed comment hides the rest of the description, as on GitHub.""" + body = re.sub(r"|\Z)", "", body, flags=re.S) + names = "|".join(re.escape(h) for h in HEADINGS) + end = r"(?=\n[ \t]*\*\*(?:" + names + r")\*\*[ \t]*(?:\n|\Z)|\Z)" + start = r"(?:^|\n)[ \t]*\*\*" + re.escape(section) + r"\*\*[ \t]*(?=\n)" + return [m.group(1) for m in re.finditer(start + r"(.*?)" + end, body, re.S)] + + def section_has_content(body, section): """True when `**section**` exists and has at least one non-placeholder line.""" - next_heading = "|".join(re.escape(h) for h in HEADINGS) - m = re.search( - r"\*\*" + re.escape(section) + r"\*\*[ \t]*\n(.*?)(?=\n\*\*(?:" + next_heading + r")\*\*|\Z)", - body, - re.S, - ) - if not m: - return False - text = re.sub(r"", "", m.group(1), flags=re.S) - for raw in text.splitlines(): - line = re.sub(r"^\s*[-*•]+\s*", "", raw).strip() - if line and line not in TEMPLATE_PLACEHOLDERS: - return True + for text in section_texts(body, section): + for raw in text.splitlines(): + line = re.sub(r"^\s*[-*•]+\s*", "", raw).strip() + if line and line not in TEMPLATE_PLACEHOLDERS: + return True return False @@ -94,8 +97,8 @@ def check(body, head, files, is_main_ancestor, *, test_only=False): if not section_has_content(body, section): problems.append(f"the **{section}** section is missing or empty") - ai = re.search(r"\*\*AI use\*\*(.*?)(\n\*\*|\Z)", body, re.S) - banned = re.findall(BANNED_MODELS, ai.group(1), re.I) if ai else [] + # the same text the section check reads: what a reader of the description sees under AI use + banned = re.findall(BANNED_MODELS, "\n".join(section_texts(body, "AI use")), re.I) if banned: problems.append("**AI use** names a model this repository does not accept for code (" + ", ".join(sorted({b.lower() for b in banned})) + "): see CONTRIBUTING.md") diff --git a/scripts/test_pr_check.py b/scripts/test_pr_check.py index e5de366a54..5473913c5a 100644 --- a/scripts/test_pr_check.py +++ b/scripts/test_pr_check.py @@ -97,5 +97,101 @@ def test_bare_bullets_and_html_comments_only_fail(self): self.assertEqual(3, len(problems)) +class SectionHeadingTests(unittest.TestCase): + """Where a required section starts and ends, and what counts as its text.""" + + FILLED = ("A change to a script.\n\n**What is in it**\n- `scripts/example.py`: the change\n\n" + "**Tested**\n- `python3 scripts/example.py`: ok\n\n**Not tested**\n- Windows\n\n" + "**AI use**\n- none\n") + + def problems(self, body): + return check(body, HEAD, [changed("scripts/example.py")], lambda _: False) + + @staticmethod + def empty(section): + return f"the **{section}** section is missing or empty" + + def test_filled_description_passes(self): + self.assertEqual([], self.problems(self.FILLED)) + self.assertEqual([], self.problems(self.FILLED.replace("\n", "\r\n"))) + + def test_section_inside_a_comment_is_not_there(self): + # the heading and its only result, both commented out + body = self.FILLED.replace("**Tested**\n- `python3 scripts/example.py`: ok\n", + "\n") + self.assertEqual([self.empty("Tested")], self.problems(body)) + # the heading is there, its only result is commented out across lines + body = self.FILLED.replace("- Windows\n", "\n") + self.assertEqual([self.empty("Not tested")], self.problems(body)) + + def test_unclosed_comment_hides_what_follows(self): + # GitHub shows nothing after an unclosed comment: the sections after it are not there + body = self.FILLED.replace("- `python3 scripts/example.py`: ok\n", "\n" + self.QWEN + self.assertEqual([self.banned("qwen")], self.problems(body)) + body = self.QWEN.replace("- `scripts/example.py`: the change\n", + "- the **AI use** section is short\n") + self.assertEqual([self.banned("qwen")], self.problems(body)) + + def test_bold_line_does_not_end_the_section(self): + body = self.FILLED.replace("- none\n", "- Claude Code\n**also** Qwen3 for the tests\n") + self.assertEqual([self.banned("qwen")], self.problems(body)) + + def test_comment_is_not_what_was_used(self): + body = self.FILLED.replace("- none\n", "\n- Claude Code\n") + self.assertEqual([], self.problems(body)) + + def test_model_in_another_section_is_not_ai_use(self): + body = self.FILLED.replace("- Windows\n", "- nothing with Llama\n") + self.assertEqual([], self.problems(body)) + + if __name__ == "__main__": unittest.main()