diff --git a/packaging/macos/nightly_plist_tests.py b/packaging/macos/nightly_plist_tests.py index 89db7898cf8..f08d2912651 100644 --- a/packaging/macos/nightly_plist_tests.py +++ b/packaging/macos/nightly_plist_tests.py @@ -3,6 +3,9 @@ import importlib.util from pathlib import Path import plistlib +import runpy +import sys +from unittest import mock import tempfile import unittest @@ -56,6 +59,49 @@ class NightlyMetadata(unittest.TestCase): POLICY.write_plist(self.template, self.output, VERSION, COMMIT) self.assertFalse(self.output.exists()) + def test_cli_writes_the_requested_identity_without_replacing_the_template(self): + helper = HERE / "nightly_plist.py" + argv = [str(helper), "--template", str(self.template), "--output", str(self.output), "--version", VERSION, "--source-commit", COMMIT] + with mock.patch.object(sys, "argv", argv): + runpy.run_path(str(helper), run_name="__main__") + result = plistlib.loads(self.output.read_bytes()) + self.assertEqual(result["PhotoCraftVersion"], VERSION) + self.assertEqual(result["PhotoCraftBuildCommit"], COMMIT) + self.assertEqual(self.template.read_bytes(), self.original) + + def test_template_alias_or_nonfile_refuses_before_output(self): + alias = self.root / "template-alias" + alias.symlink_to(self.template) + for template in (alias, self.root, self.root / "missing-template"): + with self.subTest(template=template), self.assertRaises(ValueError): + POLICY.write_plist(template, self.output, VERSION, COMMIT) + self.assertFalse(self.output.exists()) + self.assertEqual(self.template.read_bytes(), self.original) + + def test_failed_write_cleans_owned_output_but_preserves_a_replacement(self): + sentinel = b"generated replacement artifact" + for disposition in ("owned", "missing", "replacement"): + with self.subTest(disposition=disposition): + if self.output.exists(): + self.output.unlink() + + def fail_sync(_descriptor): + # macOS/Linux allow unlink of an open file: exercise the real + # cleanup ownership check, not a mocked inode comparison. + if disposition != "owned": + self.output.unlink() + if disposition == "replacement": + self.output.write_bytes(sentinel) + raise OSError("generated fsync failure") + + with mock.patch.object(POLICY.os, "fsync", side_effect=fail_sync), self.assertRaises(OSError): + POLICY.write_plist(self.template, self.output, VERSION, COMMIT) + if disposition == "replacement": + self.assertEqual(self.output.read_bytes(), sentinel) + else: + self.assertFalse(self.output.exists()) + self.assertEqual(self.template.read_bytes(), self.original) + def test_exclusive_output_preserves_existing_bytes_and_symlink_targets(self): sentinel = b"generated owned sentinel, not an existing app or user file" self.output.write_bytes(sentinel) diff --git a/packaging/nightly-archive-tests.py b/packaging/nightly-archive-tests.py index c7cd4394efb..d0f8c7dfb23 100644 --- a/packaging/nightly-archive-tests.py +++ b/packaging/nightly-archive-tests.py @@ -3,6 +3,9 @@ import gzip import importlib.util import os +import runpy +import sys +from unittest import mock from pathlib import Path import subprocess import tarfile @@ -92,6 +95,19 @@ class ArchivePolicy(unittest.TestCase): ARCHIVE.linux_archive(self.stage, self.output) self.assertFalse(self.output.exists()) + def test_cli_dispatches_only_the_supported_archive_kind(self): + helper = HERE / "nightly-archive.py" + with mock.patch.object(sys, "argv", [str(helper), "linux", str(self.stage), str(self.output)]): + runpy.run_path(str(helper), run_name="__main__") + with tarfile.open(self.output) as archive: + self.assertEqual({member.name for member in archive}, self.required) + refused = self.root / "refused.tar.gz" + for args in ([str(helper)], [str(helper), "unknown", str(self.stage), str(refused)]): + with self.subTest(args=args), mock.patch.object(sys, "argv", args): + with self.assertRaises(SystemExit): + runpy.run_path(str(helper), run_name="__main__") + self.assertFalse(refused.exists()) + def test_existing_archive_is_unchanged(self): self.output.write_bytes(b"owned earlier artifact") with self.assertRaises(FileExistsError):