import contextlib import importlib.util import io from pathlib import Path import tempfile import unittest from unittest.mock import patch import zipfile ROOT = Path(__file__).resolve().parents[1] spec = importlib.util.spec_from_file_location("portable", ROOT / "scripts/package-portable.py") portable = importlib.util.module_from_spec(spec) spec.loader.exec_module(portable) class PortableTests(unittest.TestCase): def test_bundle_contents_checksum_and_overwrite_and_platform_validation(self): with tempfile.TemporaryDirectory() as temp: root = Path(temp) target = "x86_64-pc-windows-gnu" build = root / "target" / target / "release" build.mkdir(parents=True) for name in ("pptx-reporter.exe", "pptx-reporter-gui.exe"): (build / name).write_bytes(b"MZfixture") probe = root / "ffprobe.exe" probe.write_bytes(b"MZfixture") licenses = root / "ffmpeg-licenses" licenses.mkdir() (licenses / "LICENSE").write_text("fixture notice") for file in ("LICENSE", "crates/pptx-reporter/assets/fonts/LICENSE", "docs/pptx-reporter/windows-quickstart.txt", "crates/pptx-reporter/assets/app-icon.png", "crates/pptx-reporter/assets/app-icon.ico", "crates/pptx-reporter/assets/app-icon.icns", "scripts/create-desktop-launcher.py", "scripts/desktop_icons.py"): path = root / file path.parent.mkdir(parents=True, exist_ok=True) path.write_text("fixture") (root / "Cargo.toml").write_text('[workspace.package]\nversion = "0.1.0"\n') output = root / "dist" args = ["package-portable.py", "--target", target, "--ffprobe", str(probe), "--ffprobe-licenses", str(licenses), "--output", str(output)] with patch.object(portable, "ROOT", root), patch("sys.argv", args), \ contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()): portable.main() archive, = output.glob("*.zip") original = archive.read_bytes() with zipfile.ZipFile(archive) as bundle: names = {Path(n).name for n in bundle.namelist()} self.assertTrue({"pptx-reporter.exe", "pptx-reporter-gui.exe", "ffprobe.exe", "README.txt", "LICENSE", "reporter-MIT.txt", "DejaVu.txt", "app-icon.png", "app-icon.ico", "app-icon.icns", "create-desktop-launcher.py", "desktop_icons.py"} <= names) self.assertTrue(archive.with_suffix(".zip.sha256").is_file()) with self.assertRaises(SystemExit): portable.main() self.assertEqual(archive.read_bytes(), original) fetched_args = ["package-portable.py", "--target", target, "--fetch-ffprobe", "--output", str(output / "fetched")] with patch("sys.argv", fetched_args), patch.object(portable, "fetch", return_value=(probe, licenses)) as fetch: portable.main() fetch.assert_called_once_with(target, root / "downloads/ffprobe", "latest", None) self.assertEqual(len(list((output / "fetched").glob("*.zip"))), 1) probe.write_bytes(b"\x7fELF") with self.assertRaises(SystemExit): portable.main() if __name__ == "__main__": unittest.main()