Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions scripts/pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,36 @@ def builds_runtime(path):
return path.startswith("crates/xcl/data/") or path in RUNTIME_BUILD


def heading(names):
"""A heading of one of `names`, in either case: `**Tested**` as in the template, or a markdown
heading on a line of its own (`## Tested`, `### **Tested**`)."""
alt = "|".join(re.escape(n) for n in names)
return (r"(?im:\*\*(?:" + alt + r")\*\*|^[ \t]*#{1,6}[ \t]+(?:\*\*)?(?:" + alt
+ r")(?:\*\*)?[ \t]*#*[ \t]*$)")


def mask_fences(body):
"""`body` with each line inside a fenced code block (``` or ~~~) replaced by as many backticks,
so a heading quoted in a fence is no heading, while the fence still counts as a section's text
and every offset stays where it was."""
out, fence = [], None
for line in body.split("\n"):
m = re.match(r"[ ]{0,3}(`{3,}|~{3,})", line)
if fence is None:
if m and not (m.group(1)[0] == "`" and "`" in line[m.end():]):
fence = m.group(1)
out.append(line)
elif m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence) and not line[m.end():].strip():
fence = None
out.append(line)
else:
out.append("`" * len(line))
return "\n".join(out)


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,
)
"""True when the `**section**` heading exists and has at least one non-placeholder line."""
m = re.search(heading([section]) + r"[ \t]*\n(.*?)(?=\n" + heading(HEADINGS) + r"|\Z)", body, re.S)
if not m:
return False
text = re.sub(r"<!--.*?-->", "", m.group(1), flags=re.S)
Expand All @@ -105,13 +127,18 @@ def check(body, head, files, is_main_ancestor, *, test_only=False):
size) with `size` a function giving the file's bytes; `is_main_ancestor(sha)`: whether the
commit is on main."""
body = body.replace("\r\n", "\n")
masked = mask_fences(body)
problems = []
for section in SECTIONS:
if not section_has_content(body, section):
if not section_has_content(masked, 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 **AI use** text runs to the next section of the template, not to a sub-heading of its own
# (`### Tools`); a fence in it is read as written
banned = []
for ai in re.finditer(heading(["AI use"]), masked):
end = re.compile(r"\n" + heading(HEADINGS)).search(masked, ai.end())
banned += re.findall(BANNED_MODELS, body[ai.end():end.start() if end else len(body)], 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")
Expand Down
56 changes: 56 additions & 0 deletions scripts/test_pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,61 @@ def test_bare_bullets_and_html_comments_only_fail(self):
self.assertEqual(3, len(problems))


class HeadingFormTests(unittest.TestCase):
"""Markdown headings and other cases of the section names count as the template's bold ones."""

def problems(self, body):
return check(body, HEAD, [changed("README.md")], lambda _: True)

def test_markdown_headings_and_any_case_pass(self):
for tested, not_tested, ai in (("## Tested", "## Not Tested", "## AI Use"),
("### **Tested**", "# not tested #", "###### AI use"),
("**TESTED**", "**Not Tested**", "**AI Use**")):
with self.subTest(heading=tested):
body = f"## What is in it\n- x\n{tested}\nSynthetic fixtures\n{not_tested}\nRetail titles\n{ai}\nNone\n"
self.assertEqual([], self.problems(body))

def test_empty_markdown_sections_still_fail(self):
body = "## Tested\n-\n## Not tested\n<!-- todo -->\n## AI use\n"
self.assertEqual(3, len(self.problems(body)))

def test_markdown_heading_ends_the_section_before_it(self):
body = "## Tested\n\n## Not tested\nRetail titles\n## AI use\nNone\n"
self.assertEqual(["the **Tested** section is missing or empty"], self.problems(body))

def test_heading_words_in_a_sentence_are_not_headings(self):
body = "## Tested things\nok\n## Not tested\nRetail titles\n## AI use\nNone\n"
self.assertEqual(["the **Tested** section is missing or empty"], self.problems(body))

def test_banned_model_under_a_markdown_heading_is_refused(self):
body = "## Tested\nSynthetic fixtures\n## Not tested\nRetail titles\n## AI Use\n- Qwen 3\n"
problems = self.problems(body)
self.assertEqual(1, len(problems), problems)
self.assertIn("does not accept", problems[0])

def test_banned_model_under_a_sub_heading_of_ai_use_is_refused(self):
# RobertAndreiGit on #1645: a `### Tools` heading in **AI use** hid the model from the check
for ai in ("**AI use**", "## AI use"):
with self.subTest(heading=ai):
body = f"**Tested**\nSynthetic fixtures\n**Not tested**\nRetail titles\n{ai}\n### Tools\nQwen\n"
problems = self.problems(body)
self.assertEqual(1, len(problems), problems)
self.assertIn("qwen", problems[0])

def test_headings_inside_a_code_fence_are_not_sections(self):
# RobertAndreiGit on #1645: a fenced example of a description passed as the description
example = "## Tested\nSynthetic fixtures\n## Not tested\nRetail titles\n## AI use\nNone\n"
for opening, closing in (("```", "```"), ("~~~", "~~~"), ("````markdown", "````")):
with self.subTest(fence=opening):
body = f"An example:\n{opening}\n{example}{closing}\n"
self.assertEqual(3, len(self.problems(body)))

def test_a_fence_in_a_section_is_its_text(self):
body = "## Tested\n```\n## Not tested\n18 passed\n```\n## Not tested\nRetail titles\n## AI use\nNone\n"
self.assertEqual([], self.problems(body))
body = "## Tested\nok\n## Not tested\nRetail titles\n## AI use\n```\nqwen\n```\n"
self.assertEqual(1, len(self.problems(body)))


if __name__ == "__main__":
unittest.main()
Loading