-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: JUnit format now available for
report
#233
Using the new `-j` flag, JUnit output is available for CI/CD as requested in #233
- Loading branch information
1 parent
ecd7552
commit 3c19183
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley | ||
// SPDX-License-Identifier: MIT | ||
|
||
package vacuum_report | ||
|
||
import ( | ||
"bytes" | ||
"encoding/xml" | ||
"fmt" | ||
"github.com/daveshanley/vacuum/model" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
type TestSuites struct { | ||
XMLName xml.Name `xml:"testsuites"` | ||
TestSuites []*TestSuite `xml:"testsuite"` | ||
Tests int `xml:"tests,attr"` | ||
Failures int `xml:"failures,attr"` | ||
Time float64 `xml:"time,attr"` | ||
} | ||
|
||
type TestSuite struct { | ||
XMLName xml.Name `xml:"testsuite"` | ||
Name string `xml:"name,attr"` | ||
Tests int `xml:"tests,attr"` | ||
Failures int `xml:"failures,attr"` | ||
Time float64 `xml:"time,attr"` | ||
TestCases []*TestCase `xml:"testcase"` | ||
} | ||
|
||
type TestCase struct { | ||
Name string `xml:"name,attr"` | ||
ClassName string `xml:"classname,attr"` | ||
Time float64 `xml:"time,attr"` | ||
Failure *Failure `xml:"failure,omitempty"` | ||
} | ||
|
||
type Failure struct { | ||
Message string `xml:"message,attr,omitempty"` | ||
Type string `xml:"type,attr,omitempty"` | ||
Contents string `xml:",innerxml"` | ||
} | ||
|
||
func BuildJUnitReport(resultSet *model.RuleResultSet, t time.Time) []byte { | ||
|
||
since := time.Since(t) | ||
|
||
var suites []*TestSuite | ||
|
||
var cats = model.RuleCategoriesOrdered | ||
|
||
tmpl := ` | ||
{{ .Message }} | ||
JSON Path: {{ .Path }} | ||
Rule: {{ .Rule.Id }} | ||
Severity: {{ .Rule.Severity }} | ||
Start Line: {{ .StartNode.Line }} | ||
End Line: {{ .EndNode.Line }}` | ||
|
||
parsedTemplate, _ := template.New("failure").Parse(tmpl) | ||
|
||
// try a category print out. | ||
for _, val := range cats { | ||
categoryResults := resultSet.GetResultsByRuleCategory(val.Id) | ||
|
||
f := 0 | ||
var tc []*TestCase | ||
|
||
for _, r := range categoryResults { | ||
var sb bytes.Buffer | ||
_ = parsedTemplate.Execute(&sb, r) | ||
if r.Rule.Severity == model.SeverityError || r.Rule.Severity == model.SeverityWarn { | ||
f++ | ||
} | ||
tc = append(tc, &TestCase{ | ||
Name: fmt.Sprintf("Category: %s", val.Id), | ||
ClassName: r.Rule.Id, | ||
Time: since.Seconds(), | ||
Failure: &Failure{ | ||
Message: r.Message, | ||
Type: strings.ToUpper(r.Rule.Severity), | ||
Contents: sb.String(), | ||
}, | ||
}) | ||
} | ||
|
||
if len(tc) > 0 { | ||
ts := &TestSuite{ | ||
Name: fmt.Sprintf("Category: %s", val.Id), | ||
Tests: len(categoryResults), | ||
Failures: f, | ||
Time: since.Seconds(), | ||
TestCases: tc, | ||
} | ||
|
||
suites = append(suites, ts) | ||
} | ||
} | ||
|
||
b, _ := xml.MarshalIndent(suites, "", " ") | ||
return b | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley | ||
// SPDX-License-Identifier: MIT | ||
|
||
package vacuum_report | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBuildJUnitReport(t *testing.T) { | ||
j := testhelp_generateReport() | ||
j.ResultSet.Results[0].Message = "testing, 123" | ||
j.ResultSet.Results[0].Path = "$.somewhere.out.there" | ||
j.ResultSet.Results[0].RuleId = "R0001" | ||
f := time.Now().Add(-time.Millisecond * 5) | ||
data := BuildJUnitReport(j.ResultSet, f) | ||
assert.Len(t, data, 295) | ||
} |