@@ -312,18 +312,14 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
312
312
require .NoError (t , err )
313
313
314
314
command := "sh"
315
- echoEnv := func (t * testing.T , w io.Writer , r io. Reader , env string ) string {
315
+ echoEnv := func (t * testing.T , w io.Writer , env string ) {
316
316
if runtime .GOOS == "windows" {
317
317
_ , err := fmt .Fprintf (w , "echo %%%s%%\r \n " , env )
318
318
require .NoError (t , err )
319
319
} else {
320
320
_ , err := fmt .Fprintf (w , "echo $%s\n " , env )
321
321
require .NoError (t , err )
322
322
}
323
- scanner := bufio .NewScanner (r )
324
- require .True (t , scanner .Scan ())
325
- t .Logf ("%s=%s" , env , scanner .Text ())
326
- return scanner .Text ()
327
323
}
328
324
if runtime .GOOS == "windows" {
329
325
command = "cmd.exe"
@@ -337,6 +333,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
337
333
err = session .Start (command )
338
334
require .NoError (t , err )
339
335
336
+ ctx := testutil .Context (t , testutil .WaitLong )
337
+
338
+ s := bufio .NewScanner (stdout )
339
+ out := make (chan string )
340
+ testutil .Go (t , func () {
341
+ for s .Scan () {
342
+ select {
343
+ case out <- s .Text ():
344
+ case <- ctx .Done ():
345
+ return
346
+ }
347
+ }
348
+ })
349
+
350
+ // Until we have gotten the first result, the shell may spit out some data.
351
+ first := true
340
352
//nolint:paralleltest // These tests need to run sequentially.
341
353
for k , partialV := range map [string ]string {
342
354
"CODER" : "true" , // From the agent.
@@ -347,8 +359,21 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
347
359
"PATH" : scriptBinDir + string (filepath .ListSeparator ),
348
360
} {
349
361
t .Run (k , func (t * testing.T ) {
350
- out := echoEnv (t , stdin , stdout , k )
351
- require .Contains (t , strings .TrimSpace (out ), partialV )
362
+ echoEnv (t , stdin , k )
363
+ if first {
364
+ for {
365
+ s := testutil .RequireRecvCtx (ctx , t , out )
366
+ t .Logf ("%s=%s" , k , s )
367
+ s = strings .TrimSpace (s )
368
+ if strings .Contains (s , partialV ) {
369
+ first = false
370
+ return
371
+ }
372
+ }
373
+ }
374
+ s := testutil .RequireRecvCtx (ctx , t , out )
375
+ t .Logf ("%s=%s" , k , s )
376
+ require .Contains (t , s , partialV )
352
377
})
353
378
}
354
379
}
0 commit comments