windowsInstallerCompile.test.ts 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // 26.17.0 将 NSIS 下载与路径解析收口到 toolset resolver;旧 nsisUtil 私有导出已移除。
  27. // 测试必须复用正式构建入口,避免用另一套 compiler/include/plugin 路径掩盖兼容问题。
  28. const { getMakeNsisPath, getNsisPluginsPath } = require(
  29. resolve(builderDir, "out/toolsets/windows.js"),
  30. );
  31. const [makeNsis, pluginsDir] = await Promise.all([
  32. getMakeNsisPath(undefined),
  33. getNsisPluginsPath(undefined),
  34. ]);
  35. const nsisDir = makeNsis.env?.NSISDIR;
  36. if (!nsisDir) {
  37. throw new Error("electron-builder NSIS toolset 缺少 NSISDIR");
  38. }
  39. const compiler = makeNsis.path;
  40. const temp = await mkdtemp(join(tmpdir(), "zcode-nsis-compile-"));
  41. try {
  42. for (const uninstaller of [false, true]) {
  43. const output = join(temp, uninstaller ? "uninstaller.exe" : "installer.exe");
  44. // 只编译不执行:载荷复用当前文件,所有平台都能验证真实 NSIS 插件解析。
  45. const args = [
  46. "-V2",
  47. "-INPUTCHARSET",
  48. "UTF8",
  49. ...(uninstaller ? ["-WX", "-DBUILD_UNINSTALLER"] : []),
  50. `-DNSIS_INCLUDE_DIR=${join(nsisDir, "Include")}`,
  51. `-DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${join(builderDir, "templates/nsis/include")}`,
  52. `-DNSIS_PLUGIN_DIR=${join(pluginsDir, "x86-unicode")}`,
  53. `-DZCODE_INSTALLER_NSH=${join(desktopDir, "build/installer.nsh")}`,
  54. `-DFIXTURE_EXE=${import.meta.filename}`,
  55. `-DOUTPUT_FILE=${output}`,
  56. `-DDEFAULT_LOG_PATH=${join(temp, "default.log")}`,
  57. `-DELEVATED_LOG_PATH=${join(temp, "elevated.log")}`,
  58. join(desktopDir, "test/fixtures/installer-nsh-smoke.nsi"),
  59. ];
  60. const result = await execFileAsync(compiler, args, {
  61. env: { ...process.env, ...makeNsis.env },
  62. });
  63. expect(`${result.stdout}${result.stderr}`).not.toContain("warning 6000");
  64. expect((await stat(output)).size).toBeGreaterThan(0);
  65. }
  66. // 恢复夹具必须引用新宏,避免 NSIS 裁掉未引用函数后掩盖语法或变量错误。
  67. for (const failureMode of [undefined, "BACKUP_FAILURE", "RESTORE_FAILURE"]) {
  68. const recoveryOutput = join(temp, `recovery-${failureMode ?? "normal"}.exe`);
  69. await execFileAsync(
  70. compiler,
  71. [
  72. "-V2",
  73. "-INPUTCHARSET",
  74. "UTF8",
  75. `-DNSIS_INCLUDE_DIR=${join(nsisDir, "Include")}`,
  76. `-DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${join(builderDir, "templates/nsis/include")}`,
  77. `-DNSIS_PLUGIN_DIR=${join(pluginsDir, "x86-unicode")}`,
  78. `-DZCODE_INSTALLER_NSH=${join(desktopDir, "build/installer.nsh")}`,
  79. `-DFIXTURE_EXE=${import.meta.filename}`,
  80. `-DOUTPUT_FILE=${recoveryOutput}`,
  81. "-DRECOVERY_TEST",
  82. ...(failureMode ? [`-D${failureMode}`] : []),
  83. `-DOLD_CLEANUP_EXE=${join(temp, "uninstaller.exe")}`,
  84. join(desktopDir, "test/fixtures/installer-preserve-files.nsi"),
  85. ],
  86. { env: { ...process.env, ...makeNsis.env } },
  87. );
  88. expect((await stat(recoveryOutput)).size).toBeGreaterThan(0);
  89. }
  90. } finally {
  91. await rm(temp, { recursive: true, force: true });
  92. }
  93. }, 120_000);