"""Check the packaged Windows executable on the native runner.""" import os from pathlib import Path import subprocess import sys import tempfile import tomllib import zipfile import pefile from windows_deps import resolve_dependencies def check_package(directory): with (Path(__file__).resolve().parents[1] / "Cargo.toml").open("rb") as stream: package = tomllib.load(stream)["package"] name, version = package["name"], package["version"] with tempfile.TemporaryDirectory() as temp: unpacked = Path(temp) / "portable" with zipfile.ZipFile(directory / f"{name}-{version}-windows-x86_64.zip") as archive: archive.extractall(unpacked) exe = unpacked / f"{name}.exe" # Verify closure using only files that made it into the ZIP. A globally # installed DLL on the build machine could otherwise mask an omission. resolve_dependencies(exe, require_static_crt=True) with pefile.PE(str(exe), fast_load=True) as pe: if pe.OPTIONAL_HEADER.Subsystem != pefile.SUBSYSTEM_TYPE["IMAGE_SUBSYSTEM_WINDOWS_GUI"]: raise ValueError("The packaged executable opens a console window") pe.parse_data_directories([pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_RESOURCE"]]) resources = {entry.id for entry in pe.DIRECTORY_ENTRY_RESOURCE.entries} if not {pefile.RESOURCE_TYPE["RT_ICON"], pefile.RESOURCE_TYPE["RT_GROUP_ICON"]} <= resources: raise ValueError("The packaged executable is missing its application icon") windows = Path(os.environ["SystemRoot"]) env = {**os.environ, "PATH": os.pathsep.join(map(str, (windows / "System32", windows)))} for argument, code, expected in [ ("--version", 0, f"{name} {version}"), ("--help", 0, "poe2"), ("invalid-game", 2, "invalid-game"), ]: # Exercise both pipes together and each redirected stream independently. result = subprocess.run([exe, argument], cwd=temp, env=env, text=True, timeout=30, stdout=subprocess.PIPE if not code else None, stderr=subprocess.PIPE if argument != "--help" else None) output = result.stderr if code else result.stdout if result.returncode != code or expected not in output: raise ValueError(f"{argument} returned {result.returncode:#x}: {output.strip()}") print(f"Verified the packaged {name} {version}, its GUI subsystem, icon and CLI output.") if __name__ == "__main__": check_package(Path(sys.argv[1]))