Skip to content

fix: use terminal emulator that keeps state in ReconnectingPTY tests #9765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add more pty diagnostics for terminal parsing
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis committed Sep 19, 2023
commit 2aaaec020beabf16a154e3364039d1cc90638c26
15 changes: 8 additions & 7 deletions testutil/pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ func ReadUntilString(ctx context.Context, t *testing.T, want string, r io.Reader
// ReadUntil emulates a terminal and reads one byte at a time until the matcher
// returns true or the context expires. If the matcher is nil, read until EOF.
// The PTY must be sized to 80x80 or there could be unexpected results.
func ReadUntil(ctx context.Context, t *testing.T, r io.Reader, matcher func(line string) bool) error {
func ReadUntil(ctx context.Context, t *testing.T, r io.Reader, matcher func(line string) bool) (retErr error) {
// output can contain virtual terminal sequences, so we need to parse these
// to correctly interpret getting what we want.
readBytes := make([]byte, 0)
term := vt10x.New(vt10x.WithSize(80, 80))
readErrs := make(chan error, 1)
defer func() {
// Dump the terminal contents since they can be helpful for debugging, but
// skip empty lines since much of the terminal will usually be blank.
// trim empty lines since much of the terminal will usually be blank.
got := term.String()
lines := strings.Split(got, "\n")
for _, line := range lines {
if strings.TrimSpace(line) != "" {
t.Logf("got: %v", line)
}
trimmed := strings.Trim(got, "\n")
t.Logf("Terminal contents:\n%s", trimmed)
if retErr != nil {
t.Logf("Bytes Read: %s", string(readBytes))
}
}()
for {
Expand All @@ -48,6 +48,7 @@ func ReadUntil(ctx context.Context, t *testing.T, r io.Reader, matcher func(line
if err != nil {
return err
}
readBytes = append(readBytes, b...)
_, err = term.Write(b)
if err != nil {
return err
Expand Down