macElectronLocales.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { afterEach, expect, it } from "vitest";
  2. import { mkdirSync, mkdtempSync, rmSync } from "node:fs";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import {
  6. MAC_ELECTRON_LANGUAGES,
  7. findMissingMacElectronLocales,
  8. resolveMacElectronFrameworkResourcesDir,
  9. } from "../scripts/mac-electron-locales.mjs";
  10. const tempDirs: string[] = [];
  11. afterEach(() => {
  12. for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
  13. });
  14. function createPackagedApp(lprojNames: string[]) {
  15. const root = mkdtempSync(join(tmpdir(), "zcode-mac-locales-"));
  16. tempDirs.push(root);
  17. const appPath = join(root, "ZCode.app");
  18. const resourcesDir = resolveMacElectronFrameworkResourcesDir(appPath);
  19. mkdirSync(resourcesDir, { recursive: true });
  20. for (const name of lprojNames) mkdirSync(join(resourcesDir, `${name}.lproj`));
  21. return resourcesDir;
  22. }
  23. it("保留下来的 en/zh_CN lproj 通过校验", () => {
  24. const resourcesDir = createPackagedApp(["en", "zh_CN"]);
  25. expect(findMissingMacElectronLocales(resourcesDir)).toEqual([]);
  26. });
  27. it("lproj 被全部删掉时报出缺失的语言包", () => {
  28. const resourcesDir = createPackagedApp([]);
  29. expect(findMissingMacElectronLocales(resourcesDir)).toEqual(["en", "zh_CN"]);
  30. });
  31. it("白名单写成 Windows 的 pak 名时同样报缺失", () => {
  32. const resourcesDir = createPackagedApp(["en", "zh_CN"]);
  33. expect(findMissingMacElectronLocales(resourcesDir, ["en-US", "zh-CN"])).toEqual([
  34. "en-US",
  35. "zh-CN",
  36. ]);
  37. });
  38. it("白名单就是打包配置里 mac 段使用的值", async () => {
  39. const { default: config } = await import("../electron-builder.config.js");
  40. expect(config.mac?.electronLanguages).toBe(MAC_ELECTRON_LANGUAGES);
  41. });