team-ljyzipelo

공개
126줄6.4 KB
1import argparse
2import importlib.util
3import os
4from pathlib import Path
5import struct
6import re
7import shutil
8import subprocess
9import tempfile
10import sys
11from unittest.mock import patch
12import unittest
13
14ROOT = Path(__file__).resolve().parents[2]
15SPEC = importlib.util.spec_from_file_location("windows_installer", ROOT / "packaging/windows/rust/build.py")
16MODULE = importlib.util.module_from_spec(SPEC)
17SPEC.loader.exec_module(MODULE)
18
19
20class InstallerInputTests(unittest.TestCase):
21 def setUp(self):
22 runs = Path(os.environ["ZIPELO_TEST_WORK_DIR"]) / "fixtures/windows-installer"
23 runs.mkdir(parents=True, exist_ok=True)
24 self.directory = tempfile.TemporaryDirectory(dir=runs)
25 self.addCleanup(self.directory.cleanup)
26 self.root = Path(self.directory.name)
27
28 def test_truncated_and_non_pe_payloads_are_rejected(self):
29 for content in (b"", b"MZ", b"not an executable" * 8,
30 b"MZ" + bytes(58) + struct.pack("<I", 65535)):
31 payload = self.root / "invalid.exe"
32 payload.write_bytes(content)
33 with self.assertRaises(ValueError):
34 MODULE.pe_machine(payload)
35
36 def test_x64_coff_machine_is_read_from_pe_offset(self):
37 payload = self.root / "header.exe"
38 payload.write_bytes(b"MZ" + bytes(58) + struct.pack("<I", 128)
39 + bytes(64) + b"PE\0\0" + struct.pack("<H", 0x8664))
40 self.assertEqual(MODULE.pe_machine(payload), 0x8664)
41 payload.write_bytes(payload.read_bytes()[:128] + b"badPE!")
42 with self.assertRaises(ValueError):
43 MODULE.pe_machine(payload)
44
45 def test_existing_output_is_preserved_before_any_build(self):
46 output = self.root / "output"
47 output.mkdir()
48 sentinel = output / "user-data"
49 sentinel.write_bytes(b"preserve")
50 args = argparse.Namespace(output=output, target_dir=self.root / "target")
51 with self.assertRaises(FileExistsError):
52 MODULE.build(args)
53 self.assertEqual(sentinel.read_bytes(), b"preserve")
54 self.assertEqual(list(output.iterdir()), [sentinel])
55 self.assertFalse(args.target_dir.exists())
56
57 @unittest.skipUnless(shutil.which("makensis"), "NSIS가 설치되지 않음")
58 def test_nsis_compiles_payloads_and_finish_callbacks(self):
59 payload = self.root / "payload"
60 payload.mkdir()
61 for name in ("Zipelo.exe", "zipelo-archive-worker.exe", "LICENSE", "BUILD-INFO.json", "THIRD-PARTY-LICENSES.txt"):
62 (payload / name).write_bytes(b"compile-only test fixture")
63 shutil.copyfile(ROOT / "packaging/windows/rust/installer.nsi", self.root / "installer.nsi")
64 shutil.copyfile(ROOT / "experiments/rust-ui-memory-app/src/assets/icons/zipelo.ico", self.root / "zipelo.ico")
65 completed = subprocess.run(["makensis", "-WX", "installer.nsi"], cwd=self.root,
66 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
67 self.assertEqual(completed.returncode, 0, completed.stdout)
68 installer = self.root / "Zipelo-0.2.1-windows-x64-setup.exe"
69 self.assertIn(MODULE.pe_machine(installer), (0x14C, 0x8664))
70 self.assertGreater(installer.stat().st_size, 100000)
71
72 def test_command_line_directory_is_reset_before_install_side_effects(self):
73 source = (ROOT / "packaging/windows/rust/installer.nsi").read_text()
74 section = source.split('Section "Zipelo"', 1)[1].split("SectionEnd", 1)[0]
75 commands = [line.strip() for line in section.splitlines()
76 if line.strip() and not line.strip().startswith(";")]
77 self.assertEqual(commands[0], 'StrCpy $INSTDIR "${INSTALLROOT}"')
78 root = re.search(r'^!define INSTALLROOT "([^"\n]+)"$', source, re.MULTILINE)
79 self.assertIsNotNone(root)
80 self.assertEqual(root.group(1), "$LOCALAPPDATA\\Programs\\Zipelo")
81 self.assertIn('InstallDir "${INSTALLROOT}"', source)
82 uninstaller = source.split("Function un.onInit", 1)[1].split("FunctionEnd", 1)[0]
83 self.assertIn('StrCmp $INSTDIR "${INSTALLROOT}" 0 invalid', uninstaller)
84
85 def test_source_commit_change_cannot_reuse_an_old_binary(self):
86 repo = self.root / "source"
87 repo.mkdir()
88 manifest = repo / "Cargo.toml"
89 manifest.write_text("fixture")
90 real_run = subprocess.run
91 environment = dict(MODULE.os.environ, GIT_AUTHOR_NAME="Zipelo test",
92 GIT_AUTHOR_EMAIL="[email protected]", GIT_COMMITTER_NAME="Zipelo test",
93 GIT_COMMITTER_EMAIL="[email protected]")
94
95 def git(*args):
96 real_run(["git", *args], cwd=repo, env=environment, check=True,
97 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
98
99 git("init", "-b", "features/installer-fixture")
100 git("add", ".")
101 git("commit", "-m", "첫 시험 소스를 기록함")
102
103 def cached_compiler(command, **options):
104 if command[0] != "cargo":
105 return real_run(command, **options)
106 target = Path(command[command.index("--target-dir") + 1])
107 output = target / MODULE.TARGET / "debug/fixture.exe"
108 output.parent.mkdir(parents=True, exist_ok=True)
109 if not output.exists():
110 header = b"MZ" + bytes(58) + struct.pack("<I", 128) + bytes(64)
111 output.write_bytes(header + b"PE\0\0" + struct.pack("<H", 0x8664)
112 + manifest.read_bytes())
113 return subprocess.CompletedProcess(command, 0)
114
115 with patch.dict(MODULE.os.environ, {"CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER": sys.executable}):
116 with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler):
117 first, first_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev")
118 manifest.write_text("changed fixture")
119 git("add", ".")
120 git("commit", "-m", "변경된 시험 소스를 기록함")
121 with patch.object(MODULE.subprocess, "run", side_effect=cached_compiler):
122 second, second_record = MODULE.build_binary(manifest, "fixture", self.root / "target", "dev")
123 self.assertNotEqual(first_record["commit"], second_record["commit"])
124 self.assertNotEqual(first_record["sha256"], second_record["sha256"])
125 self.assertTrue(second.read_bytes().endswith(b"changed fixture"))
126 self.assertTrue(first.read_bytes().endswith(b"fixture"))