test-sea-runtime-tools-e2e.mjs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env node
  2. import assert from "node:assert/strict";
  3. import { spawnSync } from "node:child_process";
  4. import { createHash } from "node:crypto";
  5. import {
  6. chmodSync,
  7. copyFileSync,
  8. existsSync,
  9. mkdtempSync,
  10. readFileSync,
  11. rmSync,
  12. statSync,
  13. } from "node:fs";
  14. import { tmpdir } from "node:os";
  15. import { basename, dirname, join, resolve } from "node:path";
  16. import process from "node:process";
  17. import { fileURLToPath } from "node:url";
  18. import {
  19. NATIVE_SEARCH_TOOL_VERSIONS,
  20. resolveNativeSearchReleasePlan,
  21. } from "./native-search-tools-config.mjs";
  22. import {
  23. hostTarget,
  24. outputBinaryName,
  25. } from "../apps/zcode-cli/packages/cli/scripts/sea-targets.mjs";
  26. import { resolveSpawnRuntimeOptions } from "./spawn-command.mjs";
  27. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  28. const cliWorkspace = join(repoRoot, "apps", "zcode-cli");
  29. const cliPackageRoot = join(cliWorkspace, "packages", "cli");
  30. function execute(command, args, { cwd = repoRoot, env = process.env, stdio = "inherit" } = {}) {
  31. const result = spawnSync(command, args, {
  32. cwd,
  33. encoding: "utf8",
  34. env,
  35. stdio,
  36. ...resolveSpawnRuntimeOptions(command),
  37. });
  38. if (result.error) throw result.error;
  39. assert.equal(
  40. result.status,
  41. 0,
  42. `${command} ${args.join(" ")} exited with ${result.status}\nstdout:\n${result.stdout ?? ""}\nstderr:\n${result.stderr ?? ""}`,
  43. );
  44. return result;
  45. }
  46. function sha256(path) {
  47. return createHash("sha256").update(readFileSync(path)).digest("hex");
  48. }
  49. function runtimeToolFixtures(releasePlan) {
  50. const toolIds = releasePlan.runtimeToolIds;
  51. return toolIds.map((toolId) => {
  52. const baseBinaryName = toolId === "ripgrep" ? "rg" : toolId;
  53. const binaryName = releasePlan.platform === "win32" ? `${baseBinaryName}.exe` : baseBinaryName;
  54. const sourcePath = join(
  55. repoRoot,
  56. "packages",
  57. "desktop",
  58. "bundled-tools",
  59. releasePlan.platformKey,
  60. toolId,
  61. binaryName,
  62. );
  63. assert.ok(existsSync(sourcePath), `missing host runtime tool ${toolId}: ${sourcePath}`);
  64. return {
  65. binaryName,
  66. envVar:
  67. toolId === "bfs"
  68. ? "ZCODE_BFS_BINARY"
  69. : toolId === "ugrep"
  70. ? "ZCODE_UGREP_BINARY"
  71. : "ZCODE_RG_BINARY",
  72. id: toolId,
  73. sha256: sha256(sourcePath),
  74. sourcePath,
  75. version: NATIVE_SEARCH_TOOL_VERSIONS[toolId],
  76. };
  77. });
  78. }
  79. function main() {
  80. assert.ok(
  81. process.platform === "darwin" || process.platform === "linux" || process.platform === "win32",
  82. "SEA native runtime tool E2E must run on macOS, Linux, or Windows",
  83. );
  84. const releasePlan = resolveNativeSearchReleasePlan();
  85. assert.equal(
  86. releasePlan.enabled,
  87. true,
  88. `native runtime tools are not enabled for ${releasePlan.platformKey}`,
  89. );
  90. const target = hostTarget();
  91. const fixtures = runtimeToolFixtures(releasePlan);
  92. execute("pnpm", ["--dir", cliWorkspace, "build"]);
  93. execute("pnpm", [
  94. "--dir",
  95. cliWorkspace,
  96. "--filter",
  97. "@zcode/cli",
  98. "sea",
  99. "--",
  100. "--target",
  101. target,
  102. ]);
  103. const e2eRoot = mkdtempSync(join(tmpdir(), "zcode-sea-runtime-tools-e2e-"));
  104. try {
  105. const isolatedBinary = join(e2eRoot, basename(outputBinaryName(target)));
  106. copyFileSync(join(cliPackageRoot, "dist", outputBinaryName(target)), isolatedBinary);
  107. if (process.platform !== "win32") chmodSync(isolatedBinary, 0o755);
  108. const storageRoot = join(e2eRoot, "storage");
  109. const seaEnv = {
  110. ...process.env,
  111. ZCODE_STORAGE_DIR: storageRoot,
  112. };
  113. delete seaEnv.ZCODE_BFS_BINARY;
  114. delete seaEnv.ZCODE_RG_BINARY;
  115. delete seaEnv.ZCODE_UGREP_BINARY;
  116. execute(isolatedBinary, ["--version"], {
  117. cwd: e2eRoot,
  118. env: seaEnv,
  119. });
  120. const extractedPaths = {};
  121. const initialStats = {};
  122. for (const fixture of fixtures) {
  123. const binaryPath = join(
  124. storageRoot,
  125. "cache",
  126. "runtime_tools",
  127. target,
  128. fixture.id,
  129. `${fixture.version}-${fixture.sha256}`,
  130. fixture.binaryName,
  131. );
  132. assert.ok(existsSync(binaryPath), `SEA did not extract ${fixture.id}: ${binaryPath}`);
  133. assert.equal(sha256(binaryPath), fixture.sha256, `${fixture.id} cache hash mismatch`);
  134. if (process.platform !== "win32") {
  135. assert.notEqual(statSync(binaryPath).mode & 0o111, 0, `${fixture.id} is not executable`);
  136. }
  137. execute(binaryPath, ["--version"], {
  138. cwd: e2eRoot,
  139. });
  140. extractedPaths[fixture.envVar] = binaryPath;
  141. const binaryStat = statSync(binaryPath);
  142. initialStats[fixture.id] = {
  143. ino: binaryStat.ino,
  144. mtimeMs: binaryStat.mtimeMs,
  145. };
  146. }
  147. execute(isolatedBinary, ["--version"], {
  148. cwd: e2eRoot,
  149. env: seaEnv,
  150. });
  151. for (const fixture of fixtures) {
  152. const currentStat = statSync(extractedPaths[fixture.envVar]);
  153. assert.deepEqual(
  154. { ino: currentStat.ino, mtimeMs: currentStat.mtimeMs },
  155. initialStats[fixture.id],
  156. `${fixture.id} was unnecessarily re-extracted`,
  157. );
  158. }
  159. execute(process.execPath, [join(repoRoot, "scripts", "test-native-search-tools-e2e.mjs")], {
  160. env: {
  161. ...process.env,
  162. ...extractedPaths,
  163. },
  164. });
  165. console.log(`PASS SEA runtime tools E2E (${target})`);
  166. } finally {
  167. rmSync(e2eRoot, { force: true, recursive: true });
  168. }
  169. }
  170. try {
  171. main();
  172. } catch (error) {
  173. console.error(error instanceof Error ? error.stack : error);
  174. process.exitCode = 1;
  175. }