fix(scripts): attribute bumped versions to their own matrix entry

bump_image_versions.py now locates each matrix.include entry and rewrites the
version: line of the entry whose name: it is, whatever the key order; an entry
it cannot parse unambiguously is a one-line error, and the rewrite is verified
against the YAML before it is written. Files are read and written as UTF-8 with
newline="" so CRLF survives. A shallow clone without a merge-base names the fix
(git fetch --unshallow). check_image_versions.py decodes git's stderr, so errors
no longer print a bytes repr. Untracked files that git ignores were already
skipped; a test now pins that.

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 06:39:11 +00:00
1 parent c03e492f81
commit a57714342b
4 files changed
+194 -24

No files matched your search

+88
View File
@@ -3,6 +3,7 @@
"""Run with: python3 -m unittest discover -s tests/scripts"""
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
@@ -55,6 +56,42 @@ class PureTest(unittest.TestCase):
biv.rewrite(IMAGE_YML.format(mol="0.1.0", ci="0.2.0"), {"nope": "1.0.0"})
SHUFFLED_YML = """\
jobs:
image:
strategy:
matrix:
include:
- version: {mol}
subpath: images/mol
name: mol
dockerfile: Dockerfile
- subpath: ""
version: "{ci}"
dockerfile: images/ci/Dockerfile
name: ci
"""
class EntryAttributionTest(unittest.TestCase):
def test_version_before_name_is_attributed_to_its_own_entry(self):
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0")
out = biv.rewrite(text, {"ci": "0.2.1"})
self.assertEqual(out, SHUFFLED_YML.format(mol="0.1.0", ci="0.2.1"))
out = biv.rewrite(text, {"mol": "0.1.1"})
self.assertEqual(out, SHUFFLED_YML.format(mol="0.1.1", ci="0.2.0"))
def test_entry_without_its_own_version_never_borrows_another(self):
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0").replace(' version: "0.2.0"\n', "")
with self.assertRaises(biv.civ.CheckError):
biv.rewrite(text, {"mol": "0.1.1"})
def test_flow_style_entry_is_refused(self):
text = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0") + " - {name: x, version: 1.0.0}\n"
with self.assertRaises(biv.civ.CheckError):
biv.rewrite(text, {"ci": "0.2.1"})
class RepoTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
@@ -141,6 +178,57 @@ class RepoTest(unittest.TestCase):
self.assertNotIn("Traceback", err)
self.assertEqual(len(err.strip().splitlines()), 1, err)
def test_shuffled_key_order_bumps_the_right_entry_end_to_end(self):
self.write(".gitea/workflows/image.yml", SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0"))
self.commit("shuffle")
self.write("requirements-dev.txt", "a==2\n") # an input of ci only
code, out, err = self.bump()
self.assertEqual((code, out), (0, "image versions: bumped ci 0.2.0 -> 0.2.1\n"), err)
self.assertEqual(self.versions(), SHUFFLED_YML.format(mol="0.1.0", ci="0.2.1"))
def test_unparseable_entry_is_a_one_line_failure(self):
flow = SHUFFLED_YML.format(mol="0.1.0", ci="0.2.0") + " - {name: x, version: 1.0.0}\n"
self.write(".gitea/workflows/image.yml", flow)
self.commit("flow")
self.write("requirements-dev.txt", "a==2\n")
code, _, err = self.bump()
self.assertEqual(code, 1)
self.assertEqual(len(err.strip().splitlines()), 1, err)
self.assertEqual(self.versions(), flow)
def test_crlf_line_endings_are_preserved(self):
crlf = IMAGE_YML.format(mol="0.1.0", ci="0.2.0").replace("\n", "\r\n").encode("utf-8")
path = self.repo / ".gitea/workflows/image.yml"
path.write_bytes(crlf)
self.commit("crlf")
self.write("requirements-dev.txt", "a==2\n")
self.assertEqual(self.bump()[0], 0)
self.assertEqual(path.read_bytes(), crlf.replace(b'"0.2.0"', b'"0.2.1"'))
def test_ignored_cache_under_images_is_not_an_input(self):
self.write(".gitignore", "__pycache__/\n")
self.commit("ignore")
self.write("images/mol/__pycache__/x.pyc", "cache\n")
self.assertEqual(self.bump()[1], "image versions: nothing to bump\n")
self.write("images/mol/real-file", "x\n")
self.assertEqual(self.bump()[1], "image versions: bumped mol 0.1.0 -> 0.1.1\n")
def test_shallow_clone_names_the_fix(self):
clone = self.repo.parent / (self.repo.name + "-shallow")
self.addCleanup(shutil.rmtree, clone, True)
self.write("images/mol/Dockerfile", "FROM y\n")
self.commit("two")
self.write("images/mol/Dockerfile", "FROM z\n")
self.commit("three")
run(self.repo.parent, "git", "clone", "-q", "--depth", "1", "--branch", "feature", f"file://{self.repo}",
str(clone))
run(clone, "git", "fetch", "-q", "--depth", "1", "origin", "main:refs/remotes/origin/main")
proc = subprocess.run([sys.executable, str(SCRIPTS / "bump_image_versions.py"), "origin/main"], cwd=clone,
capture_output=True, text=True, check=False)
self.assertEqual(proc.returncode, 1)
self.assertEqual(len(proc.stderr.strip().splitlines()), 1, proc.stderr)
self.assertIn("git fetch --unshallow", proc.stderr)
def test_usage(self):
self.assertEqual(self.bump("a", "b")[0], 2)
@@ -280,6 +280,13 @@ class GitTest(unittest.TestCase):
def test_unknown_base_ref_is_one_line(self):
self.assert_clean_failure(self.bump_args("bump", "nope"), "git merge-base failed")
def test_git_errors_are_decoded_text_not_a_bytes_repr(self):
code, err = self.bump_args("bump", "nope")
self.assertEqual(code, 1)
self.assertNotIn("b'", err)
self.assertNotIn('b"', err)
self.assertIn("fatal:", err)
def test_bad_version_in_matrix_is_one_line(self):
self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="latest", ci="0.2.0"))
self.commit("bad version")