test-installer-preserve-files.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { execFileSync } from "node:child_process";
  2. import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
  3. import { createRequire } from "node:module";
  4. import { tmpdir } from "node:os";
  5. import { resolve, join, dirname } from "node:path";
  6. import { fileURLToPath } from "node:url";
  7. const require = createRequire(import.meta.url);
  8. const scriptDir = dirname(fileURLToPath(import.meta.url));
  9. const desktopDir = resolve(scriptDir, "..");
  10. const workspaceDir = resolve(desktopDir, "../..");
  11. const fixturePath = resolve(desktopDir, "test/fixtures/installer-preserve-files.nsi");
  12. const installerNshPath = resolve(desktopDir, "build/installer.nsh");
  13. const appBuilderLibDir = dirname(require.resolve("app-builder-lib/package.json"));
  14. const { getBinFromUrl } = require(resolve(appBuilderLibDir, "out/binDownload.js"));
  15. const NSIS_CHECKSUM =
  16. "VKMiizYdmNdJOWpRGz4trl4lD++BvYP2irAXpMilheUP0pc93iKlWAoP843Vlraj8YG19CVn0j+dCo/hURz9+Q==";
  17. const NSIS_RESOURCES_CHECKSUM =
  18. "Dqd6g+2buwwvoG1Vyf6BHR1b+25QMmPcwZx40atOT57gH27rkjOei1L0JTldxZu4NFoEmW4kJgZ3DlSWVON3+Q==";
  19. if (process.platform !== "win32") {
  20. process.stdout.write("[installer-preserve-files] skipped: Windows only\n");
  21. } else {
  22. const [nsisDir, nsisResourcesDir] = await Promise.all([
  23. getBinFromUrl("nsis-3.0.4.1", "nsis-3.0.4.1.7z", NSIS_CHECKSUM),
  24. getBinFromUrl("nsis-resources-3.4.1", "nsis-resources-3.4.1.7z", NSIS_RESOURCES_CHECKSUM),
  25. ]);
  26. const tempRoot = mkdtempSync(join(tmpdir(), "zcode-preserve-files-"));
  27. try {
  28. const makensisPath = resolve(nsisDir, "makensis.exe");
  29. const uninstallerOutput = join(tempRoot, "uninstaller.exe");
  30. execFileSync(
  31. makensisPath,
  32. [
  33. "/V2",
  34. "/WX",
  35. "/DBUILD_UNINSTALLER",
  36. `/DNSIS_INCLUDE_DIR=${resolve(nsisDir, "Include")}`,
  37. `/DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${resolve(appBuilderLibDir, "templates/nsis/include")}`,
  38. `/DNSIS_PLUGIN_DIR=${resolve(nsisResourcesDir, "plugins/x86-unicode")}`,
  39. `/DZCODE_INSTALLER_NSH=${installerNshPath}`,
  40. `/DFIXTURE_EXE=${process.env.ComSpec ?? "C:\\Windows\\System32\\cmd.exe"}`,
  41. `/DOUTPUT_FILE=${uninstallerOutput}`,
  42. fixturePath,
  43. ],
  44. { cwd: workspaceDir, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] },
  45. );
  46. for (const [scenario, omitManifest] of [
  47. ["manifest", false],
  48. ["legacy", true],
  49. ]) {
  50. const outputPath = join(tempRoot, `${scenario}.exe`);
  51. const installDir = join(tempRoot, scenario);
  52. const compileArgs = [
  53. "/V2",
  54. `/DNSIS_INCLUDE_DIR=${resolve(nsisDir, "Include")}`,
  55. `/DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${resolve(appBuilderLibDir, "templates/nsis/include")}`,
  56. `/DNSIS_PLUGIN_DIR=${resolve(nsisResourcesDir, "plugins/x86-unicode")}`,
  57. `/DZCODE_INSTALLER_NSH=${installerNshPath}`,
  58. `/DFIXTURE_EXE=${process.env.ComSpec ?? "C:\\Windows\\System32\\cmd.exe"}`,
  59. `/DOUTPUT_FILE=${outputPath}`,
  60. fixturePath,
  61. ];
  62. if (omitManifest) compileArgs.splice(-1, 0, "/DOMIT_MANIFEST");
  63. execFileSync(makensisPath, compileArgs, {
  64. cwd: workspaceDir,
  65. encoding: "utf-8",
  66. stdio: ["ignore", "pipe", "pipe"],
  67. });
  68. execFileSync(outputPath, ["/S", `/D=${installDir}`], { encoding: "utf-8" });
  69. const ownedFileExists = existsSync(join(installDir, "owned-by-zcode.txt"));
  70. if (omitManifest !== ownedFileExists) {
  71. throw new Error(`${scenario}: ownership fallback mismatch`);
  72. }
  73. if (!existsSync(join(installDir, "user-data.txt"))) {
  74. throw new Error(`${scenario}: unrelated file was removed`);
  75. }
  76. if (readFileSync(join(installDir, "ZCode.exe")).length === 0) {
  77. throw new Error(`${scenario}: application payload was not installed`);
  78. }
  79. }
  80. const failureOutputPath = join(tempRoot, "failure.exe");
  81. const failureInstallDir = join(tempRoot, "failure");
  82. const failureLogPath = join(tempRoot, "failure-uninstaller.log");
  83. const failureCompileArgs = [
  84. "/V2",
  85. "/WX",
  86. "/DBUILD_UNINSTALLER",
  87. `/DNSIS_INCLUDE_DIR=${resolve(nsisDir, "Include")}`,
  88. `/DELECTRON_BUILDER_NSIS_INCLUDE_DIR=${resolve(appBuilderLibDir, "templates/nsis/include")}`,
  89. `/DNSIS_PLUGIN_DIR=${resolve(nsisResourcesDir, "plugins/x86-unicode")}`,
  90. `/DZCODE_INSTALLER_NSH=${installerNshPath}`,
  91. `/DFIXTURE_EXE=${process.env.ComSpec ?? "C:\\Windows\\System32\\cmd.exe"}`,
  92. `/DOUTPUT_FILE=${failureOutputPath}`,
  93. `/DZCODE_UNINSTALLER_LOG_PATH=${failureLogPath}`,
  94. "/DFAIL_DELETE",
  95. fixturePath,
  96. ];
  97. execFileSync(makensisPath, failureCompileArgs, {
  98. cwd: workspaceDir,
  99. encoding: "utf-8",
  100. stdio: ["ignore", "pipe", "pipe"],
  101. });
  102. let failureExitCode = 0;
  103. try {
  104. execFileSync(failureOutputPath, ["/S", `/D=${failureInstallDir}`], {
  105. encoding: "utf-8",
  106. });
  107. } catch (error) {
  108. failureExitCode = error.status ?? 0;
  109. }
  110. if (failureExitCode === 0) {
  111. throw new Error("manifest cleanup failure unexpectedly returned success");
  112. }
  113. const failureLog = readFileSync(failureLogPath, "utf8");
  114. for (const marker of ["cleanup-started", "cleanup-failed reason=permission-or-disk-space"]) {
  115. if (!failureLog.includes(marker)) {
  116. throw new Error(`uninstaller log is missing ${marker}: ${failureLog}`);
  117. }
  118. }
  119. if (!/\[pid=[1-9]\d*\]/.test(failureLog)) {
  120. throw new Error(`uninstaller log is missing a positive PID: ${failureLog}`);
  121. }
  122. process.stdout.write("[installer-preserve-files] passed\n");
  123. } finally {
  124. rmSync(tempRoot, { recursive: true, force: true });
  125. }
  126. }