1. Miki Tebeka
  2. go2xunit

Commits

Miki Tebeka  committed f1b7efd

bamboo support (merge from github)

  • Participants
  • Parent commits c865f85
  • Branches default

Comments (0)

Files changed (2)

File ChangeLog

View file
+2013-06-18 version 0.1.2
+    * testcases node added for bamboo compatibility, some dummy change. 
+    * xml generation now use template engine
+
 2012-07-23 version 0.1.1
     * Fix edge case with FAIL (thanks Thomas Pelletier)
 

File go2xunit.go

View file
 	"os"
 	"regexp"
 	"strings"
+	"text/template"
 )
 
 const (
 	passPrefix  = "--- PASS: "
 	failPrefix  = "--- FAIL: "
 
-	version = "0.1.1"
+	version = "0.1.2"
 )
 
 // "end of test" regexp for name and time, examples:
 	Failed              bool
 }
 
+type TestResults struct {
+	Tests  []*Test
+	Count  int
+	Failed int
+	Bamboo bool
+}
+
 // parseEnd parses "end of test" line and returns (name, time, error)
 func parseEnd(prefix, line string) (string, string, error) {
 	matches := endRegexp.FindStringSubmatch(line[len(prefix):])
 	return count
 }
 
+var xmlTemplate string = `<?xml version="1.0" encoding="utf-8"?>
+{{if .Bamboo}}<testsuites>{{end}}
+  <testsuite name="go2xunit" tests="{{.Count}}" errors="0" failures="{{.Failed}}" skip="0">
+{{range $test := .Tests}}    <testcase classname="go2xunit" name="{{$test.Name}}" time="{{$test.Time}}">
+{{if $test.Failed }}      <failure type="go.error" message="error">
+        <![CDATA[{{$test.Message}}]]></failure>
+{{end}}    </testcase>
+{{end}}  </testsuite>
+{{if .Bamboo}}</testsuites>{{end}}
+	`
+
 // writeXML exits xunit XML of tests to out
-func writeXML(tests []*Test, out io.Writer) {
-	newline := func() { fmt.Fprintln(out) }
+func writeXML(tests []*Test, out io.Writer, bamboo bool) {
+	count := len(tests)
+	failed := numFailures(tests)
 
-	fmt.Fprintf(out, `<?xml version="1.0" encoding="utf-8"?>`)
-	newline()
-	fmt.Fprintf(out, `<testsuite name="go2xunit" tests="%d" errors="0" failures="%d" skip="0">`,
-		len(tests), numFailures(tests))
-	newline()
-	for _, test := range tests {
-		fmt.Fprintf(out, `  <testcase classname="go2xunit" name="%s" time="%s"`,
-			test.Name, test.Time)
-		if !test.Failed {
-			fmt.Fprintf(out, " />\n")
-			continue
-		}
-		fmt.Fprintln(out, ">")
-		fmt.Fprintf(out, `    <failure type="go.error" message="error">`)
-		newline()
-		fmt.Fprintf(out, "<![CDATA[%s]]>\n", test.Message)
-		fmt.Fprintln(out, "    </failure>")
-		fmt.Fprintln(out, "  </testcase>")
+	testsResult := TestResults{Tests: tests, Count: count, Failed: failed, Bamboo: bamboo}
+	t := template.New("test template")
+	t, err := t.Parse(xmlTemplate)
+	if err != nil {
+		fmt.Println("Error en parse %v", err)
+		return
 	}
-	fmt.Fprintln(out, "</testsuite>")
+	err = t.Execute(out, testsResult)
+	if err != nil {
+		fmt.Println("Error en execute %v", err)
+		return
+	}
 }
 
 // getInput return input io.Reader from file name, if file name is - it will
 	outputFile := flag.String("output", "", "output file (default to stdout)")
 	fail := flag.Bool("fail", false, "fail (non zero exit) if any test failed")
 	showVersion := flag.Bool("version", false, "print version and exit")
+	bamboo := flag.Bool("bamboo", false, "xml compatible with Atlassian's Bamboo")
 	flag.Parse()
 
 	if *showVersion {
 		os.Exit(1)
 	}
 
-	writeXML(tests, output)
+	writeXML(tests, output, *bamboo)
 	if *fail && numFailures(tests) > 0 {
 		os.Exit(1)
 	}