Skip to content

Commit 6009c90

Browse files
authored
refactor: Rename 'expect' package to 'console' (#297)
Fixes #291 - renames the `expect` go package to `console`, and changes the api from `expect.NewTestConsole` to `console.New`, and a few other small changes to support the linter (ie, `ConsoleOpts` -> `Opts`)
1 parent 35291d3 commit 6009c90

16 files changed

+71
-70
lines changed

cli/clitest/clitest_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package clitest_test
33
import (
44
"testing"
55

6-
"github.com/coder/coder/cli/clitest"
7-
"github.com/coder/coder/coderd/coderdtest"
8-
"github.com/coder/coder/expect"
96
"github.com/stretchr/testify/require"
107
"go.uber.org/goleak"
8+
9+
"github.com/coder/coder/cli/clitest"
10+
"github.com/coder/coder/coderd/coderdtest"
11+
"github.com/coder/coder/console"
1112
)
1213

1314
func TestMain(m *testing.M) {
@@ -20,11 +21,11 @@ func TestCli(t *testing.T) {
2021
client := coderdtest.New(t)
2122
cmd, config := clitest.New(t)
2223
clitest.SetupConfig(t, client, config)
23-
console := expect.NewTestConsole(t, cmd)
24+
cons := console.New(t, cmd)
2425
go func() {
2526
err := cmd.Execute()
2627
require.NoError(t, err)
2728
}()
28-
_, err := console.ExpectString("coder")
29+
_, err := cons.ExpectString("coder")
2930
require.NoError(t, err)
3031
}

cli/login_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"testing"
55

66
"github.com/coder/coder/cli/clitest"
7-
"github.com/coder/coder/expect"
87
"github.com/coder/coder/coderd/coderdtest"
8+
"github.com/coder/coder/console"
99
"github.com/stretchr/testify/require"
1010
)
1111

@@ -26,7 +26,7 @@ func TestLogin(t *testing.T) {
2626
// accurately detect Windows ptys when they are not attached to a process:
2727
// https://github.com/mattn/go-isatty/issues/59
2828
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
29-
console := expect.NewTestConsole(t, root)
29+
cons := console.New(t, root)
3030
go func() {
3131
err := root.Execute()
3232
require.NoError(t, err)
@@ -42,12 +42,12 @@ func TestLogin(t *testing.T) {
4242
for i := 0; i < len(matches); i += 2 {
4343
match := matches[i]
4444
value := matches[i+1]
45-
_, err := console.ExpectString(match)
45+
_, err := cons.ExpectString(match)
4646
require.NoError(t, err)
47-
_, err = console.SendLine(value)
47+
_, err = cons.SendLine(value)
4848
require.NoError(t, err)
4949
}
50-
_, err := console.ExpectString("Welcome to Coder")
50+
_, err := cons.ExpectString("Welcome to Coder")
5151
require.NoError(t, err)
5252
})
5353
}

cli/projectcreate_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
"github.com/coder/coder/cli/clitest"
99
"github.com/coder/coder/coderd/coderdtest"
10+
"github.com/coder/coder/console"
1011
"github.com/coder/coder/database"
11-
"github.com/coder/coder/expect"
1212
"github.com/coder/coder/provisioner/echo"
1313
"github.com/coder/coder/provisionersdk/proto"
1414
)
@@ -26,7 +26,7 @@ func TestProjectCreate(t *testing.T) {
2626
cmd, root := clitest.New(t, "projects", "create", "--directory", source, "--provisioner", string(database.ProvisionerTypeEcho))
2727
clitest.SetupConfig(t, client, root)
2828
_ = coderdtest.NewProvisionerDaemon(t, client)
29-
console := expect.NewTestConsole(t, cmd)
29+
console := console.New(t, cmd)
3030
closeChan := make(chan struct{})
3131
go func() {
3232
err := cmd.Execute()
@@ -73,7 +73,7 @@ func TestProjectCreate(t *testing.T) {
7373
cmd, root := clitest.New(t, "projects", "create", "--directory", source, "--provisioner", string(database.ProvisionerTypeEcho))
7474
clitest.SetupConfig(t, client, root)
7575
coderdtest.NewProvisionerDaemon(t, client)
76-
console := expect.NewTestConsole(t, cmd)
76+
cons := console.New(t, cmd)
7777
closeChan := make(chan struct{})
7878
go func() {
7979
err := cmd.Execute()
@@ -91,9 +91,9 @@ func TestProjectCreate(t *testing.T) {
9191
for i := 0; i < len(matches); i += 2 {
9292
match := matches[i]
9393
value := matches[i+1]
94-
_, err := console.ExpectString(match)
94+
_, err := cons.ExpectString(match)
9595
require.NoError(t, err)
96-
_, err = console.SendLine(value)
96+
_, err = cons.SendLine(value)
9797
require.NoError(t, err)
9898
}
9999
<-closeChan

cli/workspacecreate_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/coder/coder/cli/clitest"
77
"github.com/coder/coder/coderd/coderdtest"
8-
"github.com/coder/coder/expect"
8+
"github.com/coder/coder/console"
99
"github.com/coder/coder/provisioner/echo"
1010
"github.com/coder/coder/provisionersdk/proto"
1111
"github.com/stretchr/testify/require"
@@ -36,7 +36,7 @@ func TestWorkspaceCreate(t *testing.T) {
3636
cmd, root := clitest.New(t, "workspaces", "create", project.Name)
3737
clitest.SetupConfig(t, client, root)
3838

39-
console := expect.NewTestConsole(t, cmd)
39+
cons := console.New(t, cmd)
4040
closeChan := make(chan struct{})
4141
go func() {
4242
err := cmd.Execute()
@@ -51,12 +51,12 @@ func TestWorkspaceCreate(t *testing.T) {
5151
for i := 0; i < len(matches); i += 2 {
5252
match := matches[i]
5353
value := matches[i+1]
54-
_, err := console.ExpectString(match)
54+
_, err := cons.ExpectString(match)
5555
require.NoError(t, err)
56-
_, err = console.SendLine(value)
56+
_, err = cons.SendLine(value)
5757
require.NoError(t, err)
5858
}
59-
_, err := console.ExpectString("Create")
59+
_, err := cons.ExpectString("Create")
6060
require.NoError(t, err)
6161
<-closeChan
6262
})
File renamed without changes.
File renamed without changes.

expect/console.go renamed to console/console.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expect
15+
package console
1616

1717
import (
1818
"bufio"
@@ -23,25 +23,25 @@ import (
2323
"os"
2424
"unicode/utf8"
2525

26-
"github.com/coder/coder/expect/pty"
26+
"github.com/coder/coder/console/pty"
2727
)
2828

2929
// Console is an interface to automate input and output for interactive
3030
// applications. Console can block until a specified output is received and send
3131
// input back on it's tty. Console can also multiplex other sources of input
3232
// and multiplex its output to other writers.
3333
type Console struct {
34-
opts ConsoleOpts
34+
opts Opts
3535
pty pty.Pty
3636
runeReader *bufio.Reader
3737
closers []io.Closer
3838
}
3939

40-
// ConsoleOpt allows setting Console options.
41-
type ConsoleOpt func(*ConsoleOpts) error
40+
// Opt allows setting Console options.
41+
type Opt func(*Opts) error
4242

43-
// ConsoleOpts provides additional options on creating a Console.
44-
type ConsoleOpts struct {
43+
// Opts provides additional options on creating a Console.
44+
type Opts struct {
4545
Logger *log.Logger
4646
Stdouts []io.Writer
4747
ExpectObservers []Observer
@@ -62,33 +62,33 @@ type Observer func(matchers []Matcher, buf string, err error)
6262
// last writer, writing to it's internal buffer for matching expects.
6363
// If a listed writer returns an error, that overall write operation stops and
6464
// returns the error; it does not continue down the list.
65-
func WithStdout(writers ...io.Writer) ConsoleOpt {
66-
return func(opts *ConsoleOpts) error {
65+
func WithStdout(writers ...io.Writer) Opt {
66+
return func(opts *Opts) error {
6767
opts.Stdouts = append(opts.Stdouts, writers...)
6868
return nil
6969
}
7070
}
7171

7272
// WithLogger adds a logger for Console to log debugging information to. By
7373
// default Console will discard logs.
74-
func WithLogger(logger *log.Logger) ConsoleOpt {
75-
return func(opts *ConsoleOpts) error {
74+
func WithLogger(logger *log.Logger) Opt {
75+
return func(opts *Opts) error {
7676
opts.Logger = logger
7777
return nil
7878
}
7979
}
8080

8181
// WithExpectObserver adds an ExpectObserver to allow monitoring Expect operations.
82-
func WithExpectObserver(observers ...Observer) ConsoleOpt {
83-
return func(opts *ConsoleOpts) error {
82+
func WithExpectObserver(observers ...Observer) Opt {
83+
return func(opts *Opts) error {
8484
opts.ExpectObservers = append(opts.ExpectObservers, observers...)
8585
return nil
8686
}
8787
}
8888

8989
// NewConsole returns a new Console with the given options.
90-
func NewConsole(opts ...ConsoleOpt) (*Console, error) {
91-
options := ConsoleOpts{
90+
func NewConsole(opts ...Opt) (*Console, error) {
91+
options := Opts{
9292
Logger: log.New(ioutil.Discard, "", 0),
9393
}
9494

@@ -105,14 +105,14 @@ func NewConsole(opts ...ConsoleOpt) (*Console, error) {
105105
closers := []io.Closer{consolePty}
106106
reader := consolePty.Reader()
107107

108-
console := &Console{
108+
cons := &Console{
109109
opts: options,
110110
pty: consolePty,
111111
runeReader: bufio.NewReaderSize(reader, utf8.UTFMax),
112112
closers: closers,
113113
}
114114

115-
return console, nil
115+
return cons, nil
116116
}
117117

118118
// Tty returns an input Tty for accepting input

expect/doc.go renamed to console/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
// applications. It is unlike expect in that it does not spawn or manage
1717
// process lifecycle. This package only focuses on expecting output and sending
1818
// input through it's psuedoterminal.
19-
package expect
19+
package console

expect/expect.go renamed to console/expect.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expect
15+
package console
1616

1717
import (
1818
"bufio"
@@ -40,8 +40,8 @@ func (c *Console) ExpectString(s string) (string, error) {
4040
// expecting input yet, it will be blocked. Sends are queued up in tty's
4141
// internal buffer so that the next Expect will read the remaining bytes (i.e.
4242
// rest of prompt) as well as its conditions.
43-
func (c *Console) Expect(opts ...Opt) (string, error) {
44-
var options Opts
43+
func (c *Console) Expect(opts ...ExpectOpt) (string, error) {
44+
var options ExpectOpts
4545
for _, opt := range opts {
4646
if err := opt(&options); err != nil {
4747
return "", err

expect/expect_opt.go renamed to console/expect_opt.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expect
15+
package console
1616

1717
import (
1818
"bytes"
1919
"strings"
2020
"time"
2121
)
2222

23-
// Opt allows settings Expect options.
24-
type Opt func(*Opts) error
23+
// ExpectOpt allows settings Expect options.
24+
type ExpectOpt func(*ExpectOpts) error
2525

26-
// ConsoleCallback is a callback function to execute if a match is found for
26+
// Callback is a callback function to execute if a match is found for
2727
// the chained matcher.
28-
type ConsoleCallback func(buf *bytes.Buffer) error
28+
type Callback func(buf *bytes.Buffer) error
2929

30-
// Opts provides additional options on Expect.
31-
type Opts struct {
30+
// ExpectOpts provides additional options on Expect.
31+
type ExpectOpts struct {
3232
Matchers []Matcher
3333
ReadTimeout *time.Duration
3434
}
3535

3636
// Match sequentially calls Match on all matchers in ExpectOpts and returns the
3737
// first matcher if a match exists, otherwise nil.
38-
func (eo Opts) Match(v interface{}) Matcher {
38+
func (eo ExpectOpts) Match(v interface{}) Matcher {
3939
for _, matcher := range eo.Matchers {
4040
if matcher.Match(v) {
4141
return matcher
@@ -83,7 +83,7 @@ func (sm *stringMatcher) Criteria() interface{} {
8383
// allMatcher fulfills the Matcher interface to match a group of ExpectOpt
8484
// against any value.
8585
type allMatcher struct {
86-
options Opts
86+
options ExpectOpts
8787
}
8888

8989
func (am *allMatcher) Match(v interface{}) bool {
@@ -109,9 +109,9 @@ func (am *allMatcher) Criteria() interface{} {
109109

110110
// All adds an Expect condition to exit if the content read from Console's tty
111111
// matches all of the provided ExpectOpt, in any order.
112-
func All(expectOpts ...Opt) Opt {
113-
return func(opts *Opts) error {
114-
var options Opts
112+
func All(expectOpts ...ExpectOpt) ExpectOpt {
113+
return func(opts *ExpectOpts) error {
114+
var options ExpectOpts
115115
for _, opt := range expectOpts {
116116
if err := opt(&options); err != nil {
117117
return err
@@ -127,8 +127,8 @@ func All(expectOpts ...Opt) Opt {
127127

128128
// String adds an Expect condition to exit if the content read from Console's
129129
// tty contains any of the given strings.
130-
func String(strs ...string) Opt {
131-
return func(opts *Opts) error {
130+
func String(strs ...string) ExpectOpt {
131+
return func(opts *ExpectOpts) error {
132132
for _, str := range strs {
133133
opts.Matchers = append(opts.Matchers, &stringMatcher{
134134
str: str,

expect/expect_opt_test.go renamed to console/expect_opt_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package expect_test
15+
package console_test
1616

1717
import (
1818
"bytes"
1919
"testing"
2020

2121
"github.com/stretchr/testify/require"
2222

23-
. "github.com/coder/coder/expect"
23+
. "github.com/coder/coder/console"
2424
)
2525

2626
func TestExpectOptString(t *testing.T) {
2727
t.Parallel()
2828

2929
tests := []struct {
3030
title string
31-
opt Opt
31+
opt ExpectOpt
3232
data string
3333
expected bool
3434
}{
@@ -63,7 +63,7 @@ func TestExpectOptString(t *testing.T) {
6363
t.Run(test.title, func(t *testing.T) {
6464
t.Parallel()
6565

66-
var options Opts
66+
var options ExpectOpts
6767
err := test.opt(&options)
6868
require.Nil(t, err)
6969

@@ -86,7 +86,7 @@ func TestExpectOptAll(t *testing.T) {
8686

8787
tests := []struct {
8888
title string
89-
opt Opt
89+
opt ExpectOpt
9090
data string
9191
expected bool
9292
}{
@@ -144,7 +144,7 @@ func TestExpectOptAll(t *testing.T) {
144144
test := test
145145
t.Run(test.title, func(t *testing.T) {
146146
t.Parallel()
147-
var options Opts
147+
var options ExpectOpts
148148
err := test.opt(&options)
149149
require.Nil(t, err)
150150

0 commit comments

Comments
 (0)