- Notifications
You must be signed in to change notification settings - Fork1k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
44 commits Select commitHold shift + click to select a range
dc71bd8
Get test passing on Linux, w/ new cross-plat pty abstraction
bryphe-coder03ea70c
Get most of the expect tests working
bryphe-coder2d1405c
Vendor conpty as well
bryphe-coderc949d44
Test out pipePty implementation
bryphe-codera73476d
Get tests passing using pipePty implementation
bryphe-coder0144a1b
No need for CR in SendLine
bryphe-coder8a71158
Get windows tests working with conpty
bryphe-coder49210ec
Bring back tty check
bryphe-codercde3ec2
Run go fmt
bryphe-coder1df68f3
Run go fmt
bryphe-coder1bff2f1
Add comment in 'isTTY' function
bryphe-coder9ea9bff
Remove unused code
bryphe-codere23745e
Fix up naming, the input/output pipes are always confusing...
bryphe-coderf61b2ef
Fix up naming, add some extra comments
bryphe-coder7b1f5df
Round of lint fixes
bryphe-coderb949301
More lint fixes
bryphe-coderc24774a
Remove unused imports
bryphe-coder8d7d782
Remaining lint fixes
bryphe-coder9922222
Add force-tty flag
bryphe-coder1faa215
Add comment describing why --force-tty is neede dfor test
bryphe-coder908b9cc
Fix typo
bryphe-coderbfe475e
Merge main
bryphe-coder2cb7256
Revert expect test changes
bryphe-coder3c08393
Update clitest to use cross-platform expect
bryphe-coder36a0d41
Mark force-tty flag as hidden
bryphe-coder7253eca
Run CLI tests on windows
bryphe-coder9b78fb7
Bring back force-tty flag for Windows
bryphe-codered2659e
Fix golang lint issue
bryphe-coder09a86e8
Run clitest_test on windows, too
bryphe-coderdb4d232
Merge branch 'main' into bryphe/experiment/241/cross-plat-expect
bryphe-coder0e8d4b6
Remove .Then()
bryphe-coder09e844b
Remove Regexp/RegexpPattern
bryphe-coder40c97c0
Remove additional unused functionality
bryphe-coderdabe9e4
Remove unused reader_lease
bryphe-coderf556c26
Close console after test
bryphe-codere76ad95
Move console cleanup to shared place
bryphe-coder4e4f3e2
Remove unused matchers
bryphe-coder5cba77d
Remove more unused options
bryphe-coder40ca4b5
Remove passthrough_pipe
bryphe-coderf6df631
Remove commented code
bryphe-coderc0e52dd
Replace test_log with test_console
bryphe-coder1962e97
Fix naming
bryphe-coder0ef0f19
Move force-tty check inside isTTY
bryphe-coder1de0f1b
Fix inverted conditional for forceTty check
bryphe-coderFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
34 changes: 0 additions & 34 deletionscli/clitest/clitest.go
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
5 changes: 2 additions & 3 deletionscli/clitest/clitest_test.go
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
3 changes: 2 additions & 1 deletioncli/login.go
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
10 changes: 6 additions & 4 deletionscli/login_test.go
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 |
---|---|---|
@@ -1,11 +1,10 @@ | ||
bryphe-coder marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
package cli_test | ||
import ( | ||
"testing" | ||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/expect" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
@@ -23,8 +22,11 @@ func TestLogin(t *testing.T) { | ||
t.Run("InitialUserTTY", func(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t) | ||
// The --force-tty flag is required on Windows, because the `isatty` library does not | ||
// accurately detect Windows ptys when they are not attached to a process: | ||
// https://github.com/mattn/go-isatty/issues/59 | ||
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty") | ||
console := expect.NewTestConsole(t, root) | ||
go func() { | ||
err := root.Execute() | ||
require.NoError(t, err) | ||
7 changes: 3 additions & 4 deletionscli/projectcreate_test.go
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
18 changes: 17 additions & 1 deletioncli/root.go
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 |
---|---|---|
@@ -21,6 +21,7 @@ import ( | ||
const ( | ||
varGlobalConfig = "global-config" | ||
varForceTty = "force-tty" | ||
) | ||
func Root() *cobra.Command { | ||
@@ -65,6 +66,12 @@ func Root() *cobra.Command { | ||
cmd.AddCommand(users()) | ||
cmd.PersistentFlags().String(varGlobalConfig, configdir.LocalConfig("coder"), "Path to the global `coder` config directory") | ||
cmd.PersistentFlags().Bool(varForceTty, false, "Force the `coder` command to run as if connected to a TTY") | ||
bryphe-coder marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
err := cmd.PersistentFlags().MarkHidden(varForceTty) | ||
if err != nil { | ||
// This should never return an error, because we just added the `--force-tty`` flag prior to calling MarkHidden. | ||
panic(err) | ||
} | ||
return cmd | ||
} | ||
@@ -113,7 +120,16 @@ func createConfig(cmd *cobra.Command) config.Root { | ||
// isTTY returns whether the passed reader is a TTY or not. | ||
// This accepts a reader to work with Cobra's "InOrStdin" | ||
// function for simple testing. | ||
func isTTY(cmd *cobra.Command) bool { | ||
// If the `--force-tty` command is available, and set, | ||
// assume we're in a tty. This is primarily for cases on Windows | ||
// where we may not be able to reliably detect this automatically (ie, tests) | ||
forceTty, err := cmd.Flags().GetBool(varForceTty) | ||
if forceTty && err == nil { | ||
return true | ||
} | ||
reader := cmd.InOrStdin() | ||
file, ok := reader.(*os.File) | ||
if !ok { | ||
return false | ||
5 changes: 2 additions & 3 deletionscli/workspacecreate_test.go
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
107 changes: 107 additions & 0 deletionsexpect/conpty/conpty.go
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,107 @@ | ||
//go:build windows | ||
// +build windows | ||
// Original copyright 2020 ActiveState Software. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file | ||
package conpty | ||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"golang.org/x/sys/windows" | ||
) | ||
// ConPty represents a windows pseudo console. | ||
type ConPty struct { | ||
hpCon windows.Handle | ||
outPipePseudoConsoleSide windows.Handle | ||
outPipeOurSide windows.Handle | ||
inPipeOurSide windows.Handle | ||
inPipePseudoConsoleSide windows.Handle | ||
consoleSize uintptr | ||
outFilePseudoConsoleSide *os.File | ||
outFileOurSide *os.File | ||
inFilePseudoConsoleSide *os.File | ||
inFileOurSide *os.File | ||
closed bool | ||
} | ||
// New returns a new ConPty pseudo terminal device | ||
func New(columns int16, rows int16) (*ConPty, error) { | ||
c := &ConPty{ | ||
consoleSize: uintptr(columns) + (uintptr(rows) << 16), | ||
} | ||
return c, c.createPseudoConsoleAndPipes() | ||
} | ||
// Close closes the pseudo-terminal and cleans up all attached resources | ||
func (c *ConPty) Close() error { | ||
// Trying to close these pipes multiple times will result in an | ||
// access violation | ||
if c.closed { | ||
return nil | ||
} | ||
err := closePseudoConsole(c.hpCon) | ||
c.outFilePseudoConsoleSide.Close() | ||
c.outFileOurSide.Close() | ||
c.inFilePseudoConsoleSide.Close() | ||
c.inFileOurSide.Close() | ||
c.closed = true | ||
return err | ||
} | ||
// OutPipe returns the output pipe of the pseudo terminal | ||
func (c *ConPty) OutPipe() *os.File { | ||
return c.outFilePseudoConsoleSide | ||
} | ||
func (c *ConPty) Reader() io.Reader { | ||
return c.outFileOurSide | ||
} | ||
// 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.inFilePseudoConsoleSide | ||
} | ||
func (c *ConPty) WriteString(str string) (int, error) { | ||
return c.inFileOurSide.WriteString(str) | ||
} | ||
func (c *ConPty) createPseudoConsoleAndPipes() error { | ||
// Create the stdin pipe | ||
if err := windows.CreatePipe(&c.inPipePseudoConsoleSide, &c.inPipeOurSide, nil, 0); err != nil { | ||
return err | ||
} | ||
// Create the stdout pipe | ||
if err := windows.CreatePipe(&c.outPipeOurSide, &c.outPipePseudoConsoleSide, nil, 0); err != nil { | ||
return err | ||
} | ||
// Create the pty with our stdin/stdout | ||
if err := createPseudoConsole(c.consoleSize, c.inPipePseudoConsoleSide, c.outPipePseudoConsoleSide, &c.hpCon); err != nil { | ||
return fmt.Errorf("failed to create pseudo console: %d, %v", uintptr(c.hpCon), err) | ||
} | ||
c.outFilePseudoConsoleSide = os.NewFile(uintptr(c.outPipePseudoConsoleSide), "|0") | ||
c.outFileOurSide = os.NewFile(uintptr(c.outPipeOurSide), "|1") | ||
c.inFilePseudoConsoleSide = os.NewFile(uintptr(c.inPipePseudoConsoleSide), "|2") | ||
c.inFileOurSide = os.NewFile(uintptr(c.inPipeOurSide), "|3") | ||
c.closed = false | ||
return nil | ||
} | ||
func (c *ConPty) Resize(cols uint16, rows uint16) error { | ||
return resizePseudoConsole(c.hpCon, uintptr(cols)+(uintptr(rows)<<16)) | ||
} |
53 changes: 53 additions & 0 deletionsexpect/conpty/syscall.go
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,53 @@ | ||
//go:build windows | ||
// +build windows | ||
// Copyright 2020 ActiveState Software. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file | ||
package conpty | ||
import ( | ||
"unsafe" | ||
"golang.org/x/sys/windows" | ||
) | ||
var ( | ||
kernel32 = windows.NewLazySystemDLL("kernel32.dll") | ||
procResizePseudoConsole = kernel32.NewProc("ResizePseudoConsole") | ||
procCreatePseudoConsole = kernel32.NewProc("CreatePseudoConsole") | ||
procClosePseudoConsole = kernel32.NewProc("ClosePseudoConsole") | ||
) | ||
func createPseudoConsole(consoleSize uintptr, ptyIn windows.Handle, ptyOut windows.Handle, hpCon *windows.Handle) (err error) { | ||
r1, _, e1 := procCreatePseudoConsole.Call( | ||
consoleSize, | ||
uintptr(ptyIn), | ||
uintptr(ptyOut), | ||
0, | ||
uintptr(unsafe.Pointer(hpCon)), | ||
) | ||
if r1 != 0 { // !S_OK | ||
err = e1 | ||
} | ||
return | ||
} | ||
func resizePseudoConsole(handle windows.Handle, consoleSize uintptr) (err error) { | ||
r1, _, e1 := procResizePseudoConsole.Call(uintptr(handle), consoleSize) | ||
if r1 != 0 { // !S_OK | ||
err = e1 | ||
} | ||
return | ||
} | ||
func closePseudoConsole(handle windows.Handle) (err error) { | ||
r1, _, e1 := procClosePseudoConsole.Call(uintptr(handle)) | ||
if r1 == 0 { | ||
err = e1 | ||
} | ||
return | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.