-
Notifications
You must be signed in to change notification settings - Fork 14
fix: use merge path for users of oh my zsh #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
k11kirky
merged 1 commit into
main
from
04-29-fix_use_merge_path_for_users_of_oh_my_zsh
Apr 29, 2026
+173
−6
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mockSpawnSync = vi.hoisted(() => vi.fn()); | ||
| const mockExistsSync = vi.hoisted(() => vi.fn(() => false)); | ||
| const mockReadFileSync = vi.hoisted(() => vi.fn()); | ||
| const mockWriteFileSync = vi.hoisted(() => vi.fn()); | ||
| const mockMkdirSync = vi.hoisted(() => vi.fn()); | ||
| const mockUserInfo = vi.hoisted(() => | ||
| vi.fn(() => ({ shell: "/bin/zsh" }) as { shell: string | null }), | ||
| ); | ||
| const mockGetUserDataDir = vi.hoisted(() => vi.fn(() => "/tmp/posthog-code")); | ||
|
|
||
| vi.mock("node:child_process", () => ({ | ||
| spawnSync: mockSpawnSync, | ||
| default: { spawnSync: mockSpawnSync }, | ||
| })); | ||
|
|
||
| vi.mock("node:fs", () => ({ | ||
| existsSync: mockExistsSync, | ||
| readFileSync: mockReadFileSync, | ||
| writeFileSync: mockWriteFileSync, | ||
| mkdirSync: mockMkdirSync, | ||
| default: { | ||
| existsSync: mockExistsSync, | ||
| readFileSync: mockReadFileSync, | ||
| writeFileSync: mockWriteFileSync, | ||
| mkdirSync: mockMkdirSync, | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("node:os", () => ({ | ||
| userInfo: mockUserInfo, | ||
| default: { userInfo: mockUserInfo }, | ||
| })); | ||
|
|
||
| vi.mock("./env", () => ({ | ||
| getUserDataDir: mockGetUserDataDir, | ||
| })); | ||
|
|
||
| const DELIMITER = "_SHELL_ENV_DELIMITER_"; | ||
|
|
||
| function shellOutput(envPath: string): string { | ||
| return `${DELIMITER}\nPATH=${envPath}\n${DELIMITER}`; | ||
| } | ||
|
|
||
| const ORIGINAL_PLATFORM = process.platform; | ||
| const ORIGINAL_PATH = process.env.PATH; | ||
|
|
||
| function setPlatform(platform: NodeJS.Platform) { | ||
| Object.defineProperty(process, "platform", { | ||
| value: platform, | ||
| configurable: true, | ||
| }); | ||
| } | ||
|
|
||
| describe("fixPath", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.resetModules(); | ||
| setPlatform("darwin"); | ||
| process.env.PATH = "/usr/bin:/bin"; | ||
| mockExistsSync.mockReturnValue(false); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| setPlatform(ORIGINAL_PLATFORM); | ||
| process.env.PATH = ORIGINAL_PATH; | ||
| }); | ||
|
|
||
| it("merges fallback paths when shell PATH lacks /opt/homebrew/bin", async () => { | ||
| mockSpawnSync.mockReturnValue({ | ||
| status: 0, | ||
| stdout: shellOutput("/usr/local/bin:/usr/bin:/bin"), | ||
| }); | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| const parts = process.env.PATH?.split(":") ?? []; | ||
| expect(parts).toContain("/opt/homebrew/bin"); | ||
| expect(parts).toContain("/opt/homebrew/sbin"); | ||
| expect(parts).toContain("/usr/local/bin"); | ||
| expect(parts).toContain("/usr/bin"); | ||
| }); | ||
|
|
||
| it("does not duplicate fallback paths already present in shell PATH", async () => { | ||
| mockSpawnSync.mockReturnValue({ | ||
| status: 0, | ||
| stdout: shellOutput("/opt/homebrew/bin:/usr/local/bin:/usr/bin"), | ||
| }); | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| const parts = process.env.PATH?.split(":") ?? []; | ||
| const homebrewCount = parts.filter((p) => p === "/opt/homebrew/bin").length; | ||
| expect(homebrewCount).toBe(1); | ||
| const usrLocalCount = parts.filter((p) => p === "/usr/local/bin").length; | ||
| expect(usrLocalCount).toBe(1); | ||
| }); | ||
|
|
||
| it("falls back to fallback paths when shell resolution fails entirely", async () => { | ||
| mockSpawnSync.mockReturnValue({ status: 1, stdout: "" }); | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| const parts = process.env.PATH?.split(":") ?? []; | ||
| expect(parts).toContain("/opt/homebrew/bin"); | ||
| expect(parts).toContain("/opt/homebrew/sbin"); | ||
| expect(parts).toContain("/usr/local/bin"); | ||
| expect(parts).toContain("/usr/bin"); | ||
| }); | ||
|
|
||
| it("merges fallback paths into a cached PATH that lacks them", async () => { | ||
| mockExistsSync.mockReturnValue(true); | ||
| mockReadFileSync.mockReturnValue( | ||
| JSON.stringify({ | ||
| path: "/usr/local/bin:/usr/bin:/bin", | ||
| timestamp: Date.now(), | ||
| }), | ||
| ); | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| const parts = process.env.PATH?.split(":") ?? []; | ||
| expect(parts).toContain("/opt/homebrew/bin"); | ||
| expect(mockSpawnSync).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("ignores stale cache and re-resolves via shell", async () => { | ||
| mockExistsSync.mockReturnValue(true); | ||
| mockReadFileSync.mockReturnValue( | ||
| JSON.stringify({ | ||
| path: "/some/old/path", | ||
| timestamp: Date.now() - 2 * 60 * 60 * 1000, | ||
| }), | ||
| ); | ||
| mockSpawnSync.mockReturnValue({ | ||
| status: 0, | ||
| stdout: shellOutput("/usr/local/bin:/usr/bin"), | ||
| }); | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| expect(mockSpawnSync).toHaveBeenCalled(); | ||
| const parts = process.env.PATH?.split(":") ?? []; | ||
| expect(parts).not.toContain("/some/old/path"); | ||
| expect(parts).toContain("/opt/homebrew/bin"); | ||
| }); | ||
|
|
||
| it("returns early on win32 without touching PATH", async () => { | ||
| setPlatform("win32"); | ||
| process.env.PATH = "C:\\Windows\\System32"; | ||
|
|
||
| const { fixPath } = await import("./fixPath"); | ||
| fixPath(); | ||
|
|
||
| expect(mockSpawnSync).not.toHaveBeenCalled(); | ||
| expect(process.env.PATH).toBe("C:\\Windows\\System32"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests
"merges fallback paths when shell PATH lacks /opt/homebrew/bin"(line 69),"falls back to fallback paths when shell resolution fails entirely"(line 92), and"merges fallback paths into a cached PATH that lacks them"(line 106) all end with the same fourtoContainassertions for the fallback paths. Per the team's stated preference for parameterised tests, these cases could be collapsed into a singleit.eachthat varies the mock setup (shell success / shell fail / cache hit) while sharing the assertion block, eliminating the duplication.Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!