forked from qiniu/reviewbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_test.go
212 lines (204 loc) · 4.97 KB
/
agent_test.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
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 linters
import (
"context"
"fmt"
"reflect"
"regexp"
"testing"
"github.com/qiniu/reviewbot/config"
)
func TestApplyTypedMessageByIssueReferences(t *testing.T) {
// Mock issue reference pattern
pattern := regexp.MustCompile(`#(\d+)`)
pattern2 := regexp.MustCompile(`#ABC`)
testCases := []struct {
name string
reportFormat config.ReportType
lintResults map[string][]LinterOutput
issueRefs []config.CompiledIssueReference
expectedOutput map[string][]LinterOutput
}{
{
name: "GithubCheckRuns format",
reportFormat: config.GitHubCheckRuns,
lintResults: map[string][]LinterOutput{
"file1.go": {
{
Message: "variable naming issue #123",
Line: 10,
},
},
},
issueRefs: []config.CompiledIssueReference{
{
Pattern: pattern,
URL: "https://github.com/qiniu/reviewbot/issues/wrongissue",
IssueNumber: 123,
},
},
expectedOutput: map[string][]LinterOutput{
"file1.go": {
{
Message: "variable naming issue #123",
TypedMessage: "variable naming issue #123\nmore info: https://github.com/qiniu/reviewbot/issues/wrongissue",
Line: 10,
},
},
},
},
{
name: "GithubPRReview format",
reportFormat: config.GitHubPRReview,
lintResults: map[string][]LinterOutput{
"file2.go": {
{
Message: "function complexity issue #456",
Line: 20,
},
},
},
issueRefs: []config.CompiledIssueReference{
{
Pattern: pattern,
URL: "https://github.com/qiniu/reviewbot/issues/wrongissue",
IssueNumber: 0,
},
},
expectedOutput: map[string][]LinterOutput{
"file2.go": {
{
Message: "function complexity issue #456",
TypedMessage: "[function complexity issue #456](https://github.com/qiniu/reviewbot/issues/wrongissue)",
Line: 20,
},
},
},
},
{
name: "GithubMixType format",
reportFormat: config.GitHubMixType,
lintResults: map[string][]LinterOutput{
"file3.go": {
{
Message: "code style issue #789",
Line: 30,
},
},
},
issueRefs: []config.CompiledIssueReference{
{
Pattern: pattern,
URL: "https://github.com/qiniu/reviewbot/issues/wrongissue",
IssueNumber: 0,
},
},
expectedOutput: map[string][]LinterOutput{
"file3.go": {
{
Message: "code style issue #789",
TypedMessage: "[code style issue #789](https://github.com/qiniu/reviewbot/issues/wrongissue)",
Line: 30,
},
},
},
},
{
name: "No matching issue reference",
reportFormat: config.GitHubCheckRuns,
lintResults: map[string][]LinterOutput{
"file4.go": {
{
Message: "regular lint message without issue reference",
Line: 40,
},
},
},
issueRefs: []config.CompiledIssueReference{
{
Pattern: pattern,
URL: "https://github.com/qiniu/reviewbot/issues/999",
IssueNumber: 0,
},
},
expectedOutput: map[string][]LinterOutput{
"file4.go": {
{
Message: "regular lint message without issue reference" + fmt.Sprintf(ReferenceFooter, ""),
Line: 40,
},
},
},
},
{
name: "Multiple files and issues",
reportFormat: config.GitHubMixType,
lintResults: map[string][]LinterOutput{
"file5.go": {
{
Message: "issue #111",
Line: 50,
},
{
Message: "issue #ABC",
Line: 51,
},
},
"file6.go": {
{
Message: "no issue reference",
Line: 60,
},
},
},
issueRefs: []config.CompiledIssueReference{
{
Pattern: pattern,
URL: "https://github.com/qiniu/reviewbot/issues/wrongissue",
IssueNumber: 0,
},
{
Pattern: pattern2,
URL: "https://github.com/qiniu/reviewbot/issues/wrongissue",
IssueNumber: 0,
},
},
expectedOutput: map[string][]LinterOutput{
"file5.go": {
{
Message: "issue #111",
TypedMessage: "[issue #111](https://github.com/qiniu/reviewbot/issues/wrongissue)",
Line: 50,
},
{
Message: "issue #ABC",
TypedMessage: "[issue #ABC](https://github.com/qiniu/reviewbot/issues/wrongissue)",
Line: 51,
},
},
"file6.go": {
{
Message: "no issue reference" + fmt.Sprintf(ReferenceFooter, ""),
Line: 60,
},
},
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create agent with test configuration
agent := &Agent{
LinterConfig: config.Linter{ReportType: tc.reportFormat},
IssueReferences: tc.issueRefs,
}
// Create context
ctx := context.Background()
// Call the method
result := agent.ApplyTypedMessageByIssueReferences(ctx, tc.lintResults)
// Compare results
if !reflect.DeepEqual(result, tc.expectedOutput) {
t.Errorf("Expected %+v, got %+v", tc.expectedOutput, result)
}
})
}
}