Skip to content

Commit

Permalink
fix tests stdout
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
  • Loading branch information
rustatian committed Feb 16, 2023
1 parent 6745e9e commit a5bb90d
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions cmd/rr/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ import (
"io"
"os"
"testing"
"time"

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

func Test_Main(t *testing.T) {
os.Args = []string{"rr", "--help"}
exitFn = func(code int) { assert.Equal(t, 0, code) }

r, w, err := os.Pipe()
require.NoError(t, err)
r, w, _ := os.Pipe()
os.Stdout = w

main()
_ = w.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
require.NoError(t, err)

_ = r.SetReadDeadline(time.Now().Add(time.Second))
_, _ = io.Copy(buf, r)

assert.Contains(t, buf.String(), "Usage:")
assert.Contains(t, buf.String(), "Available Commands:")
Expand All @@ -33,15 +33,13 @@ func Test_MainWithoutCommands(t *testing.T) {
os.Args = []string{"rr"}
exitFn = func(code int) { assert.Equal(t, 0, code) }

r, w, err := os.Pipe()
require.NoError(t, err)
r, w, _ := os.Pipe()
os.Stdout = w

main()
_ = w.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
require.NoError(t, err)
_ = r.SetReadDeadline(time.Now().Add(time.Second))
_, _ = io.Copy(buf, r)

assert.Contains(t, buf.String(), "Usage:")
assert.Contains(t, buf.String(), "Available Commands:")
Expand All @@ -52,15 +50,15 @@ func Test_MainUnknownSubcommand(t *testing.T) {
os.Args = []string{"", "foobar"}
exitFn = func(code int) { assert.Equal(t, 1, code) }

r, w, err := os.Pipe()
require.NoError(t, err)
r, w, _ := os.Pipe()
os.Stderr = w

main()
_ = w.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, r)
require.NoError(t, err)

_ = r.SetReadDeadline(time.Now().Add(time.Second))
_, _ = io.Copy(buf, r)

assert.Contains(t, buf.String(), "unknown command")
assert.Contains(t, buf.String(), "foobar")
Expand Down

0 comments on commit a5bb90d

Please sign in to comment.