import importlib.util import json from pathlib import Path import subprocess import tempfile import unittest from unittest import mock SPEC = importlib.util.spec_from_file_location("qualify", Path(__file__).with_name("qualify.py")) CI = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(CI) class QualificationTests(unittest.TestCase): def setUp(self): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) / "source" self.root.mkdir() self.git("init", "--quiet") self.git("config", "user.name", "CI Test") self.git("config", "user.email", "ci@example.invalid") (self.root / "source.txt").write_text("committed\n") self.git("add", "source.txt") self.git("commit", "--quiet", "-m", "fixture") self.commit = self.git("rev-parse", "HEAD").strip() def git(self, *args): return subprocess.check_output(["git", *args], cwd=self.root, text=True) def test_clean_exact_commit_is_accepted(self): CI.verify_checkout(self.root, self.commit) def test_stale_commit_is_rejected(self): with self.assertRaisesRegex(ValueError, "source_commit_mismatch"): CI.verify_checkout(self.root, "a" * 40) def test_branch_name_is_not_a_release_identity(self): with self.assertRaisesRegex(ValueError, "full_commit_sha_required"): CI.verify_checkout(self.root, "main") def test_local_edits_cannot_be_credited_to_a_commit(self): (self.root / "source.txt").write_text("uncommitted\n") with self.assertRaisesRegex(ValueError, "dirty_source_checkout"): CI.verify_checkout(self.root, self.commit) def test_untracked_source_is_rejected(self): (self.root / "extra.mjs").write_text("export default 1") with self.assertRaisesRegex(ValueError, "dirty_source_checkout"): CI.verify_checkout(self.root, self.commit) def test_parent_git_repository_is_not_accepted_as_a_dependency(self): child = self.root / "child" child.mkdir() with self.assertRaisesRegex(ValueError, "independent_checkout_required"): CI.verify_checkout(child, self.commit) def test_existing_operator_dependencies_are_never_overwritten(self): dependency = self.root.parent / "NODEDC_DESIGN_GUIDELINE" dependency.mkdir() marker = dependency / "local-work" marker.write_text("preserve") with self.assertRaisesRegex(ValueError, "dependency_destination_exists"): CI.fetch_dependencies(self.root, {}) self.assertEqual(marker.read_text(), "preserve") def test_lock_cannot_fetch_a_different_repository(self): lock = json.loads(Path(__file__).with_name("sources.lock.json").read_text()) lock["dependencies"]["platform"]["url"] = "https://example.invalid/other.git" (self.root / "infra/ci").mkdir(parents=True) (self.root / "infra/ci/sources.lock.json").write_text(json.dumps(lock)) with self.assertRaisesRegex(ValueError, "invalid_dependency_identity:platform"): CI.read_lock(self.root) def test_digest_is_path_independent_and_changes_with_content(self): a, b = self.root / "a", self.root / "b" for directory in (a, b): directory.mkdir() (directory / "index.html").write_text("same") self.assertEqual(CI.tree_digest(a), CI.tree_digest(b)) (b / "index.html").write_text("different") self.assertNotEqual(CI.tree_digest(a), CI.tree_digest(b)) def test_build_cannot_include_external_symlinks(self): output = self.root / "dist" output.mkdir() (output / "index.html").symlink_to(self.root / "source.txt") with self.assertRaisesRegex(ValueError, "symlink_in_build_output"): CI.tree_digest(output) def test_failed_step_never_writes_success_report_or_runs_later_steps(self): report = self.root.parent / "result.json" lock = {"node": "v22.21.0", "npm": "10.9.4"} with mock.patch.object(CI, "verify_sources"), mock.patch.object( CI, "run", side_effect=["v22.21.0\n", "10.9.4\n", subprocess.CalledProcessError(1, "npm")] ) as execute: with self.assertRaises(subprocess.CalledProcessError): CI.qualify(self.root, self.commit, lock, report) self.assertEqual(execute.call_count, 3) self.assertFalse(report.exists()) def test_existing_report_cannot_be_mistaken_for_new_success(self): report = self.root.parent / "result.json" report.write_text('{"sourceCommit":"old"}') with self.assertRaisesRegex(ValueError, "qualification_report_already_exists"): CI.qualify(self.root, self.commit, {}, report) self.assertEqual(json.loads(report.read_text())["sourceCommit"], "old") if __name__ == "__main__": unittest.main()