Skip to content

Commit

Permalink
Add a config for formatting junit xml
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Oct 13, 2019
1 parent 0059e51 commit ac42209
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
50 changes: 36 additions & 14 deletions internal/junitxml/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,53 @@ type JUnitFailure struct {
Contents string `xml:",chardata"`
}

// Config used to write a junit XML document.
type Config struct {
FormatTestSuiteName FormatFunc
FormatTestCaseClassname FormatFunc
}

// FormatFunc converts a string from one format into another.
type FormatFunc func(string) string

// Write creates an XML document and writes it to out.
func Write(out io.Writer, exec *testjson.Execution) error {
return errors.Wrap(write(out, generate(exec)), "failed to write JUnit XML")
func Write(out io.Writer, exec *testjson.Execution, cfg Config) error {
return errors.Wrap(write(out, generate(exec, cfg)), "failed to write JUnit XML")
}

func generate(exec *testjson.Execution) JUnitTestSuites {
func generate(exec *testjson.Execution, cfg Config) JUnitTestSuites {
cfg = configWithDefaults(cfg)
version := goVersion()
suites := JUnitTestSuites{}

for _, pkgname := range exec.Packages() {
pkg := exec.Package(pkgname)
junitpkg := JUnitTestSuite{
Name: pkgname,
Name: cfg.FormatTestSuiteName(pkgname),
Tests: pkg.Total,
Time: formatDurationAsSeconds(pkg.Elapsed()),
Properties: packageProperties(version),
TestCases: packageTestCases(pkg),
TestCases: packageTestCases(pkg, cfg.FormatTestCaseClassname),
Failures: len(pkg.Failed),
}
suites.Suites = append(suites.Suites, junitpkg)
}
return suites
}

func configWithDefaults(cfg Config) Config {
noop := func(v string) string {
return v
}
if cfg.FormatTestSuiteName == nil {
cfg.FormatTestSuiteName = noop
}
if cfg.FormatTestCaseClassname == nil {
cfg.FormatTestCaseClassname = noop
}
return cfg
}

func formatDurationAsSeconds(d time.Duration) string {
return fmt.Sprintf("%f", d.Seconds())
}
Expand Down Expand Up @@ -115,13 +139,11 @@ func goVersion() string {
return strings.TrimPrefix(strings.TrimSpace(string(out)), "go version ")
}

func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
func packageTestCases(pkg *testjson.Package, formatClassname FormatFunc) []JUnitTestCase {
cases := []JUnitTestCase{}

if pkg.TestMainFailed() {
jtc := newJUnitTestCase(testjson.TestCase{
Test: "TestMain",
})
jtc := newJUnitTestCase(testjson.TestCase{Test: "TestMain"}, formatClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: pkg.Output(""),
Expand All @@ -130,7 +152,7 @@ func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
}

for _, tc := range pkg.Failed {
jtc := newJUnitTestCase(tc)
jtc := newJUnitTestCase(tc, formatClassname)
jtc.Failure = &JUnitFailure{
Message: "Failed",
Contents: pkg.Output(tc.Test),
Expand All @@ -139,21 +161,21 @@ func packageTestCases(pkg *testjson.Package) []JUnitTestCase {
}

for _, tc := range pkg.Skipped {
jtc := newJUnitTestCase(tc)
jtc := newJUnitTestCase(tc, formatClassname)
jtc.SkipMessage = &JUnitSkipMessage{Message: pkg.Output(tc.Test)}
cases = append(cases, jtc)
}

for _, tc := range pkg.Passed {
jtc := newJUnitTestCase(tc)
jtc := newJUnitTestCase(tc, formatClassname)
cases = append(cases, jtc)
}
return cases
}

func newJUnitTestCase(tc testjson.TestCase) JUnitTestCase {
func newJUnitTestCase(tc testjson.TestCase, formatClassname FormatFunc) JUnitTestCase {
return JUnitTestCase{
Classname: tc.Package,
Classname: formatClassname(tc.Package),
Name: tc.Test,
Time: formatDurationAsSeconds(tc.Elapsed),
}
Expand Down
2 changes: 1 addition & 1 deletion internal/junitxml/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestWrite(t *testing.T) {
exec := createExecution(t)

defer env.Patch(t, "GOVERSION", "go7.7.7")()
err := Write(out, exec)
err := Write(out, exec, Config{})
assert.NilError(t, err)
golden.Assert(t, out.String(), "junitxml-report.golden")
}
Expand Down

0 comments on commit ac42209

Please sign in to comment.