@@ -2,8 +2,12 @@ import { describe, expect, it, vi } from "vitest";
22
33import {
44 extractPathFromShellOutput ,
5+ listLoginShellCandidates ,
6+ mergePathEntries ,
57 readEnvironmentFromLoginShell ,
8+ readPathFromLaunchctl ,
69 readPathFromLoginShell ,
10+ resolveLoginShell ,
711} from "./shell" ;
812
913describe ( "extractPathFromShellOutput" , ( ) => {
@@ -60,6 +64,38 @@ describe("readPathFromLoginShell", () => {
6064 } ) ;
6165} ) ;
6266
67+ describe ( "readPathFromLaunchctl" , ( ) => {
68+ it ( "returns a trimmed PATH value from launchctl" , ( ) => {
69+ const execFile = vi . fn <
70+ (
71+ file : string ,
72+ args : ReadonlyArray < string > ,
73+ options : { encoding : "utf8" ; timeout : number } ,
74+ ) => string
75+ > ( ( ) => " /opt/homebrew/bin:/usr/bin \n" ) ;
76+
77+ expect ( readPathFromLaunchctl ( execFile ) ) . toBe ( "/opt/homebrew/bin:/usr/bin" ) ;
78+ expect ( execFile ) . toHaveBeenCalledWith ( "/bin/launchctl" , [ "getenv" , "PATH" ] , {
79+ encoding : "utf8" ,
80+ timeout : 2000 ,
81+ } ) ;
82+ } ) ;
83+
84+ it ( "returns undefined when launchctl is unavailable" , ( ) => {
85+ const execFile = vi . fn <
86+ (
87+ file : string ,
88+ args : ReadonlyArray < string > ,
89+ options : { encoding : "utf8" ; timeout : number } ,
90+ ) => string
91+ > ( ( ) => {
92+ throw new Error ( "spawn /bin/launchctl ENOENT" ) ;
93+ } ) ;
94+
95+ expect ( readPathFromLaunchctl ( execFile ) ) . toBeUndefined ( ) ;
96+ } ) ;
97+ } ) ;
98+
6399describe ( "readEnvironmentFromLoginShell" , ( ) => {
64100 it ( "extracts multiple environment variables from a login shell command" , ( ) => {
65101 const execFile = vi . fn <
@@ -126,3 +162,38 @@ describe("readEnvironmentFromLoginShell", () => {
126162 } ) ;
127163 } ) ;
128164} ) ;
165+
166+ describe ( "listLoginShellCandidates" , ( ) => {
167+ it ( "returns env shell, user shell, then the platform fallback without duplicates" , ( ) => {
168+ expect ( listLoginShellCandidates ( "darwin" , " /opt/homebrew/bin/nu " , "/bin/zsh" ) ) . toEqual ( [
169+ "/opt/homebrew/bin/nu" ,
170+ "/bin/zsh" ,
171+ ] ) ;
172+ } ) ;
173+
174+ it ( "falls back to the platform default when no shells are available" , ( ) => {
175+ expect ( listLoginShellCandidates ( "linux" , undefined , "" ) ) . toEqual ( [ "/bin/bash" ] ) ;
176+ } ) ;
177+ } ) ;
178+
179+ describe ( "resolveLoginShell" , ( ) => {
180+ it ( "returns the first available login shell candidate" , ( ) => {
181+ expect ( resolveLoginShell ( "darwin" , undefined , "/opt/homebrew/bin/fish" ) ) . toBe (
182+ "/opt/homebrew/bin/fish" ,
183+ ) ;
184+ } ) ;
185+ } ) ;
186+
187+ describe ( "mergePathEntries" , ( ) => {
188+ it ( "prefers login-shell PATH entries and keeps inherited extras" , ( ) => {
189+ expect (
190+ mergePathEntries ( "/opt/homebrew/bin:/usr/bin" , "/Users/test/.local/bin:/usr/bin" , "darwin" ) ,
191+ ) . toBe ( "/opt/homebrew/bin:/usr/bin:/Users/test/.local/bin" ) ;
192+ } ) ;
193+
194+ it ( "uses the platform-specific delimiter" , ( ) => {
195+ expect ( mergePathEntries ( "C:\\Tools;C:\\Windows" , "C:\\Windows;C:\\Git" , "win32" ) ) . toBe (
196+ "C:\\Tools;C:\\Windows;C:\\Git" ,
197+ ) ;
198+ } ) ;
199+ } ) ;
0 commit comments