Skip to content

Commit

Permalink
used a scoped getTime method instead of the Clock interface
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Mar 4, 2024
1 parent 29ab7ae commit b4dabcb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
31 changes: 9 additions & 22 deletions cpa/tester/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,6 @@ import (
"gopkg.in/yaml.v3"
)

var (
clock Clock = StandardClock{}
)

type Clock interface {
Now() time.Time
}

type StandardClock struct{}

func (StandardClock) Now() time.Time { return time.Now() }

type MockedClock struct{}

func (MockedClock) Now() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-03-04T10:50:05Z")
return t
}

type Result struct {
Group string
Name string
Expand Down Expand Up @@ -186,7 +167,8 @@ func (jrh JSONResultHandler) HandleResults(c <-chan Result) bool {
}

type JUnitResultHandler struct {
w io.Writer
getTime func() time.Time
w io.Writer
}

func (rh JUnitResultHandler) HandleResults(c <-chan Result) bool {
Expand All @@ -202,7 +184,7 @@ func (rh JUnitResultHandler) HandleResults(c <-chan Result) bool {
return
}
currentSuite.Time = fmt.Sprintf("%.3f", currentSuiteTime.Seconds())
currentSuite.Timestamp = clock.Now().Format(time.RFC3339)
currentSuite.Timestamp = rh.getTime().Format(time.RFC3339)
currentSuiteTime = 0
root.Suites = append(root.Suites, currentSuite)
}
Expand Down Expand Up @@ -276,8 +258,13 @@ func (rh JUnitResultHandler) HandleResults(c <-chan Result) bool {
}

func MakeJUnitResultHandler(opts ResultHandlerOptions) JUnitResultHandler {
return MakeJUnitResultHandlerWithGetTime(opts, time.Now)
}

func MakeJUnitResultHandlerWithGetTime(opts ResultHandlerOptions, getTime func() time.Time) JUnitResultHandler {
return JUnitResultHandler{
w: opts.Dst,
getTime: getTime,
w: opts.Dst,
}
}

Expand Down
10 changes: 5 additions & 5 deletions cpa/tester/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"regexp"
"testing"
"time"

_ "embed"

Expand Down Expand Up @@ -101,10 +102,6 @@ func TestRunnerResults(t *testing.T) {
})

t.Run("xml", func(t *testing.T) {
oldClock := clock
defer func() { clock = oldClock }()
clock = MockedClock{}

options := RunnerOptions{
Path: "./policies/...",
Include: func() *regexp.Regexp {
Expand All @@ -122,7 +119,10 @@ func TestRunnerResults(t *testing.T) {
buf := new(bytes.Buffer)
opts := ResultHandlerOptions{Dst: buf}

MakeJUnitResultHandler(opts).HandleResults(runner.Run())
MakeJUnitResultHandlerWithGetTime(opts, func() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-03-04T10:50:05Z")
return t
}).HandleResults(runner.Run())

suites := junit.JUnitTestSuites{}
require.NoError(t, xml.Unmarshal(buf.Bytes(), &suites))
Expand Down

0 comments on commit b4dabcb

Please sign in to comment.