Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: fixed flaky tests on linux/macos #290

Merged
merged 2 commits into from Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -16,3 +16,6 @@ vendor/

# This is where we test stuff
/experimenting/

/.history
/.vscode
38 changes: 23 additions & 15 deletions 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 {
Expand Down Expand Up @@ -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() {
Expand Down