From 19de48c41d45efcaef0bd3149ca87120fffdfad1 Mon Sep 17 00:00:00 2001 From: Ben Brooks Date: Fri, 24 Jan 2020 15:38:04 +0000 Subject: [PATCH] Add Package Prefix option for report namespacing --- go-junit-report.go | 6 +++++ go-junit-report_test.go | 41 +++++++++++++++++++++++++++++----- parser/parser.go | 12 ++++++++++ parser/parser_test.go | 23 +++++++++++++++++++ testdata/33-package-prefix.txt | 4 ++++ testdata/33-report.xml | 9 ++++++++ 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 testdata/33-package-prefix.txt create mode 100644 testdata/33-report.xml diff --git a/go-junit-report.go b/go-junit-report.go index 1332f3b6..ea2a4ef9 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -12,6 +12,7 @@ import ( var ( noXMLHeader = flag.Bool("no-xml-header", false, "do not print xml header") packageName = flag.String("package-name", "", "specify a package name (compiled test have no package name in output)") + packagePrefix = flag.String("package-prefix", "", "specify a package prefix that can be used to namespace a test suite") goVersionFlag = flag.String("go-version", "", "specify the value to use for the go.version property in the generated XML") setExitCode = flag.Bool("set-exit-code", false, "set exit code to 1 if tests failed") ) @@ -32,6 +33,11 @@ func main() { os.Exit(1) } + // Replace report with a version that has prefixed package names before formatting + if *packagePrefix != "" { + report = report.PrefixPackage(*packagePrefix) + } + // Write xml err = formatter.JUnitReportXML(report, *noXMLHeader, *goVersionFlag, os.Stdout) if err != nil { diff --git a/go-junit-report_test.go b/go-junit-report_test.go index b92183d2..16a65cae 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -19,11 +19,12 @@ import ( var matchTest = flag.String("match", "", "only test testdata matching this pattern") type TestCase struct { - name string - reportName string - report *parser.Report - noXMLHeader bool - packageName string + name string + reportName string + report *parser.Report + noXMLHeader bool + packageName string + packagePrefix string } var testCases = []TestCase{ @@ -1535,6 +1536,29 @@ var testCases = []TestCase{ }, }, }, + { + name: "33-package-prefix.txt", + reportName: "33-report.xml", + report: &parser.Report{ + Packages: []parser.Package{ + { + Name: "pkg/pass", + Duration: 5 * time.Millisecond, + Time: 5, + Tests: []*parser.Test{ + { + Name: "TestOne", + Duration: 0, + Time: 0, + Result: parser.PASS, + Output: []string{}, + }, + }, + }, + }, + }, + packagePrefix: "linux/arm64/", + }, } func TestParser(t *testing.T) { @@ -1664,7 +1688,12 @@ func testJUnitFormatter(t *testing.T, goVersion string) { var junitReport bytes.Buffer - if err = formatter.JUnitReportXML(testCase.report, testCase.noXMLHeader, goVersion, &junitReport); err != nil { + testCaseReport := testCase.report + if testCase.packagePrefix != "" { + testCaseReport = testCase.report.PrefixPackage(testCase.packagePrefix) + } + + if err = formatter.JUnitReportXML(testCaseReport, testCase.noXMLHeader, goVersion, &junitReport); err != nil { t.Fatal(err) } diff --git a/parser/parser.go b/parser/parser.go index e268128a..db3ebdab 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -317,3 +317,15 @@ func (r *Report) Failures() int { return count } + +// PrefixPackage returns a copy of the given report, with the given prefix prepended to all package names. +func (r *Report) PrefixPackage(prefix string) *Report { + reportCopy := Report{ + Packages: make([]Package, len(r.Packages)), + } + for i, pkg := range r.Packages { + pkg.Name = prefix + pkg.Name + reportCopy.Packages[i] = pkg + } + return &reportCopy +} diff --git a/parser/parser_test.go b/parser/parser_test.go index 0daa7471..383d2884 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -1,6 +1,7 @@ package parser import ( + "strings" "testing" "time" ) @@ -45,3 +46,25 @@ func TestParseNanoseconds(t *testing.T) { } } } + +func TestReportPrefixPackage(t *testing.T) { + const prefix = "linux/amd64/" + originalReport := &Report{ + Packages: []Package{ + {Name: "pkg/a"}, + {Name: "pkg/b"}, + {Name: "pkg/c"}, + }, + } + + prefixedReport := originalReport.PrefixPackage(prefix) + + for i, pkg := range originalReport.Packages { + if strings.HasPrefix(pkg.Name, prefix) { + t.Errorf("expecting original report package %q not to be modified", pkg.Name) + } + if !strings.HasPrefix(prefixedReport.Packages[i].Name, prefix) { + t.Errorf("expecting prefixed report package %q to be prefixed with %q", pkg.Name, prefix) + } + } +} diff --git a/testdata/33-package-prefix.txt b/testdata/33-package-prefix.txt new file mode 100644 index 00000000..ae543535 --- /dev/null +++ b/testdata/33-package-prefix.txt @@ -0,0 +1,4 @@ +=== RUN TestOne +--- PASS: TestOne (0.00s) +PASS +ok pkg/pass 0.005s diff --git a/testdata/33-report.xml b/testdata/33-report.xml new file mode 100644 index 00000000..aabed582 --- /dev/null +++ b/testdata/33-report.xml @@ -0,0 +1,9 @@ + + + + + + + + +