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

syncShellEnvironment.test.ts

105 lines3.0 KB

1import { describe, expect, it, vi } from "vitest";
2​
3import { syncShellEnvironment } from "./syncShellEnvironment";
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: "/usr/bin",
10 };
11 const readEnvironment = vi.fn(() => ({
12 PATH: "/opt/homebrew/bin:/usr/bin",
13 SSH_AUTH_SOCK: "/tmp/secretive.sock",
14 }));
15​
16 syncShellEnvironment(env, {
17 platform: "darwin",
18 readEnvironment,
19 });
20​
21 expect(readEnvironment).toHaveBeenCalledWith("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"]);
22 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
23 expect(env.SSH_AUTH_SOCK).toBe("/tmp/secretive.sock");
24 });
25​
26 it("preserves an inherited SSH_AUTH_SOCK value", () => {
27 const env: NodeJS.ProcessEnv = {
28 SHELL: "/bin/zsh",
29 PATH: "/usr/bin",
30 SSH_AUTH_SOCK: "/tmp/inherited.sock",
31 };
32 const readEnvironment = vi.fn(() => ({
33 PATH: "/opt/homebrew/bin:/usr/bin",
34 SSH_AUTH_SOCK: "/tmp/login-shell.sock",
35 }));
36​
37 syncShellEnvironment(env, {
38 platform: "darwin",
39 readEnvironment,
40 });
41​
42 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
43 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
44 });
45​
46 it("preserves inherited values when the login shell omits them", () => {
47 const env: NodeJS.ProcessEnv = {
48 SHELL: "/bin/zsh",
49 PATH: "/usr/bin",
50 SSH_AUTH_SOCK: "/tmp/inherited.sock",
51 };
52 const readEnvironment = vi.fn(() => ({
53 PATH: "/opt/homebrew/bin:/usr/bin",
54 }));
55​
56 syncShellEnvironment(env, {
57 platform: "darwin",
58 readEnvironment,
59 });
60​
61 expect(env.PATH).toBe("/opt/homebrew/bin:/usr/bin");
62 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
63 });
64​
65 it("hydrates PATH and missing SSH_AUTH_SOCK from the login shell on linux", () => {
66 const env: NodeJS.ProcessEnv = {
67 SHELL: "/bin/zsh",
68 PATH: "/usr/bin",
69 };
70 const readEnvironment = vi.fn(() => ({
71 PATH: "/home/linuxbrew/.linuxbrew/bin:/usr/bin",
72 SSH_AUTH_SOCK: "/tmp/secretive.sock",
73 }));
74​
75 syncShellEnvironment(env, {
76 platform: "linux",
77 readEnvironment,
78 });
79​
80 expect(readEnvironment).toHaveBeenCalledWith("/bin/zsh", ["PATH", "SSH_AUTH_SOCK"]);
81 expect(env.PATH).toBe("/home/linuxbrew/.linuxbrew/bin:/usr/bin");
82 expect(env.SSH_AUTH_SOCK).toBe("/tmp/secretive.sock");
83 });
84​
85 it("does nothing outside macOS and linux", () => {
86 const env: NodeJS.ProcessEnv = {
87 SHELL: "C:/Program Files/Git/bin/bash.exe",
88 PATH: "C:\\Windows\\System32",
89 SSH_AUTH_SOCK: "/tmp/inherited.sock",
90 };
91 const readEnvironment = vi.fn(() => ({
92 PATH: "/usr/local/bin:/usr/bin",
93 SSH_AUTH_SOCK: "/tmp/secretive.sock",
94 }));
95​
96 syncShellEnvironment(env, {
97 platform: "win32",
98 readEnvironment,
99 });
100​
101 expect(readEnvironment).not.toHaveBeenCalled();
102 expect(env.PATH).toBe("C:\\Windows\\System32");
103 expect(env.SSH_AUTH_SOCK).toBe("/tmp/inherited.sock");
104 });
105});