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

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