diff --git a/commands/alias/alias_test.go b/commands/alias/alias_test.go index 85a03e6a..02333f97 100644 --- a/commands/alias/alias_test.go +++ b/commands/alias/alias_test.go @@ -1,13 +1,12 @@ package alias import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func Test_Alias(t *testing.T) { @@ -17,18 +16,7 @@ func Test_Alias(t *testing.T) { assert.Nil(t, NewCmdAlias(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"alias [command] --help\" for more information about a command.\n") } diff --git a/commands/auth/auth_test.go b/commands/auth/auth_test.go index 935de927..2c0eb836 100644 --- a/commands/auth/auth_test.go +++ b/commands/auth/auth_test.go @@ -1,13 +1,12 @@ package auth import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestIssueCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) { assert.Nil(t, NewCmdAuth(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Manage glab's authentication state\n") } diff --git a/commands/ci/ci_test.go b/commands/ci/ci_test.go index 674ce1a3..eaa61e71 100644 --- a/commands/ci/ci_test.go +++ b/commands/ci/ci_test.go @@ -1,13 +1,12 @@ package ci import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestPipelineCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestPipelineCmd(t *testing.T) { assert.Nil(t, NewCmdCI(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"ci [command] --help\" for more information about a command.\n") } diff --git a/commands/cluster/agent/agent_test.go b/commands/cluster/agent/agent_test.go index d385dd2e..34fa56f9 100644 --- a/commands/cluster/agent/agent_test.go +++ b/commands/cluster/agent/agent_test.go @@ -1,13 +1,12 @@ package cluster import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestNewCmdAgent(t *testing.T) { @@ -17,18 +16,7 @@ func TestNewCmdAgent(t *testing.T) { assert.Nil(t, NewCmdAgent(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Manage GitLab Agents for Kubernetes") } diff --git a/commands/cluster/cluster_test.go b/commands/cluster/cluster_test.go index 3f9b2d3a..39233db0 100644 --- a/commands/cluster/cluster_test.go +++ b/commands/cluster/cluster_test.go @@ -1,13 +1,12 @@ package cluster import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestNewCmdCluster(t *testing.T) { @@ -17,18 +16,7 @@ func TestNewCmdCluster(t *testing.T) { assert.Nil(t, NewCmdCluster(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Manage GitLab Agents for Kubernetes and their clusters") } diff --git a/commands/help/help_test.go b/commands/help/help_test.go index c367e2f6..f7b75e23 100644 --- a/commands/help/help_test.go +++ b/commands/help/help_test.go @@ -1,8 +1,6 @@ package help import ( - "bytes" - "io" "os" "testing" @@ -13,6 +11,7 @@ import ( "gitlab.com/gitlab-org/cli/commands/alias" "gitlab.com/gitlab-org/cli/commands/alias/set" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestDedent(t *testing.T) { @@ -98,18 +97,9 @@ USAGE alias.NewCmdAlias(&cmdutils.Factory{}).AddCommand(cmd) } RootHelpFunc(streams.Color(), cmd, tt.args.args) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) + assert.Contains(t, out, tt.wantOut) }) } diff --git a/commands/incident/incident_test.go b/commands/incident/incident_test.go index 81a23694..8074bc9b 100644 --- a/commands/incident/incident_test.go +++ b/commands/incident/incident_test.go @@ -1,13 +1,12 @@ package incident import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestIncidentCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestIncidentCmd(t *testing.T) { assert.Nil(t, NewCmdIncident(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Work with GitLab incidents\n") } diff --git a/commands/issue/board/board_test.go b/commands/issue/board/board_test.go index cd9ed75a..b4130ddb 100644 --- a/commands/issue/board/board_test.go +++ b/commands/issue/board/board_test.go @@ -1,13 +1,12 @@ package board import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestNewCmdBoard(t *testing.T) { @@ -17,18 +16,7 @@ func TestNewCmdBoard(t *testing.T) { assert.Nil(t, NewCmdBoard(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Work with GitLab Issue Boards in the given project.\n") } diff --git a/commands/issue/issue_test.go b/commands/issue/issue_test.go index c4e1dfc0..6734be91 100644 --- a/commands/issue/issue_test.go +++ b/commands/issue/issue_test.go @@ -1,13 +1,12 @@ package issue import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestIssueCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) { assert.Nil(t, NewCmdIssue(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Work with GitLab issues\n") } diff --git a/commands/label/label_test.go b/commands/label/label_test.go index 7d9787db..f2341eee 100644 --- a/commands/label/label_test.go +++ b/commands/label/label_test.go @@ -1,13 +1,12 @@ package label import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestNewCmdLabel(t *testing.T) { @@ -17,18 +16,7 @@ func TestNewCmdLabel(t *testing.T) { assert.Nil(t, NewCmdLabel(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"label [command] --help\" for more information about a command.\n") } diff --git a/commands/mr/mr_test.go b/commands/mr/mr_test.go index 68899711..b89c2ce9 100644 --- a/commands/mr/mr_test.go +++ b/commands/mr/mr_test.go @@ -1,14 +1,13 @@ package mr import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdtest" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestMain(m *testing.M) { @@ -23,18 +22,7 @@ func TestMrCmd_noARgs(t *testing.T) { assert.Nil(t, NewCmdMR(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"mr [command] --help\" for more information about a command.\n") } diff --git a/commands/project/repo_test.go b/commands/project/repo_test.go index a703a2d1..49f5c95b 100644 --- a/commands/project/repo_test.go +++ b/commands/project/repo_test.go @@ -1,13 +1,12 @@ package project import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func Test_Repo(t *testing.T) { @@ -17,18 +16,7 @@ func Test_Repo(t *testing.T) { assert.Nil(t, NewCmdRepo(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"repo [command] --help\" for more information about a command.\n") } diff --git a/commands/release/release_test.go b/commands/release/release_test.go index 4c80cb98..ef190497 100644 --- a/commands/release/release_test.go +++ b/commands/release/release_test.go @@ -1,13 +1,12 @@ package release import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func Test_Release(t *testing.T) { @@ -19,18 +18,7 @@ func Test_Release(t *testing.T) { assert.NotNil(t, cmd.Root()) assert.Nil(t, cmd.Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Manage GitLab releases") } diff --git a/commands/root_test.go b/commands/root_test.go index 375c7c2c..0efe42f3 100644 --- a/commands/root_test.go +++ b/commands/root_test.go @@ -1,14 +1,13 @@ package commands import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdtest" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestMain(m *testing.M) { @@ -23,18 +22,7 @@ func TestRootVersion(t *testing.T) { assert.Nil(t, rootCmd.Flag("version").Value.Set("true")) assert.Nil(t, rootCmd.Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Equal(t, "glab version 1.0.0 (2020-01-01)\n", out) } @@ -46,18 +34,8 @@ func TestRootNoArg(t *testing.T) { rootCmd := NewCmdRoot(cmdutils.NewFactory(), "v1.0.0", "2020-01-01") assert.Nil(t, rootCmd.Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() + out := test.ReturnBuffer(old, r, w) - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC assert.Contains(t, out, "GLab is an open source GitLab CLI tool that brings GitLab to your command line.\n") assert.Contains(t, out, `USAGE glab [flags] diff --git a/commands/schedule/run/run_test.go b/commands/schedule/run/run_test.go index 45c8b0a9..0a718e7d 100644 --- a/commands/schedule/run/run_test.go +++ b/commands/schedule/run/run_test.go @@ -1,8 +1,6 @@ package run import ( - "bytes" - "io" "os" "testing" @@ -16,6 +14,7 @@ import ( "gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/pkg/iostreams" + "gitlab.com/gitlab-org/cli/test" ) func Test_ScheduleRun(t *testing.T) { @@ -90,19 +89,7 @@ func Test_ScheduleRunNoID(t *testing.T) { assert.Error(t, NewCmdRun(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, err := io.Copy(&buf, r) - require.NoError(t, err) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stderr = old // restoring the real Stderr - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Error: accepts 1 arg(s), received 0\nUsage:\n run [flags]\n\nExamples:\nglab schedule run 1\n\n\nFlags:\n -h, --help help for run\n\n") } diff --git a/commands/snippet/snippet_test.go b/commands/snippet/snippet_test.go index 0388ebf1..2b6d58fe 100644 --- a/commands/snippet/snippet_test.go +++ b/commands/snippet/snippet_test.go @@ -1,13 +1,12 @@ package snippet import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestCmdSnippet_noARgs(t *testing.T) { @@ -17,18 +16,7 @@ func TestCmdSnippet_noARgs(t *testing.T) { assert.Nil(t, NewCmdSnippet(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"snippet [command] --help\" for more information about a command.\n") } diff --git a/commands/user/user_test.go b/commands/user/user_test.go index 94dedb1a..afe4e1bd 100644 --- a/commands/user/user_test.go +++ b/commands/user/user_test.go @@ -1,13 +1,12 @@ package user import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestIssueCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) { assert.Nil(t, NewCmdUser(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Interact with user\n") } diff --git a/commands/variable/variable_test.go b/commands/variable/variable_test.go index 2d18548d..06dcfe80 100644 --- a/commands/variable/variable_test.go +++ b/commands/variable/variable_test.go @@ -1,13 +1,12 @@ package variable import ( - "bytes" - "io" "os" "testing" "github.com/stretchr/testify/assert" "gitlab.com/gitlab-org/cli/commands/cmdutils" + "gitlab.com/gitlab-org/cli/test" ) func TestNewVariableCmd(t *testing.T) { @@ -17,18 +16,7 @@ func TestNewVariableCmd(t *testing.T) { assert.Nil(t, NewVariableCmd(&cmdutils.Factory{}).Execute()) - outC := make(chan string) - // copy the output in a separate goroutine so printing can't block indefinitely - go func() { - var buf bytes.Buffer - _, _ = io.Copy(&buf, r) - outC <- buf.String() - }() - - // back to normal state - w.Close() - os.Stdout = old // restoring the real stdout - out := <-outC + out := test.ReturnBuffer(old, r, w) assert.Contains(t, out, "Use \"variable [command] --help\" for more information about a command.\n") } diff --git a/test/helpers.go b/test/helpers.go index 6ccf172b..826be6ec 100644 --- a/test/helpers.go +++ b/test/helpers.go @@ -3,6 +3,7 @@ package test import ( "bytes" "fmt" + "io" "os" "os/exec" "regexp" @@ -117,3 +118,20 @@ func GetHostOrSkip(t testing.TB) string { } return glTestHost } + +func ReturnBuffer(old *os.File, r *os.File, w *os.File) string { + outC := make(chan string) + // copy the output in a separate goroutine so printing can't block indefinitely + go func() { + var buf bytes.Buffer + _, _ = io.Copy(&buf, r) + outC <- buf.String() + }() + + // back to normal state + w.Close() + os.Stdout = old // restoring the real stdout + out := <-outC + + return out +}