summaryrefslogtreecommitdiff
path: root/cmd/rr/command_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/rr/command_test.go')
-rw-r--r--cmd/rr/command_test.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/cmd/rr/command_test.go b/cmd/rr/command_test.go
new file mode 100644
index 00000000..0e111f4b
--- /dev/null
+++ b/cmd/rr/command_test.go
@@ -0,0 +1,67 @@
+package main
+
+import (
+ "bytes"
+ "io"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func Test_Main(t *testing.T) {
+ os.Args = []string{"", "--help"}
+ exitFn = func(code int) { assert.Equal(t, 0, code) }
+
+ r, w, err := os.Pipe()
+ require.NoError(t, err)
+ os.Stdout = w
+
+ main()
+ _ = w.Close()
+ buf := new(bytes.Buffer)
+ _, err = io.Copy(buf, r)
+ require.NoError(t, err)
+
+ assert.Contains(t, buf.String(), "Usage:")
+ assert.Contains(t, buf.String(), "Available Commands:")
+ assert.Contains(t, buf.String(), "Flags:")
+}
+
+func Test_MainWithoutCommands(t *testing.T) {
+ os.Args = []string{""}
+ exitFn = func(code int) { assert.Equal(t, 0, code) }
+
+ r, w, err := os.Pipe()
+ require.NoError(t, err)
+ os.Stdout = w
+
+ main()
+ _ = w.Close()
+ buf := new(bytes.Buffer)
+ _, err = io.Copy(buf, r)
+ require.NoError(t, err)
+
+ assert.Contains(t, buf.String(), "Usage:")
+ assert.Contains(t, buf.String(), "Available Commands:")
+ assert.Contains(t, buf.String(), "Flags:")
+}
+
+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)
+ os.Stderr = w
+
+ main()
+ _ = w.Close()
+ buf := new(bytes.Buffer)
+ _, err = io.Copy(buf, r)
+ require.NoError(t, err)
+
+ assert.Contains(t, buf.String(), "unknown command")
+ assert.Contains(t, buf.String(), "foobar")
+}