windowsInstallerCompile.test.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { execFile } from "node:child_process";
  2. import { mkdir, mkdtemp, rm, stat, writeFile } 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. `-DZCODE_INSTALLER_PROCESS_HELPER=${import.meta.filename}`,
  55. `-DFIXTURE_EXE=${import.meta.filename}`,
  56. `-DOUTPUT_FILE=${output}`,
  57. `-DDEFAULT_LOG_PATH=${join(temp, "default.log")}`,
  58. `-DELEVATED_LOG_PATH=${join(temp, "elevated.log")}`,
  59. join(desktopDir, "test/fixtures/installer-nsh-smoke.nsi"),
  60. ];
  61. const result = await execFileAsync(compiler, args, {
  62. env: { ...process.env, ...makeNsis.env },
  63. });
  64. expect(`${result.stdout}${result.stderr}`).not.toContain("warning 6000");
  65. expect((await stat(output)).size).toBeGreaterThan(0);
  66. }
  67. // 恢复夹具必须引用新宏,避免 NSIS 裁掉未引用函数后掩盖语法或变量错误。
  68. for (const failureMode of [undefined, "BACKUP_FAILURE", "RESTORE_FAILURE"]) {
  69. const recoveryOutput = join(temp, `recovery-${failureMode ?? "normal"}.exe`);
  70. await execFileAsync(
  71. compiler,
  72. [
  73. "-V2",
  74. "-INPUTCHARSET",
  75. "UTF8",
  76. `-DNSIS_INCLUDE_DIR=${join(nsisDir, "Include")}`,
  77. `-DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${join(builderDir, "templates/nsis/include")}`,
  78. `-DNSIS_PLUGIN_DIR=${join(pluginsDir, "x86-unicode")}`,
  79. `-DZCODE_INSTALLER_NSH=${join(desktopDir, "build/installer.nsh")}`,
  80. `-DZCODE_INSTALLER_PROCESS_HELPER=${import.meta.filename}`,
  81. `-DFIXTURE_EXE=${import.meta.filename}`,
  82. `-DOUTPUT_FILE=${recoveryOutput}`,
  83. "-DRECOVERY_TEST",
  84. ...(failureMode ? [`-D${failureMode}`] : []),
  85. `-DOLD_CLEANUP_EXE=${join(temp, "uninstaller.exe")}`,
  86. join(desktopDir, "test/fixtures/installer-preserve-files.nsi"),
  87. ],
  88. { env: { ...process.env, ...makeNsis.env } },
  89. );
  90. expect((await stat(recoveryOutput)).size).toBeGreaterThan(0);
  91. }
  92. // CR-01 回归:普通 NSIS 没有 APP_DIR_*;不传测试专用 define 时,必须能从 afterPack
  93. // 生成的按架构 define 文件解析 helper,缺失时明确失败而不是静默打出无收口的安装包。
  94. const generatedDir = join(desktopDir, "build", "generated");
  95. const generatedDefine = join(generatedDir, "zcode-process-helper-x64.nsh");
  96. const officialDefineArgs = [
  97. "-V2",
  98. "-INPUTCHARSET",
  99. "UTF8",
  100. `-DNSIS_INCLUDE_DIR=${join(nsisDir, "Include")}`,
  101. `-DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${join(builderDir, "templates/nsis/include")}`,
  102. `-DNSIS_PLUGIN_DIR=${join(pluginsDir, "x86-unicode")}`,
  103. `-DZCODE_INSTALLER_NSH=${join(desktopDir, "build/installer.nsh")}`,
  104. `-DFIXTURE_EXE=${import.meta.filename}`,
  105. `-DOUTPUT_FILE=${join(temp, "official-define.exe")}`,
  106. "-DRECOVERY_TEST",
  107. `-DOLD_CLEANUP_EXE=${join(temp, "uninstaller.exe")}`,
  108. join(desktopDir, "test/fixtures/installer-preserve-files.nsi"),
  109. ];
  110. await mkdir(generatedDir, { recursive: true });
  111. await writeFile(
  112. generatedDefine,
  113. `!define ZCODE_INSTALLER_PROCESS_HELPER "${import.meta.filename.replaceAll("$", "$$")}"\n`,
  114. "utf8",
  115. );
  116. try {
  117. const official = await execFileAsync(compiler, officialDefineArgs, {
  118. env: { ...process.env, ...makeNsis.env },
  119. });
  120. expect(`${official.stdout}${official.stderr}`).not.toContain("warning 6000");
  121. expect((await stat(join(temp, "official-define.exe"))).size).toBeGreaterThan(0);
  122. // 生成文件缺失时必须命中明确 !error,而不是回退到 APP_64 压缩包宏。
  123. await rm(generatedDefine, { force: true });
  124. await expect(
  125. execFileAsync(compiler, officialDefineArgs, {
  126. env: { ...process.env, ...makeNsis.env },
  127. }),
  128. ).rejects.toThrow(/ZCODE_INSTALLER_PROCESS_HELPER or generated define/);
  129. } finally {
  130. await rm(generatedDir, { recursive: true, force: true });
  131. }
  132. } finally {
  133. await rm(temp, { recursive: true, force: true });
  134. }
  135. }, 120_000);