Skip to content

Commit

Permalink
Ensure pty and terminal size matches
Browse files Browse the repository at this point in the history
  • Loading branch information
Naatan committed Aug 24, 2023
1 parent 842358a commit 396fb8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 6 additions & 5 deletions termtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type ErrorHandler func(*TermTest, error) error
type Opts struct {
Logger *log.Logger
ExpectErrorHandler ErrorHandler
Cols uint16
Rows uint16
Cols int
Rows int
Posix bool
DefaultTimeout time.Duration
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func OptTestErrorHandler(t *testing.T) SetOpt {
return OptErrorHandler(TestErrorHandler(t))
}

func OptCols(cols uint16) SetOpt {
func OptCols(cols int) SetOpt {
return func(o *Opts) error {
o.Cols = cols
return nil
Expand All @@ -135,7 +135,7 @@ func OptCols(cols uint16) SetOpt {

// OptRows sets the number of rows for the pty, increase this if you find your output appears to stop prematurely
// appears to only make a difference on Windows. Linux/Mac will happily function with a single row
func OptRows(rows uint16) SetOpt {
func OptRows(rows int) SetOpt {
return func(o *Opts) error {
o.Rows = rows
return nil
Expand Down Expand Up @@ -181,13 +181,14 @@ func (tt *TermTest) start() error {
return fmt.Errorf("already started")
}

ptmx, err := pty.StartWithSize(tt.cmd, &pty.Winsize{Cols: tt.opts.Cols, Rows: tt.opts.Rows})
ptmx, err := pty.StartWithSize(tt.cmd, &pty.Winsize{Cols: uint16(tt.opts.Cols), Rows: uint16(tt.opts.Rows)})
if err != nil {
return fmt.Errorf("could not start pty: %w", err)
}
tt.ptmx = ptmx

tt.term = vt10x.New(vt10x.WithWriter(ptmx))
tt.term.Resize(tt.opts.Cols, tt.opts.Rows)

// Start listening for output
wg := &sync.WaitGroup{}
Expand Down
22 changes: 20 additions & 2 deletions termtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package termtest
import (
"fmt"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -105,8 +106,8 @@ func Test_ExpectExitCode(t *testing.T) {
}

func Test_SendAndOutput(t *testing.T) {
var cols uint16 = 20
strColWidth := strings.Repeat("o", int(cols))
var cols int = 20
strColWidth := strings.Repeat("o", cols)
tests := []struct {
name string
termtest func(t *testing.T) *TermTest
Expand Down Expand Up @@ -181,3 +182,20 @@ func Test_PendingOutput_Output_Snapshot(t *testing.T) {
assert.Equal(t, "MATCH1 MATCH2 MATCH3", strings.TrimRight(tt.Output(), "\r\n"))
assert.Contains(t, strings.TrimRight(tt.Snapshot(), "\r\n"), "MATCH1 MATCH2 MATCH3")
}

func Test_ColSize(t *testing.T) {
size := 1000
shell := "bash"
if runtime.GOOS == "windows" {
shell = "cmd.exe"
}
tt := newTermTest(t, exec.Command(shell), true, OptCols(size))
v := strings.Repeat("a", size)
tt.SendLine("echo " + v)
tt.Expect(v)
tt.SendLine("exit")
tt.ExpectExitCode(0)

// Also test that the terminal snapshot has the right col size
require.Contains(t, tt.Snapshot(), v)
}

0 comments on commit 396fb8d

Please sign in to comment.