-
Notifications
You must be signed in to change notification settings - Fork 113
/
diagnose.go
71 lines (60 loc) · 1.85 KB
/
diagnose.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
package diagnostics
import (
"sync"
)
// RegisteredDiagnostics are a list of diagnostics that are registered
var RegisteredDiagnostics []Diagnostic
var ensureArray sync.Once
var mutex sync.Mutex
// Diagnostic is a structure that describes a test we can run. It will
// first get a chance to generate data in the Generate function. Then,
// it will have chance do any verifications in Verify. Be sure to use
// the testify framework there. The last step it will get to perform
// is any cleanup in Cleanup.
// All steps are optional, only name is required
type Diagnostic struct {
Name string
Tags Tags
Generate func(TestContext) error
Verify func(VerificationTestContext)
Cleanup func(TestContext) error
// Skip allows skipping diagnostics based on some condition
Skip func(TestContext) (bool, string, error)
}
// MatchesFilters determines if a set of TagFilters matches the diagnostic
// negations are applied in such a way that they are AND'd together
// nonnegations are OR'd together
func (d Diagnostic) MatchesFilters(tagFilters []TagFilter) bool {
// This is quite inefficient. If we ever write enough tests, we
// can write it better.
matches := true
for _, filter := range tagFilters {
if filter.IsNegate() {
continue
}
if Tag(d.Name) == filter.Tag() || d.Tags.Matches(filter.Tag()) {
matches = true
break
} else {
matches = false
}
}
for _, filter := range tagFilters {
if filter.IsNegate() {
if Tag(d.Name) == filter.Tag() || d.Tags.Matches(filter.Tag()) {
matches = false
break
}
}
}
return matches
}
// RegisterDiagnostic registers a diagnostic into the global list of diagnostics
func RegisterDiagnostic(diagnostic Diagnostic) {
ensureArray.Do(func() {
RegisteredDiagnostics = []Diagnostic{}
})
mutex.Lock()
defer mutex.Unlock()
RegisteredDiagnostics = append(RegisteredDiagnostics, diagnostic)
}