# SPDX-FileCopyrightText: Defame1297 and contributors # SPDX-License-Identifier: GPL-3.0-or-later """Run with: python3 -m unittest discover -s tests/scripts""" import os import pathlib import shutil import subprocess import sys import tempfile import unittest SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts" sys.path.insert(0, str(SCRIPTS)) import bump_image_versions as biv # noqa: E402 IMAGE_YML = """\ # header comment jobs: image: strategy: matrix: include: - name: mol version: {mol} # keep me subpath: images/mol dockerfile: Dockerfile - name: ci version: "{ci}" subpath: "" dockerfile: images/ci/Dockerfile """ CI_DOCKERFILE = "FROM x AS b\nCOPY requirements-dev.txt .pre-commit-config.yaml ./\n" def run(cwd, *cmd): return subprocess.run(cmd, cwd=cwd, check=True, capture_output=True, text=True, env={**os.environ, "GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_SYSTEM": "/dev/null"}) class PureTest(unittest.TestCase): def test_next_patch(self): self.assertEqual(biv.next_patch("0.2.9"), "0.2.10") self.assertEqual(biv.next_patch("1.0.0-rc.1"), "1.0.1") with self.assertRaises(ValueError): biv.next_patch("latest") def test_rewrite_changes_only_the_version_line(self): text = IMAGE_YML.format(mol="0.1.0", ci="0.2.0") out = biv.rewrite(text, {"mol": "0.1.1", "ci": "0.2.1"}) self.assertEqual(out, IMAGE_YML.format(mol="0.1.1", ci="0.2.1")) self.assertIn("version: 0.1.1 # keep me\n", out) self.assertIn('version: "0.2.1"\n', out) def test_rewrite_unknown_image_fails(self): with self.assertRaises(biv.civ.CheckError): 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() self.repo = pathlib.Path(self.tmp.name) self.addCleanup(self.tmp.cleanup) run(self.repo, "git", "init", "-q", "-b", "main") run(self.repo, "git", "config", "user.email", "t@example.org") run(self.repo, "git", "config", "user.name", "t") self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.1.0", ci="0.2.0")) self.write("images/ci/Dockerfile", CI_DOCKERFILE) self.write("images/mol/Dockerfile", "FROM x\n") self.write("requirements-dev.txt", "a==1\n") self.write(".pre-commit-config.yaml", "repos: []\n") self.commit("base") run(self.repo, "git", "checkout", "-q", "-b", "feature") def write(self, rel, text): p = self.repo / rel p.parent.mkdir(parents=True, exist_ok=True) p.write_text(text) def commit(self, msg): run(self.repo, "git", "add", "-A") run(self.repo, "git", "commit", "-q", "-m", msg) def script(self, name, *args): proc = subprocess.run([sys.executable, str(SCRIPTS / name), *args], cwd=self.repo, capture_output=True, text=True, check=False) return proc.returncode, proc.stdout, proc.stderr def bump(self, *args): return self.script("bump_image_versions.py", *(args or ("main",))) def versions(self): return (self.repo / ".gitea/workflows/image.yml").read_text() def test_nothing_changed(self): code, out, unused = self.bump() self.assertEqual((code, out), (0, "image versions: nothing to bump\n")) self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.0")) def test_bumps_exactly_the_changed_image_and_check_passes(self): self.write("requirements-dev.txt", "a==2\n") # an input of ci only code, out, err = self.bump() self.assertEqual(code, 0, err) self.assertEqual(out, "image versions: bumped ci 0.2.0 -> 0.2.1\n") self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.0", ci="0.2.1")) self.commit("deps and bump") self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 0) def test_check_fails_without_the_bump(self): self.write("images/mol/Dockerfile", "FROM y\n") self.commit("input only") self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 1) self.assertEqual(self.bump()[0], 0) self.commit("bump") self.assertEqual(self.script("check_image_versions.py", "bump", "main")[0], 0) def test_idempotent_and_committed_changes_count(self): self.write("images/mol/Dockerfile", "FROM y\n") self.commit("input") first = self.bump() self.assertEqual(first[1], "image versions: bumped mol 0.1.0 -> 0.1.1\n") second = self.bump() self.assertEqual(second[1], "image versions: nothing to bump\n") self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.1.1", ci="0.2.0")) def test_higher_version_already_present_is_kept(self): self.write("images/mol/Dockerfile", "FROM y\n") self.write(".gitea/workflows/image.yml", IMAGE_YML.format(mol="0.3.0", ci="0.2.0")) self.assertEqual(self.bump()[1], "image versions: nothing to bump\n") self.assertEqual(self.versions(), IMAGE_YML.format(mol="0.3.0", ci="0.2.0")) def test_both_images_and_untracked_input(self): self.write("images/mol/new-file", "x\n") # untracked self.write("requirements-dev.txt", "a==2\n") out = self.bump()[1] self.assertEqual(out, "image versions: bumped ci 0.2.0 -> 0.2.1, mol 0.1.0 -> 0.1.1\n") def test_bad_base_ref_is_a_clean_failure(self): for ref in ("--output=x", "nope"): code, unused, err = self.bump(ref) self.assertEqual(code, 1) 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, unused, 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) if __name__ == "__main__": unittest.main()