test-native-search-tools-e2e.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/usr/bin/env node
  2. import assert from "node:assert/strict";
  3. import { spawnSync } from "node:child_process";
  4. import { existsSync, mkdtempSync, rmSync } from "node:fs";
  5. import { tmpdir } from "node:os";
  6. import { dirname, join, resolve } from "node:path";
  7. import process from "node:process";
  8. import { fileURLToPath } from "node:url";
  9. import {
  10. isMacosRosettaTarget,
  11. normalizeNativeSearchArch,
  12. normalizeNativeSearchPlatform,
  13. resolveNativeSearchBuildPlan,
  14. } from "./native-search-tools-config.mjs";
  15. import { buildNativeSearchTools } from "./build-native-search-tools.mjs";
  16. import { downloadNativeSearchTools } from "./download-native-search-tools.mjs";
  17. import { verifyBuiltNativeSearchTools } from "./native-search-tools-verify.mjs";
  18. import { resolveSpawnRuntimeOptions } from "./spawn-command.mjs";
  19. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  20. function execute(command, args, { cwd, env } = {}) {
  21. const result = spawnSync(command, args, {
  22. cwd,
  23. env,
  24. encoding: "utf8",
  25. stdio: ["ignore", "pipe", "pipe"],
  26. ...resolveSpawnRuntimeOptions(command),
  27. });
  28. if (result.error) throw result.error;
  29. return result;
  30. }
  31. function expectStatus(result, status, label) {
  32. assert.equal(
  33. result.status,
  34. status,
  35. `${label} exited with ${result.status}\nstdout:\n${result.stdout ?? ""}\nstderr:\n${result.stderr ?? ""}`,
  36. );
  37. }
  38. function verifyBashEmbeddedSearch(plan) {
  39. const buildResult = execute(
  40. "pnpm",
  41. ["--dir", repoRoot, "--filter", "@zcode/bootstrap^...", "--if-present", "build"],
  42. { cwd: repoRoot, env: process.env },
  43. );
  44. expectStatus(buildResult, 0, "Bash embedded search dependency build");
  45. const integrationResult = execute(
  46. "pnpm",
  47. [
  48. "--dir",
  49. join(repoRoot, "apps/zcode-cli"),
  50. "exec",
  51. "vitest",
  52. "run",
  53. "packages/bootstrap/tests/embedded-search-native.e2e.test.ts",
  54. "--detectAsyncLeaks",
  55. "--no-file-parallelism",
  56. ],
  57. {
  58. cwd: repoRoot,
  59. env: {
  60. ...process.env,
  61. ...(plan.bfsPath ? { ZCODE_BFS_BINARY: plan.bfsPath } : {}),
  62. ZCODE_REQUIRE_NATIVE_SEARCH_E2E: "1",
  63. ZCODE_RG_BINARY: plan.rgPath,
  64. ZCODE_UGREP_BINARY: plan.ugrepPath,
  65. },
  66. },
  67. );
  68. expectStatus(integrationResult, 0, "Bash embedded search integration E2E");
  69. assert.doesNotMatch(
  70. `${integrationResult.stdout}\n${integrationResult.stderr}`,
  71. /Async Leaks \d+/u,
  72. "Bash embedded search integration E2E leaked asynchronous resources",
  73. );
  74. }
  75. async function resolveNativeSearchPlan(outputDir) {
  76. const targetPlatform = process.env.ZCODE_TARGET_OS?.trim() || process.platform;
  77. const targetArch = process.env.ZCODE_TARGET_ARCH?.trim() || process.arch;
  78. const bfsPath = process.env.ZCODE_BFS_BINARY?.trim();
  79. const rgPath = process.env.ZCODE_RG_BINARY?.trim();
  80. const ugrepPath = process.env.ZCODE_UGREP_BINARY?.trim();
  81. const normalizedPlatform = normalizeNativeSearchPlatform(targetPlatform);
  82. const requiresBfs = normalizedPlatform !== "win32";
  83. if (!requiresBfs) {
  84. assert.equal(
  85. Boolean(bfsPath),
  86. false,
  87. "Windows native search E2E must leave find on the Git Bash/system fallback",
  88. );
  89. }
  90. const hasPrebuiltNativeTools = Boolean(rgPath && ugrepPath && (!requiresBfs || bfsPath));
  91. if (!hasPrebuiltNativeTools) {
  92. assert.equal(
  93. Boolean(bfsPath || rgPath || ugrepPath),
  94. false,
  95. requiresBfs
  96. ? "ZCODE_BFS_BINARY, ZCODE_RG_BINARY, and ZCODE_UGREP_BINARY must be provided together"
  97. : "ZCODE_RG_BINARY and ZCODE_UGREP_BINARY must be provided together",
  98. );
  99. buildNativeSearchTools({
  100. platform: targetPlatform,
  101. arch: targetArch,
  102. outputDir,
  103. quiet: true,
  104. runUpstreamTests: normalizedPlatform !== "win32",
  105. });
  106. return downloadNativeSearchTools({
  107. platform: targetPlatform,
  108. arch: targetArch,
  109. outputDir,
  110. });
  111. }
  112. if (requiresBfs) assert.ok(existsSync(bfsPath), `missing prebuilt bfs: ${bfsPath}`);
  113. assert.ok(existsSync(rgPath), `missing prebuilt ripgrep: ${rgPath}`);
  114. assert.ok(existsSync(ugrepPath), `missing prebuilt ugrep: ${ugrepPath}`);
  115. const basePlan = resolveNativeSearchBuildPlan({
  116. platform: targetPlatform,
  117. arch: targetArch,
  118. outputDir,
  119. });
  120. const resolvedPlan = {
  121. ...basePlan,
  122. bfsPath: bfsPath ? resolve(bfsPath) : basePlan.bfsPath,
  123. rgPath: resolve(rgPath),
  124. ugrepPath: resolve(ugrepPath),
  125. binaries: {
  126. ...basePlan.binaries,
  127. ...(bfsPath ? { bfs: resolve(bfsPath) } : {}),
  128. ripgrep: resolve(rgPath),
  129. ugrep: resolve(ugrepPath),
  130. },
  131. };
  132. return resolvedPlan;
  133. }
  134. async function main() {
  135. assert.ok(
  136. process.platform === "darwin" || process.platform === "linux" || process.platform === "win32",
  137. "this E2E must run on macOS, Linux, or Windows",
  138. );
  139. const e2eRoot = mkdtempSync(join(tmpdir(), "zcode-native-search-e2e-"));
  140. try {
  141. const plan = await resolveNativeSearchPlan(join(e2eRoot, "runtime-tools"));
  142. const hostPlatform = normalizeNativeSearchPlatform(process.platform);
  143. const hostArch = normalizeNativeSearchArch(process.arch);
  144. assert.equal(plan.platform, hostPlatform);
  145. assert.ok(
  146. plan.arch === hostArch ||
  147. isMacosRosettaTarget({
  148. platform: plan.platform,
  149. arch: plan.arch,
  150. hostPlatform,
  151. hostArch,
  152. }),
  153. // Bugfix: Apple Silicon release runner 会通过 Rosetta 执行同机生成的 x64 产物。
  154. `native search E2E cannot execute ${plan.platformKey} on ${hostPlatform}-${hostArch}`,
  155. );
  156. assert.ok(existsSync(plan.rgPath), `missing bundled ripgrep: ${plan.rgPath}`);
  157. verifyBuiltNativeSearchTools(plan);
  158. verifyBashEmbeddedSearch(plan);
  159. console.log(`PASS native search E2E (${plan.platformKey})`);
  160. } finally {
  161. rmSync(e2eRoot, { recursive: true, force: true });
  162. }
  163. }
  164. try {
  165. await main();
  166. } catch (error) {
  167. console.error(error instanceof Error ? error.stack : error);
  168. process.exitCode = 1;
  169. }