From 27383b3a74c8be753978aff097f136c440abf4a1 Mon Sep 17 00:00:00 2001 From: Tachikoma Date: Mon, 22 Dec 2014 21:53:08 +0800 Subject: [PATCH 1/2] compiled test's output have no result line, fill "package name" with flag -package-name , and fill "time" with sum of individual tests' time. --- go-junit-report.go | 4 +++- parser.go | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/go-junit-report.go b/go-junit-report.go index 117ab5e..c44e4a3 100644 --- a/go-junit-report.go +++ b/go-junit-report.go @@ -7,16 +7,18 @@ import ( ) var noXmlHeader bool +var packageName string func init() { flag.BoolVar(&noXmlHeader, "no-xml-header", false, "do not print xml header") + flag.StringVar(&packageName, "package-name", "", "specify a package name (compiled test have no package name in output)") } func main() { flag.Parse() // Read input - report, err := Parse(os.Stdin) + report, err := Parse(os.Stdin, packageName) if err != nil { fmt.Printf("Error reading input: %s\n", err) os.Exit(1) diff --git a/parser.go b/parser.go index 3d8213e..87ef254 100644 --- a/parser.go +++ b/parser.go @@ -38,7 +38,7 @@ var ( regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s$`) ) -func Parse(r io.Reader) (*Report, error) { +func Parse(r io.Reader, specifiedPackageName string) (*Report, error) { reader := bufio.NewReader(r) report := &Report{make([]Package, 0)} @@ -46,6 +46,9 @@ func Parse(r io.Reader) (*Report, error) { // keep track of tests we find tests := make([]Test, 0) + // sum of tests' time, use this if current test has no result line (when it is compiled test) + testsTime := 0 + // current test var test *Test @@ -85,6 +88,7 @@ func Parse(r io.Reader) (*Report, error) { }) tests = make([]Test, 0) + testsTime = 0 } else if test != nil { if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 { // test status @@ -97,7 +101,9 @@ func Parse(r io.Reader) (*Report, error) { } test.Name = matches[2] - test.Time = parseTime(matches[3]) * 10 + testTime := parseTime(matches[3]) * 10 + test.Time = testTime + testsTime += testTime } else if strings.HasPrefix(line, "\t") { // test output test.Output = append(test.Output, line[1:]) @@ -105,6 +111,18 @@ func Parse(r io.Reader) (*Report, error) { } } + if test != nil { + tests = append(tests, *test) + } + + if len(tests) > 0 { //no result line found + report.Packages = append(report.Packages, Package{ + Name: specifiedPackageName, + Time: testsTime, + Tests: tests, + }) + } + return report, nil } From 4f0ad507d00945c4cd4c37db814225cc382f4625 Mon Sep 17 00:00:00 2001 From: Tachikoma Date: Mon, 22 Dec 2014 21:53:24 +0800 Subject: [PATCH 2/2] add test for compiled test --- go-junit-report_test.go | 31 ++++++++++++++++++++++++++++++- tests/07-compiled_test.txt | 5 +++++ tests/07-report.xml | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/07-compiled_test.txt create mode 100644 tests/07-report.xml diff --git a/go-junit-report_test.go b/go-junit-report_test.go index c268f0a..d1817be 100644 --- a/go-junit-report_test.go +++ b/go-junit-report_test.go @@ -15,6 +15,7 @@ type TestCase struct { reportName string report *Report noXmlHeader bool + packageName string } var testCases []TestCase = []TestCase{ @@ -156,6 +157,34 @@ var testCases []TestCase = []TestCase{ }, noXmlHeader: true, }, + + { + name: "07-compiled_test.txt", + reportName: "07-report.xml", + report: &Report{ + Packages: []Package{ + { + Name: "test/package", + Time: 160, + Tests: []Test{ + { + Name: "TestOne", + Time: 60, + Result: PASS, + Output: []string{}, + }, + { + Name: "TestTwo", + Time: 100, + Result: PASS, + Output: []string{}, + }, + }, + }, + }, + }, + packageName: "test/package", + }, } func TestParser(t *testing.T) { @@ -165,7 +194,7 @@ func TestParser(t *testing.T) { t.Fatal(err) } - report, err := Parse(file) + report, err := Parse(file, testCase.packageName) if err != nil { t.Fatalf("error parsing: %s", err) } diff --git a/tests/07-compiled_test.txt b/tests/07-compiled_test.txt new file mode 100644 index 0000000..dab2759 --- /dev/null +++ b/tests/07-compiled_test.txt @@ -0,0 +1,5 @@ +=== RUN TestOne +--- PASS: TestOne (0.06s) +=== RUN TestTwo +--- PASS: TestTwo (0.10s) +PASS \ No newline at end of file diff --git a/tests/07-report.xml b/tests/07-report.xml new file mode 100644 index 0000000..af2fa08 --- /dev/null +++ b/tests/07-report.xml @@ -0,0 +1,8 @@ + + + + + + + +