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

os-jank.test.ts

185 lines5.7 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import { fixPath } from "./os-jank.ts";
4​
5describe("fixPath", () => {
6 it("hydrates PATH on linux using the resolved login shell", () => {
7 const env: NodeJS.ProcessEnv = {
8 SHELL: "/bin/zsh",
9 PATH: "/Users/test/.local/bin:/usr/bin",
10 };
11 const readPath = vi.fn(() => "/opt/homebrew/bin:/usr/bin");
12​
13 fixPath({
14 env,
15 platform: "linux",
16 readPath,
17 });
18​
19 expect(readPath).toHaveBeenCalledWith("/bin/zsh");
20 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin");
21 });
22​
23 it("falls back to launchctl PATH on macOS when shell probing fails", () => {
24 const env: NodeJS.ProcessEnv = {
25 SHELL: "/opt/homebrew/bin/nu",
26 PATH: "/usr/bin",
27 };
28 const readPath = vi
29 .fn()
30 .mockImplementationOnce(() => {
31 throw new Error("unknown flag");
32 })
33 .mockImplementationOnce(() => undefined);
34 const readLaunchctlPath = vi.fn(() => "/opt/homebrew/bin:/usr/bin");
35 const logWarning = vi.fn();
36​
37 fixPath({
38 env,
39 platform: "darwin",
40 readPath,
41 readLaunchctlPath,
42 userShell: "/bin/zsh",
43 logWarning,
44 });
45​
46 expect(readPath).toHaveBeenNthCalledWith(1, "/opt/homebrew/bin/nu");
47 expect(readPath).toHaveBeenNthCalledWith(2, "/bin/zsh");
48 expect(readLaunchctlPath).toHaveBeenCalledTimes(1);
49 expect(logWarning).toHaveBeenCalledWith(
50 "Failed to read PATH from login shell /opt/homebrew/bin/nu.",
51 expect.any(Error),
52 );
53 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
54 });
55​
56 it("repairs PATH on Windows by merging PowerShell PATH with inherited PATH", () => {
57 const env: NodeJS.ProcessEnv = {
58 PATH: "C:\\Windows\\System32",
59 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
60 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
61 USERPROFILE: "C:\\Users\\testuser",
62 };
63 const readWindowsEnvironment = vi.fn(() => ({
64 PATH: "C:\\Custom\\Bin;C:\\Windows\\System32",
65 }));
66 const isWindowsCommandAvailable = vi.fn(() => true);
67​
68 fixPath({
69 env,
70 platform: "win32",
71 readWindowsEnvironment,
72 isWindowsCommandAvailable,
73 });
74​
75 expect(readWindowsEnvironment).toHaveBeenCalledWith(["PATH"], { loadProfile: false });
76 expect(env.PATH).toBe(
77 [
78 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
79 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
80 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
81 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
82 "C:\\Users\\testuser\\.bun\\bin",
83 "C:\\Users\\testuser\\scoop\\shims",
84 "C:\\Custom\\Bin",
85 "C:\\Windows\\System32",
86 ].join(";"),
87 );
88 });
89​
90 it("applies profile-derived fnm variables on Windows when node is missing", () => {
91 const env: NodeJS.ProcessEnv = {
92 PATH: "C:\\Windows\\System32",
93 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
94 LOCALAPPDATA: "C:\\Users\\testuser\\AppData\\Local",
95 USERPROFILE: "C:\\Users\\testuser",
96 };
97 const readWindowsEnvironment = vi.fn(
98 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) =>
99 options?.loadProfile
100 ? {
101 PATH: "C:\\Profile\\Node;C:\\Windows\\System32",
102 FNM_DIR: "C:\\Users\\testuser\\AppData\\Roaming\\fnm",
103 FNM_MULTISHELL_PATH: "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
104 }
105 : { PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" },
106 );
107 const isWindowsCommandAvailable = vi.fn().mockReturnValueOnce(false).mockReturnValueOnce(true);
108​
109 fixPath({
110 env,
111 platform: "win32",
112 readWindowsEnvironment,
113 isWindowsCommandAvailable,
114 });
115​
116 expect(env.PATH).toBe(
117 [
118 "C:\\Profile\\Node",
119 "C:\\Windows\\System32",
120 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
121 "C:\\Users\\testuser\\AppData\\Local\\Programs\\nodejs",
122 "C:\\Users\\testuser\\AppData\\Local\\Volta\\bin",
123 "C:\\Users\\testuser\\AppData\\Local\\pnpm",
124 "C:\\Users\\testuser\\.bun\\bin",
125 "C:\\Users\\testuser\\scoop\\shims",
126 "C:\\Custom\\Bin",
127 ].join(";"),
128 );
129 expect(env.FNM_DIR).toBe("C:\\Users\\testuser\\AppData\\Roaming\\fnm");
130 expect(env.FNM_MULTISHELL_PATH).toBe(
131 "C:\\Users\\testuser\\AppData\\Local\\fnm_multishells\\123",
132 );
133 });
134​
135 it("preserves baseline PATH on Windows when the profile probe fails", () => {
136 const env: NodeJS.ProcessEnv = {
137 PATH: "C:\\Windows\\System32",
138 APPDATA: "C:\\Users\\testuser\\AppData\\Roaming",
139 USERPROFILE: "C:\\Users\\testuser",
140 };
141 const readWindowsEnvironment = vi.fn(
142 (_names: ReadonlyArray<string>, options?: { loadProfile?: boolean }) => {
143 if (options?.loadProfile) {
144 throw new Error("profile load failed");
145 }
146 return { PATH: "C:\\Custom\\Bin;C:\\Windows\\System32" };
147 },
148 );
149 const isWindowsCommandAvailable = vi.fn(() => false);
150​
151 fixPath({
152 env,
153 platform: "win32",
154 readWindowsEnvironment,
155 isWindowsCommandAvailable,
156 });
157​
158 expect(env.PATH).toBe(
159 [
160 "C:\\Users\\testuser\\AppData\\Roaming\\npm",
161 "C:\\Users\\testuser\\.bun\\bin",
162 "C:\\Users\\testuser\\scoop\\shims",
163 "C:\\Custom\\Bin",
164 "C:\\Windows\\System32",
165 ].join(";"),
166 );
167 });
168​
169 it("does nothing on unsupported platforms", () => {
170 const env: NodeJS.ProcessEnv = {
171 SHELL: "C:/Program Files/Git/bin/bash.exe",
172 PATH: "C:\\Windows\\System32",
173 };
174 const readPath = vi.fn(() => "/usr/local/bin:/usr/bin");
175​
176 fixPath({
177 env,
178 platform: "freebsd",
179 readPath,
180 });
181​
182 expect(readPath).not.toHaveBeenCalled();
183 expect(env.PATH).toBe("C:\\Windows\\System32");
184 });
185});