Skip to content

Commit 7bd2ed0

Browse files
committed
fix unhandled terminal output in test
1 parent d3d880b commit 7bd2ed0

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
@@ -312,18 +312,14 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
312312
require.NoError(t, err)
313313

314314
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) {
316316
if runtime.GOOS == "windows" {
317317
_, err := fmt.Fprintf(w, "echo %%%s%%\r\n", env)
318318
require.NoError(t, err)
319319
} else {
320320
_, err := fmt.Fprintf(w, "echo $%s\n", env)
321321
require.NoError(t, err)
322322
}
323-
scanner := bufio.NewScanner(r)
324-
require.True(t, scanner.Scan())
325-
t.Logf("%s=%s", env, scanner.Text())
326-
return scanner.Text()
327323
}
328324
if runtime.GOOS == "windows" {
329325
command = "cmd.exe"
@@ -337,6 +333,22 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
337333
err = session.Start(command)
338334
require.NoError(t, err)
339335

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
340352
//nolint:paralleltest // These tests need to run sequentially.
341353
for k, partialV := range map[string]string{
342354
"CODER": "true", // From the agent.
@@ -347,8 +359,21 @@ func TestAgent_Session_EnvironmentVariables(t *testing.T) {
347359
"PATH": scriptBinDir + string(filepath.ListSeparator),
348360
} {
349361
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)
352377
})
353378
}
354379
}

0 commit comments

Comments
 (0)