runtimeToolResolver.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { chmodSync, mkdtempSync, mkdirSync, realpathSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { delimiter, dirname, join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import {
  6. appendPathEntries,
  7. buildRuntimeToolEnvPatch,
  8. findRuntimeToolBinary,
  9. prependPathEntries,
  10. } from "../src/runtime-tools/runtimeToolResolver.js";
  11. describe("runtimeToolResolver", () => {
  12. const originalCwd = process.cwd();
  13. const createdDirs: string[] = [];
  14. afterEach(() => {
  15. process.chdir(originalCwd);
  16. for (const dir of createdDirs.splice(0).reverse()) {
  17. rmSync(dir, { recursive: true, force: true });
  18. }
  19. });
  20. it("会发现内置 bfs 和 ugrep 并把两个原生目录追加到 PATH", () => {
  21. const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-native-search-tools-"));
  22. createdDirs.push(sandboxRoot);
  23. const appRepoDir = join(sandboxRoot, "z-code-2");
  24. const userBinDir = join(sandboxRoot, "user-bin");
  25. const platformKey = `${process.platform}-${process.arch}`;
  26. const executableSuffix = process.platform === "win32" ? ".exe" : "";
  27. const bfsPath = join(
  28. appRepoDir,
  29. "packages",
  30. "desktop",
  31. "bundled-tools",
  32. platformKey,
  33. "bfs",
  34. `bfs${executableSuffix}`,
  35. );
  36. const ugrepPath = join(
  37. appRepoDir,
  38. "packages",
  39. "desktop",
  40. "bundled-tools",
  41. platformKey,
  42. "ugrep",
  43. `ugrep${executableSuffix}`,
  44. );
  45. for (const binaryPath of [bfsPath, ugrepPath]) {
  46. mkdirSync(dirname(binaryPath), { recursive: true });
  47. writeFileSync(binaryPath, "native search\n");
  48. if (process.platform !== "win32") {
  49. chmodSync(binaryPath, 0o755);
  50. }
  51. }
  52. process.chdir(appRepoDir);
  53. const envPatch = buildRuntimeToolEnvPatch(["bfs", "ugrep"], {
  54. PATH: userBinDir,
  55. });
  56. expect(realpathSync(envPatch.ZCODE_BFS_BINARY!)).toBe(realpathSync(bfsPath));
  57. expect(realpathSync(envPatch.ZCODE_UGREP_BINARY!)).toBe(realpathSync(ugrepPath));
  58. const pathEntries = envPatch.PATH?.split(delimiter) ?? [];
  59. expect(realpathSync(pathEntries.at(-2)!)).toBe(realpathSync(dirname(bfsPath)));
  60. expect(realpathSync(pathEntries.at(-1)!)).toBe(realpathSync(dirname(ugrepPath)));
  61. });
  62. it("会从 bundled-tools 平台目录发现 ripgrep binary 并追加到 PATH", () => {
  63. const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-ripgrep-tool-"));
  64. createdDirs.push(sandboxRoot);
  65. const appRepoDir = join(sandboxRoot, "z-code-2");
  66. const userBinDir = join(sandboxRoot, "user-bin");
  67. const platformKey = `${process.platform}-${process.arch}`;
  68. const binaryName = process.platform === "win32" ? "rg.exe" : "rg";
  69. const binaryPath = join(
  70. appRepoDir,
  71. "packages",
  72. "desktop",
  73. "bundled-tools",
  74. platformKey,
  75. "ripgrep",
  76. binaryName,
  77. );
  78. mkdirSync(dirname(binaryPath), { recursive: true });
  79. writeFileSync(binaryPath, "rg\n");
  80. if (process.platform !== "win32") {
  81. chmodSync(binaryPath, 0o755);
  82. }
  83. process.chdir(appRepoDir);
  84. expect(realpathSync(findRuntimeToolBinary("ripgrep", { PATH: userBinDir })!)).toBe(
  85. realpathSync(binaryPath),
  86. );
  87. const envPatch = buildRuntimeToolEnvPatch(["ripgrep"], {
  88. PATH: userBinDir,
  89. });
  90. expect(realpathSync(envPatch.ZCODE_RG_BINARY!)).toBe(realpathSync(binaryPath));
  91. expect(realpathSync(envPatch.PATH?.split(delimiter).at(-1) ?? "")).toBe(
  92. realpathSync(dirname(binaryPath)),
  93. );
  94. });
  95. it("优先使用调用方环境指定的 runtime binary", () => {
  96. const sandboxRoot = mkdtempSync(join(tmpdir(), "zcode-runtime-tool-env-"));
  97. createdDirs.push(sandboxRoot);
  98. const binaryPath = join(sandboxRoot, process.platform === "win32" ? "bfs.exe" : "bfs");
  99. writeFileSync(binaryPath, "bfs\n");
  100. if (process.platform !== "win32") {
  101. chmodSync(binaryPath, 0o755);
  102. }
  103. expect(
  104. findRuntimeToolBinary("bfs", {
  105. PATH: "",
  106. ZCODE_BFS_BINARY: binaryPath,
  107. }),
  108. ).toBe(binaryPath);
  109. });
  110. it("prependPathEntries 会去重并保留前置优先级", () => {
  111. const currentPath = ["user-bin", "system-bin", "user-bin"].join(delimiter);
  112. const expectedPath = ["custom-bin", "user-bin", "system-bin"].join(delimiter);
  113. expect(prependPathEntries(currentPath, ["custom-bin", "user-bin"])).toBe(expectedPath);
  114. });
  115. it("appendPathEntries 会去重并保留用户 PATH 优先级", () => {
  116. const currentPath = ["user-bin", "system-bin", "user-bin"].join(delimiter);
  117. const expectedPath = ["user-bin", "system-bin", "custom-bin"].join(delimiter);
  118. expect(appendPathEntries(currentPath, ["custom-bin", "user-bin"])).toBe(expectedPath);
  119. });
  120. });