5
5
"bytes"
6
6
"context"
7
7
"encoding/json"
8
+ "errors"
8
9
"fmt"
9
10
"io"
10
11
"math/rand"
@@ -324,22 +325,15 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
324
325
err = session .Start (command )
325
326
require .NoError (t , err )
326
327
328
+ // Context is fine here since we're not doing a parallel subtest.
327
329
ctx := testutil .Context (t , testutil .WaitLong )
330
+ go func () {
331
+ <- ctx .Done ()
332
+ _ = session .Close ()
333
+ }()
328
334
329
335
s := bufio .NewScanner (stdout )
330
- out := make (chan string )
331
- testutil .Go (t , func () {
332
- for s .Scan () {
333
- select {
334
- case out <- s .Text ():
335
- case <- ctx .Done ():
336
- return
337
- }
338
- }
339
- })
340
336
341
- // Until we have gotten the first result, the shell may spit out some data.
342
- first := true
343
337
//nolint:paralleltest // These tests need to run sequentially.
344
338
for k , partialV := range map [string ]string {
345
339
"CODER" : "true" , // From the agent.
@@ -350,21 +344,17 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
350
344
} {
351
345
t .Run (k , func (t * testing.T ) {
352
346
echoEnv (t , stdin , k )
353
- if first {
354
- for {
355
- s := testutil .RequireRecvCtx (ctx , t , out )
356
- t .Logf ("%s=%s" , k , s )
357
- s = strings .TrimSpace (s )
358
- if strings .Contains (s , partialV ) {
359
- first = false
360
- return
361
- }
347
+ // Windows is unreliable, so keep scanning until we find a match.
348
+ for s .Scan () {
349
+ got := strings .TrimSpace (s .Text ())
350
+ t .Logf ("%s=%s" , k , got )
351
+ if strings .Contains (got , partialV ) {
352
+ break
362
353
}
363
354
}
364
- s := testutil .RequireRecvCtx (ctx , t , out )
365
- t .Logf ("%s=%s" , k , s )
366
- s = strings .TrimSpace (s )
367
- require .Contains (t , s , partialV )
355
+ if err := s .Err (); ! errors .Is (err , io .EOF ) {
356
+ require .NoError (t , err )
357
+ }
368
358
})
369
359
}
370
360
}
0 commit comments