Skip to content

fix: Avoid dirtying stdout/stderr in test #3165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cli/clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ func New(t *testing.T, args ...string) (*cobra.Command, config.Root) {
dir := t.TempDir()
root := config.Root(dir)
cmd.SetArgs(append([]string{"--global-config", dir}, args...))

// We could consider using writers
// that log via t.Log here instead.
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)

return cmd, root
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cliui/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestPasswordTerminalState(t *testing.T) {
// connect the child process's stdio to the PTY directly, not via a pipe
cmd.Stdin = ptty.Input().Reader
cmd.Stdout = ptty.Output().Writer
cmd.Stderr = os.Stderr
cmd.Stderr = ptty.Output().Writer
err := cmd.Start()
require.NoError(t, err)
process := cmd.Process
Expand Down
3 changes: 2 additions & 1 deletion cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ func TestConfigSSH(t *testing.T) {
home := filepath.Dir(filepath.Dir(sshConfigFile))
// #nosec
sshCmd := exec.Command("ssh", "-F", sshConfigFile, "coder."+workspace.Name, "echo", "test")
pty = ptytest.New(t)
// Set HOME because coder config is included from ~/.ssh/coder.
sshCmd.Env = append(sshCmd.Env, fmt.Sprintf("HOME=%s", home))
sshCmd.Stderr = os.Stderr
sshCmd.Stderr = pty.Output()
data, err := sshCmd.Output()
require.NoError(t, err)
require.Equal(t, "test", strings.TrimSpace(string(data)))
Expand Down
9 changes: 4 additions & 5 deletions cli/portforward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io"
"net"
"os"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -158,7 +157,7 @@ func TestPortForward(t *testing.T) {
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag)
clitest.SetupConfig(t, client, root)
buf := newThreadSafeBuffer()
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
cmd.SetOut(buf)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errC := make(chan error)
Expand Down Expand Up @@ -204,7 +203,7 @@ func TestPortForward(t *testing.T) {
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag1, flag2)
clitest.SetupConfig(t, client, root)
buf := newThreadSafeBuffer()
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
cmd.SetOut(buf)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errC := make(chan error)
Expand Down Expand Up @@ -257,7 +256,7 @@ func TestPortForward(t *testing.T) {
cmd, root := clitest.New(t, "port-forward", workspace.Name, flag)
clitest.SetupConfig(t, client, root)
buf := newThreadSafeBuffer()
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
cmd.SetOut(buf)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errC := make(chan error)
Expand Down Expand Up @@ -317,7 +316,7 @@ func TestPortForward(t *testing.T) {
cmd, root := clitest.New(t, append([]string{"port-forward", workspace.Name}, flags...)...)
clitest.SetupConfig(t, client, root)
buf := newThreadSafeBuffer()
cmd.SetOut(io.MultiWriter(buf, os.Stderr))
cmd.SetOut(buf)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errC := make(chan error)
Expand Down