windowsInstallerCompile.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { execFile } from "node:child_process";
  2. import { mkdtemp, rm, stat } from "node:fs/promises";
  3. import { createRequire } from "node:module";
  4. import { tmpdir } from "node:os";
  5. import { dirname, join, resolve } from "node:path";
  6. import { promisify } from "node:util";
  7. import { expect, it } from "vitest";
  8. const execFileAsync = promisify(execFile);
  9. const require = createRequire(import.meta.url);
  10. const desktopDir = resolve(import.meta.dirname, "..");
  11. const builderDir = dirname(require.resolve("app-builder-lib/package.json"));
  12. it("compiles real UAC calls when the plugin directory follows the custom include", async (context) => {
  13. // NSIS 自带的 mac 编译器是 x86_64;Apple Silicon 缺少 Rosetta 时只跳过此环境不支持的编译测试。
  14. if (process.platform === "darwin" && process.arch === "arm64") {
  15. try {
  16. await execFileAsync("/usr/bin/arch", ["-x86_64", "/usr/bin/true"]);
  17. } catch (error) {
  18. const stderr = (error as { stderr?: string }).stderr ?? "";
  19. if (stderr.includes("Bad CPU type in executable")) {
  20. context.skip("NSIS x86_64 compiler requires Rosetta on Apple Silicon");
  21. return;
  22. }
  23. throw error;
  24. }
  25. }
  26. const { NSIS_PATH, NSIS_RESOURCES_PATH, NsisTargetOptions } = require(
  27. resolve(builderDir, "out/targets/nsis/nsisUtil.js"),
  28. );
  29. NsisTargetOptions.resolve({});
  30. const [nsisDir, resourcesDir] = await Promise.all([NSIS_PATH(), NSIS_RESOURCES_PATH()]);
  31. const compiler = join(
  32. nsisDir,
  33. process.platform === "win32"
  34. ? "Bin/makensis.exe"
  35. : process.platform === "darwin"
  36. ? "mac/makensis"
  37. : "linux/makensis",
  38. );
  39. const temp = await mkdtemp(join(tmpdir(), "zcode-nsis-compile-"));
  40. try {
  41. for (const uninstaller of [false, true]) {
  42. const output = join(temp, uninstaller ? "uninstaller.exe" : "installer.exe");
  43. // 只编译不执行:载荷复用当前文件,所有平台都能验证真实 NSIS 插件解析。
  44. const args = [
  45. "-V2",
  46. ...(uninstaller ? ["-WX", "-DBUILD_UNINSTALLER"] : []),
  47. `-DNSIS_INCLUDE_DIR=${join(nsisDir, "Include")}`,
  48. `-DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${join(builderDir, "templates/nsis/include")}`,
  49. `-DNSIS_PLUGIN_DIR=${join(resourcesDir, "plugins/x86-unicode")}`,
  50. `-DZCODE_INSTALLER_NSH=${join(desktopDir, "build/installer.nsh")}`,
  51. `-DFIXTURE_EXE=${import.meta.filename}`,
  52. `-DOUTPUT_FILE=${output}`,
  53. `-DDEFAULT_LOG_PATH=${join(temp, "default.log")}`,
  54. `-DELEVATED_LOG_PATH=${join(temp, "elevated.log")}`,
  55. join(desktopDir, "test/fixtures/installer-nsh-smoke.nsi"),
  56. ];
  57. await execFileAsync(compiler, args, { env: { ...process.env, NSISDIR: nsisDir } });
  58. expect((await stat(output)).size).toBeGreaterThan(0);
  59. }
  60. } finally {
  61. await rm(temp, { recursive: true, force: true });
  62. }
  63. }, 120_000);