11// @effect -diagnostics nodeBuiltinImport:off -- The process fixture runs real shell commands with isolated config files.
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { assert , describe , it } from "@effect/vitest" ;
4+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
45import * as Effect from "effect/Effect" ;
56import * as Layer from "effect/Layer" ;
67import * as Logger from "effect/Logger" ;
@@ -72,6 +73,7 @@ function runShellEnvironment(input: {
7273 command : ChildProcess . Command ,
7374 ) => string | { output : string ; exitCode : number } ;
7475 readonly failure ?: PlatformError . PlatformError ;
76+ readonly processSpawner ?: ChildProcessSpawner . ChildProcessSpawner [ "Service" ] ;
7577} ) {
7678 const layerEnvironment = Layer . succeed (
7779 DesktopEnvironment . DesktopEnvironment ,
@@ -82,6 +84,7 @@ function runShellEnvironment(input: {
8284 const layerSpawner = Layer . succeed (
8385 ChildProcessSpawner . ChildProcessSpawner ,
8486 ChildProcessSpawner . make ( ( command ) => {
87+ if ( input . processSpawner ) return input . processSpawner . spawn ( command ) ;
8588 if ( input . failure !== undefined ) return Effect . fail ( input . failure ) ;
8689 const result = input . handler ( command ) ;
8790 return Effect . succeed (
@@ -118,6 +121,41 @@ function runShellEnvironment(input: {
118121}
119122
120123describe ( "DesktopShellEnvironment" , ( ) => {
124+ it . effect (
125+ "captures the login-shell PATH when startup writes more than a pipe buffer to stderr" ,
126+ ( ) =>
127+ Effect . gen ( function * ( ) {
128+ const spawner = yield * ChildProcessSpawner . ChildProcessSpawner . pipe (
129+ Effect . provide ( NodeServices . layer ) ,
130+ ) ;
131+ const executable = yield * HostProcess . ExecutablePath ;
132+ const env : NodeJS . ProcessEnv = { SHELL : "/test-shell" , PATH : "/usr/bin" } ;
133+ const commands : string [ ] = [ ] ;
134+ const source = [
135+ "setTimeout(() => process.exit(99), 3000).unref();" ,
136+ "process.stderr.write(Buffer.alloc(2 * 1024 * 1024, 120), () => {" ,
137+ `process.stdout.write(${ JSON . stringify ( envOutput ( { PATH : "/shell-only/bin:/usr/bin" } ) ) } );` ,
138+ "});" ,
139+ ] . join ( "\n" ) ;
140+
141+ yield * runShellEnvironment ( {
142+ env,
143+ platform : "linux" ,
144+ handler : ( ) => "" ,
145+ processSpawner : ChildProcessSpawner . make ( ( command ) => {
146+ if ( command . _tag !== "StandardCommand" ) return Effect . succeed ( makeProcess ( "" ) ) ;
147+ commands . push ( command . command ) ;
148+ return command . command === "/test-shell"
149+ ? spawner . spawn ( ChildProcess . make ( executable , [ "-e" , source ] , command . options ) )
150+ : Effect . succeed ( makeProcess ( "" ) ) ;
151+ } ) ,
152+ } ) . pipe ( Effect . provide ( Logger . layer ( [ ] ) ) ) ;
153+
154+ assert . equal ( env . PATH , "/shell-only/bin:/usr/bin" ) ;
155+ assert . deepEqual ( commands , [ "/test-shell" ] ) ;
156+ } ) ,
157+ ) ;
158+
121159 it . effect ( "rejects PATH output from a failed shell and tries the next candidate" , ( ) =>
122160 Effect . gen ( function * ( ) {
123161 const env : NodeJS . ProcessEnv = { SHELL : "/bin/failing" , PATH : "/usr/bin" } ;
0 commit comments