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

syncShellEnvironment.test.ts

289 lines9.0 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import { syncShellEnvironment } from "./syncShellEnvironment.ts";
4​
5describe("syncShellEnvironment", () => {
6 it("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS", () => {
7 const env: NodeJS.ProcessEnv = {
8 SHELL: "/bin/zsh",
9 PATH: "/Users/test/.local/bin:/usr/bin",
10 };
11 const readEnvironment = vi.fn(() => ({
12 PATH: "/opt/homebrew/bin:/usr/bin",
13 SSH_AUTH_SOCK: "/tmp/secretive.sock",
14 HOMEBREW_PREFIX: "/opt/homebrew",
15 }));
16​
17 syncShellEnvironment(env, {
18 platform: "darwin",
19 readEnvironment,
20 });
21​
22 expect(readEnvironment).toHaveBeenCalledWith("/bin/zsh", [
23 "PATH",
24 "SSH_AUTH_SOCK",
25 "HOMEBREW_PREFIX",
26 "HOMEBREW_CELLAR",
27 "HOMEBREW_REPOSITORY",
28 "XDG_CONFIG_HOME",
29 "XDG_DATA_HOME",
30 ]);
31 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
32 expect(env.SSH_AUTH_SOCK).toBe("/tmp/secretive.sock");
33 expect(env.HOMEBREW_PREFIX).toBe("/opt/homebrew");
34 });
35​
36 it("preserves an inherited SSH_AUTH_SOCK value", () => {
37 const env: NodeJS.ProcessEnv = {
38 SHELL: "/bin/zsh",
39 PATH: "/usr/bin",
40 SSH_AUTH_SOCK: "/tmp/inherited.sock",
41 };
42 const readEnvironment = vi.fn(() => ({
43 PATH: "/opt/homebrew/bin:/usr/bin",
44 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
45 }));
46​
47 syncShellEnvironment(env, {
48 platform: "darwin",
49 readEnvironment,
50 });
51​
52 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
53 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
54 });
55​
56 it("preserves inherited values when the login shell omits them", () => {
57 const env: NodeJS.ProcessEnv = {
58 SHELL: "/bin/zsh",
59 PATH: "/usr/bin",
60 SSH_AUTH_SOCK: "/tmp/inherited.sock",
61 };
62 const readEnvironment = vi.fn(() => ({
63 PATH: "/opt/homebrew/bin:/usr/bin",
64 }));
65​
66 syncShellEnvironment(env, {
67 platform: "darwin",
68 readEnvironment,
69 });
70​
71 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
72 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
73 });
74​
75 it("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () => {
76 const env: NodeJS.ProcessEnv = {
77 SHELL: "/bin/zsh",
78 PATH: "/usr/bin",
79 };
80 const readEnvironment = vi.fn(() => ({
81 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
82 SSH_AUTH_SOCK: "/tmp/secretive.sock",
83 }));
84​
85 syncShellEnvironment(env, {
86 platform: "linux",
87 readEnvironment,
88 });
89​
90 expect(readEnvironment).toHaveBeenCalledWith("/bin/zsh", [
91 "PATH",
92 "SSH_AUTH_SOCK",
93 "HOMEBREW_PREFIX",
94 "HOMEBREW_CELLAR",
95 "HOMEBREW_REPOSITORY",
96 "XDG_CONFIG_HOME",
97 "XDG_DATA_HOME",
98 ]);
99 expect(env.PATH).toBe("/home/linuxbrew/.linuxbrew/bin:/usr/bin");
100 expect(env.SSH_AUTH_SOCK).toBe("/tmp/secretive.sock");
101 });
102​
103 it("falls back to launchctl PATH on macOS when shell probing does not return one", () => {
104 const env: NodeJS.ProcessEnv = {
105 SHELL: "/opt/homebrew/bin/nu",
106 PATH: "/usr/bin",
107 };
108 const readEnvironment = vi
109 .fn()
110 .mockImplementationOnce(() => {
111 throw new Error("unknown flag");
112 })
113 .mockImplementationOnce(() => ({}));
114 const readLaunchctlPath = vi.fn(() => "/opt/homebrew/bin:/usr/bin");
115 const logWarning = vi.fn();
116​
117 syncShellEnvironment(env, {
118 platform: "darwin",
119 readEnvironment,
120 readLaunchctlPath,
121 userShell: "/bin/zsh",
122 logWarning,
123 });
124​
125 expect(readEnvironment).toHaveBeenNthCalledWith(1, "/opt/homebrew/bin/nu", [
126 "PATH",
127 "SSH_AUTH_SOCK",
128 "HOMEBREW_PREFIX",
129 "HOMEBREW_CELLAR",
130 "HOMEBREW_REPOSITORY",
131 "XDG_CONFIG_HOME",
132 "XDG_DATA_HOME",
133 ]);
134 expect(readEnvironment).toHaveBeenNthCalledWith(2, "/bin/zsh", [
135 "PATH",
136 "SSH_AUTH_SOCK",
137 "HOMEBREW_PREFIX",
138 "HOMEBREW_CELLAR",
139 "HOMEBREW_REPOSITORY",
140 "XDG_CONFIG_HOME",
141 "XDG_DATA_HOME",
142 ]);
143 expect(readLaunchctlPath).toHaveBeenCalledTimes(1);
144 expect(logWarning).toHaveBeenCalledWith(
145 "Failed to read login shell environment from /opt/homebrew/bin/nu.",
146 expect.any(Error),
147 );
148 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
149 });
150​
151 it("does nothing on unsupported platforms", () => {
152 const env: NodeJS.ProcessEnv = {
153 SHELL: "C:/Program Files/Git/bin/bash.exe",
154 PATH: "C:\\Windows\\System32",
155 SSH_AUTH_SOCK: "/tmp/inherited.sock",
156 };
157 const readEnvironment = vi.fn(() => ({
158 PATH: "/usr/local/bin:/usr/bin",
159 SSH_AUTH_SOCK: "/tmp/secretive.sock",
160 }));
161​
162 syncShellEnvironment(env, {
163 platform: "freebsd",
164 readEnvironment,
165 });
166​
167 expect(readEnvironment).not.toHaveBeenCalled();
168 expect(env.PATH).toBe("C:\\Windows\\System32");
169 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
170 });
171​
172 it("hydrates PATH on Windows by merging PowerShell PATH with inherited PATH", () => {
173 const env: NodeJS.ProcessEnv = {
174 PATH: "C:\\Windows\\System32",
175 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
176 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
177 USERPROFILE: "C:\\Users\\testuser",
178 };
179 const readWindowsEnvironment = vi.fn(() => ({
180 PATH: "C:\\Custom\\Bin;C:\\Windows\\System32",
181 }));
182 const isWindowsCommandAvailable = vi.fn(() => true);
183​
184 syncShellEnvironment(env, {
185 platform: "win32",
186 readWindowsEnvironment,
187 isWindowsCommandAvailable,
188 });
189​
190 expect(readWindowsEnvironment).toHaveBeenCalledWith(["PATH"], { loadProfile: false });
191 expect(env.PATH).toBe(
192 [
193 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
194 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
195 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
196 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
197 "C:\\Users\\testuser\\.bun\\bin",
198 "C:\\Users\\testuser\\scoop\\shims",
199 "C:\\Custom\\Bin",
200 "C:\\Windows\\System32",
201 ].join(";"),
202 );
203 expect(isWindowsCommandAvailable).toHaveBeenCalledTimes(1);
204 });
205​
206 it("loads the PowerShell profile on Windows when node is not available", () => {
207 const env: NodeJS.ProcessEnv = {
208 PATH: "C:\\Windows\\System32",
209 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
210 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
211 USERPROFILE: "C:\\Users\\testuser",
212 };
213 const readWindowsEnvironment = vi.fn(
214 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) =>
215 options?.loadProfile
216 ? {
217 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
218 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
219 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
220 }
221 : { PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" },
222 );
223 const isWindowsCommandAvailable = vi.fn().mockReturnValueOnce(false).mockReturnValueOnce(true);
224​
225 syncShellEnvironment(env, {
226 platform: "win32",
227 readWindowsEnvironment,
228 isWindowsCommandAvailable,
229 });
230​
231 expect(env.PATH).toBe(
232 [
233 "C:\\Profile\\Node",
234 "C:\\Windows\\System32",
235 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
236 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
237 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
238 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
239 "C:\\Users\\testuser\\.bun\\bin",
240 "C:\\Users\\testuser\\scoop\\shims",
241 "C:\\Custom\\Bin",
242 ].join(";"),
243 );
244 expect(env.FNM_DIR).toBe("C:\\Users\\testuser\\AppData\\Roaming\\fnm");
245 expect(env.FNM_MULTISHELL_PATH).toBe(
246 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
247 );
248 expect(readWindowsEnvironment).toHaveBeenNthCalledWith(1, ["PATH"], { loadProfile: false });
249 expect(readWindowsEnvironment).toHaveBeenNthCalledWith(
250 2,
251 ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"],
252 { loadProfile: true },
253 );
254 });
255​
256 it("preserves baseline Windows env when the profile probe fails", () => {
257 const env: NodeJS.ProcessEnv = {
258 PATH: "C:\\Windows\\System32",
259 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
260 USERPROFILE: "C:\\Users\\testuser",
261 };
262 const readWindowsEnvironment = vi.fn(
263 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) => {
264 if (options?.loadProfile) {
265 throw new Error("profile load failed");
266 }
267 return { PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" };
268 },
269 );
270 const isWindowsCommandAvailable = vi.fn(() => false);
271​
272 syncShellEnvironment(env, {
273 platform: "win32",
274 readWindowsEnvironment,
275 isWindowsCommandAvailable,
276 });
277​
278 expect(env.PATH).toBe(
279 [
280 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
281 "C:\\Users\\testuser\\.bun\\bin",
282 "C:\\Users\\testuser\\scoop\\shims",
283 "C:\\Custom\\Bin",
284 "C:\\Windows\\System32",
285 ].join(";"),
286 );
287 expect(env.SSH_AUTH_SOCK).toBeUndefined();
288 });
289});