Merge pull request #13 from yxxhero/mv_captureStdout_as_public_func

mv captureStdout as public func
This commit is contained in:
Yusuke Kuoka 2022-04-04 10:06:03 +09:00 committed by GitHub
commit 4a52ce0ac2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 37 deletions

View File

@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -18,6 +17,7 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/roboll/helmfile/pkg/remote" "github.com/roboll/helmfile/pkg/remote"
"github.com/roboll/helmfile/pkg/testutil"
"gotest.tools/v3/assert" "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) { func TestPrint_SingleStateFile(t *testing.T) {
files := map[string]string{ files := map[string]string{
"/path/to/helmfile.yaml": ` "/path/to/helmfile.yaml": `
@ -4726,7 +4696,7 @@ releases:
expectNoCallsToHelm(app) expectNoCallsToHelm(app)
out := captureStdout(func() { out := testutil.CaptureStdout(func() {
err := app.PrintState(configImpl{}) err := app.PrintState(configImpl{})
assert.NilError(t, err) assert.NilError(t, err)
}) })
@ -4773,7 +4743,7 @@ releases:
expectNoCallsToHelm(app) expectNoCallsToHelm(app)
out := captureStdout(func() { out := testutil.CaptureStdout(func() {
err := app.PrintState(configImpl{}) err := app.PrintState(configImpl{})
assert.NilError(t, err) assert.NilError(t, err)
}) })
@ -4834,7 +4804,7 @@ releases:
expectNoCallsToHelm(app) expectNoCallsToHelm(app)
out := captureStdout(func() { out := testutil.CaptureStdout(func() {
err := app.ListReleases(configImpl{}) err := app.ListReleases(configImpl{})
assert.NilError(t, err) assert.NilError(t, err)
}) })
@ -4896,7 +4866,7 @@ releases:
expectNoCallsToHelm(app) expectNoCallsToHelm(app)
out := captureStdout(func() { out := testutil.CaptureStdout(func() {
err := app.ListReleases(configImpl{ err := app.ListReleases(configImpl{
output: "json", output: "json",
}) })

View File

@ -3,6 +3,8 @@ package app
import ( import (
"os" "os"
"testing" "testing"
"github.com/roboll/helmfile/pkg/testutil"
) )
// TestFormatAsTable tests the FormatAsTable function. // TestFormatAsTable tests the FormatAsTable function.
@ -35,7 +37,7 @@ func TestFormatAsTable(t *testing.T) {
t.Errorf("error reading %s: %v", tableoutput, err) t.Errorf("error reading %s: %v", tableoutput, err)
} }
result := captureStdout(func() { result := testutil.CaptureStdout(func() {
FormatAsTable(h) FormatAsTable(h)
}) })
if result != string(expectd) { if result != string(expectd) {
@ -69,7 +71,7 @@ func TestFormatAsJson(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error reading %s: %v", output, err) t.Errorf("error reading %s: %v", output, err)
} }
result := captureStdout(func() { result := testutil.CaptureStdout(func() {
FormatAsJson(h) FormatAsJson(h)
}) })

40
pkg/testutil/testutil.go Normal file
View File

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

View File

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