mirror of
https://github.com/jstemmer/go-junit-report.git
synced 2025-04-05 05:00:15 -05:00
parser/gotest: Parse package coverage
This commit is contained in:
parent
34ffb9d16d
commit
6026e8f15e
@ -13,14 +13,14 @@ import (
|
|||||||
type Event struct {
|
type Event struct {
|
||||||
Type string
|
Type string
|
||||||
|
|
||||||
Id int
|
Id int
|
||||||
Name string
|
Name string
|
||||||
Result string
|
Result string
|
||||||
Duration time.Duration
|
Duration time.Duration
|
||||||
Data string
|
Data string
|
||||||
Indent int
|
Indent int
|
||||||
CovPct float64
|
CovPct float64
|
||||||
Hints map[string]string
|
CovPackages []string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -28,8 +28,8 @@ var (
|
|||||||
regexStatus = regexp.MustCompile(`^(PASS|FAIL|SKIP)$`)
|
regexStatus = regexp.MustCompile(`^(PASS|FAIL|SKIP)$`)
|
||||||
regexSummary = regexp.MustCompile(`^(ok|FAIL)\s+([^ ]+)\s+` +
|
regexSummary = regexp.MustCompile(`^(ok|FAIL)\s+([^ ]+)\s+` +
|
||||||
`(?:(\d+\.\d+)s|\(cached\)|(\[\w+ failed]))` +
|
`(?:(\d+\.\d+)s|\(cached\)|(\[\w+ failed]))` +
|
||||||
`(?:\s+coverage:\s+(\d+\.\d+)%\sof\sstatements(?:\sin\s.+)?)?$`)
|
`(?:\s+coverage:\s+(\d+\.\d+)%\sof\sstatements(?:\sin\s(.+))?)?$`)
|
||||||
regexCoverage = regexp.MustCompile(`^coverage:\s+(\d+|\d+\.\d+)%\s+of\s+statements(?:\sin\s.+)?$`)
|
regexCoverage = regexp.MustCompile(`^coverage:\s+(\d+|\d+\.\d+)%\s+of\s+statements(?:\sin\s(.+))?$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse parses Go test output from the given io.Reader r.
|
// Parse parses Go test output from the given io.Reader r.
|
||||||
@ -61,10 +61,10 @@ func (p *parser) parseLine(line string) {
|
|||||||
p.endTest(matches[1], matches[2], matches[3], matches[4])
|
p.endTest(matches[1], matches[2], matches[3], matches[4])
|
||||||
} else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 2 {
|
} else if matches := regexStatus.FindStringSubmatch(line); len(matches) == 2 {
|
||||||
p.status(matches[1])
|
p.status(matches[1])
|
||||||
} else if matches := regexSummary.FindStringSubmatch(line); len(matches) == 6 {
|
} else if matches := regexSummary.FindStringSubmatch(line); len(matches) == 7 {
|
||||||
p.summary(matches[1], matches[2], matches[3], matches[4], matches[5])
|
p.summary(matches[1], matches[2], matches[3], matches[4], matches[5], matches[6])
|
||||||
} else if matches := regexCoverage.FindStringSubmatch(line); len(matches) == 2 {
|
} else if matches := regexCoverage.FindStringSubmatch(line); len(matches) == 3 {
|
||||||
p.coverage(matches[1])
|
p.coverage(matches[1], matches[2])
|
||||||
} else {
|
} else {
|
||||||
p.output(line)
|
p.output(line)
|
||||||
}
|
}
|
||||||
@ -112,21 +112,23 @@ func (p *parser) status(result string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) summary(result, name, duration, failure, covpct string) {
|
func (p *parser) summary(result, name, duration, failure, covpct, packages string) {
|
||||||
p.add(Event{
|
p.add(Event{
|
||||||
Type: "summary",
|
Type: "summary",
|
||||||
Result: result,
|
Result: result,
|
||||||
Name: name,
|
Name: name,
|
||||||
Duration: parseSeconds(duration),
|
Duration: parseSeconds(duration),
|
||||||
Data: failure,
|
Data: failure,
|
||||||
CovPct: parseCoverage(covpct),
|
CovPct: parseCoverage(covpct),
|
||||||
|
CovPackages: parsePackages(packages),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *parser) coverage(percent string) {
|
func (p *parser) coverage(percent, packages string) {
|
||||||
p.add(Event{
|
p.add(Event{
|
||||||
Type: "coverage",
|
Type: "coverage",
|
||||||
CovPct: parseCoverage(percent),
|
CovPct: parseCoverage(percent),
|
||||||
|
CovPackages: parsePackages(packages),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,6 +157,13 @@ func parseCoverage(percent string) float64 {
|
|||||||
return pct
|
return pct
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parsePackages(pkgList string) []string {
|
||||||
|
if len(pkgList) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return strings.Split(pkgList, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
func stripIndent(line string) (string, int) {
|
func stripIndent(line string) (string, int) {
|
||||||
var indent int
|
var indent int
|
||||||
for indent = 0; strings.HasPrefix(line, " "); indent++ {
|
for indent = 0; strings.HasPrefix(line, " "); indent++ {
|
||||||
|
@ -272,11 +272,29 @@ var tests = []struct {
|
|||||||
{Type: "output", Data: "exit status 1"},
|
{Type: "output", Data: "exit status 1"},
|
||||||
{Type: "summary", Result: "FAIL", Name: "race_test", Duration: 15 * time.Millisecond},
|
{Type: "summary", Result: "FAIL", Name: "race_test", Duration: 15 * time.Millisecond},
|
||||||
}},
|
}},
|
||||||
{"18-coverpkg", []Event{}},
|
{"18-coverpkg",
|
||||||
{"19-pass", []Event{}},
|
[]Event{
|
||||||
{"20-parallel", []Event{}},
|
{Type: "run_test", Id: 1, Name: "TestA"},
|
||||||
{"21-cached", []Event{}},
|
{Type: "end_test", Id: 1, Name: "TestA", Result: "PASS", Duration: 100 * time.Millisecond},
|
||||||
{"22-whitespace", []Event{}},
|
{Type: "run_test", Id: 2, Name: "TestB"},
|
||||||
|
{Type: "end_test", Id: 2, Name: "TestB", Result: "PASS", Duration: 300 * time.Millisecond},
|
||||||
|
{Type: "status", Result: "PASS"},
|
||||||
|
{Type: "coverage", CovPct: 10, CovPackages: []string{"fmt", "encoding/xml"}},
|
||||||
|
{Type: "summary", Result: "ok", Name: "package1/foo", Duration: 400 * time.Millisecond, CovPct: 10, CovPackages: []string{"fmt", "encoding/xml"}},
|
||||||
|
{Type: "run_test", Id: 3, Name: "TestC"},
|
||||||
|
{Type: "end_test", Id: 3, Name: "TestC", Result: "PASS", Duration: 4200 * time.Millisecond},
|
||||||
|
{Type: "status", Result: "PASS"},
|
||||||
|
{Type: "coverage", CovPct: 99.8, CovPackages: []string{"fmt", "encoding/xml"}},
|
||||||
|
{Type: "summary", Result: "ok", Name: "package2/bar", Duration: 4200 * time.Millisecond, CovPct: 99.8, CovPackages: []string{"fmt", "encoding/xml"}},
|
||||||
|
}},
|
||||||
|
{"19-pass",
|
||||||
|
[]Event{}},
|
||||||
|
{"20-parallel",
|
||||||
|
[]Event{}},
|
||||||
|
{"21-cached",
|
||||||
|
[]Event{}},
|
||||||
|
{"22-whitespace",
|
||||||
|
[]Event{}},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user