Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a43941

Browse files
committed
Merge main
2 parents 669f2ca + e5db936 commit 0a43941

39 files changed

+713
-1203
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"drpcserver",
3434
"fatih",
3535
"goleak",
36+
"gossh",
3637
"hashicorp",
3738
"httpmw",
3839
"isatty",
@@ -51,12 +52,16 @@
5152
"protobuf",
5253
"provisionerd",
5354
"provisionersdk",
55+
"ptty",
56+
"ptytest",
5457
"retrier",
5558
"sdkproto",
5659
"stretchr",
60+
"tcpip",
5761
"tfexec",
5862
"tfstate",
5963
"unconvert",
64+
"webrtc",
6065
"xerrors",
6166
"yamux"
6267
]

cli/clitest/clitest_test.go

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

99
"github.com/coder/coder/cli/clitest"
1010
"github.com/coder/coder/coderd/coderdtest"
11-
"github.com/coder/coder/console"
11+
"github.com/coder/coder/pty/ptytest"
1212
)
1313

1414
func TestMain(m *testing.M) {
@@ -21,11 +21,12 @@ func TestCli(t *testing.T) {
2121
client := coderdtest.New(t)
2222
cmd, config := clitest.New(t)
2323
clitest.SetupConfig(t, client, config)
24-
cons := console.New(t, cmd)
24+
pty := ptytest.New(t)
25+
cmd.SetIn(pty.Input())
26+
cmd.SetOut(pty.Output())
2527
go func() {
2628
err := cmd.Execute()
2729
require.NoError(t, err)
2830
}()
29-
_, err := cons.ExpectString("coder")
30-
require.NoError(t, err)
31+
pty.ExpectMatch("coder")
3132
}

cli/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func login() *cobra.Command {
154154
},
155155
})
156156
if err != nil {
157-
return xerrors.Errorf("specify email prompt: %w", err)
157+
return xerrors.Errorf("paste token prompt: %w", err)
158158
}
159159

160160
err = saveSessionToken(cmd, client, apiKey, serverURL)

cli/login_test.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/coder/coder/cli/clitest"
1010
"github.com/coder/coder/coderd"
1111
"github.com/coder/coder/coderd/coderdtest"
12-
"github.com/coder/coder/console"
12+
"github.com/coder/coder/pty/ptytest"
1313
)
1414

1515
func TestLogin(t *testing.T) {
@@ -29,7 +29,9 @@ func TestLogin(t *testing.T) {
2929
// accurately detect Windows ptys when they are not attached to a process:
3030
// https://github.com/mattn/go-isatty/issues/59
3131
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
32-
cons := console.New(t, root)
32+
pty := ptytest.New(t)
33+
root.SetIn(pty.Input())
34+
root.SetOut(pty.Output())
3335
go func() {
3436
err := root.Execute()
3537
require.NoError(t, err)
@@ -45,13 +47,10 @@ func TestLogin(t *testing.T) {
4547
for i := 0; i < len(matches); i += 2 {
4648
match := matches[i]
4749
value := matches[i+1]
48-
_, err := cons.ExpectString(match)
49-
require.NoError(t, err)
50-
_, err = cons.SendLine(value)
51-
require.NoError(t, err)
50+
pty.ExpectMatch(match)
51+
pty.WriteLine(value)
5252
}
53-
_, err := cons.ExpectString("Welcome to Coder")
54-
require.NoError(t, err)
53+
pty.ExpectMatch("Welcome to Coder")
5554
})
5655

5756
t.Run("ExistingUserValidTokenTTY", func(t *testing.T) {
@@ -71,18 +70,17 @@ func TestLogin(t *testing.T) {
7170
require.NoError(t, err)
7271

7372
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
74-
cons := console.New(t, root)
73+
pty := ptytest.New(t)
74+
root.SetIn(pty.Input())
75+
root.SetOut(pty.Output())
7576
go func() {
7677
err := root.Execute()
7778
require.NoError(t, err)
7879
}()
7980

80-
_, err = cons.ExpectString("Paste your token here:")
81-
require.NoError(t, err)
82-
_, err = cons.SendLine(token.SessionToken)
83-
require.NoError(t, err)
84-
_, err = cons.ExpectString("Welcome to Coder")
85-
require.NoError(t, err)
81+
pty.ExpectMatch("Paste your token here:")
82+
pty.WriteLine(token.SessionToken)
83+
pty.ExpectMatch("Welcome to Coder")
8684
})
8785

8886
t.Run("ExistingUserInvalidTokenTTY", func(t *testing.T) {
@@ -97,17 +95,17 @@ func TestLogin(t *testing.T) {
9795
require.NoError(t, err)
9896

9997
root, _ := clitest.New(t, "login", client.URL.String(), "--force-tty")
100-
cons := console.New(t, root)
98+
pty := ptytest.New(t)
99+
root.SetIn(pty.Input())
100+
root.SetOut(pty.Output())
101101
go func() {
102102
err := root.Execute()
103+
// An error is expected in this case, since the login wasn't successful:
103104
require.Error(t, err)
104105
}()
105106

106-
_, err = cons.ExpectString("Paste your token here:")
107-
require.NoError(t, err)
108-
_, err = cons.SendLine("an-invalid-token")
109-
require.NoError(t, err)
110-
_, err = cons.ExpectString("That's not a valid token!")
111-
require.NoError(t, err)
107+
pty.ExpectMatch("Paste your token here:")
108+
pty.WriteLine("an-invalid-token")
109+
pty.ExpectMatch("That's not a valid token!")
112110
})
113111
}

cli/projectcreate_test.go

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

88
"github.com/coder/coder/cli/clitest"
99
"github.com/coder/coder/coderd/coderdtest"
10-
"github.com/coder/coder/console"
1110
"github.com/coder/coder/database"
1211
"github.com/coder/coder/provisioner/echo"
1312
"github.com/coder/coder/provisionersdk/proto"
13+
"github.com/coder/coder/pty/ptytest"
1414
)
1515

1616
func TestProjectCreate(t *testing.T) {
@@ -26,7 +26,9 @@ 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 := console.New(t, cmd)
29+
pty := ptytest.New(t)
30+
cmd.SetIn(pty.Input())
31+
cmd.SetOut(pty.Output())
3032
closeChan := make(chan struct{})
3133
go func() {
3234
err := cmd.Execute()
@@ -43,10 +45,8 @@ func TestProjectCreate(t *testing.T) {
4345
for i := 0; i < len(matches); i += 2 {
4446
match := matches[i]
4547
value := matches[i+1]
46-
_, err := console.ExpectString(match)
47-
require.NoError(t, err)
48-
_, err = console.SendLine(value)
49-
require.NoError(t, err)
48+
pty.ExpectMatch(match)
49+
pty.WriteLine(value)
5050
}
5151
<-closeChan
5252
})
@@ -73,7 +73,9 @@ 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-
cons := console.New(t, cmd)
76+
pty := ptytest.New(t)
77+
cmd.SetIn(pty.Input())
78+
cmd.SetOut(pty.Output())
7779
closeChan := make(chan struct{})
7880
go func() {
7981
err := cmd.Execute()
@@ -91,10 +93,8 @@ func TestProjectCreate(t *testing.T) {
9193
for i := 0; i < len(matches); i += 2 {
9294
match := matches[i]
9395
value := matches[i+1]
94-
_, err := cons.ExpectString(match)
95-
require.NoError(t, err)
96-
_, err = cons.SendLine(value)
97-
require.NoError(t, err)
96+
pty.ExpectMatch(match)
97+
pty.WriteLine(value)
9898
}
9999
<-closeChan
100100
})

cli/root.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/manifoldco/promptui"
1313
"github.com/mattn/go-isatty"
1414
"github.com/spf13/cobra"
15-
"golang.org/x/xerrors"
1615

1716
"github.com/coder/coder/cli/config"
1817
"github.com/coder/coder/coderd"
@@ -138,14 +137,9 @@ func isTTY(cmd *cobra.Command) bool {
138137
}
139138

140139
func prompt(cmd *cobra.Command, prompt *promptui.Prompt) (string, error) {
141-
var ok bool
142-
prompt.Stdin, ok = cmd.InOrStdin().(io.ReadCloser)
143-
if !ok {
144-
return "", xerrors.New("stdin must be a readcloser")
145-
}
146-
prompt.Stdout, ok = cmd.OutOrStdout().(io.WriteCloser)
147-
if !ok {
148-
return "", xerrors.New("stdout must be a readcloser")
140+
prompt.Stdin = io.NopCloser(cmd.InOrStdin())
141+
prompt.Stdout = readWriteCloser{
142+
Writer: cmd.OutOrStdout(),
149143
}
150144

151145
// The prompt library displays defaults in a jarring way for the user
@@ -199,3 +193,10 @@ func prompt(cmd *cobra.Command, prompt *promptui.Prompt) (string, error) {
199193

200194
return value, err
201195
}
196+
197+
// readWriteCloser fakes reads, writes, and closing!
198+
type readWriteCloser struct {
199+
io.Reader
200+
io.Writer
201+
io.Closer
202+
}

cli/workspacecreate_test.go

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

88
"github.com/coder/coder/cli/clitest"
99
"github.com/coder/coder/coderd/coderdtest"
10-
"github.com/coder/coder/console"
1110
"github.com/coder/coder/provisioner/echo"
1211
"github.com/coder/coder/provisionersdk/proto"
12+
"github.com/coder/coder/pty/ptytest"
1313
)
1414

1515
func TestWorkspaceCreate(t *testing.T) {
@@ -37,7 +37,9 @@ func TestWorkspaceCreate(t *testing.T) {
3737
cmd, root := clitest.New(t, "workspaces", "create", project.Name)
3838
clitest.SetupConfig(t, client, root)
3939

40-
cons := console.New(t, cmd)
40+
pty := ptytest.New(t)
41+
cmd.SetIn(pty.Input())
42+
cmd.SetOut(pty.Output())
4143
closeChan := make(chan struct{})
4244
go func() {
4345
err := cmd.Execute()
@@ -52,13 +54,10 @@ func TestWorkspaceCreate(t *testing.T) {
5254
for i := 0; i < len(matches); i += 2 {
5355
match := matches[i]
5456
value := matches[i+1]
55-
_, err := cons.ExpectString(match)
56-
require.NoError(t, err)
57-
_, err = cons.SendLine(value)
58-
require.NoError(t, err)
57+
pty.ExpectMatch(match)
58+
pty.WriteLine(value)
5959
}
60-
_, err := cons.ExpectString("Create")
61-
require.NoError(t, err)
60+
pty.ExpectMatch("Create")
6261
<-closeChan
6362
})
6463
}

console/conpty/conpty.go

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)