bash-startup-script.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { mkdtempSync, readdirSync, readFileSync, rmSync, statSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import {
  6. applyBashSourceScript,
  7. applyBashSourcesToExecutionRequest,
  8. materializeBashInternalSourceScript,
  9. } from "../src/exec/bash-startup-script.js";
  10. describe("bash source script materializer", () => {
  11. it("writes a hash-scoped script and sources it before the command", () => {
  12. const rootDir = mkdtempSync(join(tmpdir(), "zcode-bash-startup-"));
  13. try {
  14. const materialized = materializeBashInternalSourceScript(
  15. {
  16. id: "session/with spaces:bash-startup",
  17. content: "zcode_startup_marker() { echo startup-ok; }\n",
  18. },
  19. {
  20. rootDir,
  21. sessionId: "session/with spaces",
  22. shellDialect: "posix",
  23. },
  24. );
  25. expect(materialized).toBeDefined();
  26. expect(materialized?.path).toContain("session-with-spaces");
  27. expect(readFileSync(materialized!.path, "utf8")).toBe(
  28. "zcode_startup_marker() { echo startup-ok; }\n",
  29. );
  30. expect(statSync(materialized!.path).isFile()).toBe(true);
  31. const command = applyBashSourceScript("zcode_startup_marker", materialized!);
  32. expect(command).toBe(`. '${materialized!.path}'\nzcode_startup_marker`);
  33. } finally {
  34. rmSync(rootDir, { force: true, recursive: true });
  35. }
  36. });
  37. it("skips CMD and legacy shell dialects", () => {
  38. const rootDir = mkdtempSync(join(tmpdir(), "zcode-bash-startup-"));
  39. try {
  40. for (const shellDialect of ["cmd", "legacy-shell"] as const) {
  41. expect(
  42. materializeBashInternalSourceScript(
  43. {
  44. id: "startup",
  45. content: "echo no\n",
  46. },
  47. {
  48. rootDir,
  49. sessionId: "session",
  50. shellDialect,
  51. },
  52. ),
  53. ).toBeUndefined();
  54. }
  55. } finally {
  56. rmSync(rootDir, { force: true, recursive: true });
  57. }
  58. });
  59. it("converts materialized Windows paths for Git Bash source commands", () => {
  60. const materialized = {
  61. path: "C:\\Users\\me\\AppData\\Local\\ZCode\\startup.sh",
  62. shellPath: "/c/Users/me/AppData/Local/ZCode/startup.sh",
  63. };
  64. expect(applyBashSourceScript("echo ok", materialized)).toBe(
  65. ". /c/Users/me/AppData/Local/ZCode/startup.sh\necho ok",
  66. );
  67. });
  68. it("sources shell init snapshots before embedded search prelude scripts", () => {
  69. const rootDir = mkdtempSync(join(tmpdir(), "zcode-bash-startup-"));
  70. try {
  71. const request = {
  72. command: {
  73. mode: "shell" as const,
  74. command: "find . -name '*.ts'",
  75. shellProfile: "posix-bash" as const,
  76. },
  77. bashPrelude: {
  78. kind: "embedded-search" as const,
  79. backend: {
  80. kind: "internal-cli" as const,
  81. command: "zcode",
  82. args: ["__internal-search"],
  83. },
  84. },
  85. };
  86. const rewritten = applyBashSourcesToExecutionRequest(request, {
  87. leadingSources: [
  88. {
  89. path: "/tmp/init snapshot.sh",
  90. shellPath: "/tmp/init snapshot.sh",
  91. optional: true,
  92. },
  93. ],
  94. rootDir,
  95. sessionId: "session",
  96. shellDialect: "posix",
  97. });
  98. expect(rewritten.command.command).toMatch(
  99. /^\. '\/tmp\/init snapshot\.sh' 2>\/dev\/null \|\| true\n\. '.+embedded-search-startup-.+\.sh'\nfind \. -name '\*\.ts'$/u,
  100. );
  101. } finally {
  102. rmSync(rootDir, { force: true, recursive: true });
  103. }
  104. });
  105. it("keeps Git Bash shell snapshot sources ahead of converted embedded search prelude scripts", () => {
  106. const rootDir = mkdtempSync(join(tmpdir(), "zcode-bash-startup-"));
  107. try {
  108. const request = {
  109. command: {
  110. mode: "shell" as const,
  111. command: "grep needle file.txt",
  112. shellProfile: "posix-bash" as const,
  113. },
  114. bashPrelude: {
  115. kind: "embedded-search" as const,
  116. backend: {
  117. kind: "internal-cli" as const,
  118. command: "C:\\Program Files\\ZCode\\zcode.exe",
  119. args: ["C:\\Users\\me\\z-code\\apps\\zcode-cli\\src\\run.ts", "__internal-search"],
  120. },
  121. },
  122. };
  123. const rewritten = applyBashSourcesToExecutionRequest(request, {
  124. leadingSources: [
  125. {
  126. path: "C:\\Users\\me\\AppData\\Local\\ZCode\\shell-snapshots\\snapshot-bash-1.sh",
  127. shellPath: "/c/Users/me/AppData/Local/ZCode/shell-snapshots/snapshot-bash-1.sh",
  128. optional: true,
  129. },
  130. ],
  131. rootDir,
  132. sessionId: "session",
  133. shellDialect: "git-bash",
  134. });
  135. expect(rewritten.command.command).toMatch(
  136. /^\. \/c\/Users\/me\/AppData\/Local\/ZCode\/shell-snapshots\/snapshot-bash-1\.sh 2>\/dev\/null \|\| true\n\. '.+embedded-search-startup-.+\.sh'\ngrep needle file\.txt$/u,
  137. );
  138. const startupDir = join(rootDir, "bash-startup", "session");
  139. const startupFile = readdirSync(startupDir).find((file) =>
  140. file.startsWith("embedded-search-startup-"),
  141. );
  142. expect(startupFile).toBeDefined();
  143. const startupContent = readFileSync(join(startupDir, startupFile!), "utf8");
  144. expect(startupContent).toContain(
  145. "command '/c/Program Files/ZCode/zcode.exe' /c/Users/me/z-code/apps/zcode-cli/src/run.ts __internal-search grep \"$@\"",
  146. );
  147. } finally {
  148. rmSync(rootDir, { force: true, recursive: true });
  149. }
  150. });
  151. });