# SPDX-FileCopyrightText: Defame1297 and contributors # SPDX-License-Identifier: GPL-3.0-or-later """Run with: python3 -m unittest discover -s tests/scripts""" import pathlib import sys import tempfile import unittest SCRIPTS = pathlib.Path(__file__).resolve().parents[2] / "scripts" sys.path.insert(0, str(SCRIPTS)) import next_image_version as niv def no_label(tag): raise AssertionError(f"no label lookup expected, got {tag}") class NextVersion(unittest.TestCase): def test_first_version_of_a_base_is_patch_zero(self): self.assertEqual( niv.next_version("0.3", ["0.2.38", "latest"], "c1", no_label), "0.3.0" ) def test_changed_inputs_raise_the_numerically_newest_patch(self): seen = [] def label_of(tag): seen.append(tag) return "old" tags = ["0.2.9", "0.2.10", "0.20.99", "1.2.11", "0.2.10-rc1", "latest"] self.assertEqual(niv.next_version("0.2", tags, "new", label_of), "0.2.11") self.assertEqual(seen, ["0.2.10"]) def test_unchanged_inputs_publish_nothing(self): self.assertIsNone( niv.next_version("0.1", ["0.1.4"], "same", lambda tag: "same") ) def test_an_unlabelled_newest_version_counts_as_changed(self): self.assertEqual( niv.next_version("0.1", ["0.1.4"], "c", lambda tag: None), "0.1.5" ) CI = {"subpath": "", "dockerfile": "images/ci/Dockerfile"} MOL = {"subpath": "images/mol", "dockerfile": "Dockerfile"} class InputChecksum(unittest.TestCase): def setUp(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.root = pathlib.Path(tmp.name) self.write("images/ci/Dockerfile", "FROM ubuntu:26.04\nCOPY pins.txt ./\n") self.write("images/mol/Dockerfile", "FROM ubuntu:24.04\n") self.write("pins.txt", "a==1\n") self.write("README.md", "readme\n") def write(self, path, text): target = self.root / path target.parent.mkdir(parents=True, exist_ok=True) target.write_text(text, encoding="utf-8") def checksum(self, entry): return niv.input_checksum(self.root, entry) def test_a_copied_file_is_an_input_and_an_unrelated_file_is_not(self): before = self.checksum(CI) self.write("README.md", "changed\n") self.assertEqual(self.checksum(CI), before) self.write("pins.txt", "a==2\n") self.assertNotEqual(self.checksum(CI), before) def test_the_build_context_of_a_subpath_image_is_its_input(self): before = self.checksum(MOL) self.write("pins.txt", "a==2\n") self.assertEqual(self.checksum(MOL), before) self.write("images/mol/files/new.conf", "x\n") self.assertNotEqual(self.checksum(MOL), before) def test_a_mode_change_is_an_input_change(self): before = self.checksum(CI) (self.root / "pins.txt").chmod(0o755) self.assertNotEqual(self.checksum(CI), before) def test_a_renamed_input_is_an_input_change(self): self.write("images/mol/a.conf", "x\n") before = self.checksum(MOL) (self.root / "images/mol/a.conf").rename(self.root / "images/mol/b.conf") self.assertNotEqual(self.checksum(MOL), before) if __name__ == "__main__": unittest.main()