fix(scripts): read the image matrix without PyYAML
Molecule / molecule (pull_request) Skipped
Build / build (pull_request) Successful in 11s
Lint / lint (pull_request) Successful in 12s

Dry run 2453 ran the bump post-upgrade task in the Renovate image and it
failed with ModuleNotFoundError: yaml, so every bump PR would have opened
without the version bump. matrix_of() is now a small block-style reader that
fails closed on anything else; a test compares it with PyYAML on the real
image.yml and another forbids importing yaml in the scripts.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
Defame1297andClaude Sonnet 5.5 committed 2026-10-26 16:57:08 +00:00
1 parent c8c23687e2
commit eaa38cb03a
3 files changed
+141 -51

No files matched your search

+49 -18
View File
@@ -151,7 +151,7 @@ class PureTest(unittest.TestCase):
for text in ("", "# only a comment\n", "- a\n- b\n", "just a string\n"):
with self.assertRaises(civ.CheckError, msg=repr(text)) as ctx:
civ.matrix_of(text)
self.assertIn("image.yml is empty or not a mapping", str(ctx.exception))
self.assertIn("no jobs.image.strategy.matrix.include list", str(ctx.exception))
def test_semver_is_strict(self):
for bad in ("0.1.0\n", "01.0.0", "1.02.0", "1.0.00", "1.0.0-01", "1.0.0-rc.01", "1.0.0 ", "v1.0.0"):
@@ -170,30 +170,61 @@ class PureTest(unittest.TestCase):
self.assertEqual(len(civ.check_pins(m, {"f": "x: [git.rkdr.net/mortis/ci:0.1.0@sha256:abc, y]"})), 1)
def test_matrix_shape_errors(self):
def doc(include):
return "jobs:\n image:\n strategy:\n matrix:\n include: " + include + "\n"
def doc(body):
return "jobs:\n image:\n strategy:\n matrix:\n include:\n" + body
entry = "{name: a, version: '0.1.0', dockerfile: D"
entry = " - name: a\n version: '0.1.0'\n dockerfile: D\n"
cases = {
"[]": "non-empty list",
"{}": "non-empty list",
"[1]": "not a mapping",
"[{name: 1, version: '0.1.0', dockerfile: D}]": "name must be a non-empty string",
"[" + entry + ", subpath: 5}]": "subpath must be a string",
"[" + entry + ", subpath: [x]}]": "subpath must be a string",
"[{name: a, version: 0.1, dockerfile: D}]": "version must be a string",
"[" + entry + "}, " + entry + "}]": "duplicate image name 'a'",
"": "non-empty list",
" []\n": "non-empty block list",
" - 1\n": "not a mapping",
" - name: 1\n version: '0.1.0'\n dockerfile: D\n": "name must be a non-empty string",
entry + " subpath: [x]\n": "cannot read the value",
entry.replace("'0.1.0'", "0.1") + "": "version must be a string",
entry + entry: "duplicate image name 'a'",
entry + " version: '0.2.0'\n": "duplicate key 'version'",
entry.replace("version: '0.1.0'", "version: *anchor"): "cannot read the value",
entry.replace("version: '0.1.0'", "version: \"0\\x2e1.0\""): "cannot read the quoted value",
entry.replace(" - name: a\n", " - name: ''\n"): "name must be a non-empty string",
" - name: a\n dockerfile: D\n": "lacks the key 'version'",
}
for include, text in cases.items():
with self.assertRaises(civ.CheckError, msg=include) as ctx:
civ.matrix_of(doc(include))
self.assertIn(text, str(ctx.exception))
for body, text in cases.items():
with self.assertRaises(civ.CheckError, msg=body) as ctx:
civ.matrix_of(doc(body))
self.assertIn(text, str(ctx.exception), body)
self.assertNotIn("\n", str(ctx.exception))
self.assertEqual(civ.matrix_of(doc("[" + entry + ", subpath: }]"))["a"]["subpath"], "")
self.assertEqual(civ.matrix_of(doc(entry + " subpath:\n"))["a"]["subpath"], "")
with self.assertRaises(civ.CheckError) as ctx:
civ.matrix_of("jobs: [unclosed\n")
self.assertNotIn("\n", str(ctx.exception))
def test_matrix_reader_accepts_comments_quotes_and_key_order(self):
text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0")
text = text.replace(" - name: ci\n version: 0.2.0\n",
" # the ci image\n - version: \"0.2.0\" # bumped by the bot\n"
" name: ci\n")
self.assertEqual(civ.matrix_of(text)["ci"]["version"], "0.2.0")
self.assertEqual(civ.matrix_of(text)["mol"]["subpath"], "images/mol")
def test_matrix_reader_agrees_with_pyyaml_on_the_real_file(self):
# The reader replaces PyYAML in the Renovate image (which has none); here PyYAML is the oracle.
import yaml
real = (pathlib.Path(__file__).resolve().parents[2] / civ.MATRIX_FILE).read_text()
want = {e["name"]: {"version": e["version"], "subpath": e.get("subpath") or "", "dockerfile": e["dockerfile"]}
for e in yaml.safe_load(real)["jobs"]["image"]["strategy"]["matrix"]["include"]}
self.assertEqual(civ.matrix_of(real), want)
self.assertEqual(civ.matrix_of(IMAGE_YML.format(mol="0.1.0", ci="0.2.0")),
{e["name"]: {"version": e["version"], "subpath": e.get("subpath") or "",
"dockerfile": e["dockerfile"]}
for e in yaml.safe_load(IMAGE_YML.format(mol="0.1.0", ci="0.2.0"))
["jobs"]["image"]["strategy"]["matrix"]["include"]})
def test_scripts_do_not_import_yaml(self):
# The bump step runs in the Renovate image, whose python3 has no PyYAML (dry run 2453).
for script in ("check_image_versions.py", "bump_image_versions.py"):
source = (pathlib.Path(__file__).resolve().parents[2] / "scripts" / script).read_text()
self.assertNotRegex(source, r"(?m)^\s*(import|from)\s+yaml\b", script)
def test_bump_logic(self):
def read(_path):
return CI_DOCKERFILE
@@ -326,7 +357,7 @@ class GitTest(unittest.TestCase):
def test_empty_image_yml_says_so(self):
self.write(".gitea/workflows/image.yml", "")
self.commit("empty matrix")
self.assert_clean_failure(self.bump(), "image.yml is empty or not a mapping")
self.assert_clean_failure(self.bump(), "no jobs.image.strategy.matrix.include list")
def test_unsupported_dockerfile_source_fails_with_file_and_line(self):
self.write("images/ci/Dockerfile", "FROM x\nCOPY $SRC ./\n")