From e0244bc341bfb1a23f8b7418db76484db46cbdf7 Mon Sep 17 00:00:00 2001 From: yxxhero Date: Sun, 3 Apr 2022 22:24:38 +0800 Subject: [PATCH] mv captureStdout as public func Signed-off-by: yxxhero --- pkg/app/app_test.go | 40 +++++------------------------------ pkg/app/formatters_test.go | 6 ++++-- pkg/testutil/testutil.go | 40 +++++++++++++++++++++++++++++++++++ pkg/testutil/testutil_test.go | 40 +++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 37 deletions(-) create mode 100644 pkg/testutil/testutil.go create mode 100644 pkg/testutil/testutil_test.go diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 0c2713fd..86d4f3e9 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "log" "os" "path/filepath" "reflect" @@ -18,6 +17,7 @@ import ( "github.com/google/go-cmp/cmp" "github.com/roboll/helmfile/pkg/remote" + "github.com/roboll/helmfile/pkg/testutil" "gotest.tools/v3/assert" @@ -4668,36 +4668,6 @@ See https://github.com/roboll/helmfile/issues/878 for more information. } } -func captureStdout(f func()) string { - reader, writer, err := os.Pipe() - if err != nil { - panic(err) - } - stdout := os.Stdout - defer func() { - os.Stdout = stdout - log.SetOutput(os.Stderr) - }() - os.Stdout = writer - log.SetOutput(writer) - out := make(chan string) - wg := new(sync.WaitGroup) - wg.Add(1) - go func() { - var buf bytes.Buffer - wg.Done() - _, err = io.Copy(&buf, reader) - if err != nil { - panic(err) - } - out <- buf.String() - }() - wg.Wait() - f() - writer.Close() - return <-out -} - func TestPrint_SingleStateFile(t *testing.T) { files := map[string]string{ "/path/to/helmfile.yaml": ` @@ -4726,7 +4696,7 @@ releases: expectNoCallsToHelm(app) - out := captureStdout(func() { + out := testutil.CaptureStdout(func() { err := app.PrintState(configImpl{}) assert.NilError(t, err) }) @@ -4773,7 +4743,7 @@ releases: expectNoCallsToHelm(app) - out := captureStdout(func() { + out := testutil.CaptureStdout(func() { err := app.PrintState(configImpl{}) assert.NilError(t, err) }) @@ -4834,7 +4804,7 @@ releases: expectNoCallsToHelm(app) - out := captureStdout(func() { + out := testutil.CaptureStdout(func() { err := app.ListReleases(configImpl{}) assert.NilError(t, err) }) @@ -4896,7 +4866,7 @@ releases: expectNoCallsToHelm(app) - out := captureStdout(func() { + out := testutil.CaptureStdout(func() { err := app.ListReleases(configImpl{ output: "json", }) diff --git a/pkg/app/formatters_test.go b/pkg/app/formatters_test.go index 77bdf2b3..e21e5e20 100644 --- a/pkg/app/formatters_test.go +++ b/pkg/app/formatters_test.go @@ -3,6 +3,8 @@ package app import ( "os" "testing" + + "github.com/roboll/helmfile/pkg/testutil" ) // TestFormatAsTable tests the FormatAsTable function. @@ -35,7 +37,7 @@ func TestFormatAsTable(t *testing.T) { t.Errorf("error reading %s: %v", tableoutput, err) } - result := captureStdout(func() { + result := testutil.CaptureStdout(func() { FormatAsTable(h) }) if result != string(expectd) { @@ -69,7 +71,7 @@ func TestFormatAsJson(t *testing.T) { if err != nil { t.Errorf("error reading %s: %v", output, err) } - result := captureStdout(func() { + result := testutil.CaptureStdout(func() { FormatAsJson(h) }) diff --git a/pkg/testutil/testutil.go b/pkg/testutil/testutil.go new file mode 100644 index 00000000..35ad39e8 --- /dev/null +++ b/pkg/testutil/testutil.go @@ -0,0 +1,40 @@ +package testutil + +import ( + "bytes" + "io" + "log" + "os" + "sync" +) + +// CaptureStdout is a helper function to capture stdout. +func CaptureStdout(f func()) string { + reader, writer, err := os.Pipe() + if err != nil { + panic(err) + } + stdout := os.Stdout + defer func() { + os.Stdout = stdout + log.SetOutput(os.Stderr) + }() + os.Stdout = writer + log.SetOutput(writer) + out := make(chan string) + wg := new(sync.WaitGroup) + wg.Add(1) + go func() { + var buf bytes.Buffer + wg.Done() + _, err = io.Copy(&buf, reader) + if err != nil { + panic(err) + } + out <- buf.String() + }() + wg.Wait() + f() + writer.Close() + return <-out +} diff --git a/pkg/testutil/testutil_test.go b/pkg/testutil/testutil_test.go new file mode 100644 index 00000000..06460756 --- /dev/null +++ b/pkg/testutil/testutil_test.go @@ -0,0 +1,40 @@ +package testutil + +import ( + "fmt" + "testing" +) + +// TestCaptureStdout tests the CaptureStdout function. +func TestCaptureStdout(t *testing.T) { + tests := []struct { + output string + expected string + }{ + { + output: "123", + expected: "123", + }, + { + output: "test", + expected: "test", + }, + { + output: "", + expected: "", + }, + { + output: "...", + expected: "...", + }, + } + + for _, test := range tests { + result := CaptureStdout(func() { + fmt.Print(test.output) + }) + if result != test.expected { + t.Errorf("CaptureStdout() = %v, want %v", result, test.expected) + } + } +}