Open History to see the last commit
1import { describe, expect, it, vi } from "vitest";
2
3import { extractPathFromShellOutput, readPathFromLoginShell } from "./shell";
4
5describe("extractPathFromShellOutput", () => {
6 it("extracts the path between capture markers", () => {
7 expect(
8 extractPathFromShellOutput("__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n"),
9 ).toBe("/opt/homebrew/bin:/usr/bin");
10 });
11
12 it("ignores shell startup noise around the capture markers", () => {
13 expect(
14 extractPathFromShellOutput(
15 "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n",
16 ),
17 ).toBe("/opt/homebrew/bin:/usr/bin");
18 });
19
20 it("returns null when the markers are missing", () => {
21 expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull();
22 });
23});
24
25describe("readPathFromLoginShell", () => {
26 it("uses a shell-agnostic printenv PATH probe", () => {
27 const execFile = vi.fn<
28 (
29 file: string,
30 args: ReadonlyArray<string>,
31 options: { encoding: "utf8"; timeout: number },
32 ) => string
33 >(() => "__T3CODE_PATH_START__\n/a:/b\n__T3CODE_PATH_END__\n");
34
35 expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b");
36 expect(execFile).toHaveBeenCalledTimes(1);
37
38 const firstCall = execFile.mock.calls[0] as
39 | [string, ReadonlyArray<string>, { encoding: "utf8"; timeout: number }]
40 | undefined;
41 expect(firstCall).toBeDefined();
42 if (!firstCall) {
43 throw new Error("Expected execFile to be called");
44 }
45
46 const [shell, args, options] = firstCall;
47 expect(shell).toBe("/opt/homebrew/bin/fish");
48 expect(args).toHaveLength(2);
49 expect(args?.[0]).toBe("-ilc");
50 expect(args?.[1]).toContain("printenv PATH");
51 expect(args?.[1]).toContain("__T3CODE_PATH_START__");
52 expect(args?.[1]).toContain("__T3CODE_PATH_END__");
53 expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
54 });
55});