diff --git a/go-junit-report.go b/go-junit-report.go index 309c964..117ab5e 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -1,11 +1,20 @@ package main import ( + "flag" "fmt" "os" ) +var noXmlHeader bool + +func init() { + flag.BoolVar(&noXmlHeader, "no-xml-header", false, "do not print xml header") +} + func main() { + flag.Parse() + // Read input report, err := Parse(os.Stdin) if err != nil { @@ -14,7 +23,7 @@ func main() { } // Write xml - err = JUnitReportXML(report, os.Stdout) + err = JUnitReportXML(report, noXmlHeader, os.Stdout) if err != nil { fmt.Printf("Error writing XML: %s\n", err) os.Exit(1) diff --git a/go-junit-report_test.go b/go-junit-report_test.go index 853d614..c268f0a 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -11,9 +11,10 @@ import ( ) type TestCase struct { - name string - reportName string - report *Report + name string + reportName string + report *Report + noXmlHeader bool } var testCases []TestCase = []TestCase{ @@ -128,6 +129,33 @@ var testCases []TestCase = []TestCase{ }, }, }, + { + name: "05-no_xml_header.txt", + reportName: "05-report.xml", + report: &Report{ + Packages: []Package{ + { + Name: "package/name", + Time: 160, + Tests: []Test{ + { + Name: "TestOne", + Time: 60, + Result: PASS, + Output: []string{}, + }, + { + Name: "TestTwo", + Time: 100, + Result: PASS, + Output: []string{}, + }, + }, + }, + }, + }, + noXmlHeader: true, + }, } func TestParser(t *testing.T) { @@ -200,7 +228,7 @@ func TestJUnitFormatter(t *testing.T) { var junitReport bytes.Buffer - if err = JUnitReportXML(testCase.report, &junitReport); err != nil { + if err = JUnitReportXML(testCase.report, testCase.noXmlHeader, &junitReport); err != nil { t.Fatal(err) } diff --git a/junit-formatter.go b/junit-formatter.go index 0b58f31..2f1f887 100644 --- a/junit-formatter.go +++ b/junit-formatter.go @@ -52,7 +52,7 @@ func NewJUnitProperty(name, value string) JUnitProperty { // JUnitReportXML writes a junit xml representation of the given report to w // in the format described at http://windyroad.org/dl/Open%20Source/JUnit.xsd -func JUnitReportXML(report *Report, w io.Writer) error { +func JUnitReportXML(report *Report, noXmlHeader bool, w io.Writer) error { suites := []JUnitTestSuite{} // convert Report to JUnit test suites @@ -111,7 +111,9 @@ func JUnitReportXML(report *Report, w io.Writer) error { writer := bufio.NewWriter(w) - writer.WriteString(xml.Header) + if !noXmlHeader { + writer.WriteString(xml.Header) + } writer.Write(bytes) writer.WriteByte('\n') writer.Flush() diff --git a/tests/05-no_xml_header.txt b/tests/05-no_xml_header.txt new file mode 100644 index 0000000..13fe892 --- /dev/null +++ b/tests/05-no_xml_header.txt @@ -0,0 +1,6 @@ +=== RUN TestOne +--- PASS: TestOne (0.06s) +=== RUN TestTwo +--- PASS: TestTwo (0.10s) +PASS +ok package/name 0.160s \ No newline at end of file diff --git a/tests/05-report.xml b/tests/05-report.xml new file mode 100644 index 0000000..5a372aa --- /dev/null +++ b/tests/05-report.xml @@ -0,0 +1,7 @@ + + + + + + +