-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.go
More file actions
212 lines (187 loc) · 5.69 KB
/
eval.go
File metadata and controls
212 lines (187 loc) · 5.69 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package sight
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
)
// EvalCase defines a single test case for evaluating review quality.
type EvalCase struct {
Name string `json:"name"`
Diff string `json:"diff"`
ExpectFindings []EvalExpectation `json:"expect_findings"`
DenyFindings []EvalDenial `json:"deny_findings"`
}
// EvalExpectation defines what we expect the reviewer to find.
type EvalExpectation struct {
Concern string `json:"concern,omitempty"`
MinSeverity string `json:"min_severity,omitempty"`
MessageContains string `json:"message_contains,omitempty"`
File string `json:"file,omitempty"`
}
// EvalDenial defines what the reviewer should NOT report (false positive check).
type EvalDenial struct {
MessageContains string `json:"message_contains,omitempty"`
Concern string `json:"concern,omitempty"`
}
// EvalResult is the outcome of running one eval case.
type EvalResult struct {
Case string `json:"case"`
Passed bool `json:"passed"`
Failures []string `json:"failures,omitempty"`
Findings []Finding `json:"findings"`
}
// EvalSuite is a collection of eval cases.
type EvalSuite struct {
Cases []EvalCase `json:"cases"`
}
// RunEval executes an evaluation suite against the reviewer with the given options.
// For each case it runs Review() on the diff, then checks expectations and denials.
func RunEval(ctx context.Context, suite *EvalSuite, opts ...Option) ([]EvalResult, error) {
if suite == nil || len(suite.Cases) == 0 {
return nil, nil
}
results := make([]EvalResult, 0, len(suite.Cases))
for _, ec := range suite.Cases {
if ctx.Err() != nil {
return results, ctx.Err()
}
er := EvalResult{Case: ec.Name}
reviewResult, err := Review(ctx, ec.Diff, opts...)
if err != nil {
er.Failures = append(er.Failures, fmt.Sprintf("review error: %v", err))
results = append(results, er)
continue
}
er.Findings = reviewResult.Findings
// Check expectations: each must be matched by at least one finding.
for i, exp := range ec.ExpectFindings {
if !matchExpectation(exp, reviewResult.Findings) {
er.Failures = append(er.Failures,
fmt.Sprintf("expect_findings[%d]: no finding matched %s", i, describeExpectation(exp)))
}
}
// Check denials: none should be matched by any finding.
for i, deny := range ec.DenyFindings {
if matchDenial(deny, reviewResult.Findings) {
er.Failures = append(er.Failures,
fmt.Sprintf("deny_findings[%d]: found unexpected match for %s", i, describeDenial(deny)))
}
}
er.Passed = len(er.Failures) == 0
results = append(results, er)
}
return results, nil
}
// LoadEvalSuite loads eval cases from a JSON file.
func LoadEvalSuite(path string) (*EvalSuite, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("loading eval suite: %w", err)
}
return ParseEvalSuite(data)
}
// ParseEvalSuite parses eval cases from JSON bytes.
func ParseEvalSuite(data []byte) (*EvalSuite, error) {
var suite EvalSuite
if err := json.Unmarshal(data, &suite); err != nil {
return nil, fmt.Errorf("parsing eval suite: %w", err)
}
return &suite, nil
}
// EvalSummary returns pass/fail counts and overall success rate.
func EvalSummary(results []EvalResult) (passed, failed int, rate float64) {
for _, r := range results {
if r.Passed {
passed++
} else {
failed++
}
}
total := passed + failed
if total > 0 {
rate = float64(passed) / float64(total)
}
return
}
// matchExpectation returns true if at least one finding matches all non-empty
// fields in the expectation.
func matchExpectation(exp EvalExpectation, findings []Finding) bool {
for _, f := range findings {
if matchesSingleExpectation(exp, f) {
return true
}
}
return false
}
func matchesSingleExpectation(exp EvalExpectation, f Finding) bool {
if exp.Concern != "" && !strings.EqualFold(f.Concern, exp.Concern) {
return false
}
if exp.MinSeverity != "" {
minSev := ParseSeverity(exp.MinSeverity)
if !f.Severity.AtLeast(minSev) {
return false
}
}
if exp.MessageContains != "" && !strings.Contains(
strings.ToLower(f.Message), strings.ToLower(exp.MessageContains)) {
return false
}
if exp.File != "" && !strings.EqualFold(f.File, exp.File) {
return false
}
return true
}
// matchDenial returns true if any finding matches all non-empty fields in the denial.
func matchDenial(deny EvalDenial, findings []Finding) bool {
for _, f := range findings {
if matchesSingleDenial(deny, f) {
return true
}
}
return false
}
func matchesSingleDenial(deny EvalDenial, f Finding) bool {
if deny.Concern != "" && !strings.EqualFold(f.Concern, deny.Concern) {
return false
}
if deny.MessageContains != "" && !strings.Contains(
strings.ToLower(f.Message), strings.ToLower(deny.MessageContains)) {
return false
}
return true
}
func describeExpectation(exp EvalExpectation) string {
var parts []string
if exp.Concern != "" {
parts = append(parts, fmt.Sprintf("concern=%q", exp.Concern))
}
if exp.MinSeverity != "" {
parts = append(parts, fmt.Sprintf("min_severity=%q", exp.MinSeverity))
}
if exp.MessageContains != "" {
parts = append(parts, fmt.Sprintf("message_contains=%q", exp.MessageContains))
}
if exp.File != "" {
parts = append(parts, fmt.Sprintf("file=%q", exp.File))
}
if len(parts) == 0 {
return "{}"
}
return "{" + strings.Join(parts, ", ") + "}"
}
func describeDenial(deny EvalDenial) string {
var parts []string
if deny.Concern != "" {
parts = append(parts, fmt.Sprintf("concern=%q", deny.Concern))
}
if deny.MessageContains != "" {
parts = append(parts, fmt.Sprintf("message_contains=%q", deny.MessageContains))
}
if len(parts) == 0 {
return "{}"
}
return "{" + strings.Join(parts, ", ") + "}"
}