From d72fb56d06512ebf07d42c7be6798c981285d916 Mon Sep 17 00:00:00 2001 From: Joel Stemmer Date: Fri, 16 Mar 2012 15:39:57 +0100 Subject: [PATCH] Add properties to xml report Include the go runtime version as a property. --- go-junit-report_test.go | 24 +++++++++++++++++++----- junit-formatter.go | 40 +++++++++++++++++++++++++++++----------- tests/01-report.xml | 3 +++ tests/02-report.xml | 3 +++ 4 files changed, 54 insertions(+), 16 deletions(-) diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 886b379..75e43cb 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -2,8 +2,11 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os" + "runtime" + "strings" "testing" ) @@ -129,20 +132,31 @@ func TestParser(t *testing.T) { func TestJUnitFormatter(t *testing.T) { for _, testCase := range testCases { - file, err := ioutil.ReadFile("tests/" + testCase.reportName) + report, err := loadTestReport(testCase.reportName) if err != nil { t.Fatal(err) } var junitReport bytes.Buffer - err = JUnitReportXML(testCase.report, &junitReport) - if err != nil { + if err = JUnitReportXML(testCase.report, &junitReport); err != nil { t.Fatal(err) } - if string(junitReport.Bytes()) != string(file) { - t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), string(file)) + if string(junitReport.Bytes()) != report { + t.Fatalf("Report xml ==\n%s, want\n%s", string(junitReport.Bytes()), report) } } } + +func loadTestReport(name string) (string, error) { + contents, err := ioutil.ReadFile("tests/" + name) + if err != nil { + return "", err + } + + // replace value="1.0" With actual version + report := strings.Replace(string(contents), `value="1.0"`, fmt.Sprintf(`value="%s"`, runtime.Version()), 1) + + return report, nil +} diff --git a/junit-formatter.go b/junit-formatter.go index 94e2810..af43dee 100644 --- a/junit-formatter.go +++ b/junit-formatter.go @@ -5,16 +5,18 @@ import ( "encoding/xml" "fmt" "io" + "runtime" "strings" ) type JUnitTestSuite struct { - XMLName xml.Name `xml:"testsuite"` - Tests int `xml:"tests,attr"` - Failures int `xml:"failures,attr"` - Time string `xml:"time,attr"` - Name string `xml:"name,attr"` - TestCases []JUnitTestCase + XMLName xml.Name `xml:"testsuite"` + Tests int `xml:"tests,attr"` + Failures int `xml:"failures,attr"` + Time string `xml:"time,attr"` + Name string `xml:"name,attr"` + Properties []JUnitProperty `xml:"properties>property,omitempty"` + TestCases []JUnitTestCase } type JUnitTestCase struct { @@ -25,17 +27,30 @@ type JUnitTestCase struct { Failure string `xml:"failure,omitempty"` } +type JUnitProperty struct { + Name string `xml:"name,attr"` + Value string `xml:"value,attr"` +} + +func NewJUnitProperty(name, value string) JUnitProperty { + return JUnitProperty{ + Name: name, + Value: value, + } +} + func JUnitReportXML(report *Report, w io.Writer) error { suites := []JUnitTestSuite{} // convert Report to JUnit test suites for _, pkg := range report.Packages { ts := JUnitTestSuite{ - Tests: len(pkg.Tests), - Failures: 0, - Time: formatTime(pkg.Time), - Name: pkg.Name, - TestCases: []JUnitTestCase{}, + Tests: len(pkg.Tests), + Failures: 0, + Time: formatTime(pkg.Time), + Name: pkg.Name, + Properties: []JUnitProperty{}, + TestCases: []JUnitTestCase{}, } classname := pkg.Name @@ -43,6 +58,9 @@ func JUnitReportXML(report *Report, w io.Writer) error { classname = pkg.Name[idx+1:] } + // properties + ts.Properties = append(ts.Properties, NewJUnitProperty("go.version", runtime.Version())) + // individual test cases for _, test := range pkg.Tests { testCase := JUnitTestCase{ diff --git a/tests/01-report.xml b/tests/01-report.xml index 2729148..2585b80 100644 --- a/tests/01-report.xml +++ b/tests/01-report.xml @@ -1,5 +1,8 @@ + + + diff --git a/tests/02-report.xml b/tests/02-report.xml index 1dada47..151b605 100644 --- a/tests/02-report.xml +++ b/tests/02-report.xml @@ -1,5 +1,8 @@ + + +