diff --git a/.gitignore b/.gitignore index 739a43dda..6d045049d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ vendor/ # This is where we test stuff /experimenting/ + +/.history +/.vscode diff --git a/utils_test.go b/utils_test.go index 36b7e3dc2..d7c2b870f 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,19 +1,25 @@ package pterm_test import ( + "bytes" "fmt" "io" - "io/ioutil" "os" "testing" "github.com/MarvinJWendt/testza" - "github.com/gookit/color" "github.com/pterm/pterm" ) var printables = []interface{}{"Hello, World!", 1337, true, false, -1337, 'c', 1.5, "\\", "%s"} +func TestMain(m *testing.M) { + setupStdoutCapture() + exitVal := m.Run() + teardownStdoutCapture() + os.Exit(exitVal) +} + // testPrintContains can be used to test Print methods. func testPrintContains(t *testing.T, logic func(w io.Writer, a interface{})) { for _, printable := range printables { @@ -182,22 +188,24 @@ func testDoesNotOutput(t *testing.T, logic func(w io.Writer)) { pterm.EnableStyling() } -// captureStdout captures everything written to the terminal and returns it as a string. -func captureStdout(f func(w io.Writer)) string { - originalStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - color.SetOutput(w) +var outBuf bytes.Buffer - f(w) +// setupStdoutCapture sets up a fake stdout capture. +func setupStdoutCapture() { + outBuf.Reset() + pterm.SetDefaultOutput(&outBuf) +} - _ = w.Close() - out, _ := ioutil.ReadAll(r) - os.Stdout = originalStdout - color.SetOutput(w) - _ = r.Close() +// teardownStdoutCapture restores the real stdout. +func teardownStdoutCapture() { + pterm.SetDefaultOutput(os.Stdout) +} - return string(out) +// captureStdout simulates capturing of os.stdout with a buffer and returns what was writted to the screen +func captureStdout(f func(w io.Writer)) string { + setupStdoutCapture() + f(&outBuf) + return outBuf.String() } func proxyToDevNull() {