Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

shell.test.ts

128 lines3.9 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import {
4 extractPathFromShellOutput,
5 readEnvironmentFromLoginShell,
6 readPathFromLoginShell,
7} from "./shell";
8​
9describe("extractPathFromShellOutput", () => {
10 it("extracts the path between capture markers", () => {
11 expect(
12 extractPathFromShellOutput(
13 "__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\n",
14 ),
15 ).toBe("/opt/homebrew/bin:/usr/bin");
16 });
17​
18 it("ignores shell startup noise around the capture markers", () => {
19 expect(
20 extractPathFromShellOutput(
21 "Welcome to fish\n__T3CODE_PATH_START__\n/opt/homebrew/bin:/usr/bin\n__T3CODE_PATH_END__\nBye\n",
22 ),
23 ).toBe("/opt/homebrew/bin:/usr/bin");
24 });
25​
26 it("returns null when the markers are missing", () => {
27 expect(extractPathFromShellOutput("/opt/homebrew/bin /usr/bin")).toBeNull();
28 });
29});
30​
31describe("readPathFromLoginShell", () => {
32 it("uses a shell-agnostic printenv PATH probe", () => {
33 const execFile = vi.fn<
34 (
35 file: string,
36 args: ReadonlyArray<string>,
37 options: { encoding: "utf8"; timeout: number },
38 ) => string
39 >(() => "__T3CODE_ENV_PATH_START__\n/a:/b\n__T3CODE_ENV_PATH_END__\n");
40​
41 expect(readPathFromLoginShell("/opt/homebrew/bin/fish", execFile)).toBe("/a:/b");
42 expect(execFile).toHaveBeenCalledTimes(1);
43​
44 const firstCall = execFile.mock.calls[0] as
45 | [string, ReadonlyArray<string>, { encoding: "utf8"; timeout: number }]
46 | undefined;
47 expect(firstCall).toBeDefined();
48 if (!firstCall) {
49 throw new Error("Expected execFile to be called");
50 }
51​
52 const [shell, args, options] = firstCall;
53 expect(shell).toBe("/opt/homebrew/bin/fish");
54 expect(args).toHaveLength(2);
55 expect(args?.[0]).toBe("-ilc");
56 expect(args?.[1]).toContain("printenv PATH || true");
57 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_START__");
58 expect(args?.[1]).toContain("__T3CODE_ENV_PATH_END__");
59 expect(options).toEqual({ encoding: "utf8", timeout: 5000 });
60 });
61});
62​
63describe("readEnvironmentFromLoginShell", () => {
64 it("extracts multiple environment variables from a login shell command", () => {
65 const execFile = vi.fn<
66 (
67 file: string,
68 args: ReadonlyArray<string>,
69 options: { encoding: "utf8"; timeout: number },
70 ) => string
71 >(() =>
72 [
73 "__T3CODE_ENV_PATH_START__",
74 "/a:/b",
75 "__T3CODE_ENV_PATH_END__",
76 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
77 "/tmp/secretive.sock",
78 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
79 ].join("\n"),
80 );
81​
82 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
83 PATH: "/a:/b",
84 SSH_AUTH_SOCK: "/tmp/secretive.sock",
85 });
86 expect(execFile).toHaveBeenCalledTimes(1);
87 });
88​
89 it("omits environment variables that are missing or empty", () => {
90 const execFile = vi.fn<
91 (
92 file: string,
93 args: ReadonlyArray<string>,
94 options: { encoding: "utf8"; timeout: number },
95 ) => string
96 >(() =>
97 [
98 "__T3CODE_ENV_PATH_START__",
99 "/a:/b",
100 "__T3CODE_ENV_PATH_END__",
101 "__T3CODE_ENV_SSH_AUTH_SOCK_START__",
102 "__T3CODE_ENV_SSH_AUTH_SOCK_END__",
103 ].join("\n"),
104 );
105​
106 expect(readEnvironmentFromLoginShell("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"], execFile)).toEqual({
107 PATH: "/a:/b",
108 });
109 });
110​
111 it("preserves surrounding whitespace in captured values", () => {
112 const execFile = vi.fn<
113 (
114 file: string,
115 args: ReadonlyArray<string>,
116 options: { encoding: "utf8"; timeout: number },
117 ) => string
118 >(() =>
119 ["__T3CODE_ENV_CUSTOM_VAR_START__", " padded value ", "__T3CODE_ENV_CUSTOM_VAR_END__"].join(
120 "\n",
121 ),
122 );
123​
124 expect(readEnvironmentFromLoginShell("/bin/zsh", ["CUSTOM_VAR"], execFile)).toEqual({
125 CUSTOM_VAR: " padded value ",
126 });
127 });
128});