-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathEnvironmentTests.cs
More file actions
288 lines (250 loc) · 10.4 KB
/
EnvironmentTests.cs
File metadata and controls
288 lines (250 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using Xunit;
namespace GitHub.Copilot.SDK.Test;
/// <summary>
/// Regression tests for the Environment merge-vs-replace bug (Issue #441).
///
/// Background:
/// Before the fix, <see cref="CopilotClientOptions.Environment"/> was handled with:
///
/// startInfo.Environment.Clear(); // ← BUG: wiped PATH, SystemRoot, COMSPEC, TEMP, etc.
/// foreach (var (key, value) in options.Environment)
/// startInfo.Environment[key] = value;
///
/// ProcessStartInfo.Environment is pre-populated with the current process's inherited
/// environment. The Clear() call threw it all away, so supplying even ONE custom key
/// caused the Node.js-based CLI subprocess to crash on Windows because essential system
/// variables (PATH, SystemRoot, COMSPEC) were gone.
///
/// After the fix, user-supplied keys are merged (override or add) into the inherited
/// environment -- the CLI subprocess receives all inherited vars plus any overrides.
///
/// How the tests prove the fix:
/// Every test below that provides a non-null Environment dict would have thrown an
/// IOException ("CLI process exited unexpectedly") BEFORE the fix. After the fix they
/// all pass because PATH/SystemRoot/COMSPEC remain available to the subprocess.
/// </summary>
public class EnvironmentTests
{
// ── Null / empty cases ────────────────────────────────────────────────────
[Fact]
public void Environment_DefaultsToNull()
{
// Verify the documented default: null means "fully inherit from parent process".
var options = new CopilotClientOptions();
Assert.Null(options.Environment);
}
[Fact]
public async Task Should_Start_When_Environment_Is_Null()
{
// Baseline: null Environment → all inherited vars are present → CLI starts.
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = null,
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("null-env");
Assert.Equal("pong: null-env", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Fact]
public async Task Should_Start_When_Environment_Is_An_Empty_Dictionary()
{
// An empty dictionary supplies no keys, so the loop in Client.cs runs zero
// iterations -- the inherited environment is completely unchanged.
// Before the fix: Clear() was still called → crash.
// After the fix: no Clear(); inherited env untouched → CLI starts normally.
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = new Dictionary<string, string>(),
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("empty-env");
Assert.Equal("pong: empty-env", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
// ── Partial-dict merge cases ──────────────────────────────────────────────
[Fact]
public async Task Should_Start_When_Environment_Has_One_Custom_Key()
{
// This is the canonical regression test for Issue #441.
//
// The user provides a single custom environment variable -- a perfectly
// reasonable thing to do (e.g. to set COPILOT_API_URL, a proxy, etc.).
//
// Before the fix:
// startInfo.Environment.Clear() ← removes PATH, SystemRoot, COMSPEC …
// startInfo.Environment["MY_KEY"] = "value"
// → CLI subprocess starts with only MY_KEY → crashes immediately
// → StartAsync() throws IOException
//
// After the fix:
// startInfo.Environment["MY_KEY"] = "value" (merged)
// → CLI subprocess retains all inherited vars + MY_KEY → starts normally
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = new Dictionary<string, string>
{
["MY_CUSTOM_SDK_VAR"] = "hello_world",
},
});
try
{
// This line would throw before the fix:
// System.IO.IOException: CLI process exited unexpectedly …
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("one-key-env");
Assert.Equal("pong: one-key-env", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Fact]
public async Task Should_Start_When_Environment_Has_Multiple_Custom_Keys()
{
// Multiple custom keys, none of them system variables.
// Proves that the merge works for an arbitrary number of custom entries.
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = new Dictionary<string, string>
{
["SDK_TEST_VAR_A"] = "alpha",
["SDK_TEST_VAR_B"] = "beta",
["SDK_TEST_VAR_C"] = "gamma",
},
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("multi-key-env");
Assert.Equal("pong: multi-key-env", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
[Fact]
public async Task Should_Start_When_Environment_Overrides_An_Inherited_Key()
{
// Overriding an EXISTING env var (e.g. COPILOT_LOG_LEVEL) should work:
// the override takes effect, and all other inherited vars remain.
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = new Dictionary<string, string>
{
// Override a var that is almost certainly already present in the
// parent process environment so we exercise the "override" code path.
["PATH"] = System.Environment.GetEnvironmentVariable("PATH") ?? "/usr/bin",
},
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("override-inherited-key");
Assert.Equal("pong: override-inherited-key", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
// ── Verifying the merge semantics via the harness pattern ──────────────
[Fact]
public async Task TestHarness_GetEnvironment_Pattern_Works_After_Fix()
{
// The E2E test harness (E2ETestContext.GetEnvironment) follows this pattern:
//
// var env = Environment.GetEnvironmentVariables()
// .Cast<DictionaryEntry>()
// .ToDictionary(...);
// env["COPILOT_API_URL"] = proxyUrl; // ← override
// env["XDG_CONFIG_HOME"] = homeDir; // ← override
// env["XDG_STATE_HOME"] = homeDir; // ← override
// return env;
//
// This pattern always supplied the FULL environment, so it happened to work
// even before the fix. Here we verify the same pattern continues to work.
var fullEnvWithOverrides = System.Environment.GetEnvironmentVariables()
.Cast<System.Collections.DictionaryEntry>()
.ToDictionary(e => (string)e.Key, e => e.Value?.ToString() ?? "");
fullEnvWithOverrides["SDK_HARNESS_STYLE_OVERRIDE"] = "harness_value";
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = fullEnvWithOverrides,
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("harness-pattern");
Assert.Equal("pong: harness-pattern", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
// ── NODE_DEBUG is always stripped ─────────────────────────────────────────
[Fact]
public async Task Should_Strip_NODE_DEBUG_When_Environment_Dict_Is_Provided()
{
// Client.cs always calls startInfo.Environment.Remove("NODE_DEBUG") after
// the merge step, so the CLI subprocess never sees NODE_DEBUG regardless of
// whether the parent process has it set. The CLI must start normally.
var envWithNodeDebug = System.Environment.GetEnvironmentVariables()
.Cast<System.Collections.DictionaryEntry>()
.ToDictionary(e => (string)e.Key, e => e.Value?.ToString() ?? "");
envWithNodeDebug["NODE_DEBUG"] = "http,net"; // would pollute CLI stdout if kept
using var client = new CopilotClient(new CopilotClientOptions
{
UseStdio = true,
Environment = envWithNodeDebug,
});
try
{
await client.StartAsync();
Assert.Equal(ConnectionState.Connected, client.State);
var pong = await client.PingAsync("node-debug-stripped");
Assert.Equal("pong: node-debug-stripped", pong.Message);
await client.StopAsync();
}
finally
{
await client.ForceStopAsync();
}
}
}