forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testcollector.go
99 lines (82 loc) · 1.8 KB
/
testcollector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json2test
import (
"fmt"
"log"
"sync"
)
type TestState int
const (
StateSkip TestState = iota
StateFail
StatePass
StatePaused
StateRunning
)
var actionToState = map[Action]TestState{
Skip: StateSkip,
Fail: StateFail,
Pass: StatePass,
Pause: StatePaused,
Run: StateRunning,
Continue: StateRunning,
}
type TestKind int
const (
KindTest TestKind = iota
KindBenchmark
)
// TestResult is an individual tests' outcome.
type TestResult struct {
Kind TestKind
State TestState
FullOutput string
}
type TestCollector struct {
mu sync.Mutex
// Package collects all output for a particular package.
Packages map[string]string
// Tests are indexed by fully-qualified packageName.TestName strings.
Tests map[string]*TestResult
}
// NewTestCollector returns a Handler that collects test results.
func NewTestCollector() *TestCollector {
return &TestCollector{
Packages: make(map[string]string),
Tests: make(map[string]*TestResult),
}
}
// Handle implements Handler.
func (tc *TestCollector) Handle(e TestEvent) {
tc.mu.Lock()
defer tc.mu.Unlock()
if _, ok := tc.Packages[e.Package]; !ok {
tc.Packages[e.Package] = ""
}
tc.Packages[e.Package] += e.Output
if len(e.Test) == 0 {
return
}
testName := fmt.Sprintf("%s.%s", e.Package, e.Test)
t, ok := tc.Tests[testName]
if !ok {
t = &TestResult{
Kind: KindTest,
}
tc.Tests[testName] = t
}
switch e.Action {
case Benchmark:
t.Kind = KindBenchmark
case Output:
default:
s, ok := actionToState[e.Action]
if !ok {
log.Printf("Unknown action %q in event %v", e.Action, e)
}
t.State = s
}
t.FullOutput = t.FullOutput + e.Output
}