Fail package in case of build error

In case of build error, the unittest fail, but the junit-report
with the set-exit-code flag on returns status code 0.
This commit will make it return the right status code.
This commit is contained in:
Eyal Posener 2017-01-22 02:12:25 +02:00
parent 6aeed679b6
commit 3c05bc8cb6
5 changed files with 95 additions and 33 deletions

View File

@ -37,7 +37,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if setExitCode && report.Failures() > 0 { if setExitCode && report.Failed() {
os.Exit(1) os.Exit(1)
} }
} }

View File

@ -27,8 +27,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestZ", Name: "TestZ",
@ -53,8 +54,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 151, Time: 151,
Result: parser.FAIL,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -84,8 +86,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 150, Time: 150,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -112,8 +115,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -138,8 +142,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -165,8 +170,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name1", Name: "package/name1",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -183,8 +189,9 @@ var testCases = []TestCase{
}, },
}, },
{ {
Name: "package/name2", Name: "package/name2",
Time: 151, Time: 151,
Result: parser.FAIL,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -215,8 +222,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "test/package", Name: "test/package",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -242,8 +250,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "github.com/dmitris/test-go-junit-report", Name: "github.com/dmitris/test-go-junit-report",
Time: 440, Time: 440,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestDoFoo", Name: "TestDoFoo",
@ -268,8 +277,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 160, Time: 160,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestZ", Name: "TestZ",
@ -295,8 +305,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package1/foo", Name: "package1/foo",
Time: 400, Time: 400,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestA", Name: "TestA",
@ -314,8 +325,9 @@ var testCases = []TestCase{
CoveragePct: "10.0", CoveragePct: "10.0",
}, },
{ {
Name: "package2/bar", Name: "package2/bar",
Time: 4200, Time: 4200,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestC", Name: "TestC",
@ -335,8 +347,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 50, Time: 50,
Result: parser.PASS,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -361,8 +374,9 @@ var testCases = []TestCase{
report: &parser.Report{ report: &parser.Report{
Packages: []parser.Package{ Packages: []parser.Package{
{ {
Name: "package/name", Name: "package/name",
Time: 50, Time: 50,
Result: parser.FAIL,
Tests: []*parser.Test{ Tests: []*parser.Test{
{ {
Name: "TestOne", Name: "TestOne",
@ -487,6 +501,19 @@ var testCases = []TestCase{
}, },
}, },
}, },
{
name: "13-syntax-error.txt",
reportName: "13-report.xml",
report: &parser.Report{
Packages: []parser.Package{
{
Name: "package/name",
Result: parser.FAIL,
Tests: []*parser.Test{},
},
},
},
},
} }
func TestParser(t *testing.T) { func TestParser(t *testing.T) {

View File

@ -29,6 +29,7 @@ type Package struct {
Time int Time int
Tests []*Test Tests []*Test
CoveragePct string CoveragePct string
Result Result
} }
// Test contains the results of a single test. // Test contains the results of a single test.
@ -40,10 +41,11 @@ type Test struct {
} }
var ( var (
regexStatus = regexp.MustCompile(`^\s*--- (PASS|FAIL|SKIP): (.+) \((\d+\.\d+)(?: seconds|s)\)$`) regexStatus = regexp.MustCompile(`^\s*--- (PASS|FAIL|SKIP): (.+) \((\d+\.\d+)(?: seconds|s)\)$`)
regexCoverage = regexp.MustCompile(`^coverage:\s+(\d+\.\d+)%\s+of\s+statements$`) regexCoverage = regexp.MustCompile(`^coverage:\s+(\d+\.\d+)%\s+of\s+statements$`)
regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s(?:\s+coverage:\s+(\d+\.\d+)%\s+of\s+statements)?$`) regexResult = regexp.MustCompile(`^(ok|FAIL)\s+(.+)\s(\d+\.\d+)s(?:\s+coverage:\s+(\d+\.\d+)%\s+of\s+statements)?$`)
regexOutput = regexp.MustCompile(`( )*\t(.*)`) regexBuildFailure = regexp.MustCompile(`^FAIL\s+(.+)\s\[build failed]$`)
regexOutput = regexp.MustCompile(`( )*\t(.*)`)
) )
// Parse parses go test output from reader r and returns a report with the // Parse parses go test output from reader r and returns a report with the
@ -66,6 +68,9 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
// coverage percentage report for current package // coverage percentage report for current package
var coveragePct string var coveragePct string
// whole package test result
packageResult := FAIL
// parse lines // parse lines
for { for {
l, _, err := reader.ReadLine() l, _, err := reader.ReadLine()
@ -88,6 +93,9 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
} else if matches := regexResult.FindStringSubmatch(line); len(matches) == 5 { } else if matches := regexResult.FindStringSubmatch(line); len(matches) == 5 {
if matches[4] != "" { if matches[4] != "" {
coveragePct = matches[4] coveragePct = matches[4]
if matches[0] == "ok" {
packageResult = PASS
}
} }
// all tests in this package are finished // all tests in this package are finished
@ -96,12 +104,19 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
Time: parseTime(matches[3]), Time: parseTime(matches[3]),
Tests: tests, Tests: tests,
CoveragePct: coveragePct, CoveragePct: coveragePct,
Result: packageResult,
}) })
tests = make([]*Test, 0) tests = make([]*Test, 0)
coveragePct = "" coveragePct = ""
cur = "" cur = ""
testsTime = 0 testsTime = 0
} else if matches := regexBuildFailure.FindStringSubmatch(line); len(matches) == 2 {
report.Packages = append(report.Packages, Package{
Name: matches[1],
Result: FAIL,
})
} else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 { } else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 4 {
cur = matches[2] cur = matches[2]
test := findTest(tests, cur) test := findTest(tests, cur)
@ -143,6 +158,7 @@ func Parse(r io.Reader, pkgName string) (*Report, error) {
Time: testsTime, Time: testsTime,
Tests: tests, Tests: tests,
CoveragePct: coveragePct, CoveragePct: coveragePct,
Result: packageResult,
}) })
} }
@ -180,3 +196,12 @@ func (r *Report) Failures() int {
return count return count
} }
func (r *Report) Failed() bool {
for _, p := range r.Packages {
if p.Result == FAIL {
return true
}
}
return false
}

8
tests/13-report.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite tests="0" failures="0" time="0.000" name="package/name">
<properties>
<property name="go.version" value="1.0"></property>
</properties>
</testsuite>
</testsuites>

View File

@ -0,0 +1,2 @@
package/name/file_test.go:9: undefined: x
FAIL package/name [build failed]