diff --git a/builder_test.go b/builder_test.go index 1392e0f..a5bc8b6 100644 --- a/builder_test.go +++ b/builder_test.go @@ -2,6 +2,7 @@ package godog import ( "go/build" + "os" "path/filepath" "reflect" "testing" @@ -35,3 +36,33 @@ func TestVendorPaths(t *testing.T) { gopaths = filepath.SplitList(build.Default.GOPATH) } + +func TestBuildTestRunner(t *testing.T) { + bin := filepath.Join(os.TempDir(), "godog.test") + if err := Build(bin); err != nil { + t.Fatalf("failed to build godog test binary: %v", err) + } + os.Remove(bin) +} + +func TestBuildTestRunnerWithoutGoFiles(t *testing.T) { + bin := filepath.Join(os.TempDir(), "godog.test") + pwd, err := os.Getwd() + if err != nil { + t.Fatalf("failed to get working directory: %v", err) + } + + wd := filepath.Join(pwd, "features") + if err := os.Chdir(wd); err != nil { + t.Fatalf("failed to change working directory: %v", err) + } + + defer func() { + os.Chdir(pwd) // get back to current dir + }() + + if err := Build(bin); err != nil { + t.Fatalf("failed to build godog test binary: %v", err) + } + os.Remove(bin) +} diff --git a/flags.go b/flags.go index 3b15e9a..99e6025 100644 --- a/flags.go +++ b/flags.go @@ -132,7 +132,7 @@ func usage(set *flag.FlagSet, w io.Writer) func() { // --- GENERAL --- fmt.Fprintln(w, colors.Yellow("Usage:")) - fmt.Printf(s(2) + "godog [options] []\n\n") + fmt.Fprintf(w, s(2)+"godog [options] []\n\n") // description fmt.Fprintln(w, "Builds a test package and runs given feature files.") fmt.Fprintf(w, "Command should be run from the directory of tested package and contain buildable go source.\n\n") diff --git a/flags_test.go b/flags_test.go new file mode 100644 index 0000000..270645b --- /dev/null +++ b/flags_test.go @@ -0,0 +1,77 @@ +package godog + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "github.com/DATA-DOG/godog/colors" +) + +func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) { + var opt Options + flags := FlagSet(&opt) + if err := flags.Parse([]string{"--random"}); err != nil { + t.Fatalf("unable to parse flags: %v", err) + } + + if opt.Randomize <= 0 { + t.Fatal("should have generated random seed") + } +} + +func TestFlagsShouldRandomizeByGivenSeed(t *testing.T) { + var opt Options + flags := FlagSet(&opt) + if err := flags.Parse([]string{"--random=123"}); err != nil { + t.Fatalf("unable to parse flags: %v", err) + } + + if opt.Randomize != 123 { + t.Fatalf("expected random seed to be: 123, but it was: %d", opt.Randomize) + } +} + +func TestFlagsShouldParseFormat(t *testing.T) { + cases := map[string][]string{ + "pretty": {}, + "progress": {"-f", "progress"}, + "junit": {"-f=junit"}, + "custom": {"--format", "custom"}, + "cust": {"--format=cust"}, + } + + for format, args := range cases { + var opt Options + flags := FlagSet(&opt) + if err := flags.Parse(args); err != nil { + t.Fatalf("unable to parse flags: %v", err) + } + + if opt.Format != format { + t.Fatalf("expected format: %s, but it was: %s", format, opt.Format) + } + } +} + +func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) { + var buf bytes.Buffer + output := colors.Uncolored(&buf) + + // register some custom formatter + Format("custom", "custom format description", junitFunc) + + var opt Options + flags := FlagSet(&opt) + usage(flags, output)() + + out := buf.String() + + for name, desc := range AvailableFormatters() { + match := fmt.Sprintf("%s: %s\n", name, desc) + if idx := strings.Index(out, match); idx == -1 { + t.Fatalf("could not locate format: %s description in flag usage", name) + } + } +}