from pathlib import Path import importlib.util import pytest ROOT = Path(__file__).parents[1] def release_module(): spec = importlib.util.spec_from_file_location("build_release", ROOT / "packaging/build_release.py") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def skeleton(tmp_path): (tmp_path / "MentorPDFExporter.exe").write_bytes(b"stub") assets = tmp_path / "_internal/mentor_pdf_export/assets" assets.mkdir(parents=True) (assets / "app.ico").write_bytes(b"icon") return tmp_path def test_release_version_matches_official_metadata(): assert release_module().project_version() == "1.0.0" requirements = (ROOT / "packaging/requirements-release.txt").read_text(encoding="utf8") assert "cryptography==45.0.7" in requirements and "cffi==2.1.1" in requirements @pytest.mark.parametrize("name", ["private.prj", "library.lmc", "output.pdf", "Arial.ttf", "SimSun.ttc", "font.otf", "sch2pdf.exe"]) def test_release_rejects_engineering_data_fonts_and_mentor(tmp_path, name): bundle = skeleton(tmp_path) (bundle / name).write_bytes(b"private") with pytest.raises(ValueError): release_module().validate_bundle(bundle) def test_release_requires_packaged_window_icon(tmp_path): bundle = skeleton(tmp_path) (bundle / "_internal/mentor_pdf_export/assets/app.ico").unlink() with pytest.raises(ValueError, match="icon"): release_module().validate_bundle(bundle) def test_release_allows_runtime_library_archive(tmp_path): bundle = skeleton(tmp_path) (bundle / "_internal/base_library.zip").write_bytes(b"runtime") release_module().validate_bundle(bundle) @pytest.mark.parametrize("relative", ["license.lic", "license.dat", "database/icdb.dat", "runs/report.json", ".git/config", "_internal/runs/report.json", "_internal/private.dat", "unexpected.txt"]) def test_release_rejects_private_license_database_logs_and_git(tmp_path, relative): bundle = skeleton(tmp_path) private = bundle / relative private.parent.mkdir(parents=True, exist_ok=True) private.write_bytes(b"private") with pytest.raises(ValueError): release_module().validate_bundle(bundle) def test_release_runtime_must_match_build_allowlist(tmp_path): bundle = skeleton(tmp_path) module = release_module() allowed = {p.relative_to(bundle).as_posix(): module.sha256(p) for p in bundle.rglob("*") if p.is_file()} (bundle / "_internal/unknown.dll").write_bytes(b"not collected by PyInstaller") with pytest.raises(ValueError): module.validate_bundle(bundle, runtime_files=allowed) def make_archive(path, bundle, *, omit=None, extra=None, duplicate=None, changed=None): import zipfile with zipfile.ZipFile(path, "w") as archive: for file in bundle.rglob("*"): if not file.is_file() or file.name == omit: continue name = bundle.name + "/" + file.relative_to(bundle).as_posix() archive.writestr(name, b"tampered" if file.name == changed else file.read_bytes()) if file.name == duplicate: archive.writestr(name, file.read_bytes()) if extra: archive.writestr(extra, b"private") def test_archive_matches_complete_verified_directory(tmp_path): bundle = tmp_path / "release" bundle.mkdir() skeleton(bundle) archive = tmp_path / "release.zip" make_archive(archive, bundle) release_module().validate_archive(archive, bundle) @pytest.mark.parametrize("case", ["missing", "extra", "duplicate", "traversal", "modified"]) def test_archive_rejects_incomplete_extra_duplicate_unsafe_or_changed_members(tmp_path, case): import warnings bundle = tmp_path / "release" bundle.mkdir() skeleton(bundle) archive = tmp_path / "release.zip" options = { "missing": {"omit": "app.ico"}, "extra": {"extra": "release/license.lic"}, "duplicate": {"duplicate": "MentorPDFExporter.exe"}, "traversal": {"extra": "release/../private.txt"}, "modified": {"changed": "app.ico"}, }[case] with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) make_archive(archive, bundle, **options) with pytest.raises(ValueError): release_module().validate_archive(archive, bundle)