Skip to content

fix: Run expect tests on Windows with conpty pseudo-terminal #276

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 44 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
dc71bd8
Get test passing on Linux, w/ new cross-plat pty abstraction
bryphe-coder Feb 11, 2022
03ea70c
Get most of the expect tests working
bryphe-coder Feb 11, 2022
2d1405c
Vendor conpty as well
bryphe-coder Feb 11, 2022
c949d44
Test out pipePty implementation
bryphe-coder Feb 11, 2022
a73476d
Get tests passing using pipePty implementation
bryphe-coder Feb 12, 2022
0144a1b
No need for CR in SendLine
bryphe-coder Feb 12, 2022
8a71158
Get windows tests working with conpty
bryphe-coder Feb 12, 2022
49210ec
Bring back tty check
bryphe-coder Feb 12, 2022
cde3ec2
Run go fmt
bryphe-coder Feb 12, 2022
1df68f3
Run go fmt
bryphe-coder Feb 12, 2022
1bff2f1
Add comment in 'isTTY' function
bryphe-coder Feb 12, 2022
9ea9bff
Remove unused code
bryphe-coder Feb 12, 2022
e23745e
Fix up naming, the input/output pipes are always confusing...
bryphe-coder Feb 12, 2022
f61b2ef
Fix up naming, add some extra comments
bryphe-coder Feb 12, 2022
7b1f5df
Round of lint fixes
bryphe-coder Feb 12, 2022
b949301
More lint fixes
bryphe-coder Feb 12, 2022
c24774a
Remove unused imports
bryphe-coder Feb 12, 2022
8d7d782
Remaining lint fixes
bryphe-coder Feb 12, 2022
9922222
Add force-tty flag
bryphe-coder Feb 12, 2022
1faa215
Add comment describing why --force-tty is neede dfor test
bryphe-coder Feb 12, 2022
908b9cc
Fix typo
bryphe-coder Feb 12, 2022
bfe475e
Merge main
bryphe-coder Feb 14, 2022
2cb7256
Revert expect test changes
bryphe-coder Feb 14, 2022
3c08393
Update clitest to use cross-platform expect
bryphe-coder Feb 14, 2022
36a0d41
Mark force-tty flag as hidden
bryphe-coder Feb 14, 2022
7253eca
Run CLI tests on windows
bryphe-coder Feb 14, 2022
9b78fb7
Bring back force-tty flag for Windows
bryphe-coder Feb 14, 2022
ed2659e
Fix golang lint issue
bryphe-coder Feb 14, 2022
09a86e8
Run clitest_test on windows, too
bryphe-coder Feb 14, 2022
db4d232
Merge branch 'main' into bryphe/experiment/241/cross-plat-expect
bryphe-coder Feb 14, 2022
0e8d4b6
Remove .Then()
bryphe-coder Feb 14, 2022
09e844b
Remove Regexp/RegexpPattern
bryphe-coder Feb 14, 2022
40c97c0
Remove additional unused functionality
bryphe-coder Feb 14, 2022
dabe9e4
Remove unused reader_lease
bryphe-coder Feb 14, 2022
f556c26
Close console after test
bryphe-coder Feb 14, 2022
e76ad95
Move console cleanup to shared place
bryphe-coder Feb 14, 2022
4e4f3e2
Remove unused matchers
bryphe-coder Feb 14, 2022
5cba77d
Remove more unused options
bryphe-coder Feb 14, 2022
40ca4b5
Remove passthrough_pipe
bryphe-coder Feb 14, 2022
f6df631
Remove commented code
bryphe-coder Feb 14, 2022
c0e52dd
Replace test_log with test_console
bryphe-coder Feb 14, 2022
1962e97
Fix naming
bryphe-coder Feb 14, 2022
0ef0f19
Move force-tty check inside isTTY
bryphe-coder Feb 14, 2022
1de0f1b
Fix inverted conditional for forceTty check
bryphe-coder Feb 15, 2022
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
Prev Previous commit
Next Next commit
More lint fixes
  • Loading branch information
bryphe-coder committed Feb 12, 2022
commit b949301b99c971d13e7a8a1d814aead83365dc44
16 changes: 8 additions & 8 deletions expect/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ type ConsoleOpts struct {
Logger *log.Logger
Stdouts []io.Writer
Closers []io.Closer
ExpectObservers []ExpectObserver
ExpectObservers []Observer
SendObservers []SendObserver
}

// ExpectObserver provides an interface for a function callback that will
// Observer provides an interface for a function callback that will
// be called after each Expect operation.
// matchers will be the list of active matchers when an error occurred,
// or a list of matchers that matched `buf` when err is nil.
// buf is the captured output that was matched against.
// err is error that might have occurred. May be nil.
type ExpectObserver func(matchers []Matcher, buf string, err error)
type Observer func(matchers []Matcher, buf string, err error)

// SendObserver provides an interface for a function callback that will
// be called after each Send operation.
Expand Down Expand Up @@ -97,7 +97,7 @@ func WithLogger(logger *log.Logger) ConsoleOpt {
}

// WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations.
func WithExpectObserver(observers ...ExpectObserver) ConsoleOpt {
func WithExpectObserver(observers ...Observer) ConsoleOpt {
return func(opts *ConsoleOpts) error {
opts.ExpectObservers = append(opts.ExpectObservers, observers...)
return nil
Expand All @@ -124,12 +124,12 @@ func NewConsole(opts ...ConsoleOpt) (*Console, error) {
}
}

pty, err := pty.New()
consolePty, err := pty.New()
if err != nil {
return nil, err
}
closers := append(options.Closers, pty)
reader := pty.Reader()
closers := append(options.Closers, consolePty)
reader := consolePty.Reader()

passthroughPipe, err := NewPassthroughPipe(reader)
if err != nil {
Expand All @@ -139,7 +139,7 @@ func NewConsole(opts ...ConsoleOpt) (*Console, error) {

console := &Console{
opts: options,
pty: pty,
pty: consolePty,
passthroughPipe: passthroughPipe,
runeReader: bufio.NewReaderSize(passthroughPipe, utf8.UTFMax),
closers: closers,
Expand Down
4 changes: 2 additions & 2 deletions expect/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (c *Console) ExpectEOF() (string, error) {
// expecting input yet, it will be blocked. Sends are queued up in tty's
// internal buffer so that the next Expect will read the remaining bytes (i.e.
// rest of prompt) as well as its conditions.
func (c *Console) Expect(opts ...ExpectOpt) (string, error) {
var options ExpectOpts
func (c *Console) Expect(opts ...Opt) (string, error) {
var options Opts
for _, opt := range opts {
if err := opt(&options); err != nil {
return "", err
Expand Down
44 changes: 22 additions & 22 deletions expect/expect_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ import (
"time"
)

// ExpectOpt allows settings Expect options.
type ExpectOpt func(*ExpectOpts) error
// Opt allows settings Expect options.
type Opt func(*Opts) error

// ConsoleCallback is a callback function to execute if a match is found for
// the chained matcher.
type ConsoleCallback func(buf *bytes.Buffer) error

// Then returns an Expect condition to execute a callback if a match is found
// for the chained matcher.
func (eo ExpectOpt) Then(consoleCallback ConsoleCallback) ExpectOpt {
return func(opts *ExpectOpts) error {
var options ExpectOpts
func (eo Opt) Then(consoleCallback ConsoleCallback) Opt {
return func(opts *Opts) error {
var options Opts
err := eo(&options)
if err != nil {
return err
Expand All @@ -51,15 +51,15 @@ func (eo ExpectOpt) Then(consoleCallback ConsoleCallback) ExpectOpt {
}
}

// ExpectOpts provides additional options on Expect.
type ExpectOpts struct {
// Opts provides additional options on Expect.
type Opts struct {
Matchers []Matcher
ReadTimeout *time.Duration
}

// Match sequentially calls Match on all matchers in ExpectOpts and returns the
// first matcher if a match exists, otherwise nil.
func (eo ExpectOpts) Match(v interface{}) Matcher {
func (eo Opts) Match(v interface{}) Matcher {
for _, matcher := range eo.Matchers {
if matcher.Match(v) {
return matcher
Expand Down Expand Up @@ -189,7 +189,7 @@ func (rm *regexpMatcher) Criteria() interface{} {
// allMatcher fulfills the Matcher interface to match a group of ExpectOpt
// against any value.
type allMatcher struct {
options ExpectOpts
options Opts
}

func (am *allMatcher) Match(v interface{}) bool {
Expand All @@ -215,9 +215,9 @@ func (am *allMatcher) Criteria() interface{} {

// All adds an Expect condition to exit if the content read from Console's tty
// matches all of the provided ExpectOpt, in any order.
func All(expectOpts ...ExpectOpt) ExpectOpt {
return func(opts *ExpectOpts) error {
var options ExpectOpts
func All(expectOpts ...Opt) Opt {
return func(opts *Opts) error {
var options Opts
for _, opt := range expectOpts {
if err := opt(&options); err != nil {
return err
Expand All @@ -233,8 +233,8 @@ func All(expectOpts ...ExpectOpt) ExpectOpt {

// String adds an Expect condition to exit if the content read from Console's
// tty contains any of the given strings.
func String(strs ...string) ExpectOpt {
return func(opts *ExpectOpts) error {
func String(strs ...string) Opt {
return func(opts *Opts) error {
for _, str := range strs {
opts.Matchers = append(opts.Matchers, &stringMatcher{
str: str,
Expand All @@ -246,8 +246,8 @@ func String(strs ...string) ExpectOpt {

// Regexp adds an Expect condition to exit if the content read from Console's
// tty matches the given Regexp.
func Regexp(res ...*regexp.Regexp) ExpectOpt {
return func(opts *ExpectOpts) error {
func Regexp(res ...*regexp.Regexp) Opt {
return func(opts *Opts) error {
for _, re := range res {
opts.Matchers = append(opts.Matchers, &regexpMatcher{
re: re,
Expand All @@ -260,8 +260,8 @@ func Regexp(res ...*regexp.Regexp) ExpectOpt {
// RegexpPattern adds an Expect condition to exit if the content read from
// Console's tty matches the given Regexp patterns. Expect returns an error if
// the patterns were unsuccessful in compiling the Regexp.
func RegexpPattern(ps ...string) ExpectOpt {
return func(opts *ExpectOpts) error {
func RegexpPattern(ps ...string) Opt {
return func(opts *Opts) error {
var res []*regexp.Regexp
for _, p := range ps {
re, err := regexp.Compile(p)
Expand All @@ -276,8 +276,8 @@ func RegexpPattern(ps ...string) ExpectOpt {

// Error adds an Expect condition to exit if reading from Console's tty returns
// one of the provided errors.
func Error(errs ...error) ExpectOpt {
return func(opts *ExpectOpts) error {
func Error(errs ...error) Opt {
return func(opts *Opts) error {
for _, err := range errs {
opts.Matchers = append(opts.Matchers, &errorMatcher{
err: err,
Expand All @@ -289,7 +289,7 @@ func Error(errs ...error) ExpectOpt {

// EOF adds an Expect condition to exit if io.EOF is returned from reading
// Console's tty.
func EOF(opts *ExpectOpts) error {
func EOF(opts *Opts) error {
return Error(io.EOF)(opts)
}

Expand All @@ -298,7 +298,7 @@ func EOF(opts *ExpectOpts) error {
// on Linux while reading from the ptm after the pts is closed.
// Further Reading:
// https://github.com/kr/pty/issues/21#issuecomment-129381749
func PTSClosed(opts *ExpectOpts) error {
func PTSClosed(opts *Opts) error {
opts.Matchers = append(opts.Matchers, &pathErrorMatcher{
pathError: os.PathError{
Op: "read",
Expand Down
53 changes: 41 additions & 12 deletions expect/expect_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ import (
)

func TestExpectOptString(t *testing.T) {
t.Parallel()

tests := []struct {
title string
opt ExpectOpt
opt Opt
data string
expected bool
}{
Expand Down Expand Up @@ -60,8 +62,11 @@ func TestExpectOptString(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()

var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand All @@ -80,9 +85,11 @@ func TestExpectOptString(t *testing.T) {
}

func TestExpectOptRegexp(t *testing.T) {
t.Parallel()

tests := []struct {
title string
opt ExpectOpt
opt Opt
data string
expected bool
}{
Expand Down Expand Up @@ -113,8 +120,11 @@ func TestExpectOptRegexp(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()

var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand All @@ -133,9 +143,11 @@ func TestExpectOptRegexp(t *testing.T) {
}

func TestExpectOptRegexpPattern(t *testing.T) {
t.Parallel()

tests := []struct {
title string
opt ExpectOpt
opt Opt
data string
expected bool
}{
Expand Down Expand Up @@ -166,8 +178,11 @@ func TestExpectOptRegexpPattern(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()

var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand All @@ -186,9 +201,11 @@ func TestExpectOptRegexpPattern(t *testing.T) {
}

func TestExpectOptError(t *testing.T) {
t.Parallel()

tests := []struct {
title string
opt ExpectOpt
opt Opt
data error
expected bool
}{
Expand Down Expand Up @@ -219,8 +236,11 @@ func TestExpectOptError(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()

var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand All @@ -235,14 +255,16 @@ func TestExpectOptError(t *testing.T) {
}

func TestExpectOptThen(t *testing.T) {
t.Parallel()

var (
errFirst = errors.New("first")
errSecond = errors.New("second")
)

tests := []struct {
title string
opt ExpectOpt
opt Opt
data string
match bool
expected error
Expand Down Expand Up @@ -290,8 +312,11 @@ func TestExpectOptThen(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()

var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand All @@ -318,9 +343,11 @@ func TestExpectOptThen(t *testing.T) {
}

func TestExpectOptAll(t *testing.T) {
t.Parallel()

tests := []struct {
title string
opt ExpectOpt
opt Opt
data string
expected bool
}{
Expand Down Expand Up @@ -387,8 +414,10 @@ func TestExpectOptAll(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.title, func(t *testing.T) {
var options ExpectOpts
t.Parallel()
var options Opts
err := test.opt(&options)
require.Nil(t, err)

Expand Down
Loading