mv captureStdout as public func
Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
parent
01b5b79a01
commit
e0244bc341
|
|
@ -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",
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue