gtr: Append output to tests

This commit is contained in:
Joël Stemmer 2018-04-28 19:38:05 +01:00
parent cc15e48982
commit bdaa9a9626
2 changed files with 45 additions and 2 deletions

View File

@ -41,6 +41,7 @@ type Package struct {
Name string Name string
Duration time.Duration Duration time.Duration
Coverage float64 Coverage float64
Output []string
Tests []Test Tests []Test
} }
@ -60,10 +61,12 @@ func FromEvents(events []gotest.Event) Report {
case "run_test": case "run_test":
report.CreateTest(ev.Id, ev.Name) report.CreateTest(ev.Id, ev.Name)
case "end_test": case "end_test":
report.UpdateTest(ev.Id, ev.Result, ev.Duration) report.EndTest(ev.Id, ev.Result, ev.Duration)
case "status": // ignore for now case "status": // ignore for now
case "summary": case "summary":
report.CreatePackage(ev.Name, ev.Duration) report.CreatePackage(ev.Name, ev.Duration)
case "output":
report.AppendOutput(ev.Data)
default: default:
fmt.Printf("unhandled event type: %v\n", ev.Type) fmt.Printf("unhandled event type: %v\n", ev.Type)
} }
@ -76,6 +79,10 @@ type ReportBuilder struct {
packages []Package packages []Package
ids []int ids []int
tests map[int]Test tests map[int]Test
// state
output []string
last int // last test id // TODO: stack?
} }
func NewReportBuilder() *ReportBuilder { func NewReportBuilder() *ReportBuilder {
@ -98,13 +105,17 @@ func (b *ReportBuilder) Build() Report {
func (b *ReportBuilder) CreateTest(id int, name string) { func (b *ReportBuilder) CreateTest(id int, name string) {
b.ids = append(b.ids, id) b.ids = append(b.ids, id)
b.tests[id] = Test{Name: name} b.tests[id] = Test{Name: name}
b.last = id
} }
func (b *ReportBuilder) UpdateTest(id int, result string, duration time.Duration) { func (b *ReportBuilder) EndTest(id int, result string, duration time.Duration) {
t := b.tests[id] t := b.tests[id]
t.Result = parseResult(result) t.Result = parseResult(result)
t.Duration = duration t.Duration = duration
b.tests[id] = t b.tests[id] = t
b.last = id
} }
func (b *ReportBuilder) CreatePackage(name string, duration time.Duration) { func (b *ReportBuilder) CreatePackage(name string, duration time.Duration) {
@ -116,9 +127,22 @@ func (b *ReportBuilder) CreatePackage(name string, duration time.Duration) {
Name: name, Name: name,
Duration: duration, Duration: duration,
Tests: tests, Tests: tests,
Output: b.output,
}) })
b.ids = nil b.ids = nil
b.tests = make(map[int]Test) b.tests = make(map[int]Test)
b.output = nil
}
func (b *ReportBuilder) AppendOutput(line string) {
if b.last <= 0 {
b.output = append(b.output, line)
return
}
t := b.tests[b.last]
t.Output = append(t.Output, line)
b.tests[b.last] = t
} }
func parseResult(r string) Result { func parseResult(r string) Result {

View File

@ -15,6 +15,11 @@ func TestFromEvents(t *testing.T) {
{Type: "end_test", Id: 1, Name: "TestOne", Result: "PASS", Duration: 1 * time.Millisecond}, {Type: "end_test", Id: 1, Name: "TestOne", Result: "PASS", Duration: 1 * time.Millisecond},
{Type: "status", Result: "PASS"}, {Type: "status", Result: "PASS"},
{Type: "summary", Result: "ok", Name: "package/name", Duration: 1 * time.Millisecond}, {Type: "summary", Result: "ok", Name: "package/name", Duration: 1 * time.Millisecond},
{Type: "run_test", Id: 2, Name: "TestOne"},
{Type: "output", Data: "\tfile_test.go:10: error"},
{Type: "end_test", Id: 2, Name: "TestOne", Result: "FAIL", Duration: 1 * time.Millisecond},
{Type: "status", Result: "FAIL"},
{Type: "summary", Result: "FAIL", Name: "package/name2", Duration: 1 * time.Millisecond},
} }
expected := Report{ expected := Report{
Packages: []Package{ Packages: []Package{
@ -29,6 +34,20 @@ func TestFromEvents(t *testing.T) {
}, },
}, },
}, },
{
Name: "package/name2",
Duration: 1 * time.Millisecond,
Tests: []Test{
{
Name: "TestOne",
Duration: 1 * time.Millisecond,
Result: FAIL,
Output: []string{
"\tfile_test.go:10: error",
},
},
},
},
}, },
} }