Skip to content

Commit

Permalink
updated JUnit rendering
Browse files Browse the repository at this point in the history
Now contains accurate time, line attribute and properties for all the annotations!
  • Loading branch information
daveshanley committed Jan 6, 2025
1 parent d05fd73 commit f2331c6
Showing 1 changed file with 42 additions and 12 deletions.
54 changes: 42 additions & 12 deletions vacuum-report/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,25 @@ type TestSuite struct {
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 Properties struct {
xml.Name `xml:"properties"`
Properties []*Property `xml:"property"`
}

type Property struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}

type TestCase struct {
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Time float64 `xml:"time,attr"`
Failure *Failure `xml:"failure,omitempty"`
Name string `xml:"name,attr"`
ClassName string `xml:"classname,attr"`
Line int `xml:"line,attr,omitempty"`
Failure *Failure `xml:"failure,omitempty"`
Properties *Properties `xml:"properties,omitempty"`
}

type Failure struct {
Expand All @@ -52,11 +62,11 @@ func BuildJUnitReport(resultSet *model.RuleResultSet, t time.Time) []byte {

tmpl := `
{{ .Message }}
JSON Path: {{ .Path }}
JSON Path: {{ .Path }}
Rule: {{ .Rule.Id }}
Severity: {{ .Rule.Severity }}
Start Line: {{ .StartNode.Line }}
End Line: {{ .EndNode.Line }}`
Line: {{ .StartNode.Line }}`

parsedTemplate, _ := template.New("failure").Parse(tmpl)

Expand All @@ -77,23 +87,42 @@ func BuildJUnitReport(resultSet *model.RuleResultSet, t time.Time) []byte {
gf++
}
tc = append(tc, &TestCase{
Name: fmt.Sprintf("Category: %s", val.Id),
Line: r.StartNode.Line,
Name: fmt.Sprintf("%s", val.Name),
ClassName: r.Rule.Id,
Time: since.Seconds(),
Failure: &Failure{
Message: r.Message,
Type: strings.ToUpper(r.Rule.Severity),
Contents: sb.String(),
},
Properties: &Properties{
Properties: []*Property{
{
Name: "path",
Value: r.Path,
},
{
Name: "rule",
Value: r.Rule.Id,
},
{
Name: "severity",
Value: r.Rule.Severity,
},
{
Name: "line",
Value: fmt.Sprintf("%d", r.StartNode.Line),
},
},
},
})
}

if len(tc) > 0 {
ts := &TestSuite{
Name: fmt.Sprintf("Category: %s", val.Id),
Name: fmt.Sprintf("%s", val.Name),
Tests: len(categoryResults),
Failures: f,
Time: since.Seconds(),
TestCases: tc,
}

Expand All @@ -106,6 +135,7 @@ func BuildJUnitReport(resultSet *model.RuleResultSet, t time.Time) []byte {
TestSuites: suites,
Tests: gtc,
Failures: gf,
Time: since.Seconds(),
}

b, _ := xml.MarshalIndent(allSuites, "", " ")
Expand Down

0 comments on commit f2331c6

Please sign in to comment.