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
Get windows tests working with conpty
  • Loading branch information
bryphe-coder committed Feb 12, 2022
commit 8a711580ac4271cd1751ced290079310fd592377
47 changes: 29 additions & 18 deletions expect/conpty/conpty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ type ConPty struct {
hpCon windows.Handle
pipeFdIn windows.Handle
pipeFdOut windows.Handle
pipe3 windows.Handle
pipe4 windows.Handle
consoleSize uintptr
inPipe *os.File
outPipe *os.File
outputR *os.File
outputW *os.File
inputR *os.File
inputW *os.File
closed bool
}

// New returns a new ConPty pseudo terminal device
Expand All @@ -35,38 +40,45 @@ func New(columns int16, rows int16) (*ConPty, error) {

// Close closes the pseudo-terminal and cleans up all attached resources
func (c *ConPty) Close() error {
if (c.closed) {
return nil
}

err := closePseudoConsole(c.hpCon)
c.inPipe.Close()
c.outPipe.Close()
c.outputR.Close()
c.outputW.Close()
c.inputR.Close()
c.inputW.Close()
c.closed = true
return err
}

// OutPipe returns the output pipe of the pseudo terminal
func (c *ConPty) OutPipe() *os.File {
return c.inPipe
return c.outputR
}

func (c *ConPty) Reader() io.Reader {
return c.outPipe
return c.outputW
}

// InPipe returns input pipe of the pseudo terminal
// Note: It is safer to use the Write method to prevent partially-written VT sequences
// from corrupting the terminal
func (c *ConPty) InPipe() *os.File {
return c.outPipe
return c.inputR
}

func (c *ConPty) WriteString(str string) (int, error) {
return c.inPipe.WriteString(str)
return c.inputW.WriteString(str)
}

func (c *ConPty) createPseudoConsoleAndPipes() error {
// These are the readers/writers for "stdin", but we only need this to
// successfully call CreatePseudoConsole. After, we can throw it away.
var hPipeInW, hPipeInR windows.Handle

// Create the stdin pipe although we never use this.
// Create the stdin pipe
if err := windows.CreatePipe(&hPipeInR, &hPipeInW, nil, 0); err != nil {
return err
}
Expand All @@ -81,16 +93,15 @@ func (c *ConPty) createPseudoConsoleAndPipes() error {
return fmt.Errorf("failed to create pseudo console: %d, %v", uintptr(c.hpCon), err)
}

// Close our stdin cause we're never going to use it
if hPipeInR != windows.InvalidHandle {
windows.CloseHandle(hPipeInR)
}
if hPipeInW != windows.InvalidHandle {
windows.CloseHandle(hPipeInW)
}
c.pipe3 = hPipeInR
c.pipe4 = hPipeInW

c.outputR = os.NewFile(uintptr(c.pipeFdIn), "|0")
c.outputW = os.NewFile(uintptr(c.pipeFdOut), "|1")

c.inPipe = os.NewFile(uintptr(c.pipeFdIn), "|0")
c.outPipe = os.NewFile(uintptr(c.pipeFdOut), "|1")
c.inputR = os.NewFile(uintptr(c.pipe3), "|2")
c.inputW = os.NewFile(uintptr(c.pipe4), "|3")
c.closed = false

return nil
}
Expand Down
10 changes: 0 additions & 10 deletions expect/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io/ioutil"
"log"
"os"
"time"
"unicode/utf8"

"github.com/coder/coder/expect/pty"
Expand Down Expand Up @@ -49,7 +48,6 @@ type ConsoleOpts struct {
Closers []io.Closer
ExpectObservers []ExpectObserver
SendObservers []SendObserver
ReadTimeout *time.Duration
}

// ExpectObserver provides an interface for a function callback that will
Expand Down Expand Up @@ -114,14 +112,6 @@ func WithSendObserver(observers ...SendObserver) ConsoleOpt {
}
}

// 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{
Expand Down
13 changes: 0 additions & 13 deletions expect/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"bytes"
"fmt"
"io"
"time"
"unicode/utf8"
)

Expand Down Expand Up @@ -59,11 +58,6 @@ func (c *Console) Expect(opts ...ExpectOpt) (string, error) {
writer := io.MultiWriter(append(c.opts.Stdouts, buf)...)
runeWriter := bufio.NewWriterSize(writer, utf8.UTFMax)

readTimeout := c.opts.ReadTimeout
if options.ReadTimeout != nil {
readTimeout = options.ReadTimeout
}

var matcher Matcher
var err error

Expand All @@ -78,13 +72,6 @@ func (c *Console) Expect(opts ...ExpectOpt) (string, error) {
}()

for {
if readTimeout != nil {
err = c.passthroughPipe.SetReadDeadline(time.Now().Add(*readTimeout))
if err != nil {
return buf.String(), err
}
}

var r rune
r, _, err = c.runeReader.ReadRune()
if err != nil {
Expand Down
176 changes: 1 addition & 175 deletions expect/expect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ import (
"bufio"
"errors"
"fmt"
"io"
// "io/ioutil"
//"log"
// "os"
// "os/exec"
"io"
"runtime/debug"
"strings"
"sync"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -70,7 +65,6 @@ func newTestConsole(t *testing.T, opts ...ConsoleOpt) (*Console, error) {
opts = append([]ConsoleOpt{
expectNoError(t),
sendNoError(t),
WithDefaultTimeout(time.Second),
}, opts...)
return NewTestConsole(t, opts...)
}
Expand Down Expand Up @@ -131,14 +125,12 @@ func TestExpectf(t *testing.T) {
c.SendLine("2")
c.Expectf("What is %s backwards?", "Netflix")
c.SendLine("xilfteN")
c.ExpectEOF()
}()

err = Prompt(c.InTty(), c.OutTty())
if err != nil {
t.Errorf("Expected no error but got '%s'", err)
}
testCloser(t, c)
wg.Wait()
}

Expand All @@ -159,15 +151,12 @@ func TestExpect(t *testing.T) {
c.SendLine("2")
c.ExpectString("What is Netflix backwards?")
c.SendLine("xilfteN")
//c.ExpectEOF()
}()

err = Prompt(c.InTty(), c.OutTty())
if err != nil {
t.Errorf("Expected no error but got '%s'", err)
}
// close the pts so we can expect EOF
testCloser(t, c)
wg.Wait()
}

Expand All @@ -187,174 +176,11 @@ func TestExpectOutput(t *testing.T) {
defer wg.Done()
c.ExpectString("What is 1+1?")
c.SendLine("3")
//c.ExpectEOF()
}()

err = Prompt(c.InTty(), c.OutTty())
if err == nil || err != ErrWrongAnswer {
t.Errorf("Expected error '%s' but got '%s' instead", ErrWrongAnswer, err)
}
testCloser(t, c)
wg.Wait()
}

// TODO: Needs to be updated to work on Windows
// func TestExpectDefaultTimeout(t *testing.T) {
// t.Parallel()

// c, err := NewTestConsole(t, WithDefaultTimeout(0))
// if err != nil {
// t.Errorf("Expected no error but got'%s'", err)
// }
// defer testCloser(t, c)

// var wg sync.WaitGroup
// wg.Add(1)
// go func() {
// defer wg.Done()
// Prompt(c.InTty(), c.OutTty())
// }()

// _, err = c.ExpectString("What is 1+2?")
// if err == nil || !strings.Contains(err.Error(), "i/o timeout") {
// t.Errorf("Expected error to contain 'i/o timeout' but got '%s' instead", err)
// }

// //Close to unblock Prompt and wait for the goroutine to exit.
// c.Close()
// wg.Wait()
// }

// func TestExpectTimeout(t *testing.T) {
// t.Parallel()

// c, err := NewTestConsole(t)
// if err != nil {
// t.Errorf("Expected no error but got'%s'", err)
// }
// defer testCloser(t, c)

// var wg sync.WaitGroup
// wg.Add(1)
// go func() {
// defer wg.Done()
// Prompt(c.InTty(), c.OutTty())
// }()

// _, err = c.Expect(String("What is 1+2?"), WithTimeout(0))
// if err == nil || !strings.Contains(err.Error(), "i/o timeout") {
// t.Errorf("Expected error to contain 'i/o timeout' but got '%s' instead", err)
// }

// //Close to unblock Prompt and wait for the goroutine to exit.
// c.Close()
// wg.Wait()
// }

// func TestExpectDefaultTimeoutOverride(t *testing.T) {
// t.Parallel()

// c, err := newTestConsole(t, WithDefaultTimeout(100*time.Millisecond))
// if err != nil {
// t.Errorf("Expected no error but got'%s'", err)
// }
// defer testCloser(t, c)

// var wg sync.WaitGroup
// wg.Add(1)
// go func() {
// defer wg.Done()
// err = Prompt(c.InTty(), c.OutTty())
// if err != nil {
// t.Errorf("Expected no error but got '%s'", err)
// }
// time.Sleep(200 * time.Millisecond)
// c.Close()
// }()

// c.ExpectString("What is 1+1?")
// c.SendLine("2")
// c.ExpectString("What is Netflix backwards?")
// c.SendLine("xilfteN")
// c.Expect(EOF, PTSClosed, WithTimeout(time.Second))

// wg.Wait()
// }

// func TestEditor(t *testing.T) {
// if _, err := exec.LookPath("vi"); err != nil {
// t.Skip("vi not found in PATH")
// }
// t.Parallel()

// c, err := NewConsole(expectNoError(t), sendNoError(t))
// if err != nil {
// t.Errorf("Expected no error but got '%s'", err)
// }
// defer testCloser(t, c)

// file, err := ioutil.TempFile("", "")
// if err != nil {
// t.Errorf("Expected no error but got '%s'", err)
// }

// cmd := exec.Command("vi", file.Name())
// cmd.Stdin = c.InTty()
// cmd.Stdout = c.OutTty()
// cmd.Stderr = c.OutTty()

// var wg sync.WaitGroup
// wg.Add(1)
// go func() {
// defer wg.Done()
// c.Send("iHello world\x1b")
// c.SendLine(":wq!")
// c.ExpectEOF()
// }()

// err = cmd.Run()
// if err != nil {
// t.Errorf("Expected no error but got '%s'", err)
// }

// testCloser(t, c)
// wg.Wait()

// data, err := ioutil.ReadFile(file.Name())
// if err != nil {
// t.Errorf("Expected no error but got '%s'", err)
// }
// if string(data) != "Hello world\n" {
// t.Errorf("Expected '%s' to equal '%s'", string(data), "Hello world\n")
// }
// }

// func ExampleConsole_echo() {
// c, err := NewConsole(WithStdout(os.Stdout))
// if err != nil {
// log.Fatal(err)
// }
// defer c.Close()

// cmd := exec.Command("echo")
// cmd.Stdin = c.InTty()
// cmd.Stdout = c.OutTty()
// cmd.Stderr = c.OutTty()

// err = cmd.Start()
// if err != nil {
// log.Fatal(err)
// }

// c.Send("Hello world")
// c.ExpectString("Hello world")
// c.Close()
// c.ExpectEOF()

// err = cmd.Wait()
// if err != nil {
// log.Fatal(err)
// }

// Output: Hello world
// }
Loading