fix(ci): make check_image_versions pass ansible-test sanity and harden its input handling
Sanity (pylint, shebang) failed the Build check: drop the shebang and the executable bit, simplify a comprehension, define functions instead of assigning lambdas and pass check= to every subprocess.run. Read the changed files with git diff -z so non-ASCII paths are compared unquoted. Validate the matrix shape (non-empty list of mappings, string name/version/dockerfile/subpath, no duplicate names) with one-line errors. Match semver with fullmatch and reject leading zeros. Let a pin end at , ) ] } or a quote. Document the known limits. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
8a0c5bf3dd
commit
4fabe1358e
2 files changed
+102
-20
No files matched your search
@@ -84,8 +84,52 @@ class PureTest(unittest.TestCase):
|
||||
self.assertEqual(len(civ.check_pins(m, {"f": "git.rkdr.net/mortis/ci:0.2.0"})), 1)
|
||||
self.assertEqual(len(civ.check_pins(m, {"f": "git.rkdr.net/mortis/ci@sha256:abc"})), 1)
|
||||
|
||||
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"):
|
||||
with self.assertRaises(ValueError, msg=repr(bad)):
|
||||
civ.parse_version(bad)
|
||||
for good in ("0.0.0", "1.0.0-0", "1.0.0-0a", "1.0.0-rc.0", "10.20.30"):
|
||||
civ.parse_version(good)
|
||||
|
||||
def test_pin_followed_by_punctuation_is_parsed(self):
|
||||
m = self.matrix()
|
||||
for tail in (",", ")", "]", "}", "'", '"', ";"):
|
||||
text = f"x: [git.rkdr.net/mortis/ci:0.1.0@{DIGEST}{tail if tail != ';' else ''} y]\n"
|
||||
self.assertEqual(civ.check_pins(m, {"f": text}), [], tail)
|
||||
ahead = f"x: (git.rkdr.net/mortis/ci:0.3.0@{DIGEST}), z\n"
|
||||
self.assertEqual(len(civ.check_pins(m, {"f": ahead})), 1)
|
||||
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"
|
||||
|
||||
entry = "{name: a, version: '0.1.0', dockerfile: D"
|
||||
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'",
|
||||
}
|
||||
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))
|
||||
self.assertNotIn("\n", str(ctx.exception))
|
||||
self.assertEqual(civ.matrix_of(doc("[" + entry + ", subpath: }]"))["a"]["subpath"], "")
|
||||
with self.assertRaises(civ.CheckError) as ctx:
|
||||
civ.matrix_of("jobs: [unclosed\n")
|
||||
self.assertNotIn("\n", str(ctx.exception))
|
||||
|
||||
def test_bump_logic(self):
|
||||
base, read = self.matrix(), lambda p: CI_DOCKERFILE
|
||||
def read(_path):
|
||||
return CI_DOCKERFILE
|
||||
|
||||
base = self.matrix()
|
||||
same = self.matrix()
|
||||
for path in ("images/mol/Dockerfile", "images/ci/Dockerfile", "requirements-dev.txt", ".pre-commit-config.yaml"):
|
||||
self.assertTrue(civ.check_bump(base, same, [path], read), path)
|
||||
@@ -123,12 +167,12 @@ class GitTest(unittest.TestCase):
|
||||
|
||||
def bump(self):
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_image_versions.py"), "bump", "main"],
|
||||
cwd=self.repo, capture_output=True, text=True)
|
||||
cwd=self.repo, capture_output=True, text=True, check=False)
|
||||
return proc.returncode, proc.stderr
|
||||
|
||||
def bump_args(self, *args, cwd=None):
|
||||
proc = subprocess.run([sys.executable, str(SCRIPTS / "check_image_versions.py"), *args],
|
||||
cwd=cwd or self.repo, capture_output=True, text=True)
|
||||
cwd=cwd or self.repo, capture_output=True, text=True, check=False)
|
||||
return proc.returncode, proc.stderr
|
||||
|
||||
def assert_clean_failure(self, result, text):
|
||||
@@ -177,6 +221,14 @@ class GitTest(unittest.TestCase):
|
||||
run(self.repo, "git", "rm", "-q", ".gitea/workflows/image.yml")
|
||||
self.assert_clean_failure(self.bump_args("pins"), "cannot read a file")
|
||||
|
||||
def test_non_ascii_input_path_counts(self):
|
||||
self.write("images/mol/caf\u00e9", "x\n")
|
||||
self.commit("non-ascii input")
|
||||
code, err = self.bump()
|
||||
self.assertEqual(code, 1, err)
|
||||
self.assertIn("image 'mol'", err)
|
||||
self.assertIn("caf\u00e9", err)
|
||||
|
||||
def test_unrelated_change_passes(self):
|
||||
self.write("README.md", "x\n")
|
||||
self.commit("docs")
|
||||
|
||||
Reference in new issue
Block a user