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

os-jank.test.ts

72 lines2.0 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import { fixPath } from "./os-jank";
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("does nothing outside macOS and linux even when SHELL is set", () => {
57 const env: NodeJS.ProcessEnv = {
58 SHELL: "C:/Program Files/Git/bin/bash.exe",
59 PATH: "C:\\Windows\\System32",
60 };
61 const readPath = vi.fn(() => "/usr/local/bin:/usr/bin");
62​
63 fixPath({
64 env,
65 platform: "win32",
66 readPath,
67 });
68​
69 expect(readPath).not.toHaveBeenCalled();
70 expect(env.PATH).toBe("C:\\Windows\\System32");
71 });
72});