Skip to content

Commit

Permalink
Merge pull request #122 from JulesFaucherre/CLI-ISSUE#1048
Browse files Browse the repository at this point in the history
add timestamp to JUnit output format.
  • Loading branch information
SemanticallyNull authored Mar 4, 2024
2 parents e01f013 + b4dabcb commit 84f6ea9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
42 changes: 21 additions & 21 deletions cpa/tester/policies/results.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<testsuites name="root" tests="54" failures="0" errors="0" time="0">
<testsuite tests="12" failures="0" time="0" name="&lt;opa.tests&gt;" timestamp="">
<testsuite tests="12" failures="0" time="0" name="&lt;opa.tests&gt;" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="&lt;opa.tests&gt;" name="data.org.test_to_array_scalar" time="0"></testcase>
<testcase classname="&lt;opa.tests&gt;" name="data.org.test_to_array_array" time="0"></testcase>
Expand All @@ -14,56 +14,56 @@
<testcase classname="&lt;opa.tests&gt;" name="data.org.test_is_parameterized_expression_for_pipeline_true" time="0"></testcase>
<testcase classname="&lt;opa.tests&gt;" name="data.org.test_is_parameterized_expression_false" time="0"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/common" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/common" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="3" failures="0" time="0" name="policies/common/base" timestamp="">
<testsuite tests="3" failures="0" time="0" name="policies/common/base" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/base" name="test_base_policy" time="0"></testcase>
<testcase classname="policies/common/base" name="test_base_policy/not_bob" time="0"></testcase>
<testcase classname="policies/common/base" name="test_base_policy/not_bob/hard_fail" time="0"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0" name="policies/common/enable_hard" timestamp="">
<testsuite tests="1" failures="0" time="0" name="policies/common/enable_hard" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/enable_hard" name="test_enable_hard" time="0"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0" name="policies/common/error" timestamp="">
<testsuite tests="1" failures="0" time="0" name="policies/common/error" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/error" name="test_error" time="0"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0" name="policies/common/no_enabled_rules" timestamp="">
<testsuite tests="1" failures="0" time="0" name="policies/common/no_enabled_rules" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/no_enabled_rules" name="test_no_enabled_rules" time="0"></testcase>
</testsuite>
<testsuite tests="4" failures="0" time="0" name="policies/common/reason_types" timestamp="">
<testsuite tests="4" failures="0" time="0" name="policies/common/reason_types" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/reason_types" name="test_reason_types" time="0"></testcase>
<testcase classname="policies/common/reason_types" name="test_reason_types/array" time="0"></testcase>
<testcase classname="policies/common/reason_types" name="test_reason_types/map" time="0"></testcase>
<testcase classname="policies/common/reason_types" name="test_reason_types/string" time="0"></testcase>
</testsuite>
<testsuite tests="1" failures="0" time="0" name="policies/common/soft_and_hard_fail_together" timestamp="">
<testsuite tests="1" failures="0" time="0" name="policies/common/soft_and_hard_fail_together" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/soft_and_hard_fail_together" name="test_soft_and_hard_fail_together" time="0"></testcase>
</testsuite>
<testsuite tests="2" failures="0" time="0" name="policies/common/structure" timestamp="">
<testsuite tests="2" failures="0" time="0" name="policies/common/structure" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/common/structure" name="test_structure/with_meta" time="0"></testcase>
<testcase classname="policies/common/structure" name="test_structure/with_meta/good" time="0"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/helpers" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/helpers" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="17" failures="0" time="0" name="policies/helpers/contexts" timestamp="">
<testsuite tests="17" failures="0" time="0" name="policies/helpers/contexts" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/helpers/contexts" name="test_allowlist" time="0"></testcase>
<testcase classname="policies/helpers/contexts" name="test_allowlist/invalid_context" time="0"></testcase>
Expand All @@ -83,51 +83,51 @@
<testcase classname="policies/helpers/contexts" name="test_reservelist/test_multiple_invalid_contexts_in_job" time="0"></testcase>
<testcase classname="policies/helpers/contexts" name="test_reservelist/test_unreserved_contexts" time="0"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/helpers/orbs" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/helpers/orbs" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="3" failures="0" time="0" name="policies/helpers/orbs/allowlist" timestamp="">
<testsuite tests="3" failures="0" time="0" name="policies/helpers/orbs/allowlist" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/helpers/orbs/allowlist" name="test_allow_orbs" time="0"></testcase>
<testcase classname="policies/helpers/orbs/allowlist" name="test_allow_orbs/bad_orb_present" time="0"></testcase>
<testcase classname="policies/helpers/orbs/allowlist" name="test_allow_orbs/no_orbs_present" time="0"></testcase>
</testsuite>
<testsuite tests="5" failures="0" time="0" name="policies/helpers/orbs/ban_version" timestamp="">
<testsuite tests="5" failures="0" time="0" name="policies/helpers/orbs/ban_version" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/helpers/orbs/ban_version" name="test_ban_orbs" time="0"></testcase>
<testcase classname="policies/helpers/orbs/ban_version" name="test_ban_orbs/orb_present" time="0"></testcase>
<testcase classname="policies/helpers/orbs/ban_version" name="test_ban_orbs_version" time="0"></testcase>
<testcase classname="policies/helpers/orbs/ban_version" name="test_ban_orbs_version/exact_match" time="0"></testcase>
<testcase classname="policies/helpers/orbs/ban_version" name="test_ban_orbs_version/wrong_version" time="0"></testcase>
</testsuite>
<testsuite tests="3" failures="0" time="0" name="policies/helpers/runner" timestamp="">
<testsuite tests="3" failures="0" time="0" name="policies/helpers/runner" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/helpers/runner" name="test_runner_helper" time="0"></testcase>
<testcase classname="policies/helpers/runner" name="test_runner_helper/project_medium" time="0"></testcase>
<testcase classname="policies/helpers/runner" name="test_runner_helper/project_small" time="0"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/helpers/utils" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/helpers/utils" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="1" failures="0" time="0" name="policies/multifile" timestamp="">
<testsuite tests="1" failures="0" time="0" name="policies/multifile" timestamp="2024-03-04T10:50:05Z">
<properties></properties>
<testcase classname="policies/multifile" name="test_multifile_policy" time="0"></testcase>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub0" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub0" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub1" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub1" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
</testsuite>
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub1/sub1_0" timestamp="">
<testsuite tests="0" failures="0" time="0" name="policies/multifile/sub1/sub1_0" timestamp="2024-03-04T10:50:05Z">
<properties>
<property name="skipped" value="no tests"></property>
</properties>
Expand Down
11 changes: 9 additions & 2 deletions cpa/tester/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,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 @@ -183,6 +184,7 @@ func (rh JUnitResultHandler) HandleResults(c <-chan Result) bool {
return
}
currentSuite.Time = fmt.Sprintf("%.3f", currentSuiteTime.Seconds())
currentSuite.Timestamp = rh.getTime().Format(time.RFC3339)
currentSuiteTime = 0
root.Suites = append(root.Suites, currentSuite)
}
Expand Down Expand Up @@ -256,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
6 changes: 5 additions & 1 deletion 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 @@ -118,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 84f6ea9

Please sign in to comment.