packaging(nightly): read the whole push block, blank lines included

The push: assertion matched contiguous four-space lines, so a path filter after a blank
line (still inside the mapping) escaped it. It now walks the block by indentation and rejects
paths and paths-ignore, with a test for the blank-line case. Carried over from the batch-1
review work in this worktree; the suite passes here (21 tests).
This commit is contained in:
skylartaylor
2026-11-25 00:55:19 +01:00
parent 348634ed14
commit 6b38347326
+55 -7
View File
@@ -296,6 +296,36 @@ class NightlyEnabledCallers(unittest.TestCase):
reviewed_manual_actors(mutated_source, mutated_policy)
ISSUE_METRICS = {"bugs": "BUG", "vulnerabilities": "VULNERABILITY", "code_smells": "CODE_SMELL",
"security_rating": "VULNERABILITY", "new_security_rating": "VULNERABILITY"}
def sonar_issue_scope(sonar):
"""The reviewed issue scope; the pinned validator requires every type to map from an accepted condition."""
mapped = {ISSUE_METRICS[c["metric"]] for c in sonar["accepted_conditions"]}
if not sonar["issue_types"] or mapped != set(sonar["issue_types"]):
raise ValueError("accepted conditions do not map onto the reviewed issue scope")
return set(sonar["issue_types"])
def push_block(source):
"""The whole `push:` mapping, blank lines and nested keys included.
A blank line does not end a YAML mapping, so the block is delimited by indentation rather than
by contiguous non-empty lines; a path filter after a blank line stays visible to the check.
"""
lines = source.splitlines()
for index, line in enumerate(lines):
if re.fullmatch(r" push:.*", line):
block = []
for follow in lines[index + 1:]:
if follow.strip() and not follow.startswith(" "):
break
block.append(follow)
return "\n".join(block)
return None
class NightlyInstalledPolicy(unittest.TestCase):
"""The installed v3 policy matches the callers, the repository and its documented sources."""
@@ -358,16 +388,37 @@ class NightlyInstalledPolicy(unittest.TestCase):
self.assertIs(type(row["workflow_id"]), int)
self.assertNotEqual(row["workflow_path"], self.policy["producer_workflow_path"])
source = (ROOT / row["workflow_path"]).read_text()
push = re.search(r"(?m)^ push:\n((?: .*\n)+)", source)
push = push_block(source)
self.assertIsNotNone(push, "a required workflow must run on main pushes")
self.assertIn(" branches: [main]\n", push[1])
self.assertNotRegex(push[1], r"paths", "a path filter would leave candidates without a run")
self.assertIn("branches: [main]", push)
self.assertNotRegex(push, r"(?m)^\s+paths(?:-ignore)?:", "a path filter would leave candidates without a run")
names = [job["name"] for job in row["jobs"]]
self.assertEqual(len(names), len(set(names)))
for job in row["jobs"]:
self.assertEqual(set(job), {"name", "app_id", "applicability"})
self.assertEqual((job["app_id"], job["applicability"]), (15368, "required"))
def test_a_reviewed_issue_scope_needs_a_matching_accepted_condition(self):
# The pinned nightly_candidate.contract() requires this equality before it reads Sonar, so a
# policy that names an issue type without an accepted condition mapping onto it is refused.
sonar = copy.deepcopy(self.policy["candidate_policy"]["sonar"])
self.assertEqual(sonar_issue_scope(sonar), set(sonar["issue_types"]))
sonar["accepted_conditions"] = []
with self.assertRaises(ValueError):
sonar_issue_scope(sonar)
def test_a_path_filter_after_a_blank_line_is_still_rejected(self):
workflow = "on:\n push:\n branches: [main]\n\n paths: ['src/**']\n workflow_dispatch: {}\n"
block = push_block(workflow)
self.assertIsNotNone(block)
self.assertIn("branches: [main]", block)
self.assertRegex(block, r"(?m)^\s+paths(?:-ignore)?:")
for ignored in ("paths: ['src/**']", "paths-ignore: ['docs/**']"):
with self.subTest(ignored=ignored):
variant = workflow.replace(ignored.split(":")[0] + ": ['src/**']", ignored)
self.assertRegex(push_block(variant), r"(?m)^\s+paths(?:-ignore)?:",
"a quoted or unquoted filter must not hide behind a blank line")
def test_sonar_identity_and_reviewed_scope(self):
sonar = self.policy["candidate_policy"]["sonar"]
properties = dict(line.split("=", 1) for line in (ROOT / "sonar-project.properties").read_text().splitlines()
@@ -376,10 +427,7 @@ class NightlyInstalledPolicy(unittest.TestCase):
self.assertEqual(sonar["project"], properties["sonar.projectKey"])
self.assertEqual((sonar["app_id"], sonar["check_name"]), (12526, "SonarCloud Code Analysis"))
self.assertEqual(sonar["details_url"], "https://sonarcloud.io/dashboard?id=" + sonar["project"] + "&branch=main")
accepted = {"bugs": "BUG", "vulnerabilities": "VULNERABILITY", "code_smells": "CODE_SMELL",
"security_rating": "VULNERABILITY", "new_security_rating": "VULNERABILITY"}
for cond in sonar["accepted_conditions"]:
self.assertIn(accepted[cond["metric"]], sonar["issue_types"])
self.assertEqual(sonar_issue_scope(sonar), set(sonar["issue_types"]))
metrics = [c["metric"] for c in sonar["accepted_conditions"] + sonar["required_conditions"]]
self.assertEqual(len(metrics), len(set(metrics)))
keys = [item["key"] for item in sonar["debt"]]