| 1 | import os |
| 2 | from pathlib import Path |
| 3 | import shutil |
| 4 | import subprocess |
| 5 | import tempfile |
| 6 | import unittest |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parents[2] |
| 9 | |
| 10 | |
| 11 | class IconBuildTests(unittest.TestCase): |
| 12 | @classmethod |
| 13 | def setUpClass(cls): |
| 14 | (ROOT / "tests/work").mkdir(parents=True, exist_ok=True) |
| 15 | cls.scratch = tempfile.TemporaryDirectory(dir=ROOT / "tests/work") |
| 16 | cls.addClassCleanup(cls.scratch.cleanup) |
| 17 | cls.root = Path(cls.scratch.name) |
| 18 | cls.builder = cls.root / "build.exe" |
| 19 | cls.compiler = cls.root / "fake-rc.exe" |
| 20 | fake = cls.root / "fake.rs" |
| 21 | fake.write_text('''fn main() { |
| 22 | if std::env::var_os("FAKE_RC_FAIL").is_some() { |
| 23 | std::process::exit(1); |
| 24 | } |
| 25 | let output = std::env::args().find_map(|arg| arg.strip_prefix("/fo").map(str::to_owned)).unwrap(); |
| 26 | std::fs::write(output, std::env::current_exe().unwrap().to_string_lossy().as_bytes()).unwrap(); |
| 27 | } |
| 28 | ''') |
| 29 | for source, output in ((ROOT / "experiments/rust-ui-memory-app/build.rs", cls.builder), |
| 30 | (fake, cls.compiler)): |
| 31 | subprocess.run(["rustc", "+1.99.1", "--edition=2024", str(source), "-o", str(output)], |
| 32 | check=True, capture_output=True, text=True, encoding="utf-8") |
| 33 | |
| 34 | def setUp(self): |
| 35 | self.directory = tempfile.TemporaryDirectory(dir=self.root) |
| 36 | self.addCleanup(self.directory.cleanup) |
| 37 | self.work = Path(self.directory.name) |
| 38 | self.output = self.work / "out" |
| 39 | self.output.mkdir() |
| 40 | self.programs = self.work / "Program Files (x86)" |
| 41 | |
| 42 | def sdk(self, version): |
| 43 | path = self.programs / "Windows Kits/10/bin" / version / "x64/rc.exe" |
| 44 | path.parent.mkdir(parents=True) |
| 45 | shutil.copy2(self.compiler, path) |
| 46 | return path |
| 47 | |
| 48 | def run_build(self, target="msvc", **overrides): |
| 49 | excluded = {key.casefold() for key in ("RC", "ProgramFiles", "ProgramFiles(x86)", "FAKE_RC_FAIL", "PATH")} |
| 50 | environment = {key: value for key, value in os.environ.items() if key.casefold() not in excluded} |
| 51 | environment.update({"CARGO_CFG_TARGET_OS": "windows", "CARGO_CFG_TARGET_ENV": target, |
| 52 | "CARGO_FEATURE_UI": "1", "OUT_DIR": str(self.output), |
| 53 | "CARGO_MANIFEST_DIR": str(ROOT / "experiments/rust-ui-memory-app"), |
| 54 | "ProgramFiles(x86)": str(self.programs), "PATH": ""}) |
| 55 | environment.update(overrides) |
| 56 | return subprocess.run([str(self.builder)], env=environment, capture_output=True, text=True, encoding="utf-8") |
| 57 | |
| 58 | def test_sdk_uses_highest_numeric_version_with_x64_compiler(self): |
| 59 | self.sdk("10.0.9.0") |
| 60 | latest = self.sdk("10.0.10.0") |
| 61 | (self.programs / "Windows Kits/10/bin/10.0.99.0/x64").mkdir(parents=True) |
| 62 | result = self.run_build() |
| 63 | self.assertEqual(result.returncode, 0, result.stderr) |
| 64 | self.assertIn("cargo:rustc-link-arg-bin=", result.stdout) |
| 65 | self.assertTrue(Path((self.output / "zipelo.res").read_text()).samefile(latest)) |
| 66 | |
| 67 | def test_explicit_rc_overrides_sdk(self): |
| 68 | self.sdk("10.0.99.0") |
| 69 | result = self.run_build(RC=str(self.compiler)) |
| 70 | self.assertEqual(result.returncode, 0, result.stderr) |
| 71 | self.assertTrue(Path((self.output / "zipelo.res").read_text()).samefile(self.compiler)) |
| 72 | |
| 73 | def test_path_compiler_remains_available_without_sdk(self): |
| 74 | path = self.work / "bin/rc.exe" |
| 75 | path.parent.mkdir() |
| 76 | shutil.copy2(self.compiler, path) |
| 77 | result = self.run_build(PATH=str(path.parent)) |
| 78 | self.assertEqual(result.returncode, 0, result.stderr) |
| 79 | self.assertTrue(Path((self.output / "zipelo.res").read_text()).samefile(path)) |
| 80 | |
| 81 | def test_missing_msvc_compiler_warns_without_linking(self): |
| 82 | result = self.run_build() |
| 83 | self.assertEqual(result.returncode, 0, result.stderr) |
| 84 | self.assertIn("cargo:warning=아이콘 없이 빌드", result.stdout) |
| 85 | self.assertNotIn("cargo:rustc-link-arg-bin=", result.stdout) |
| 86 | self.assertFalse((self.output / "zipelo.res").exists()) |
| 87 | |
| 88 | def test_msvc_compiler_failure_warns_without_linking(self): |
| 89 | result = self.run_build(RC=str(self.compiler), FAKE_RC_FAIL="1") |
| 90 | self.assertEqual(result.returncode, 0, result.stderr) |
| 91 | self.assertIn("cargo:warning=아이콘 없이 빌드", result.stdout) |
| 92 | self.assertNotIn("cargo:rustc-link-arg-bin=", result.stdout) |
| 93 | |
| 94 | def test_invalid_explicit_rc_does_not_fall_back_to_sdk(self): |
| 95 | self.sdk("10.0.99.0") |
| 96 | result = self.run_build(RC=str(self.work / "missing.exe")) |
| 97 | self.assertEqual(result.returncode, 0, result.stderr) |
| 98 | self.assertIn("cargo:warning=아이콘 없이 빌드", result.stdout) |
| 99 | self.assertNotIn("cargo:rustc-link-arg-bin=", result.stdout) |
| 100 | |
| 101 | def test_missing_gnu_compiler_remains_a_build_failure(self): |
| 102 | result = self.run_build(target="gnu", RC=str(self.work / "missing.exe")) |
| 103 | self.assertNotEqual(result.returncode, 0) |
| 104 | self.assertNotIn("cargo:warning=아이콘 없이 빌드", result.stdout) |