-
Notifications
You must be signed in to change notification settings - Fork 889
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 all commits
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
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
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
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
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,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)) | ||
} |
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.
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.