refactor: Refactor code that returns buffer contents in tests

This commit is contained in:
Gary Holtz 2024-03-20 10:08:37 -05:00
parent 4983225503
commit 5263e4b2a5
No known key found for this signature in database
GPG Key ID: 0C3B92677CFEE92F
19 changed files with 56 additions and 263 deletions

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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)
})
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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 <command> <subcommand> [flags]

View File

@ -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 <id> [flags]\n\nExamples:\nglab schedule run 1\n\n\nFlags:\n -h, --help help for run\n\n")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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")
}

View File

@ -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
}