diff options
author | Valery Piashchynski <[email protected]> | 2022-01-15 15:52:23 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2022-01-15 15:52:23 +0300 |
commit | 65541b95a6ebc72aef818f1d2e4b0dc721f59e22 (patch) | |
tree | d9dbe5577c72810fadf1f2bc1e5fcee889fda983 /cmd | |
parent | 5254c8eb27311e2a8a53a4c90c3829cf1238c563 (diff) |
initial commit
Signed-off-by: Valery Piashchynski <[email protected]>
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/rr/command_test.go | 67 | ||||
-rw-r--r-- | cmd/rr/main.go | 28 |
2 files changed, 95 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") +} diff --git a/cmd/rr/main.go b/cmd/rr/main.go new file mode 100644 index 00000000..054b192e --- /dev/null +++ b/cmd/rr/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "path/filepath" + + "github.com/fatih/color" + "github.com/roadrunner-server/roadrunner/v2/internal/cli" +) + +// exitFn is a function for application exiting. +var exitFn = os.Exit //nolint:gochecknoglobals + +// main CLI application entrypoint. +func main() { exitFn(run()) } + +// run this CLI application. +func run() int { + cmd := cli.NewCommand(filepath.Base(os.Args[0])) + + if err := cmd.Execute(); err != nil { + _, _ = color.New(color.FgHiRed, color.Bold).Fprintln(os.Stderr, err.Error()) + + return 1 + } + + return 0 +} |