gets rid of fatal func, which exits application on unexpected error
Этот коммит содержится в:
родитель
a3338b5849
коммит
8e9c01d484
5 изменённых файлов: 54 добавлений и 30 удалений
13
fmt.go
13
fmt.go
|
@ -53,20 +53,13 @@ type registeredFormatter struct {
|
||||||
|
|
||||||
var formatters []*registeredFormatter
|
var formatters []*registeredFormatter
|
||||||
|
|
||||||
func findFmt(format string) (f FormatterFunc, err error) {
|
func findFmt(format string) FormatterFunc {
|
||||||
var names []string
|
|
||||||
for _, el := range formatters {
|
for _, el := range formatters {
|
||||||
if el.name == format {
|
if el.name == format {
|
||||||
f = el.fmt
|
return el.fmt
|
||||||
break
|
|
||||||
}
|
}
|
||||||
names = append(names, el.name)
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
if f == nil {
|
|
||||||
err = fmt.Errorf(`unregistered formatter name: "%s", use one of: %s`, format, strings.Join(names, ", "))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format registers a feature suite output
|
// Format registers a feature suite output
|
||||||
|
|
23
fmt_test.go
23
fmt_test.go
|
@ -2,6 +2,7 @@ package godog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"testing"
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog/gherkin"
|
"github.com/DATA-DOG/godog/gherkin"
|
||||||
)
|
)
|
||||||
|
@ -32,3 +33,25 @@ func (f *testFormatter) Node(node interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *testFormatter) Summary() {}
|
func (f *testFormatter) Summary() {}
|
||||||
|
|
||||||
|
func TestShouldFindFormatter(t *testing.T) {
|
||||||
|
cases := map[string]bool{
|
||||||
|
"progress": true, // true means should be available
|
||||||
|
"unknown": false,
|
||||||
|
"junit": true,
|
||||||
|
"cucumber": true,
|
||||||
|
"pretty": true,
|
||||||
|
"custom": true, // is available for test purposes only
|
||||||
|
"undef": false,
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, shouldFind := range cases {
|
||||||
|
actual := findFmt(name)
|
||||||
|
if actual == nil && shouldFind {
|
||||||
|
t.Fatalf("expected %s formatter should be available", name)
|
||||||
|
}
|
||||||
|
if actual != nil && !shouldFind {
|
||||||
|
t.Fatalf("expected %s formatter should not be available", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
32
run.go
32
run.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog/colors"
|
"github.com/DATA-DOG/godog/colors"
|
||||||
)
|
)
|
||||||
|
@ -93,7 +94,7 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt
|
||||||
s := &Suite{}
|
s := &Suite{}
|
||||||
contextInitializer(s)
|
contextInitializer(s)
|
||||||
s.printStepDefinitions(output)
|
s.printStepDefinitions(output)
|
||||||
return 0
|
return 2 // showing help or printing definitions, results exit code - 2
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(opt.Paths) == 0 {
|
if len(opt.Paths) == 0 {
|
||||||
|
@ -104,13 +105,28 @@ func RunWithOptions(suite string, contextInitializer func(suite *Suite), opt Opt
|
||||||
}
|
}
|
||||||
|
|
||||||
if opt.Concurrency > 1 && !supportsConcurrency(opt.Format) {
|
if opt.Concurrency > 1 && !supportsConcurrency(opt.Format) {
|
||||||
fatal(fmt.Errorf("format \"%s\" does not support concurrent execution", opt.Format))
|
fmt.Fprintln(os.Stderr, fmt.Errorf("format \"%s\" does not support concurrent execution", opt.Format))
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
formatter := findFmt(opt.Format)
|
||||||
|
if nil == formatter {
|
||||||
|
var names []string
|
||||||
|
for name := range AvailableFormatters() {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
fmt.Fprintln(os.Stderr, fmt.Errorf(
|
||||||
|
`unregistered formatter name: "%s", use one of: %s`,
|
||||||
|
opt.Format,
|
||||||
|
strings.Join(names, ", "),
|
||||||
|
))
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
formatter, err := findFmt(opt.Format)
|
|
||||||
fatal(err)
|
|
||||||
|
|
||||||
features, err := parseFeatures(opt.Tags, opt.Paths)
|
features, err := parseFeatures(opt.Tags, opt.Paths)
|
||||||
fatal(err)
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
r := runner{
|
r := runner{
|
||||||
fmt: formatter(suite, output),
|
fmt: formatter(suite, output),
|
||||||
|
@ -152,8 +168,10 @@ func Run(suite string, contextInitializer func(suite *Suite)) int {
|
||||||
var opt Options
|
var opt Options
|
||||||
opt.Output = colors.Colored(os.Stdout)
|
opt.Output = colors.Colored(os.Stdout)
|
||||||
flagSet := FlagSet(&opt)
|
flagSet := FlagSet(&opt)
|
||||||
err := flagSet.Parse(os.Args[1:])
|
if err := flagSet.Parse(os.Args[1:]); err != nil {
|
||||||
fatal(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
opt.Paths = flagSet.Args()
|
opt.Paths = flagSet.Args()
|
||||||
|
|
||||||
|
|
|
@ -137,9 +137,9 @@ func (s *suiteContext) ResetBeforeEachScenario(interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *suiteContext) iRunFeatureSuiteWithFormatter(name string) error {
|
func (s *suiteContext) iRunFeatureSuiteWithFormatter(name string) error {
|
||||||
f, err := findFmt(name)
|
f := findFmt(name)
|
||||||
if err != nil {
|
if f == nil {
|
||||||
return err
|
return fmt.Errorf(`formatter "%s" is not available`, name)
|
||||||
}
|
}
|
||||||
s.testedSuite.fmt = f("godog", &s.out)
|
s.testedSuite.fmt = f("godog", &s.out)
|
||||||
if err := s.parseFeatures(); err != nil {
|
if err := s.parseFeatures(); err != nil {
|
||||||
|
|
10
utils.go
10
utils.go
|
@ -1,8 +1,6 @@
|
||||||
package godog
|
package godog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -27,14 +25,6 @@ func s(n int) string {
|
||||||
return strings.Repeat(" ", n)
|
return strings.Repeat(" ", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// checks the error and exits with error status code
|
|
||||||
func fatal(err error) {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var timeNowFunc = func() time.Time {
|
var timeNowFunc = func() time.Time {
|
||||||
return time.Now()
|
return time.Now()
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче