Skip to content

Commit 7d7cc27

Browse files
authored
test: batch 07 of refactoring CLI tests not to use PTY (#25997)
Closes [coder/internal#1400](coder/internal#1400) Final batch of refactored CLI tests to avoid creating PTYs.
1 parent 88d9ce5 commit 7d7cc27

22 files changed

Lines changed: 216 additions & 334 deletions

cli/exp_scaletest_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"cdr.dev/slog/v3/sloggers/slogtest"
1111
"github.com/coder/coder/v2/cli/clitest"
1212
"github.com/coder/coder/v2/coderd/coderdtest"
13-
"github.com/coder/coder/v2/pty/ptytest"
1413
"github.com/coder/coder/v2/testutil"
1514
)
1615

@@ -56,10 +55,6 @@ func TestScaleTestCreateWorkspaces(t *testing.T) {
5655
"--max-failures", "1",
5756
)
5857
clitest.SetupConfig(t, client, root)
59-
pty := ptytest.New(t)
60-
inv.Stdout = pty.Output()
61-
inv.Stderr = pty.Output()
62-
6358
err := inv.WithContext(ctx).Run()
6459
require.ErrorContains(t, err, "could not find template \"doesnotexist\" in any organization")
6560
}
@@ -91,10 +86,6 @@ func TestScaleTestWorkspaceTraffic(t *testing.T) {
9186
"--ssh",
9287
)
9388
clitest.SetupConfig(t, client, root)
94-
pty := ptytest.New(t)
95-
inv.Stdout = pty.Output()
96-
inv.Stderr = pty.Output()
97-
9889
err := inv.WithContext(ctx).Run()
9990
require.ErrorContains(t, err, "no scaletest workspaces exist")
10091
}
@@ -120,10 +111,6 @@ func TestScaleTestWorkspaceTraffic_Template(t *testing.T) {
120111
"--template", "doesnotexist",
121112
)
122113
clitest.SetupConfig(t, client, root)
123-
pty := ptytest.New(t)
124-
inv.Stdout = pty.Output()
125-
inv.Stderr = pty.Output()
126-
127114
err := inv.WithContext(ctx).Run()
128115
require.ErrorContains(t, err, "could not find template \"doesnotexist\" in any organization")
129116
}
@@ -149,10 +136,6 @@ func TestScaleTestWorkspaceTraffic_TargetWorkspaces(t *testing.T) {
149136
"--target-workspaces", "0:0",
150137
)
151138
clitest.SetupConfig(t, client, root)
152-
pty := ptytest.New(t)
153-
inv.Stdout = pty.Output()
154-
inv.Stderr = pty.Output()
155-
156139
err := inv.WithContext(ctx).Run()
157140
require.ErrorContains(t, err, "invalid target workspaces \"0:0\": start and end cannot be equal")
158141
}
@@ -178,10 +161,6 @@ func TestScaleTestCleanup_Template(t *testing.T) {
178161
"--template", "doesnotexist",
179162
)
180163
clitest.SetupConfig(t, client, root)
181-
pty := ptytest.New(t)
182-
inv.Stdout = pty.Output()
183-
inv.Stderr = pty.Output()
184-
185164
err := inv.WithContext(ctx).Run()
186165
require.ErrorContains(t, err, "could not find template \"doesnotexist\" in any organization")
187166
}
@@ -208,10 +187,6 @@ func TestScaleTestDashboard(t *testing.T) {
208187
"--interval", "0s",
209188
)
210189
clitest.SetupConfig(t, client, root)
211-
pty := ptytest.New(t)
212-
inv.Stdout = pty.Output()
213-
inv.Stderr = pty.Output()
214-
215190
err := inv.WithContext(ctx).Run()
216191
require.ErrorContains(t, err, "--interval must be greater than zero")
217192
})
@@ -232,10 +207,6 @@ func TestScaleTestDashboard(t *testing.T) {
232207
"--jitter", "1s",
233208
)
234209
clitest.SetupConfig(t, client, root)
235-
pty := ptytest.New(t)
236-
inv.Stdout = pty.Output()
237-
inv.Stderr = pty.Output()
238-
239210
err := inv.WithContext(ctx).Run()
240211
require.ErrorContains(t, err, "--jitter must be less than --interval")
241212
})
@@ -260,10 +231,6 @@ func TestScaleTestDashboard(t *testing.T) {
260231
"--rand-seed", "1234567890",
261232
)
262233
clitest.SetupConfig(t, client, root)
263-
pty := ptytest.New(t)
264-
inv.Stdout = pty.Output()
265-
inv.Stderr = pty.Output()
266-
267234
err := inv.WithContext(ctx).Run()
268235
require.NoError(t, err, "")
269236
})
@@ -283,10 +250,6 @@ func TestScaleTestDashboard(t *testing.T) {
283250
"--target-users", "0:0",
284251
)
285252
clitest.SetupConfig(t, client, root)
286-
pty := ptytest.New(t)
287-
inv.Stdout = pty.Output()
288-
inv.Stderr = pty.Output()
289-
290253
err := inv.WithContext(ctx).Run()
291254
require.ErrorContains(t, err, "invalid target users \"0:0\": start and end cannot be equal")
292255
})

cli/externalauth_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import (
1010
"github.com/coder/coder/v2/cli/cliui"
1111
"github.com/coder/coder/v2/coderd/httpapi"
1212
"github.com/coder/coder/v2/codersdk/agentsdk"
13-
"github.com/coder/coder/v2/pty/ptytest"
13+
"github.com/coder/coder/v2/testutil"
14+
"github.com/coder/coder/v2/testutil/expecter"
1415
)
1516

1617
func TestExternalAuth(t *testing.T) {
1718
t.Parallel()
1819
t.Run("CanceledWithURL", func(t *testing.T) {
1920
t.Parallel()
21+
ctx := testutil.Context(t, testutil.WaitMedium)
2022
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2123
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
2224
URL: "https://github.com",
@@ -25,14 +27,14 @@ func TestExternalAuth(t *testing.T) {
2527
t.Cleanup(srv.Close)
2628
url := srv.URL
2729
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github")
28-
pty := ptytest.New(t)
29-
inv.Stdout = pty.Output()
30+
stdout := expecter.NewAttachedToInvocation(t, inv)
3031
waiter := clitest.StartWithWaiter(t, inv)
31-
pty.ExpectMatch("https://github.com")
32+
stdout.ExpectMatchContext(ctx, "https://github.com")
3233
waiter.RequireIs(cliui.ErrCanceled)
3334
})
3435
t.Run("SuccessWithToken", func(t *testing.T) {
3536
t.Parallel()
37+
ctx := testutil.Context(t, testutil.WaitMedium)
3638
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3739
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
3840
AccessToken: "bananas",
@@ -41,10 +43,9 @@ func TestExternalAuth(t *testing.T) {
4143
t.Cleanup(srv.Close)
4244
url := srv.URL
4345
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github")
44-
pty := ptytest.New(t)
45-
inv.Stdout = pty.Output()
46+
stdout := expecter.NewAttachedToInvocation(t, inv)
4647
clitest.Start(t, inv)
47-
pty.ExpectMatch("bananas")
48+
stdout.ExpectMatchContext(ctx, "bananas")
4849
})
4950
t.Run("NoArgs", func(t *testing.T) {
5051
t.Parallel()
@@ -61,6 +62,7 @@ func TestExternalAuth(t *testing.T) {
6162
})
6263
t.Run("SuccessWithExtra", func(t *testing.T) {
6364
t.Parallel()
65+
ctx := testutil.Context(t, testutil.WaitMedium)
6466
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6567
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
6668
AccessToken: "bananas",
@@ -72,9 +74,8 @@ func TestExternalAuth(t *testing.T) {
7274
t.Cleanup(srv.Close)
7375
url := srv.URL
7476
inv, _ := clitest.New(t, "--agent-url", url, "--agent-token", "foo", "external-auth", "access-token", "github", "--extra", "hey")
75-
pty := ptytest.New(t)
76-
inv.Stdout = pty.Output()
77+
stdout := expecter.NewAttachedToInvocation(t, inv)
7778
clitest.Start(t, inv)
78-
pty.ExpectMatch("there")
79+
stdout.ExpectMatchContext(ctx, "there")
7980
})
8081
}

cli/gitaskpass_test.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ import (
1515
"github.com/coder/coder/v2/coderd/httpapi"
1616
"github.com/coder/coder/v2/codersdk"
1717
"github.com/coder/coder/v2/codersdk/agentsdk"
18-
"github.com/coder/coder/v2/pty/ptytest"
1918
"github.com/coder/coder/v2/testutil"
19+
"github.com/coder/coder/v2/testutil/expecter"
2020
)
2121

2222
func TestGitAskpass(t *testing.T) {
2323
t.Parallel()
2424
t.Run("UsernameAndPassword", func(t *testing.T) {
2525
t.Parallel()
26+
ctx := testutil.Context(t, testutil.WaitMedium)
2627
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2728
httpapi.Write(context.Background(), w, http.StatusOK, agentsdk.ExternalAuthResponse{
2829
Username: "something",
@@ -34,22 +35,21 @@ func TestGitAskpass(t *testing.T) {
3435
inv, _ := clitest.New(t, "--agent-url", url, "Username for 'https://github.com':")
3536
inv.Environ.Set("GIT_PREFIX", "/")
3637
inv.Environ.Set("CODER_AGENT_TOKEN", "fake-token")
37-
pty := ptytest.New(t)
38-
inv.Stdout = pty.Output()
38+
stdout := expecter.NewAttachedToInvocation(t, inv)
3939
clitest.Start(t, inv)
40-
pty.ExpectMatch("something")
40+
stdout.ExpectMatchContext(ctx, "something")
4141

4242
inv, _ = clitest.New(t, "--agent-url", url, "Password for 'https://potato@github.com':")
4343
inv.Environ.Set("GIT_PREFIX", "/")
4444
inv.Environ.Set("CODER_AGENT_TOKEN", "fake-token")
45-
pty = ptytest.New(t)
46-
inv.Stdout = pty.Output()
45+
stdout = expecter.NewAttachedToInvocation(t, inv)
4746
clitest.Start(t, inv)
48-
pty.ExpectMatch("bananas")
47+
stdout.ExpectMatchContext(ctx, "bananas")
4948
})
5049

5150
t.Run("NoHost", func(t *testing.T) {
5251
t.Parallel()
52+
ctx := testutil.Context(t, testutil.WaitMedium)
5353
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5454
httpapi.Write(context.Background(), w, http.StatusNotFound, codersdk.Response{
5555
Message: "Nope!",
@@ -60,11 +60,10 @@ func TestGitAskpass(t *testing.T) {
6060
inv, _ := clitest.New(t, "--agent-url", url, "--no-open", "Username for 'https://github.com':")
6161
inv.Environ.Set("GIT_PREFIX", "/")
6262
inv.Environ.Set("CODER_AGENT_TOKEN", "fake-token")
63-
pty := ptytest.New(t)
64-
inv.Stderr = pty.Output()
63+
stdout := expecter.NewAttachedToInvocation(t, inv)
6564
err := inv.Run()
6665
require.ErrorIs(t, err, cliui.ErrCanceled)
67-
pty.ExpectMatch("Nope!")
66+
stdout.ExpectMatchContext(ctx, "Nope!")
6867
})
6968

7069
t.Run("Poll", func(t *testing.T) {
@@ -92,20 +91,19 @@ func TestGitAskpass(t *testing.T) {
9291
inv, _ := clitest.New(t, "--agent-url", url, "--no-open", "Username for 'https://github.com':")
9392
inv.Environ.Set("GIT_PREFIX", "/")
9493
inv.Environ.Set("CODER_AGENT_TOKEN", "fake-token")
95-
stdout := ptytest.New(t)
96-
inv.Stdout = stdout.Output()
97-
stderr := ptytest.New(t)
98-
inv.Stderr = stderr.Output()
94+
var stdout, stderr *expecter.Expecter
95+
stdout, inv.Stdout = expecter.NewPiped(t)
96+
stderr, inv.Stderr = expecter.NewPiped(t)
9997
go func() {
10098
err := inv.Run()
10199
assert.NoError(t, err)
102100
}()
103101
testutil.RequireReceive(ctx, t, poll)
104-
stderr.ExpectMatch("Open the following URL to authenticate")
102+
stderr.ExpectMatchContext(ctx, "Open the following URL to authenticate")
105103
resp.Store(&agentsdk.ExternalAuthResponse{
106104
Username: "username",
107105
Password: "password",
108106
})
109-
stdout.ExpectMatch("username")
107+
stdout.ExpectMatchContext(ctx, "username")
110108
})
111109
}

cli/gitssh_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/coder/coder/v2/coderd/database/dbfake"
2828
"github.com/coder/coder/v2/codersdk"
2929
"github.com/coder/coder/v2/codersdk/agentsdk"
30-
"github.com/coder/coder/v2/pty/ptytest"
3130
"github.com/coder/coder/v2/testutil"
3231
)
3332

@@ -194,7 +193,6 @@ func TestGitSSH(t *testing.T) {
194193
}, "\n")), 0o600)
195194
require.NoError(t, err)
196195

197-
pty := ptytest.New(t)
198196
cmdArgs := []string{
199197
"gitssh",
200198
"--agent-url", client.SDK.URL.String(),
@@ -205,8 +203,6 @@ func TestGitSSH(t *testing.T) {
205203
}
206204
// Test authentication via local private key.
207205
inv, _ := clitest.New(t, cmdArgs...)
208-
inv.Stdout = pty.Output()
209-
inv.Stderr = pty.Output()
210206
// This occasionally times out at 15s on Windows CI runners. Use a
211207
// longer timeout to reduce flakes.
212208
ctx := testutil.Context(t, testutil.WaitSuperLong)
@@ -225,8 +221,6 @@ func TestGitSSH(t *testing.T) {
225221

226222
// With the local file deleted, the coder key should be used.
227223
inv, _ = clitest.New(t, cmdArgs...)
228-
inv.Stdout = pty.Output()
229-
inv.Stderr = pty.Output()
230224
// This occasionally times out at 15s on Windows CI runners. Use a
231225
// longer timeout to reduce flakes.
232226
ctx = testutil.Context(t, testutil.WaitSuperLong) // Reset context for second cmd test.

0 commit comments

Comments
 (0)