allow run options to configure output writer

Этот коммит содержится в:
gedi 2016-10-30 22:34:39 +02:00
родитель 115923c97f
коммит e2069934b3
5 изменённых файлов: 46 добавлений и 11 удалений

Просмотреть файл

@ -20,6 +20,6 @@ Feature: get version
And the response should match json: And the response should match json:
""" """
{ {
"version": "v0.5.4" "version": "v0.6.0"
} }
""" """

Просмотреть файл

@ -137,7 +137,7 @@ func (j *junitFormatter) Summary() {
j.suite.Time = time.Since(j.started).String() j.suite.Time = time.Since(j.started).String()
io.WriteString(j.out, xml.Header) io.WriteString(j.out, xml.Header)
enc := xml.NewEncoder(os.Stdout) enc := xml.NewEncoder(j.out)
enc.Indent("", s(2)) enc.Indent("", s(2))
if err := enc.Encode(j.suite); err != nil { if err := enc.Encode(j.suite); err != nil {
fmt.Fprintln(os.Stderr, "failed to write junit xml:", err) fmt.Fprintln(os.Stderr, "failed to write junit xml:", err)

Просмотреть файл

@ -42,4 +42,4 @@ Godog was inspired by Behat and Cucumber the above description is taken from it'
package godog package godog
// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/ // Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
const Version = "v0.5.4" const Version = "v0.6.0"

Просмотреть файл

@ -1,5 +1,7 @@
package godog package godog
import "io"
// Options are suite run options // Options are suite run options
// flags are mapped to these options. // flags are mapped to these options.
// //
@ -8,11 +10,28 @@ package godog
// //
// See the flags for more details // See the flags for more details
type Options struct { type Options struct {
// Print step definitions found and exit
ShowStepDefinitions bool ShowStepDefinitions bool
StopOnFailure bool
NoColors bool // Stops on the first failure
Tags string StopOnFailure bool
Format string
Concurrency int // Forces ansi color stripping
Paths []string 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
} }

20
run.go
Просмотреть файл

@ -2,7 +2,10 @@ package godog
import ( import (
"fmt" "fmt"
"io"
"os" "os"
"github.com/DATA-DOG/godog/colors"
) )
type initializer func(*Suite) type initializer func(*Suite)
@ -69,6 +72,17 @@ func (r *runner) run() (failed bool) {
// godog in for example TestMain function together // godog in for example TestMain function together
// with go tests // with go tests
func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Options) int { 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 { if opt.ShowStepDefinitions {
s := &Suite{} s := &Suite{}
contextInitializer(s) contextInitializer(s)
@ -93,7 +107,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt
fatal(err) fatal(err)
r := runner{ r := runner{
fmt: formatter(suite, os.Stdout), fmt: formatter(suite, output),
initializer: contextInitializer, initializer: contextInitializer,
features: features, features: features,
stopOnFailure: opt.StopOnFailure, stopOnFailure: opt.StopOnFailure,
@ -105,7 +119,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt
} else { } else {
failed = r.run() failed = r.run()
} }
if failed && format != "events" { if failed && opt.Format != "events" {
return 1 return 1
} }
return 0 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 { func Run(suite string, contextInitializer func(suite *Suite)) int {
var opt Options var opt Options
flagSet := FlagSet( flagSet := FlagSet(
colors.Colored(os.Stdout),
&opt.Format, &opt.Format,
&opt.Tags, &opt.Tags,
&opt.ShowStepDefinitions, &opt.ShowStepDefinitions,
@ -135,6 +150,7 @@ func Run(suite string, contextInitializer func(suite *Suite)) int {
) )
err := flagSet.Parse(os.Args[1:]) err := flagSet.Parse(os.Args[1:])
fatal(err) fatal(err)
opt.Paths = flagSet.Args() opt.Paths = flagSet.Args()
return RunWithOptions(suite, contextInitializer, opt) return RunWithOptions(suite, contextInitializer, opt)