Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Package Prefix option for report namespacing #102

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions go-junit-report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand All @@ -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 {
Expand Down
41 changes: 35 additions & 6 deletions go-junit-report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
12 changes: 12 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
23 changes: 23 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -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)
}
}
}
4 changes: 4 additions & 0 deletions testdata/33-package-prefix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
=== RUN TestOne
--- PASS: TestOne (0.00s)
PASS
ok pkg/pass 0.005s
9 changes: 9 additions & 0 deletions testdata/33-report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="1" failures="0" time="0.005" name="linux/arm64/pkg/pass">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
<testcase classname="pass" name="TestOne" time="0.000"></testcase>
</testsuite>
</testsuites>