Skip to content

Commit

Permalink
Merge pull request #5 from dhaus67/main
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaus67 authored Jan 4, 2023
2 parents 589da35 + ff1bfb9 commit 4858350
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 178 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
junit2jira
.idea
67 changes: 63 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func findFailedTests(dirName string, env map[string]string, threshold int) ([]te
if tc.Error == nil {
continue
}
failedTests = append(failedTests, NewTestCase(tc, env))
failedTests = addFailedTest(failedTests, tc, env)
}
}
log.Printf("Found %d failed tests", len(failedTests))
Expand Down Expand Up @@ -220,6 +220,37 @@ func mergeFailedTests(failedTests []testCase, env map[string]string) ([]testCase
return []testCase{tc}, nil
}

func addFailedTest(failedTests []testCase, tc junit.Test, env map[string]string) []testCase {
if !isSubTest(tc) {
return append(failedTests, NewTestCase(tc, env))
}
return addSubTestToFailedTest(tc, failedTests, env)
}

func isSubTest(tc junit.Test) bool {
return strings.Contains(tc.Name, "/")
}

func addSubTestToFailedTest(subTest junit.Test, failedTests []testCase, env map[string]string) []testCase {
// As long as the separator is not empty, split will always return a slice of length 1.
name := strings.Split(subTest.Name, "/")[0]
for i, failedTest := range failedTests {
// Only consider a failed test a "parent" of the test if the name matches _and_ the class name is the same.
if isGoTest(subTest.Classname) && failedTest.Name == name && failedTest.Suite == subTest.Classname {
failedTest.addSubTest(subTest)
failedTests[i] = failedTest
return failedTests
}
}
// In case we found no matches, we will default to add the subtest plain.
return append(failedTests, NewTestCase(subTest, env))
}

// isGoTest will verify that the corresponding classname refers to a go package by expecting the go module name as prefix.
func isGoTest(className string) bool {
return strings.HasPrefix(className, "github.com/stackrox/rox")
}

const (
desc = `
{{- if .Message }}
Expand All @@ -237,6 +268,11 @@ const (
{{ .Stdout }}
{code}
{{- end }}
{{- if .Error }}
{code:title=ERROR|borderStyle=solid}
{{ .Error }}
{code}
{{- end }}
|| ENV || Value ||
| BUILD ID | [{{- .BuildId -}}|https://prow.ci.openshift.org/view/gs/origin-ci-test/logs/{{- .JobName -}}/{{- .BuildId -}}]|
Expand All @@ -254,6 +290,7 @@ type testCase struct {
Message string
Stdout string
Stderr string
Error string
BuildId string
Cluster string
JobName string
Expand All @@ -265,7 +302,7 @@ type testCase struct {
func NewTestCase(tc junit.Test, env map[string]string) testCase {
jobSpec := env["JOB_SPEC"]
baseLink := gjson.Get(jobSpec, "refs.base_link").String()
return testCase{
c := testCase{
Name: tc.Name,
Message: tc.Message,
Stdout: tc.SystemOut,
Expand All @@ -278,10 +315,15 @@ func NewTestCase(tc junit.Test, env map[string]string) testCase {
BuildTag: env["STACKROX_BUILD_TAG"],
BaseLink: baseLink,
}

if tc.Error != nil {
c.Error = tc.Error.Error()
}
return c
}

func (tc testCase) description() (string, error) {
return render(tc, desc)
func (tc *testCase) description() (string, error) {
return render(*tc, desc)
}

func (tc testCase) summary() (string, error) {
Expand All @@ -292,6 +334,23 @@ func (tc testCase) summary() (string, error) {
return clearString(s), nil
}

const subTestFormat = "\nSub test %s: %s"

func (tc *testCase) addSubTest(subTest junit.Test) {
if subTest.Message != "" {
tc.Message += fmt.Sprintf(subTestFormat, subTest.Name, subTest.Message)
}
if subTest.SystemOut != "" {
tc.Stdout += fmt.Sprintf(subTestFormat, subTest.Name, subTest.SystemOut)
}
if subTest.SystemErr != "" {
tc.Stderr += fmt.Sprintf(subTestFormat, subTest.Name, subTest.SystemErr)
}
if subTest.Error != nil {
tc.Error += fmt.Sprintf(subTestFormat, subTest.Name, subTest.Error.Error())
}
}

func render(tc testCase, text string) (string, error) {
tmpl, err := template.New("test").Parse(text)
if err != nil {
Expand Down
Loading

0 comments on commit 4858350

Please sign in to comment.