-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
tagpr_test.go
352 lines (340 loc) · 7.46 KB
/
tagpr_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package tagpr
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"github.com/google/go-github/v66/github"
)
func TestBuildChunkSearchIssuesQuery(t *testing.T) {
tests := []struct {
queryBase string
shasStr string
want []string
}{
{
"repo:/Songmu/tagpr is:pr is:closed",
"",
nil,
},
{
"repo:/Songmu/tagpr is:pr is:closed",
`
`,
nil,
},
{
"repo:/Songmu/tagpr is:pr is:closed",
`aeed69aa554533dcb4332a1778e4771165a909b5
`,
[]string{"repo:/Songmu/tagpr is:pr is:closed aeed69aa554533dcb4332a1778e4771165a909b5"},
},
{
"repo:Songmu/tagpr is:pr is:closed",
`1a8bb97
1b7691b
a9462b9
4d2b5e9
9ce4268
1eccbf8
1c3fbfc
968ade5
531c782
780bb71
6025fbf
cc369ba
a1f3e39
792bc85
3e3c4e1
37832de
ac97702
d742186
217eb5d
0f900f7
5ef33d1
1d2ec15
2f37752
066ad7b
2e19b14
52b3706
f5134ae
ea39bbf
76b0630
ee3c6e6
2336be4
423a209
63caa74
3296052
3c98d78
86b8739
2264ec5
5c1d87b
4ffe09c
7c5d0de
3de9ed0
1b6b58c
2b643ec
53bf089
e8e96d5
3dac4b0
0605ba4
86cb76d
358c7c1
a139f86
33c16b6
c91f8ff
a109671
b4029bd
f985b4f
b74ef35
53d9ab3
6f57b07
0a84d90
43aa57d
75b6f79
def3db8
c0fc143
`,
[]string{
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14",
"repo:Songmu/tagpr is:pr is:closed 52b3706 f5134ae ea39bbf 76b0630 ee3c6e6 2336be4 423a209 63caa74 3296052 3c98d78 86b8739 2264ec5 5c1d87b 4ffe09c 7c5d0de 3de9ed0 1b6b58c 2b643ec 53bf089 e8e96d5 3dac4b0 0605ba4 86cb76d 358c7c1 a139f86",
"repo:Songmu/tagpr is:pr is:closed 33c16b6 c91f8ff a109671 b4029bd f985b4f b74ef35 53d9ab3 6f57b07 0a84d90 43aa57d 75b6f79 def3db8 c0fc143",
},
},
{
"repo:Songmu/tagpr is:pr is:closed",
`1a8bb97
1b7691b
a9462b9
4d2b5e9
9ce4268
1eccbf8
1c3fbfc
968ade5
531c782
780bb71
6025fbf
cc369ba
a1f3e39
792bc85
3e3c4e1
37832de
ac97702
d742186
217eb5d
0f900f7
5ef33d1
1d2ec15
2f37752
066ad7b
2e19b14
`,
[]string{
"repo:Songmu/tagpr is:pr is:closed 1a8bb97 1b7691b a9462b9 4d2b5e9 9ce4268 1eccbf8 1c3fbfc 968ade5 531c782 780bb71 6025fbf cc369ba a1f3e39 792bc85 3e3c4e1 37832de ac97702 d742186 217eb5d 0f900f7 5ef33d1 1d2ec15 2f37752 066ad7b 2e19b14",
},
},
}
for _, tt := range tests {
got := buildChunkSearchIssuesQuery(tt.queryBase, tt.shasStr)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got:\n%s,\nwant:\n%s", got, tt.want)
}
}
}
func TestGeneratenNextLabels(t *testing.T) {
major := "major"
minor := "minor"
enhancement := "enhancement"
breakingChange := "breaking-change"
tests := map[string]struct {
labels []*github.Label
majorLabels string
minorLabels string
want []string
}{
"onlyMajor": {
[]*github.Label{newGithubLabel(&major)},
"",
"",
[]string{"tagpr:major"},
},
"onlyMinor": {
[]*github.Label{newGithubLabel(&minor)},
"",
"",
[]string{"tagpr:minor"},
},
"other": {
[]*github.Label{newGithubLabel(new(string))},
"",
"",
[]string{},
},
"enhancement": {
[]*github.Label{newGithubLabel(&enhancement)},
"",
"",
[]string{},
},
"breakingChange": {
[]*github.Label{newGithubLabel(&breakingChange)},
"",
"",
[]string{},
},
"empty": {
[]*github.Label{},
"",
"",
[]string{},
},
"majorAndMinor": {
[]*github.Label{newGithubLabel(&major), newGithubLabel(&minor)},
"",
"",
[]string{"tagpr:minor", "tagpr:major"},
},
"minorAndMajor": {
[]*github.Label{newGithubLabel(&minor), newGithubLabel(&major)},
"",
"",
[]string{"tagpr:minor", "tagpr:major"},
},
"Set breakingChange to majorLabels": {
[]*github.Label{newGithubLabel(&breakingChange)},
breakingChange,
"",
[]string{"tagpr:major"},
},
"Set enhancement to minorLabels": {
[]*github.Label{newGithubLabel(&enhancement)},
"",
enhancement,
[]string{"tagpr:minor"},
},
"Include in majorLabels to breakingChange": {
[]*github.Label{newGithubLabel(&breakingChange)},
fmt.Sprintf("other, %s", breakingChange),
"",
[]string{"tagpr:major"},
},
"Include in minorLabels to enhancement": {
[]*github.Label{newGithubLabel(&enhancement)},
"",
fmt.Sprintf(" %s,other", enhancement),
[]string{"tagpr:minor"},
},
"Invalid default label": {
[]*github.Label{newGithubLabel(&major), newGithubLabel(&minor)},
breakingChange,
enhancement,
[]string{},
},
"Include default label": {
[]*github.Label{newGithubLabel(&major), newGithubLabel(&enhancement)},
"major,breaking-change",
"minor,enhancement",
[]string{"tagpr:minor", "tagpr:major"},
},
}
for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
if tt.majorLabels != "" {
t.Setenv(envMajorLabels, tt.majorLabels)
}
if tt.minorLabels != "" {
t.Setenv(envMinorLabels, tt.minorLabels)
}
tp, err := newTagPR(context.Background(), &commander{
gitPath: "git", outStream: os.Stdout, errStream: os.Stderr, dir: "."},
)
if err != nil {
t.Error(err)
}
prIssues := []*github.Issue{
{
ID: new(int64),
Number: new(int),
State: new(string),
Locked: new(bool),
Title: new(string),
Body: new(string),
AuthorAssociation: new(string),
User: &github.User{},
Labels: tt.labels,
Assignee: &github.User{},
Comments: new(int),
ClosedAt: &github.Timestamp{},
CreatedAt: &github.Timestamp{},
UpdatedAt: &github.Timestamp{},
ClosedBy: &github.User{},
URL: new(string),
HTMLURL: new(string),
CommentsURL: new(string),
EventsURL: new(string),
LabelsURL: new(string),
RepositoryURL: new(string),
Milestone: &github.Milestone{},
PullRequestLinks: &github.PullRequestLinks{},
Repository: &github.Repository{},
Reactions: &github.Reactions{},
Assignees: []*github.User{},
NodeID: new(string),
TextMatches: []*github.TextMatch{},
ActiveLockReason: new(string),
},
}
got := tp.generatenNextLabels(prIssues)
if len(got) == 0 && len(tt.want) == 0 {
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got:\n%s,\nwant:\n%s", got, tt.want)
}
})
}
}
func TestLatestSemverTag(t *testing.T) {
vPrefixTrue := true
vPrefixFalse := false
tests := []struct {
name string
vPrefix *bool
wantVer bool
}{
{"github.com/Songmu/tagpr has a semver tag with 'v' prefix", &vPrefixTrue, true},
{"github.com/Songmu/tagpr has no semver tag without 'v' prefix", &vPrefixFalse, false},
{"github.com/Songmu/tagpr returns the first tag when no config exists", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tp, err := newTagPR(context.Background(), &commander{
gitPath: "git", outStream: os.Stdout, errStream: os.Stderr, dir: "."},
)
if err != nil {
t.Error(err)
return
}
tp.cfg = &config{
vPrefix: tt.vPrefix,
}
got := tp.latestSemverTag()
if (got != "") != tt.wantVer {
t.Errorf("got: %s, wantVer: %t", got, tt.wantVer)
}
})
}
}
func newGithubLabel(name *string) *github.Label {
return &github.Label{
ID: new(int64),
URL: new(string),
Name: name,
Color: new(string),
Description: new(string),
Default: new(bool),
NodeID: new(string),
}
}