-
Notifications
You must be signed in to change notification settings - Fork 894
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
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 03ea70c
Get most of the expect tests working
bryphe-coder 2d1405c
Vendor conpty as well
bryphe-coder c949d44
Test out pipePty implementation
bryphe-coder a73476d
Get tests passing using pipePty implementation
bryphe-coder 0144a1b
No need for CR in SendLine
bryphe-coder 8a71158
Get windows tests working with conpty
bryphe-coder 49210ec
Bring back tty check
bryphe-coder cde3ec2
Run go fmt
bryphe-coder 1df68f3
Run go fmt
bryphe-coder 1bff2f1
Add comment in 'isTTY' function
bryphe-coder 9ea9bff
Remove unused code
bryphe-coder e23745e
Fix up naming, the input/output pipes are always confusing...
bryphe-coder f61b2ef
Fix up naming, add some extra comments
bryphe-coder 7b1f5df
Round of lint fixes
bryphe-coder b949301
More lint fixes
bryphe-coder c24774a
Remove unused imports
bryphe-coder 8d7d782
Remaining lint fixes
bryphe-coder 9922222
Add force-tty flag
bryphe-coder 1faa215
Add comment describing why --force-tty is neede dfor test
bryphe-coder 908b9cc
Fix typo
bryphe-coder bfe475e
Merge main
bryphe-coder 2cb7256
Revert expect test changes
bryphe-coder 3c08393
Update clitest to use cross-platform expect
bryphe-coder 36a0d41
Mark force-tty flag as hidden
bryphe-coder 7253eca
Run CLI tests on windows
bryphe-coder 9b78fb7
Bring back force-tty flag for Windows
bryphe-coder ed2659e
Fix golang lint issue
bryphe-coder 09a86e8
Run clitest_test on windows, too
bryphe-coder db4d232
Merge branch 'main' into bryphe/experiment/241/cross-plat-expect
bryphe-coder 0e8d4b6
Remove .Then()
bryphe-coder 09e844b
Remove Regexp/RegexpPattern
bryphe-coder 40c97c0
Remove additional unused functionality
bryphe-coder dabe9e4
Remove unused reader_lease
bryphe-coder f556c26
Close console after test
bryphe-coder e76ad95
Move console cleanup to shared place
bryphe-coder 4e4f3e2
Remove unused matchers
bryphe-coder 5cba77d
Remove more unused options
bryphe-coder 40ca4b5
Remove passthrough_pipe
bryphe-coder f6df631
Remove commented code
bryphe-coder c0e52dd
Replace test_log with test_console
bryphe-coder 1962e97
Fix naming
bryphe-coder 0ef0f19
Move force-tty check inside isTTY
bryphe-coder 1de0f1b
Fix inverted conditional for forceTty check
bryphe-coder File filter
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
Get test passing on Linux, w/ new cross-plat pty abstraction
- Loading branch information
commit dc71bd86401f851d19de029392843e5c143e1fe8
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
// Copyright 2018 Netflix, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package expect | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
"unicode/utf8" | ||
|
||
"github.com/coder/coder/expect/pty" | ||
) | ||
|
||
// Console is an interface to automate input and output for interactive | ||
// applications. Console can block until a specified output is received and send | ||
// input back on it's tty. Console can also multiplex other sources of input | ||
// and multiplex its output to other writers. | ||
type Console struct { | ||
opts ConsoleOpts | ||
pty pty.Pty | ||
passthroughPipe *PassthroughPipe | ||
runeReader *bufio.Reader | ||
closers []io.Closer | ||
} | ||
|
||
// ConsoleOpt allows setting Console options. | ||
type ConsoleOpt func(*ConsoleOpts) error | ||
|
||
// ConsoleOpts provides additional options on creating a Console. | ||
type ConsoleOpts struct { | ||
Logger *log.Logger | ||
Stdouts []io.Writer | ||
Closers []io.Closer | ||
ExpectObservers []ExpectObserver | ||
SendObservers []SendObserver | ||
ReadTimeout *time.Duration | ||
} | ||
|
||
// ExpectObserver 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) | ||
|
||
// SendObserver provides an interface for a function callback that will | ||
// be called after each Send operation. | ||
// msg is the string that was sent. | ||
// num is the number of bytes actually sent. | ||
// err is the error that might have occured. May be nil. | ||
type SendObserver func(msg string, num int, err error) | ||
|
||
// WithStdout adds writers that Console duplicates writes to, similar to the | ||
// Unix tee(1) command. | ||
// | ||
// Each write is written to each listed writer, one at a time. Console is the | ||
// last writer, writing to it's internal buffer for matching expects. | ||
// If a listed writer returns an error, that overall write operation stops and | ||
// returns the error; it does not continue down the list. | ||
func WithStdout(writers ...io.Writer) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.Stdouts = append(opts.Stdouts, writers...) | ||
return nil | ||
} | ||
} | ||
|
||
// WithCloser adds closers that are closed in order when Console is closed. | ||
func WithCloser(closer ...io.Closer) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.Closers = append(opts.Closers, closer...) | ||
return nil | ||
} | ||
} | ||
|
||
// WithLogger adds a logger for Console to log debugging information to. By | ||
// default Console will discard logs. | ||
func WithLogger(logger *log.Logger) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.Logger = logger | ||
return nil | ||
} | ||
} | ||
|
||
// WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations. | ||
func WithExpectObserver(observers ...ExpectObserver) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.ExpectObservers = append(opts.ExpectObservers, observers...) | ||
return nil | ||
} | ||
} | ||
|
||
// WithSendObserver adds a SendObserver to allow monitoring Send operations. | ||
func WithSendObserver(observers ...SendObserver) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.SendObservers = append(opts.SendObservers, observers...) | ||
return nil | ||
} | ||
} | ||
|
||
// WithDefaultTimeout sets a default read timeout during Expect statements. | ||
func WithDefaultTimeout(timeout time.Duration) ConsoleOpt { | ||
return func(opts *ConsoleOpts) error { | ||
opts.ReadTimeout = &timeout | ||
return nil | ||
} | ||
} | ||
|
||
// NewConsole returns a new Console with the given options. | ||
func NewConsole(opts ...ConsoleOpt) (*Console, error) { | ||
options := ConsoleOpts{ | ||
Logger: log.New(ioutil.Discard, "", 0), | ||
} | ||
|
||
for _, opt := range opts { | ||
if err := opt(&options); err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
pty, err := pty.New() | ||
if err != nil { | ||
return nil, err | ||
} | ||
closers := append(options.Closers, pty) | ||
reader := pty.Reader() | ||
|
||
passthroughPipe, err := NewPassthroughPipe(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
closers = append(closers, passthroughPipe) | ||
|
||
c := &Console{ | ||
opts: options, | ||
pty: pty, | ||
passthroughPipe: passthroughPipe, | ||
runeReader: bufio.NewReaderSize(passthroughPipe, utf8.UTFMax), | ||
closers: closers, | ||
} | ||
|
||
/*for _, stdin := range options.Stdins { | ||
go func(stdin io.Reader) { | ||
_, err := io.Copy(c, stdin) | ||
if err != nil { | ||
c.Logf("failed to copy stdin: %s", err) | ||
} | ||
}(stdin) | ||
}*/ | ||
|
||
return c, nil | ||
} | ||
|
||
// Tty returns an input Tty for accepting input | ||
func (c *Console) InTty() *os.File { | ||
return c.pty.InPipe() | ||
} | ||
|
||
// OutTty returns an output tty for writing | ||
func (c *Console) OutTty() *os.File { | ||
return c.pty.OutPipe() | ||
} | ||
|
||
// Read reads bytes b from Console's tty. | ||
/*func (c *Console) Read(b []byte) (int, error) { | ||
return c.ptm.Read(b) | ||
}*/ | ||
|
||
// Write writes bytes b to Console's tty. | ||
/*func (c *Console) Write(b []byte) (int, error) { | ||
c.Logf("console write: %q", b) | ||
return c.ptm.Write(b) | ||
}*/ | ||
|
||
// Fd returns Console's file descripting referencing the master part of its | ||
// pty. | ||
/*func (c *Console) Fd() uintptr { | ||
return c.ptm.Fd() | ||
}*/ | ||
bryphe-coder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Close closes Console's tty. Calling Close will unblock Expect and ExpectEOF. | ||
func (c *Console) Close() error { | ||
for _, fd := range c.closers { | ||
err := fd.Close() | ||
if err != nil { | ||
c.Logf("failed to close: %s", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Send writes string s to Console's tty. | ||
func (c *Console) Send(s string) (int, error) { | ||
c.Logf("console send: %q", s) | ||
n, err := c.pty.WriteString(s) | ||
for _, observer := range c.opts.SendObservers { | ||
observer(s, n, err) | ||
} | ||
return n, err | ||
} | ||
|
||
// SendLine writes string s to Console's tty with a trailing newline. | ||
func (c *Console) SendLine(s string) (int, error) { | ||
return c.Send(fmt.Sprintf("%s\n", s)) | ||
} | ||
|
||
// Log prints to Console's logger. | ||
// Arguments are handled in the manner of fmt.Print. | ||
func (c *Console) Log(v ...interface{}) { | ||
c.opts.Logger.Print(v...) | ||
} | ||
|
||
// Logf prints to Console's logger. | ||
// Arguments are handled in the manner of fmt.Printf. | ||
func (c *Console) Logf(format string, v ...interface{}) { | ||
c.opts.Logger.Printf(format, v...) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018 Netflix, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package expect provides an expect-like interface to automate control of | ||
// applications. It is unlike expect in that it does not spawn or manage | ||
// process lifecycle. This package only focuses on expecting output and sending | ||
// input through it's psuedoterminal. | ||
package expect |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.