@@ -9,10 +9,14 @@ import * as Sink from "effect/Sink";
99import * as Stream from "effect/Stream" ;
1010import * as ChildProcess from "effect/process/ChildProcess" ;
1111import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner" ;
12+ import * as NodeOS from "node:os" ;
13+ import { vi } from "vite-plus/test" ;
1214
1315import * as DesktopEnvironment from "../app/DesktopEnvironment.ts" ;
1416import * as DesktopShellEnvironment from "./DesktopShellEnvironment.ts" ;
1517
18+ vi . mock ( "node:os" , { spy : true } ) ;
19+
1620const textEncoder = new TextEncoder ( ) ;
1721
1822const isDesktopShellEnvironmentCommandError = Schema . is (
@@ -29,14 +33,14 @@ function envOutput(values: Readonly<Record<string, string>>): string {
2933 . join ( "\n" ) ;
3034}
3135
32- function makeProcess ( output : string ) : ChildProcessSpawner . ChildProcessHandle {
36+ function makeProcess ( output : string , exitCode = 0 ) : ChildProcessSpawner . ChildProcessHandle {
3337 const stdout = output . length === 0 ? Stream . empty : Stream . make ( textEncoder . encode ( output ) ) ;
3438 return ChildProcessSpawner . makeHandle ( {
3539 pid : ChildProcessSpawner . ProcessId ( 123 ) ,
3640 stdout,
3741 stderr : Stream . empty ,
3842 all : stdout ,
39- exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( 0 ) ) ,
43+ exitCode : Effect . succeed ( ChildProcessSpawner . ExitCode ( exitCode ) ) ,
4044 isRunning : Effect . succeed ( false ) ,
4145 kill : ( ) => Effect . void ,
4246 stdin : Sink . drain ,
@@ -67,7 +71,10 @@ function withProcessEnv<A, E, R>(
6771function runShellEnvironment ( input : {
6872 readonly env : NodeJS . ProcessEnv ;
6973 readonly platform : NodeJS . Platform ;
70- readonly handler : ( command : ChildProcess . Command ) => string ;
74+ readonly userShell ?: string ;
75+ readonly handler : (
76+ command : ChildProcess . Command ,
77+ ) => string | { output : string ; exitCode : number } ;
7178 readonly failure ?: PlatformError . PlatformError ;
7279} ) {
7380 const layerEnvironment = Layer . succeed (
@@ -78,11 +85,15 @@ function runShellEnvironment(input: {
7885 ) ;
7986 const layerSpawner = Layer . succeed (
8087 ChildProcessSpawner . ChildProcessSpawner ,
81- ChildProcessSpawner . make ( ( command ) =>
82- input . failure === undefined
83- ? Effect . succeed ( makeProcess ( input . handler ( command ) ) )
84- : Effect . fail ( input . failure ) ,
85- ) ,
88+ ChildProcessSpawner . make ( ( command ) => {
89+ if ( input . failure !== undefined ) return Effect . fail ( input . failure ) ;
90+ const result = input . handler ( command ) ;
91+ return Effect . succeed (
92+ typeof result === "string"
93+ ? makeProcess ( result )
94+ : makeProcess ( result . output , result . exitCode ) ,
95+ ) ;
96+ } ) ,
8697 ) ;
8798
8899 const program = Effect . gen ( function * ( ) {
@@ -96,10 +107,88 @@ function runShellEnvironment(input: {
96107 ) ,
97108 ) ;
98109
99- return withProcessEnv ( input . env , program ) ;
110+ return Effect . acquireUseRelease (
111+ Effect . sync ( ( ) =>
112+ vi . spyOn ( NodeOS , "userInfo" ) . mockReturnValue ( {
113+ ...NodeOS . userInfo ( ) ,
114+ shell : input . userShell ?? null ,
115+ } ) ,
116+ ) ,
117+ ( ) => withProcessEnv ( input . env , program ) ,
118+ ( spy ) => Effect . sync ( ( ) => spy . mockRestore ( ) ) ,
119+ ) ;
100120}
101121
102122describe ( "DesktopShellEnvironment" , ( ) => {
123+ it . effect ( "rejects PATH output from a failed shell and tries the next candidate" , ( ) =>
124+ Effect . gen ( function * ( ) {
125+ const env : NodeJS . ProcessEnv = { SHELL : "/bin/failing" , PATH : "/usr/bin" } ;
126+ const commands : string [ ] = [ ] ;
127+
128+ yield * runShellEnvironment ( {
129+ env,
130+ platform : "linux" ,
131+ userShell : "/usr/local/bin/fish" ,
132+ handler : ( command ) => {
133+ if ( command . _tag !== "StandardCommand" ) return "" ;
134+ commands . push ( command . command ) ;
135+ return command . command === "/bin/failing"
136+ ? { output : envOutput ( { PATH : "/failed/bin" } ) , exitCode : 7 }
137+ : envOutput ( { PATH : "/home/test/.local/bin:/usr/bin" } ) ;
138+ } ,
139+ } ) . pipe ( Effect . provide ( Logger . layer ( [ ] ) ) ) ;
140+
141+ assert . deepEqual ( commands , [ "/bin/failing" , "/usr/local/bin/fish" ] ) ;
142+ assert . equal ( env . PATH , "/home/test/.local/bin:/usr/bin" ) ;
143+ } ) ,
144+ ) ;
145+
146+ it . effect . each ( [ "darwin" , "linux" ] as const ) (
147+ "uses the account shell when SHELL is missing on %s" ,
148+ ( platform ) =>
149+ Effect . gen ( function * ( ) {
150+ const env : NodeJS . ProcessEnv = { PATH : "/usr/bin" } ;
151+ const commands : string [ ] = [ ] ;
152+
153+ yield * runShellEnvironment ( {
154+ env,
155+ platform,
156+ userShell : "/usr/local/bin/fish" ,
157+ handler : ( command ) => {
158+ if ( command . _tag !== "StandardCommand" ) return "" ;
159+ commands . push ( command . command ) ;
160+ return envOutput ( { PATH : "/home/test/.local/bin:/usr/bin" } ) ;
161+ } ,
162+ } ) ;
163+
164+ assert . deepEqual ( commands , [ "/usr/local/bin/fish" ] ) ;
165+ assert . equal ( env . PATH , "/home/test/.local/bin:/usr/bin" ) ;
166+ } ) ,
167+ ) ;
168+
169+ it . effect ( "tries the account shell before the platform fallback when SHELL has no PATH" , ( ) =>
170+ Effect . gen ( function * ( ) {
171+ const env : NodeJS . ProcessEnv = { SHELL : "/bin/missing" , PATH : "/usr/bin" } ;
172+ const commands : string [ ] = [ ] ;
173+
174+ yield * runShellEnvironment ( {
175+ env,
176+ platform : "linux" ,
177+ userShell : "/usr/local/bin/fish" ,
178+ handler : ( command ) => {
179+ if ( command . _tag !== "StandardCommand" ) return "" ;
180+ commands . push ( command . command ) ;
181+ return command . command === "/usr/local/bin/fish"
182+ ? envOutput ( { PATH : "/home/test/.local/bin:/usr/bin" } )
183+ : "" ;
184+ } ,
185+ } ) ;
186+
187+ assert . deepEqual ( commands , [ "/bin/missing" , "/usr/local/bin/fish" ] ) ;
188+ assert . equal ( env . PATH , "/home/test/.local/bin:/usr/bin" ) ;
189+ } ) ,
190+ ) ;
191+
103192 it . effect ( "hydrates PATH and missing SSH_AUTH_SOCK from the login shell on macOS" , ( ) =>
104193 Effect . gen ( function * ( ) {
105194 const env : NodeJS . ProcessEnv = {
0 commit comments