Skip to content

Commit 233aa17

Browse files
authored
fix: Avoid dirtying stdout/stderr in test (#3165)
* fix: Default all clitest commands to io.Discard stdout/err * fix: Never write to stdout or stderr in tests
1 parent ad2b29a commit 233aa17

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

cli/clitest/clitest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
2525
dir := t.TempDir()
2626
root := config.Root(dir)
2727
cmd.SetArgs(append([]string{"--global-config", dir}, args...))
28+
29+
// We could consider using writers
30+
// that log via t.Log here instead.
31+
cmd.SetOut(io.Discard)
32+
cmd.SetErr(io.Discard)
33+
2834
return cmd, root
2935
}
3036

cli/cliui/prompt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestPasswordTerminalState(t *testing.T) {
185185
// connect the child process's stdio to the PTY directly, not via a pipe
186186
cmd.Stdin = ptty.Input().Reader
187187
cmd.Stdout = ptty.Output().Writer
188-
cmd.Stderr = os.Stderr
188+
cmd.Stderr = ptty.Output().Writer
189189
err := cmd.Start()
190190
require.NoError(t, err)
191191
process := cmd.Process

cli/configssh_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ func TestConfigSSH(t *testing.T) {
173173
home := filepath.Dir(filepath.Dir(sshConfigFile))
174174
// #nosec
175175
sshCmd := exec.Command("ssh", "-F", sshConfigFile, "coder."+workspace.Name, "echo", "test")
176+
pty = ptytest.New(t)
176177
// Set HOME because coder config is included from ~/.ssh/coder.
177178
sshCmd.Env = append(sshCmd.Env, fmt.Sprintf("HOME=%s", home))
178-
sshCmd.Stderr = os.Stderr
179+
sshCmd.Stderr = pty.Output()
179180
data, err := sshCmd.Output()
180181
require.NoError(t, err)
181182
require.Equal(t, "test", strings.TrimSpace(string(data)))

cli/portforward_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"io"
88
"net"
9-
"os"
109
"path/filepath"
1110
"runtime"
1211
"strings"
@@ -158,7 +157,7 @@ func TestPortForward(t *testing.T) {
158157
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag)
159158
clitest.SetupConfig(t, client, root)
160159
buf := newThreadSafeBuffer()
161-
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
160+
cmd.SetOut(buf)
162161
ctx, cancel := context.WithCancel(context.Background())
163162
defer cancel()
164163
errC := make(chan error)
@@ -204,7 +203,7 @@ func TestPortForward(t *testing.T) {
204203
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag1, flag2)
205204
clitest.SetupConfig(t, client, root)
206205
buf := newThreadSafeBuffer()
207-
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
206+
cmd.SetOut(buf)
208207
ctx, cancel := context.WithCancel(context.Background())
209208
defer cancel()
210209
errC := make(chan error)
@@ -257,7 +256,7 @@ func TestPortForward(t *testing.T) {
257256
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag)
258257
clitest.SetupConfig(t, client, root)
259258
buf := newThreadSafeBuffer()
260-
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
259+
cmd.SetOut(buf)
261260
ctx, cancel := context.WithCancel(context.Background())
262261
defer cancel()
263262
errC := make(chan error)
@@ -317,7 +316,7 @@ func TestPortForward(t *testing.T) {
317316
cmd, root := clitest.New(t, append([]string{"port-forward", workspace.Name}, flags...)...)
318317
clitest.SetupConfig(t, client, root)
319318
buf := newThreadSafeBuffer()
320-
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
319+
cmd.SetOut(buf)
321320
ctx, cancel := context.WithCancel(context.Background())
322321
defer cancel()
323322
errC := make(chan error)

0 commit comments

Comments
 (0)