mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: do terminal emulation in reconnecting pty tests (#9114)
It looks like it is possible for screen to use control sequences instead of literal newlines which fails the tests. This reuses the existing readUntil function used in other pty tests.
This commit is contained in:
+1
-36
@@ -5,11 +5,9 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hinshun/vt10x"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
@@ -73,7 +71,7 @@ func Test_Start_truncation(t *testing.T) {
|
||||
n := 1
|
||||
for n <= countEnd {
|
||||
want := fmt.Sprintf("%d", n)
|
||||
err := readUntil(ctx, t, want, pc.OutputReader())
|
||||
err := testutil.ReadUntilString(ctx, t, want, pc.OutputReader())
|
||||
assert.NoError(t, err, "want: %s", want)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -141,36 +139,3 @@ func Test_Start_cancel_context(t *testing.T) {
|
||||
t.Error("cmd.Wait() timed out")
|
||||
}
|
||||
}
|
||||
|
||||
// readUntil reads one byte at a time until we either see the string we want, or the context expires
|
||||
func readUntil(ctx context.Context, t *testing.T, want string, r io.Reader) error {
|
||||
// output can contain virtual terminal sequences, so we need to parse these
|
||||
// to correctly interpret getting what we want.
|
||||
term := vt10x.New(vt10x.WithSize(80, 80))
|
||||
readErrs := make(chan error, 1)
|
||||
for {
|
||||
b := make([]byte, 1)
|
||||
go func() {
|
||||
_, err := r.Read(b)
|
||||
readErrs <- err
|
||||
}()
|
||||
select {
|
||||
case err := <-readErrs:
|
||||
if err != nil {
|
||||
t.Logf("err: %v\ngot: %v", err, term)
|
||||
return err
|
||||
}
|
||||
term.Write(b)
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
got := term.String()
|
||||
lines := strings.Split(got, "\n")
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line) == want {
|
||||
t.Logf("want: %v\n got:%v", want, line)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user