diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 000000000..3dec5908b --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,58 @@ +package cmd_test + +import ( + "fmt" + "io" + "io/ioutil" + "os" + "strings" + "testing" + + "time" + + "github.com/pilosa/pilosa/cmd" + "github.com/spf13/cobra" +) + +func tExec(t *testing.T, cmd *cobra.Command, out io.Reader, w io.WriteCloser) (output []byte) { + done := make(chan struct{}) + go func() { + var err error + output, err = ioutil.ReadAll(out) + if err != nil { + t.Fatal(err) + } + close(done) + }() + fmt.Println("executing") + err := cmd.Execute() + if err != nil { + t.Fatal(err) + } + if err := w.Close(); err != nil { + t.Fatalf("closing cmd's stdout: %v", err) + } + select { + case <-done: + case <-time.After(time.Second * 1): + t.Fatal("Test failed due to command execution timeout") + } + return output +} + +func ExecNewRootCommand(t *testing.T, args ...string) string { + out, w := io.Pipe() + rc := cmd.NewRootCommand(os.Stdin, w, w) + rc.SetArgs(args) + output := tExec(t, rc, out, w) + return string(output) +} + +func TestRootCommand(t *testing.T) { + outStr := ExecNewRootCommand(t, "--help") + if !strings.Contains(outStr, "Usage:") || + !strings.Contains(outStr, "Available Commands:") || + !strings.Contains(outStr, "--help") { + t.Fatalf("Expected standard usage message from RootCommand, but got: %s", outStr) + } +} diff --git a/cmd/server_test.go b/cmd/server_test.go new file mode 100644 index 000000000..c2a9edea0 --- /dev/null +++ b/cmd/server_test.go @@ -0,0 +1,14 @@ +package cmd_test + +import ( + "strings" + "testing" +) + +func TestServerHelp(t *testing.T) { + output := ExecNewRootCommand(t, "server", "--help") + if !strings.Contains(output, "Usage:") || + !strings.Contains(output, "Flags:") { + t.Fatalf("Command 'server --help' not working, got: %s", output) + } +}