beffroi-tools/tests/test_prepare_release.py

82 lines
4.3 KiB
Python

import hashlib
import importlib.util
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[1]
spec = importlib.util.spec_from_file_location("prepare_release", ROOT / "scripts/prepare-release.py")
adapter = importlib.util.module_from_spec(spec)
spec.loader.exec_module(adapter)
class PrepareReleaseTests(unittest.TestCase):
def test_payload_for_each_target_and_missing_or_unsafe_notices(self):
pins = json.loads((ROOT / "crates/pptx-reporter/ffprobe-pins.json").read_text())
for target, pin in pins.items():
with self.subTest(target=target), tempfile.TemporaryDirectory(prefix="bundle é ") as temp:
root = Path(temp)
out = root / "output"
out.mkdir()
binary = root / "downloaded-probe"
binary.write_bytes(b"fixture executable")
notices = root / "notices"
(notices / "upstream").mkdir(parents=True)
license_file = notices / "upstream/COPYING"
license_file.write_text("upstream license")
provenance = notices / "provenance.json"
provenance.write_text('{"provider":"fixture"}')
manifest = root / "manifest.json"
ext = ".exe" if "windows" in target else ""
context = {
"schema_version": 1, "target": target, "binary_ext": ext,
"package_root": str(ROOT / "crates/pptx-reporter"),
"output_directory": str(out), "cache_directory": str(root / "cache"),
"manifest_path": str(manifest),
}
with patch.object(adapter, "fetch", return_value=(binary, notices)) as fetch, \
patch.object(adapter, "build_macos", return_value=(binary, notices)) as build:
adapter.prepare(context, manifest)
if target.endswith("-apple-darwin"):
build.assert_called_once_with(target, root / "cache", pin)
fetch.assert_not_called()
else:
fetch.assert_called_once_with(target, root / "cache", pin["release"],
pin["sha256"], asset_name=pin["asset"])
build.assert_not_called()
result = json.loads(manifest.read_text())
self.assertEqual(result["target"], target)
files = {f["destination"]: f for f in result["files"]}
self.assertEqual(set(files), {
"ffprobe" + ext, "LICENSES/ffmpeg/upstream/COPYING",
"LICENSES/ffmpeg/LICENSE.txt", "LICENSES/ffmpeg/provenance.json",
})
self.assertEqual(files["ffprobe" + ext]["kind"], "executable")
for item in files.values():
payload = out / item["source"]
self.assertEqual(hashlib.sha256(payload.read_bytes()).hexdigest(), item["sha256"])
self.assertFalse(payload.samefile(binary))
manifest.unlink()
provenance.unlink()
with self.assertRaisesRegex(ValueError, "provenance"):
adapter.prepare(context, manifest)
self.assertFalse(manifest.exists())
provenance.write_text("{}")
license_file.unlink()
with self.assertRaisesRegex(ValueError, "license"):
adapter.prepare(context, manifest)
license_file.symlink_to(binary)
with self.assertRaisesRegex(ValueError, "Unsafe notice"):
adapter.prepare(context, manifest)
self.assertFalse(manifest.exists())
def test_invalid_context_fails_before_download(self):
with patch.object(adapter, "fetch") as fetch:
with self.assertRaisesRegex(ValueError, "schema"):
adapter.prepare({"schema_version": 2}, Path("/unused"))
with self.assertRaisesRegex(ValueError, "Manifest path"):
adapter.prepare({"schema_version": 1, "manifest_path": "/different"}, Path("/unused"))
fetch.assert_not_called()