nsis-install-section-patch.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { describe, expect, it } from "vitest";
  2. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  3. import { createRequire } from "node:module";
  4. import { tmpdir } from "node:os";
  5. import { dirname, join } from "node:path";
  6. import {
  7. PATCH_MARKER,
  8. patchNsisInstallUtilSource,
  9. patchNsisInstallSectionFile,
  10. patchNsisInstallSectionSource,
  11. restoreNsisInstallSectionFileSync,
  12. } from "../scripts/patch-nsis-install-section.mjs";
  13. function createInstallSectionFixture(eol = "\r\n") {
  14. return [
  15. "InitPluginsDir",
  16. "",
  17. "${IfNot} ${Silent}",
  18. " SetDetailsPrint none",
  19. "${endif}",
  20. "",
  21. "!insertmacro uninstallOldVersion SHELL_CONTEXT",
  22. "!insertmacro handleUninstallResult SHELL_CONTEXT",
  23. "",
  24. '${if} $installMode == "all"',
  25. " !insertmacro uninstallOldVersion HKEY_CURRENT_USER",
  26. " !insertmacro handleUninstallResult HKEY_CURRENT_USER",
  27. "${endIf}",
  28. "",
  29. "SetOutPath $INSTDIR",
  30. "",
  31. "!insertmacro installApplicationFiles",
  32. "!insertmacro registryAddInstallInfo",
  33. "!insertmacro addStartMenuLink $keepShortcuts",
  34. "!insertmacro addDesktopLink $keepShortcuts",
  35. ].join(eol);
  36. }
  37. describe("NSIS installSection template patch", () => {
  38. it("当前真实模板可接入阶段,文件级重复调用幂等且可恢复原字节", async () => {
  39. const require = createRequire(import.meta.url);
  40. const source = await readFile(
  41. join(
  42. dirname(require.resolve("app-builder-lib/package.json")),
  43. "templates/nsis/installSection.nsh",
  44. ),
  45. "utf8",
  46. );
  47. const root = await mkdtemp(join(tmpdir(), "zcode-nsis-template-"));
  48. const path = join(root, "installSection.nsh");
  49. try {
  50. // 只修改隔离副本,不污染开发依赖;真实模板锚点变更不能被手写 fixture 掩盖。
  51. await writeFile(path, source);
  52. const first = await patchNsisInstallSectionFile(path);
  53. expect(first.changed).toBe(true);
  54. expect(first.originalSource).toBe(source);
  55. const patched = await readFile(path, "utf8");
  56. const sequence = [
  57. "!insertmacro customInstallSectionStarted",
  58. "!insertmacro customInstallCleanupStarted",
  59. "!insertmacro uninstallOldVersion SHELL_CONTEXT",
  60. "!insertmacro uninstallOldVersion HKEY_CURRENT_USER",
  61. "!insertmacro customInstallCleanupCompleted",
  62. "!insertmacro customInstallExtractStarted",
  63. "!insertmacro installApplicationFiles",
  64. "!insertmacro customInstallExtractCompleted",
  65. "!insertmacro customInstallShortcutsStarted",
  66. "!insertmacro addStartMenuLink",
  67. "!insertmacro customInstallShortcutsCompleted",
  68. ].map((marker) => patched.indexOf(marker));
  69. expect(
  70. sequence.every((index, offset) => index >= 0 && (!offset || index > sequence[offset - 1]!)),
  71. ).toBe(true);
  72. expect(await patchNsisInstallSectionFile(path)).toEqual({
  73. changed: false,
  74. originalSource: null,
  75. });
  76. restoreNsisInstallSectionFileSync({ filePath: path, originalSource: first.originalSource });
  77. expect(await readFile(path, "utf8")).toBe(source);
  78. } finally {
  79. await rm(root, { recursive: true, force: true });
  80. }
  81. });
  82. it("opens file details and inserts the install phases without changing CRLF", () => {
  83. const source = createInstallSectionFixture();
  84. const patched = patchNsisInstallSectionSource(source);
  85. expect(patched).toContain(PATCH_MARKER.replaceAll("\n", "\r\n"));
  86. expect(patched).toContain(" SetDetailsPrint listonly\r\n");
  87. expect(patched).toContain("!insertmacro customInstallExtractStarted\r\n");
  88. expect(patched).toContain("!insertmacro customInstallExtractCompleted\r\n");
  89. expect(patched).toContain("!insertmacro customInstallShortcutsStarted\r\n");
  90. expect(patched).toContain("!insertmacro customInstallShortcutsCompleted\r\n");
  91. expect(patched).not.toContain("SetDetailsPrint none");
  92. expect(patchNsisInstallSectionSource(patched)).toBe(patched);
  93. });
  94. it("模板结构变化时应立即失败,而不是静默生成缺少诊断的安装器", () => {
  95. expect(() => patchNsisInstallSectionSource("InitPluginsDir\n")).toThrow(
  96. "electron-builder installSection.nsh 缺少预期锚点",
  97. );
  98. });
  99. });
  100. describe("NSIS old uninstaller cleanup protection", () => {
  101. it("patches the real uninstaller before destructive cleanup and removes the false running-app prompt", async () => {
  102. const require = createRequire(import.meta.url);
  103. const source = await readFile(
  104. join(
  105. dirname(require.resolve("app-builder-lib/package.json")),
  106. "templates/nsis/include/installUtil.nsh",
  107. ),
  108. "utf8",
  109. );
  110. const patched = patchNsisInstallUtilSource(source);
  111. expect(patched.indexOf("!insertmacro customOldUninstallPrepare")).toBeLessThan(
  112. patched.indexOf("UninstallLoop:"),
  113. );
  114. expect(patched).toContain("!ifmacrodef customOldUninstallPrepare\n !insertmacro customOldUninstallPrepare\n!endif");
  115. expect(patched).not.toContain("ZCodeStopPreviousInstallProcesses");
  116. expect(patched.indexOf("!insertmacro customOldUninstallRetryFailed")).toBeGreaterThan(
  117. patched.indexOf("UninstallLoop:"),
  118. );
  119. expect(patched).toContain("!else\n MessageBox MB_RETRYCANCEL");
  120. expect(patchNsisInstallUtilSource(patched)).toBe(patched);
  121. expect(patchNsisInstallUtilSource(source.replaceAll("\n", "\r\n"))).toContain(
  122. "customOldUninstallPrepare\r\n",
  123. );
  124. expect(() => patchNsisInstallUtilSource("unknown template")).toThrow("缺少预期锚点");
  125. });
  126. });