Skip to content

Commit

Permalink
Merge pull request #2 from psturc/RHTAP-1851-ginkgojunit
Browse files Browse the repository at this point in the history
fix: utilize ginkgo junit
  • Loading branch information
psturc authored Nov 3, 2023
2 parents 4e2b175 + ee01c6b commit dd68aa5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/redhat-appstudio-qe/junit2html

go 1.19

require github.com/jstemmer/go-junit-report/v2 v2.0.0
require github.com/onsi/ginkgo/v2 v2.13.0
18 changes: 14 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jstemmer/go-junit-report/v2 v2.0.0 h1:bMZNO9B16VFn07tKyi4YJFIbZtVmJaa5Xakv9dcwK58=
github.com/jstemmer/go-junit-report/v2 v2.0.0/go.mod h1:mgHVr7VUo5Tn8OLVr1cKnLuEy0M92wdRntM99h7RkgQ=
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 h1:yAJXTCF9TqKcTiHJAE8dj7HMvPfh66eeA2JYW7eFpSE=
github.com/onsi/ginkgo/v2 v2.13.0 h1:0jY9lJquiL8fcf3M4LAXN5aMlS/b2BV86HFFPCPMgE4=
github.com/onsi/ginkgo/v2 v2.13.0/go.mod h1:TE309ZR8s5FsKKpuB1YAQYBzCaAfUgatB/xlT/ETL/o=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"fmt"
"os"

"github.com/jstemmer/go-junit-report/v2/junit"
reporters "github.com/onsi/ginkgo/v2/reporters"
"github.com/redhat-appstudio-qe/junit2html/pkg/convert"
)

func main() {
suites := &junit.Testsuites{}
suites := &reporters.JUnitTestSuites{}

err := xml.NewDecoder(os.Stdin).Decode(suites)
if err != nil {
Expand All @@ -21,5 +21,4 @@ func main() {
panic(err)
}
fmt.Println(html)

}
58 changes: 30 additions & 28 deletions pkg/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (
"strings"
"time"

"github.com/jstemmer/go-junit-report/v2/junit"
reporters "github.com/onsi/ginkgo/v2/reporters"
)

//go:embed style.css
var styles string

var output string

func Convert(suites *junit.Testsuites) (string, error) {
func Convert(suites *reporters.JUnitTestSuites) (string, error) {
output += "<html>"
output += "<head>"
output += "<meta charset=\"UTF-8\">"
Expand All @@ -24,16 +24,16 @@ func Convert(suites *junit.Testsuites) (string, error) {
output += "<body>"

failures, total := 0, 0
for _, s := range suites.Suites {
for _, s := range suites.TestSuites {
failures += s.Failures
total += len(s.Testcases)
total += len(s.TestCases)
}
output += fmt.Sprintf("<p>%d of %d tests failed</p>\n", failures, total)
printLinkToReport(suites.Suites)
for _, s := range suites.Suites {
printLinkToReport(suites.TestSuites)
for _, s := range suites.TestSuites {
if s.Failures > 0 {
printSuiteHeader(s)
for _, c := range s.Testcases {
for _, c := range s.TestCases {
if f := c.Failure; f != nil {
printTest(s, c)
}
Expand All @@ -42,9 +42,9 @@ func Convert(suites *junit.Testsuites) (string, error) {
printGatherLinks(s)
}

for _, s := range suites.Suites {
for _, s := range suites.TestSuites {
printSuiteHeader(s)
for _, c := range s.Testcases {
for _, c := range s.TestCases {
if c.Failure == nil {
printTest(s, c)
}
Expand All @@ -55,7 +55,7 @@ func Convert(suites *junit.Testsuites) (string, error) {
return output, nil
}

func printTest(testSuite junit.Testsuite, testCase junit.Testcase) {
func printTest(testSuite reporters.JUnitTestSuite, testCase reporters.JUnitTestCase) {
id := fmt.Sprintf("%s.%s.%s", testSuite.Name, testCase.Classname, testCase.Name)
class, text := "passed", "Pass"
failure := testCase.Failure
Expand All @@ -72,29 +72,31 @@ func printTest(testSuite junit.Testsuite, testCase junit.Testcase) {
output += fmt.Sprintf("<label for='%s' class='toggle'>%s<span class='badge'>%s</span></a></label>\n", id, testCase.Name, text)
output += fmt.Sprintf("<input type='checkbox' name='one' id='%s' class='hide-input'>", id)
output += "<div class='toggle-el'>\n"

d := time.Duration(testCase.Time * float64(time.Second)).Round(time.Second)
output += fmt.Sprintf("<p class='duration' title='Test duration'>Test duration: %v</p>\n", d)
if failure != nil {
failure.Data = strings.ReplaceAll(failure.Data, `<bool>`, `"bool"`)
failure.Message = strings.ReplaceAll(failure.Message, `<bool>`, `"bool"`)
failure.Description = strings.ReplaceAll(failure.Description, `<bool>`, `"bool"`)
output += fmt.Sprintf("<div class='content'><b>Failure message:</b> \n\n%s</div>\n", failure.Message)
output += fmt.Sprintf("<div class='content'><b>Failure data:</b> \n\n%s</div>\n", failure.Data)
if testCase.SystemErr != nil {
testCase.SystemErr.Data = strings.ReplaceAll(testCase.SystemErr.Data, `<bool>`, `"bool"`)
output += fmt.Sprintf("<div class='content'><b>Log:</b> \n\n%s</div>\n", testCase.SystemErr.Data)
}
output += fmt.Sprintf("<div class='content'><b>Failure description:</b> \n\n%s</div>\n", failure.Description)
} else if skipped != nil {
output += fmt.Sprintf("<div class='content'>%s</div>\n", skipped.Message)
}
d, _ := time.ParseDuration(testCase.Time)
output += fmt.Sprintf("<p class='duration' title='Test duration'>%v</p>\n", d)
if testCase.SystemErr != "" {
testCase.SystemErr = strings.ReplaceAll(testCase.SystemErr, `<bool>`, `"bool"`)
output += fmt.Sprintf("<div class='content'><b>Log:</b> \n\n%s</div>\n", testCase.SystemErr)
}

output += "</div>\n"
output += "</div>\n"

}

func printSuiteHeader(s junit.Testsuite) {
func printSuiteHeader(s reporters.JUnitTestSuite) {
output += "<h4>"
output += s.Name
if s.Properties != nil {
for _, p := range *s.Properties {
if len(s.Properties.Properties) != 0 {
for _, p := range s.Properties.Properties {
if strings.HasPrefix(p.Name, "coverage.") {
v, _ := strconv.ParseFloat(p.Value, 32)
output += fmt.Sprintf("<span class='coverage' title='%s'>%.0f%%</span>\n", p.Name, v)
Expand All @@ -105,20 +107,20 @@ func printSuiteHeader(s junit.Testsuite) {
output += "</h4>"
}

func printGatherLinks(s junit.Testsuite) {
if s.Properties != nil {
for _, p := range *s.Properties {
func printGatherLinks(s reporters.JUnitTestSuite) {
if len(s.Properties.Properties) != 0 {
for _, p := range s.Properties.Properties {
if strings.Contains(p.Name, "gather") {
output += fmt.Sprintf("<a href='%s'>Link to %s artifacts</a>\n", p.Value, p.Name)
}
}
}
}

func printLinkToReport(suites []junit.Testsuite) {
func printLinkToReport(suites []reporters.JUnitTestSuite) {
for _, suite := range suites {
if suite.Properties != nil {
for _, p := range *suite.Properties {
if len(suite.Properties.Properties) != 0 {
for _, p := range suite.Properties.Properties {
if strings.Contains(p.Name, "html-report-link") {
output += fmt.Sprintf("<a href='%s' target=”_blank” >Having trouble viewing this report? Click here to open it in another tab</a>\n", p.Value)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/convert/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ label {
}

.duration {
font-size: x-small;
font-size: medium;
}

div:target .expando {
Expand Down

0 comments on commit dd68aa5

Please sign in to comment.