From ef794d57cae79fa9c26a943fd881940d0fb742de Mon Sep 17 00:00:00 2001 From: gedi Date: Fri, 28 Apr 2017 10:27:55 +0300 Subject: [PATCH] resets time to zero for all godog tests --- fmt.go | 2 +- fmt_cucumber.go | 8 ++++---- fmt_events.go | 17 ++++++++--------- fmt_junit.go | 12 ++++++------ fmt_pretty.go | 3 +-- fmt_progress.go | 3 +-- fmt_test.go | 3 +-- utils.go | 5 +++++ utils_test.go | 19 +++++++++++++++++++ 9 files changed, 46 insertions(+), 26 deletions(-) create mode 100644 utils_test.go diff --git a/fmt.go b/fmt.go index dd8ac7c..8aff4aa 100644 --- a/fmt.go +++ b/fmt.go @@ -305,7 +305,7 @@ func (f *basefmt) Summary() { scenarios = append(scenarios, green(fmt.Sprintf("%d passed", passed))) } scenarios = append(scenarios, parts...) - elapsed := time.Since(f.started) + elapsed := timeNowFunc().Sub(f.started) fmt.Fprintln(f.out, "") if total == 0 { diff --git a/fmt_cucumber.go b/fmt_cucumber.go index c7de56c..b0afe03 100644 --- a/fmt_cucumber.go +++ b/fmt_cucumber.go @@ -29,7 +29,7 @@ func init() { func cucumberFunc(suite string, out io.Writer) Formatter { formatter := &cukefmt{ basefmt: basefmt{ - started: time.Now(), + started: timeNowFunc(), indent: 2, out: out, }, @@ -252,7 +252,7 @@ func (f *cukefmt) step(res *stepResult) { // determine if test case has finished switch t := f.owner.(type) { case *gherkin.TableRow: - d := int(time.Since(f.startTime).Nanoseconds()) + d := int(timeNowFunc().Sub(f.startTime).Nanoseconds()) f.curStep.Result.Duration = &d f.curStep.Line = t.Location.Line f.curStep.Result.Status = res.typ.String() @@ -260,7 +260,7 @@ func (f *cukefmt) step(res *stepResult) { f.curStep.Result.Error = res.err.Error() } case *gherkin.Scenario: - d := int(time.Since(f.startTime).Nanoseconds()) + d := int(timeNowFunc().Sub(f.startTime).Nanoseconds()) f.curStep.Result.Duration = &d f.curStep.Result.Status = res.typ.String() if res.err != nil { @@ -271,7 +271,7 @@ func (f *cukefmt) step(res *stepResult) { func (f *cukefmt) Defined(step *gherkin.Step, def *StepDef) { - f.startTime = time.Now() // start timing the step + f.startTime = timeNowFunc() // start timing the step f.curElement.Steps = append(f.curElement.Steps, cukeStep{}) f.curStep = &f.curElement.Steps[len(f.curElement.Steps)-1] diff --git a/fmt_events.go b/fmt_events.go index e22b417..275b021 100644 --- a/fmt_events.go +++ b/fmt_events.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "io" - "time" "github.com/DATA-DOG/godog/gherkin" ) @@ -19,7 +18,7 @@ func init() { func eventsFunc(suite string, out io.Writer) Formatter { formatter := &events{ basefmt: basefmt{ - started: time.Now(), + started: timeNowFunc(), indent: 2, out: out, }, @@ -33,7 +32,7 @@ func eventsFunc(suite string, out io.Writer) Formatter { }{ "TestRunStarted", spec, - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, suite, }) @@ -87,7 +86,7 @@ func (f *events) Node(n interface{}) { }{ "TestCaseStarted", id, - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, }) if undefined { @@ -101,7 +100,7 @@ func (f *events) Node(n interface{}) { }{ "TestCaseFinished", id, - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, "undefined", }) } @@ -148,7 +147,7 @@ func (f *events) Summary() { }{ "TestRunFinished", status.String(), - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, snips, "", // @TODO not sure that could be correctly implemented }) @@ -168,7 +167,7 @@ func (f *events) step(res *stepResult) { }{ "TestStepFinished", fmt.Sprintf("%s:%d", f.path, res.step.Location.Line), - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, res.typ.String(), errMsg, }) @@ -194,7 +193,7 @@ func (f *events) step(res *stepResult) { }{ "TestCaseFinished", fmt.Sprintf("%s:%d", f.path, line), - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, f.stat.String(), }) } @@ -236,7 +235,7 @@ func (f *events) Defined(step *gherkin.Step, def *StepDef) { }{ "TestStepStarted", fmt.Sprintf("%s:%d", f.path, step.Location.Line), - time.Now().UnixNano() / nanoSec, + timeNowFunc().UnixNano() / nanoSec, }) } diff --git a/fmt_junit.go b/fmt_junit.go index 4395994..6eafb18 100644 --- a/fmt_junit.go +++ b/fmt_junit.go @@ -21,7 +21,7 @@ func junitFunc(suite string, out io.Writer) Formatter { TestSuites: make([]*junitTestSuite, 0), }, out: out, - started: time.Now(), + started: timeNowFunc(), } } @@ -45,9 +45,9 @@ func (j *junitFormatter) Feature(feature *gherkin.Feature, path string, c []byte } if len(j.suite.TestSuites) > 0 { - j.current().Time = time.Since(j.featStarted).String() + j.current().Time = timeNowFunc().Sub(j.featStarted).String() } - j.featStarted = time.Now() + j.featStarted = timeNowFunc() j.suite.TestSuites = append(j.suite.TestSuites, testSuite) } @@ -79,9 +79,9 @@ func (j *junitFormatter) Node(node interface{}) { return } if len(suite.TestCases) > 0 { - suite.current().Time = time.Since(j.caseStarted).String() + suite.current().Time = timeNowFunc().Sub(j.caseStarted).String() } - j.caseStarted = time.Now() + j.caseStarted = timeNowFunc() suite.TestCases = append(suite.TestCases, tcase) } @@ -134,7 +134,7 @@ func (j *junitFormatter) Pending(step *gherkin.Step, match *StepDef) { } func (j *junitFormatter) Summary() { - j.suite.Time = time.Since(j.started).String() + j.suite.Time = timeNowFunc().Sub(j.started).String() io.WriteString(j.out, xml.Header) enc := xml.NewEncoder(j.out) diff --git a/fmt_pretty.go b/fmt_pretty.go index e08e551..f6788c6 100644 --- a/fmt_pretty.go +++ b/fmt_pretty.go @@ -6,7 +6,6 @@ import ( "math" "regexp" "strings" - "time" "unicode/utf8" "github.com/DATA-DOG/godog/colors" @@ -20,7 +19,7 @@ func init() { func prettyFunc(suite string, out io.Writer) Formatter { return &pretty{ basefmt: basefmt{ - started: time.Now(), + started: timeNowFunc(), indent: 2, out: out, }, diff --git a/fmt_progress.go b/fmt_progress.go index ba47b11..f1af01c 100644 --- a/fmt_progress.go +++ b/fmt_progress.go @@ -5,7 +5,6 @@ import ( "io" "math" "sync" - "time" "github.com/DATA-DOG/godog/gherkin" ) @@ -17,7 +16,7 @@ func init() { func progressFunc(suite string, out io.Writer) Formatter { return &progress{ basefmt: basefmt{ - started: time.Now(), + started: timeNowFunc(), indent: 2, out: out, }, diff --git a/fmt_test.go b/fmt_test.go index 0458f5c..a6604bb 100644 --- a/fmt_test.go +++ b/fmt_test.go @@ -2,7 +2,6 @@ package godog import ( "io" - "time" "github.com/DATA-DOG/godog/gherkin" ) @@ -15,7 +14,7 @@ type testFormatter struct { func testFormatterFunc(suite string, out io.Writer) Formatter { return &testFormatter{ basefmt: basefmt{ - started: time.Now(), + started: timeNowFunc(), indent: 2, out: out, }, diff --git a/utils.go b/utils.go index 7d6fa86..989427f 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "strings" + "time" "github.com/DATA-DOG/godog/colors" ) @@ -33,3 +34,7 @@ func fatal(err error) { os.Exit(1) } } + +var timeNowFunc = func() time.Time { + return time.Now() +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..50373be --- /dev/null +++ b/utils_test.go @@ -0,0 +1,19 @@ +package godog + +import ( + "testing" + "time" +) + +func init() { + timeNowFunc = func() time.Time { + return time.Time{} + } +} + +func TestTimeNowFunc(t *testing.T) { + now := timeNowFunc() + if !now.IsZero() { + t.Fatalf("expected zeroed time, but got: %s", now.Format(time.RFC3339)) + } +}