Skip to content
Open
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
29 changes: 16 additions & 13 deletions scripts/pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
57 changes: 57 additions & 0 deletions scripts/test_pr_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,62 @@ 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**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- 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", "<!-- results to come\n")
self.assertEqual([self.empty(s) for s in ("Tested", "Not tested", "AI use")], self.problems(body))

def test_heading_is_a_line_of_its_own(self):
# a section name in bold at the end of a sentence is not the section's heading
body = self.FILLED.replace("**Tested**\n- `python3 scripts/example.py`: ok\n",
"Nothing here was **Tested**\nas far as I know.\n")
self.assertEqual([self.empty("Tested")], self.problems(body))
# indented, or with spaces after it, it still is
self.assertEqual([], self.problems(self.FILLED.replace("**Tested**\n", " **Tested** \n")))

def test_heading_name_at_the_start_of_a_sentence_is_text(self):
# only a heading on a line of its own ends the section before it
body = self.FILLED.replace("- `python3 scripts/example.py`: ok\n",
"**AI use** below covers the test too; 3 passed\n")
self.assertEqual([], self.problems(body))

def test_text_in_bold_is_text(self):
# a line in bold that is not a heading of the outline does not end the section
body = self.FILLED.replace("- Windows\n", "**AMD**: not run\n")
self.assertEqual([], self.problems(body))

def test_empty_section_before_another_one(self):
body = self.FILLED.replace("- `python3 scripts/example.py`: ok\n", "")
self.assertEqual([self.empty("Tested")], self.problems(body))
self.assertEqual([self.empty("AI use")], self.problems(self.FILLED.replace("- none\n", "")))


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