Skip to content

Commit af8a377

Browse files
committed
fix unhandled terminal output in test
1 parent 112a0d0 commit af8a377

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

agent/agent_test.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,14 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
303303
require.NoError(t, err)
304304

305305
command := "sh"
306-
echoEnv := func(t *testing.T, w io.Writer, r io.Reader, env string) string {
306+
echoEnv := func(t *testing.T, w io.Writer, env string) {
307307
if runtime.GOOS == "windows" {
308308
_, err := fmt.Fprintf(w, "echo %%%s%%\r\n", env)
309309
require.NoError(t, err)
310310
} else {
311311
_, err := fmt.Fprintf(w, "echo $%s\n", env)
312312
require.NoError(t, err)
313313
}
314-
scanner := bufio.NewScanner(r)
315-
require.True(t, scanner.Scan())
316-
t.Logf("%s=%s", env, scanner.Text())
317-
return scanner.Text()
318314
}
319315
if runtime.GOOS == "windows" {
320316
command = "cmd.exe"
@@ -328,6 +324,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
328324
err = session.Start(command)
329325
require.NoError(t, err)
330326

327+
ctx := testutil.Context(t, testutil.WaitLong)
328+
329+
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+
341+
// Until we have gotten the first result, the shell may spit out some data.
342+
first := true
331343
//nolint:paralleltest // These tests need to run sequentially.
332344
for k, partialV := range map[string]string{
333345
"CODER": "true", // From the agent.
@@ -337,8 +349,21 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
337349
"MY_SESSION": "true", // From the session.
338350
} {
339351
t.Run(k, func(t *testing.T) {
340-
out := echoEnv(t, stdin, stdout, k)
341-
require.Contains(t, strings.TrimSpace(out), partialV)
352+
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+
}
362+
}
363+
}
364+
s := testutil.RequireRecvCtx(ctx, t, out)
365+
t.Logf("%s=%s", k, s)
366+
require.Contains(t, s, partialV)
342367
})
343368
}
344369
}

0 commit comments

Comments
 (0)