Skip to content

Commit

Permalink
Add test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed Aug 28, 2022
1 parent f5b4dae commit f38e79f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/stdout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tests

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestStdout(t *testing.T, fn func()) string {
t.Helper()
old := os.Stdout
t.Cleanup(func() {
os.Stdout = old
})
r, w, err := os.Pipe()
require.NoError(t, err)

os.Stdout = w
fn()
w.Close()

var buffer bytes.Buffer
_, err = buffer.ReadFrom(r)
require.NoError(t, err)

s := buffer.String()
return s[:len(s)-1]
}

0 comments on commit f38e79f

Please sign in to comment.