add some proof of concept command tests

This commit is contained in:
Matt Jaffee 2017-03-13 18:28:12 -05:00
parent 961d18a3bf
commit 076cc0960a
2 changed files with 72 additions and 0 deletions

58
cmd/root_test.go Normal file
View file

@ -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)
}
}

14
cmd/server_test.go Normal file
View file

@ -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)
}
}