1
1
import { JSONRPCMessage } from "../types.js" ;
2
2
import { StdioClientTransport , StdioServerParameters , DEFAULT_INHERITED_ENV_VARS , getDefaultEnvironment } from "./stdio.js" ;
3
+ import { AsyncLocalStorage } from "node:async_hooks" ;
3
4
4
5
const serverParameters : StdioServerParameters = {
5
6
command : "/usr/bin/tee" ,
6
7
} ;
7
8
8
-
9
- let spawnEnv : Record < string , string > | undefined ;
9
+ const envAsyncLocalStorage = new AsyncLocalStorage < { env : Record < string , string > } > ( ) ;
10
10
11
11
jest . mock ( 'cross-spawn' , ( ) => {
12
12
const originalSpawn = jest . requireActual ( 'cross-spawn' ) ;
13
13
return jest . fn ( ( command , args , options ) => {
14
- spawnEnv = options . env ;
14
+ const env = envAsyncLocalStorage . getStore ( ) ;
15
+ if ( env ) {
16
+ env . env = options . env ;
17
+ }
15
18
return originalSpawn ( command , args , options ) ;
16
19
} ) ;
17
20
} ) ;
@@ -72,25 +75,29 @@ test("should read messages", async () => {
72
75
} ) ;
73
76
74
77
test ( "should properly set default environment variables in spawned process" , async ( ) => {
78
+ await envAsyncLocalStorage . run ( { env : { } } , async ( ) => {
75
79
const client = new StdioClientTransport ( serverParameters ) ;
76
80
77
81
await client . start ( ) ;
78
82
await client . close ( ) ;
79
83
80
84
// Get the default environment variables
81
85
const defaultEnv = getDefaultEnvironment ( ) ;
82
-
86
+ const spawnEnv = envAsyncLocalStorage . getStore ( ) ?. env ;
87
+ expect ( spawnEnv ) . toBeDefined ( ) ;
83
88
// Verify that all default environment variables are present
84
89
for ( const key of DEFAULT_INHERITED_ENV_VARS ) {
85
90
if ( process . env [ key ] && ! process . env [ key ] . startsWith ( "()" ) ) {
86
91
expect ( spawnEnv ) . toHaveProperty ( key ) ;
87
92
expect ( spawnEnv ! [ key ] ) . toBe ( process . env [ key ] ) ;
88
- expect ( spawnEnv ! [ key ] ) . toBe ( defaultEnv [ key ] ) ;
93
+ expect ( spawnEnv ! [ key ] ) . toBe ( defaultEnv [ key ] ) ;
94
+ }
89
95
}
90
- }
96
+ } ) ;
91
97
} ) ;
92
98
93
99
test ( "should override default environment variables with custom ones" , async ( ) => {
100
+ await envAsyncLocalStorage . run ( { env : { } } , async ( ) => {
94
101
const customEnv = {
95
102
HOME : "/custom/home" ,
96
103
PATH : "/custom/path" ,
@@ -104,7 +111,9 @@ test("should override default environment variables with custom ones", async ()
104
111
105
112
await client . start ( ) ;
106
113
await client . close ( ) ;
107
-
114
+
115
+ const spawnEnv = envAsyncLocalStorage . getStore ( ) ?. env ;
116
+ expect ( spawnEnv ) . toBeDefined ( ) ;
108
117
// Verify that custom environment variables override default ones
109
118
for ( const [ key , value ] of Object . entries ( customEnv ) ) {
110
119
expect ( spawnEnv ) . toHaveProperty ( key ) ;
@@ -117,5 +126,6 @@ test("should override default environment variables with custom ones", async ()
117
126
expect ( spawnEnv ) . toHaveProperty ( key ) ;
118
127
expect ( spawnEnv ! [ key ] ) . toBe ( process . env [ key ] ) ;
119
128
}
120
- }
121
- } ) ;
129
+ }
130
+ } ) ;
131
+ } ) ;
0 commit comments