From e2069934b36afe2db654792984fe6dec1877192f Mon Sep 17 00:00:00 2001 From: gedi Date: Sun, 30 Oct 2016 22:34:39 +0200 Subject: [PATCH] allow run options to configure output writer --- examples/api/version.feature | 2 +- fmt_junit.go | 2 +- godog.go | 2 +- options.go | 31 +++++++++++++++++++++++++------ run.go | 20 ++++++++++++++++++-- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/examples/api/version.feature b/examples/api/version.feature index 30101e7..0ca7db7 100644 --- a/examples/api/version.feature +++ b/examples/api/version.feature @@ -20,6 +20,6 @@ Feature: get version And the response should match json: """ { - "version": "v0.5.4" + "version": "v0.6.0" } """ diff --git a/fmt_junit.go b/fmt_junit.go index 08a10b4..4395994 100644 --- a/fmt_junit.go +++ b/fmt_junit.go @@ -137,7 +137,7 @@ func (j *junitFormatter) Summary() { j.suite.Time = time.Since(j.started).String() io.WriteString(j.out, xml.Header) - enc := xml.NewEncoder(os.Stdout) + enc := xml.NewEncoder(j.out) enc.Indent("", s(2)) if err := enc.Encode(j.suite); err != nil { fmt.Fprintln(os.Stderr, "failed to write junit xml:", err) diff --git a/godog.go b/godog.go index 5da8d34..a92b386 100644 --- a/godog.go +++ b/godog.go @@ -42,4 +42,4 @@ Godog was inspired by Behat and Cucumber the above description is taken from it' package godog // Version of package - based on Semantic Versioning 2.0.0 http://semver.org/ -const Version = "v0.5.4" +const Version = "v0.6.0" diff --git a/options.go b/options.go index ab18b6c..29a6070 100644 --- a/options.go +++ b/options.go @@ -1,5 +1,7 @@ package godog +import "io" + // Options are suite run options // flags are mapped to these options. // @@ -8,11 +10,28 @@ package godog // // See the flags for more details type Options struct { + // Print step definitions found and exit ShowStepDefinitions bool - StopOnFailure bool - NoColors bool - Tags string - Format string - Concurrency int - Paths []string + + // Stops on the first failure + StopOnFailure bool + + // Forces ansi color stripping + NoColors bool + + // Various filters for scenarios parsed + // from feature files + Tags string + + // The formatter name + Format string + + // Concurrency rate, not all formatters accepts this + Concurrency int + + // All feature file paths + Paths []string + + // Where it should print formatter output + Output io.Writer } diff --git a/run.go b/run.go index 2c900e8..70dde36 100644 --- a/run.go +++ b/run.go @@ -2,7 +2,10 @@ package godog import ( "fmt" + "io" "os" + + "github.com/DATA-DOG/godog/colors" ) type initializer func(*Suite) @@ -69,6 +72,17 @@ func (r *runner) run() (failed bool) { // godog in for example TestMain function together // with go tests func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Options) int { + var output io.Writer = os.Stdout + if nil != opt.Output { + output = opt.Output + } + + if opt.NoColors { + output = colors.Uncolored(output) + } else { + output = colors.Colored(output) + } + if opt.ShowStepDefinitions { s := &Suite{} contextInitializer(s) @@ -93,7 +107,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt fatal(err) r := runner{ - fmt: formatter(suite, os.Stdout), + fmt: formatter(suite, output), initializer: contextInitializer, features: features, stopOnFailure: opt.StopOnFailure, @@ -105,7 +119,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt } else { failed = r.run() } - if failed && format != "events" { + if failed && opt.Format != "events" { return 1 } return 0 @@ -126,6 +140,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt func Run(suite string, contextInitializer func(suite *Suite)) int { var opt Options flagSet := FlagSet( + colors.Colored(os.Stdout), &opt.Format, &opt.Tags, &opt.ShowStepDefinitions, @@ -135,6 +150,7 @@ func Run(suite string, contextInitializer func(suite *Suite)) int { ) err := flagSet.Parse(os.Args[1:]) fatal(err) + opt.Paths = flagSet.Args() return RunWithOptions(suite, contextInitializer, opt)