mirror of
https://github.com/coder/coder.git
synced 2026-11-17 23:20:04 +08:00
This brings together a bunch of random, partially implemented packages for support of the new(ish) Windows [`conpty`](https://devblogs.microsoft.com/commandline/windows-command-line-introducing-the-windows-pseudo-console-conpty/) API - such that we can leverage the `expect` style of CLI tests, but in a way that works in Linux/OSX `pty`s and Windows `conpty`. These include: - Vendoring the `go-expect` library from Netflix w/ some tweaks to work cross-platform - Vendoring the `pty` cross-platform implementation from [waypoint-plugin-sdk](https://github.com/hashicorp/waypoint-plugin-sdk/tree/b55c787a65ff9b7d2b32cfae80681b78f8f2275e/internal/pkg/pty) - Vendoring the `conpty` Windows-specific implementation from [waypoint-plugin-sdk](https://github.com/hashicorp/waypoint-plugin-sdk/tree/b55c787a65ff9b7d2b32cfae80681b78f8f2275e/internal/pkg/conpty) - Adjusting the `pty` interface to work with `go-expect` + the cross-plat version There were several limitations with the current packages: - `go-expect` requires the same `os.File` (TTY) for input / output, but `conhost` requires separate file handles - `conpty` does not handle input, only output - The cross-platform `pty` didn't expose the full set of primitives needed for `console` Therefore, the following changes were made: - Handling of `stdin` was added to the `conpty` interface - We weren't using the full extent of the `go-expect` interface, so some portions were removed (ie, exec'ing a process) to simplify our implementation and make it easier to extend cross-platform - Instead of `console` exposing just a `Tty`, it exposes an `InTty` and `OutTty`, to help encapsulate the difference on Windows (on Linux, these point to the same pipe) Future improvements: - The `isatty` implementation doesn't support accurate detection of `conhost` pty's without an associated process. In lieu of a more robust check, I've added a `--force-tty` flag intended for test case use - that forces the CLI to run in tty mode. - It seems the windows implementation doesn't support setting a deadline. This is needed for the expect.Timeout API, but isn't used by us yet. Fixes #241
164 lines
4.5 KiB
Go
164 lines
4.5 KiB
Go
// 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"
|
|
"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
|
|
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
|
|
ExpectObservers []Observer
|
|
}
|
|
|
|
// 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 Observer func(matchers []Matcher, buf string, 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
|
|
}
|
|
}
|
|
|
|
// 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 ...Observer) ConsoleOpt {
|
|
return func(opts *ConsoleOpts) error {
|
|
opts.ExpectObservers = append(opts.ExpectObservers, observers...)
|
|
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
|
|
}
|
|
}
|
|
|
|
consolePty, err := pty.New()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
closers := []io.Closer{consolePty}
|
|
reader := consolePty.Reader()
|
|
|
|
console := &Console{
|
|
opts: options,
|
|
pty: consolePty,
|
|
runeReader: bufio.NewReaderSize(reader, utf8.UTFMax),
|
|
closers: closers,
|
|
}
|
|
|
|
return console, 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()
|
|
}
|
|
|
|
// 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)
|
|
return n, err
|
|
}
|
|
|
|
// SendLine writes string s to Console's tty with a trailing newline.
|
|
func (c *Console) SendLine(s string) (int, error) {
|
|
bytes, err := c.Send(fmt.Sprintf("%s\n", s))
|
|
|
|
return bytes, err
|
|
}
|
|
|
|
// 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...)
|
|
}
|