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 package alias
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func Test_Alias(t *testing.T) { func Test_Alias(t *testing.T) {
@ -17,18 +16,7 @@ func Test_Alias(t *testing.T) {
assert.Nil(t, NewCmdAlias(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdAlias(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"alias [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"alias [command] --help\" for more information about a command.\n")
} }

View File

@ -1,13 +1,12 @@
package auth package auth
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestIssueCmd(t *testing.T) { func TestIssueCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) {
assert.Nil(t, NewCmdAuth(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdAuth(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Manage glab's authentication state\n") assert.Contains(t, out, "Manage glab's authentication state\n")
} }

View File

@ -1,13 +1,12 @@
package ci package ci
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestPipelineCmd(t *testing.T) { func TestPipelineCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestPipelineCmd(t *testing.T) {
assert.Nil(t, NewCmdCI(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdCI(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"ci [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"ci [command] --help\" for more information about a command.\n")
} }

View File

@ -1,13 +1,12 @@
package cluster package cluster
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestNewCmdAgent(t *testing.T) { func TestNewCmdAgent(t *testing.T) {
@ -17,18 +16,7 @@ func TestNewCmdAgent(t *testing.T) {
assert.Nil(t, NewCmdAgent(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdAgent(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Manage GitLab Agents for Kubernetes") assert.Contains(t, out, "Manage GitLab Agents for Kubernetes")
} }

View File

@ -1,13 +1,12 @@
package cluster package cluster
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestNewCmdCluster(t *testing.T) { func TestNewCmdCluster(t *testing.T) {
@ -17,18 +16,7 @@ func TestNewCmdCluster(t *testing.T) {
assert.Nil(t, NewCmdCluster(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdCluster(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Manage GitLab Agents for Kubernetes and their clusters") assert.Contains(t, out, "Manage GitLab Agents for Kubernetes and their clusters")
} }

View File

@ -1,8 +1,6 @@
package help package help
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
@ -13,6 +11,7 @@ import (
"gitlab.com/gitlab-org/cli/commands/alias" "gitlab.com/gitlab-org/cli/commands/alias"
"gitlab.com/gitlab-org/cli/commands/alias/set" "gitlab.com/gitlab-org/cli/commands/alias/set"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestDedent(t *testing.T) { func TestDedent(t *testing.T) {
@ -98,18 +97,9 @@ USAGE
alias.NewCmdAlias(&cmdutils.Factory{}).AddCommand(cmd) alias.NewCmdAlias(&cmdutils.Factory{}).AddCommand(cmd)
} }
RootHelpFunc(streams.Color(), cmd, tt.args.args) 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 out := test.ReturnBuffer(old, r, w)
w.Close()
os.Stdout = old // restoring the real stdout
out := <-outC
assert.Contains(t, out, tt.wantOut) assert.Contains(t, out, tt.wantOut)
}) })
} }

View File

@ -1,13 +1,12 @@
package incident package incident
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestIncidentCmd(t *testing.T) { func TestIncidentCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestIncidentCmd(t *testing.T) {
assert.Nil(t, NewCmdIncident(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdIncident(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Work with GitLab incidents\n") assert.Contains(t, out, "Work with GitLab incidents\n")
} }

View File

@ -1,13 +1,12 @@
package board package board
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestNewCmdBoard(t *testing.T) { func TestNewCmdBoard(t *testing.T) {
@ -17,18 +16,7 @@ func TestNewCmdBoard(t *testing.T) {
assert.Nil(t, NewCmdBoard(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdBoard(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Work with GitLab Issue Boards in the given project.\n") assert.Contains(t, out, "Work with GitLab Issue Boards in the given project.\n")
} }

View File

@ -1,13 +1,12 @@
package issue package issue
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestIssueCmd(t *testing.T) { func TestIssueCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) {
assert.Nil(t, NewCmdIssue(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdIssue(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Work with GitLab issues\n") assert.Contains(t, out, "Work with GitLab issues\n")
} }

View File

@ -1,13 +1,12 @@
package label package label
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestNewCmdLabel(t *testing.T) { func TestNewCmdLabel(t *testing.T) {
@ -17,18 +16,7 @@ func TestNewCmdLabel(t *testing.T) {
assert.Nil(t, NewCmdLabel(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdLabel(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"label [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"label [command] --help\" for more information about a command.\n")
} }

View File

@ -1,14 +1,13 @@
package mr package mr
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdtest" "gitlab.com/gitlab-org/cli/commands/cmdtest"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
@ -23,18 +22,7 @@ func TestMrCmd_noARgs(t *testing.T) {
assert.Nil(t, NewCmdMR(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdMR(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"mr [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"mr [command] --help\" for more information about a command.\n")
} }

View File

@ -1,13 +1,12 @@
package project package project
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func Test_Repo(t *testing.T) { func Test_Repo(t *testing.T) {
@ -17,18 +16,7 @@ func Test_Repo(t *testing.T) {
assert.Nil(t, NewCmdRepo(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdRepo(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"repo [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"repo [command] --help\" for more information about a command.\n")
} }

View File

@ -1,13 +1,12 @@
package release package release
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func Test_Release(t *testing.T) { func Test_Release(t *testing.T) {
@ -19,18 +18,7 @@ func Test_Release(t *testing.T) {
assert.NotNil(t, cmd.Root()) assert.NotNil(t, cmd.Root())
assert.Nil(t, cmd.Execute()) assert.Nil(t, cmd.Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Manage GitLab releases") assert.Contains(t, out, "Manage GitLab releases")
} }

View File

@ -1,14 +1,13 @@
package commands package commands
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdtest" "gitlab.com/gitlab-org/cli/commands/cmdtest"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestMain(m *testing.M) { 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.Flag("version").Value.Set("true"))
assert.Nil(t, rootCmd.Execute()) assert.Nil(t, rootCmd.Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Equal(t, "glab version 1.0.0 (2020-01-01)\n", out) 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") rootCmd := NewCmdRoot(cmdutils.NewFactory(), "v1.0.0", "2020-01-01")
assert.Nil(t, rootCmd.Execute()) assert.Nil(t, rootCmd.Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "GLab is an open source GitLab CLI tool that brings GitLab to your command line.\n") 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 assert.Contains(t, out, `USAGE
glab <command> <subcommand> [flags] glab <command> <subcommand> [flags]

View File

@ -1,8 +1,6 @@
package run package run
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
@ -16,6 +14,7 @@ import (
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/internal/config" "gitlab.com/gitlab-org/cli/internal/config"
"gitlab.com/gitlab-org/cli/pkg/iostreams" "gitlab.com/gitlab-org/cli/pkg/iostreams"
"gitlab.com/gitlab-org/cli/test"
) )
func Test_ScheduleRun(t *testing.T) { func Test_ScheduleRun(t *testing.T) {
@ -90,19 +89,7 @@ func Test_ScheduleRunNoID(t *testing.T) {
assert.Error(t, NewCmdRun(&cmdutils.Factory{}).Execute()) assert.Error(t, NewCmdRun(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
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") 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 package snippet
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestCmdSnippet_noARgs(t *testing.T) { func TestCmdSnippet_noARgs(t *testing.T) {
@ -17,18 +16,7 @@ func TestCmdSnippet_noARgs(t *testing.T) {
assert.Nil(t, NewCmdSnippet(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdSnippet(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"snippet [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"snippet [command] --help\" for more information about a command.\n")
} }

View File

@ -1,13 +1,12 @@
package user package user
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestIssueCmd(t *testing.T) { func TestIssueCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestIssueCmd(t *testing.T) {
assert.Nil(t, NewCmdUser(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewCmdUser(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Interact with user\n") assert.Contains(t, out, "Interact with user\n")
} }

View File

@ -1,13 +1,12 @@
package variable package variable
import ( import (
"bytes"
"io"
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gitlab.com/gitlab-org/cli/commands/cmdutils" "gitlab.com/gitlab-org/cli/commands/cmdutils"
"gitlab.com/gitlab-org/cli/test"
) )
func TestNewVariableCmd(t *testing.T) { func TestNewVariableCmd(t *testing.T) {
@ -17,18 +16,7 @@ func TestNewVariableCmd(t *testing.T) {
assert.Nil(t, NewVariableCmd(&cmdutils.Factory{}).Execute()) assert.Nil(t, NewVariableCmd(&cmdutils.Factory{}).Execute())
outC := make(chan string) out := test.ReturnBuffer(old, r, w)
// 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
assert.Contains(t, out, "Use \"variable [command] --help\" for more information about a command.\n") assert.Contains(t, out, "Use \"variable [command] --help\" for more information about a command.\n")
} }

View File

@ -3,6 +3,7 @@ package test
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
@ -117,3 +118,20 @@ func GetHostOrSkip(t testing.TB) string {
} }
return glTestHost 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
}