-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_test.go
728 lines (693 loc) · 16.4 KB
/
main_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"
)
func TestMain(t *testing.T) {
os.Unsetenv("GITHUB_ACTIONS")
tests := []struct {
name string
opts options
files map[string]string
changedFiles []string
stdout []string
err string
}{
{
name: "one file",
opts: options{
format: "text",
baseRef: "$baseRef",
headRef: "$headRef",
},
files: map[string]string{
"CODENOTIFY": "**/*.md @markdown",
"file.md": "",
},
changedFiles: []string{
"file.md",
},
stdout: []string{
"$baseRef...$headRef",
"@markdown -> file.md",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gitroot, err := ioutil.TempDir("", "codenotify")
if err != nil {
t.Fatalf("unable to create temporary directory: %s", err)
}
defer os.RemoveAll(gitroot)
if err := os.Chdir(gitroot); err != nil {
t.Fatalf("unable to change working directory to %s: %s", gitroot, err)
}
for file, content := range test.files {
dir := filepath.Dir(file)
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatalf("unable to make directory %s: %s", dir, err)
}
if err := ioutil.WriteFile(file, []byte(content), 0666); err != nil {
t.Fatalf("unable to write file %s: %s", file, err)
}
}
if out, err := exec.Command("git", "init").CombinedOutput(); err != nil {
t.Fatalf("unable to git init: %s\n%s", err, string(out))
}
if out, err := exec.Command("git", "add", ".").CombinedOutput(); err != nil {
t.Fatalf("unable to git add: %s\n%s", err, string(out))
}
if out, err := exec.Command("git", "-c", "user.name=test", "-c", "[email protected]", "commit", "-m", "init").CombinedOutput(); err != nil {
t.Fatalf("unable to make first commit: %s\n%s", err, string(out))
}
br, err := exec.Command("git", "rev-parse", "--short", "HEAD").CombinedOutput()
if err != nil {
t.Fatalf("unable to git rev-parse: %s\n%s", err, string(br))
}
for _, file := range test.changedFiles {
// Easiest way to change a file is to remove it
if out, err := exec.Command("git", "rm", file).CombinedOutput(); err != nil {
t.Fatalf("unable to git rm: %s\n%s", err, string(out))
}
}
if out, err := exec.Command("git", "-c", "user.name=test", "-c", "[email protected]", "commit", "-m", "headRev").CombinedOutput(); err != nil {
t.Fatalf("unable to git commit: %s\n%s", err, string(out))
}
hr, err := exec.Command("git", "rev-parse", "--short", "HEAD").CombinedOutput()
if err != nil {
t.Fatalf("unable to git rev-parse: %s\n%s", err, string(hr))
}
stdout := &bytes.Buffer{}
baseRef := strings.TrimSpace(string(br))
headRef := strings.TrimSpace(string(hr))
err = testableMain(stdout, []string{
"-cwd", gitroot,
"-baseRef", baseRef,
"-headRef", headRef,
"-format", test.opts.format,
})
switch {
case err != nil && test.err == "":
t.Errorf("expected nil error; got %s", err)
case err == nil && test.err != "":
t.Errorf("expected error %q; got nil", test.err)
}
expectedStdout := joinLines(test.stdout)
expectedStdout = strings.ReplaceAll(expectedStdout, "$baseRef", baseRef)
expectedStdout = strings.ReplaceAll(expectedStdout, "$headRef", headRef)
if stdout.String() != expectedStdout {
t.Errorf("want stdout:\n%s\ngot:\n%s", expectedStdout, stdout.String())
}
})
}
}
func TestCliOptions(t *testing.T) {
var originalVerbose io.Writer = verbose
defer func() { verbose = originalVerbose }()
tests := []struct {
name string
args []string
verbose io.Writer
}{
{
name: "no arguments",
args: []string{},
verbose: ioutil.Discard,
},
{
name: "verbose option",
args: []string{"-verbose"},
verbose: os.Stderr,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
cliOptions(stdout, test.args)
if verbose != test.verbose {
t.Errorf("expected verbose to be %v; got %v", test.verbose, verbose)
}
})
}
}
func TestWriteNotifications(t *testing.T) {
tests := []struct {
name string
opts options
notifs map[string][]string
err string
output []string
}{
{
name: "empty markdown",
opts: options{
filename: "CODENOTIFY",
format: "markdown",
baseRef: "a",
headRef: "b",
},
notifs: nil,
output: []string{
"<!-- codenotify:CODENOTIFY report -->",
"[Codenotify](https://github.com/sourcegraph/codenotify): Notifying subscribers in CODENOTIFY files for diff a...b.",
"",
"No notifications.",
},
},
{
name: "empty text",
opts: options{
filename: "CODENOTIFY",
format: "text",
baseRef: "a",
headRef: "b",
},
notifs: nil,
output: []string{
"a...b",
"No notifications.",
},
},
{
name: "markdown",
opts: options{
filename: "CODENOTIFY",
format: "markdown",
baseRef: "a",
headRef: "b",
},
notifs: map[string][]string{
"@go": {"file.go", "dir/file.go"},
"@js": {"file.js", "dir/file.js"},
},
output: []string{
"<!-- codenotify:CODENOTIFY report -->",
"[Codenotify](https://github.com/sourcegraph/codenotify): Notifying subscribers in CODENOTIFY files for diff a...b.",
"",
"| Notify | File(s) |",
"|-|-|",
"| @go | file.go<br>dir/file.go |",
"| @js | file.js<br>dir/file.js |",
},
},
{
name: "text",
opts: options{
filename: "CODENOTIFY",
format: "text",
baseRef: "a",
headRef: "b",
},
notifs: map[string][]string{
"@go": {"file.go", "dir/file.go"},
"@js": {"file.js", "dir/file.js"},
},
output: []string{
"a...b",
"@go -> file.go, dir/file.go",
"@js -> file.js, dir/file.js",
},
},
{
name: "unsupported format",
opts: options{
format: "pdf",
},
notifs: map[string][]string{
"@go": {"file.go", "dir/file.go"},
},
err: "unsupported format: pdf",
},
{
name: "exceeded subscriber threshold",
opts: options{
subscriberThreshold: 1,
},
notifs: map[string][]string{
"@go": {"file.go", "dir/file.go"},
"@js": {"file.js", "dir/file.js"},
},
output: []string{
"Not notifying subscribers because the number of notifying subscribers (2) has exceeded the threshold (1).",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualOutput := bytes.Buffer{}
err := test.opts.writeNotifications(&actualOutput, test.notifs)
switch {
case err != nil && test.err == "":
t.Errorf("expected nil error; got %s", err)
case err == nil && test.err != "":
t.Errorf("expected error %q; got nil", test.err)
}
expectedOutput := joinLines(test.output)
if expectedOutput != actualOutput.String() {
t.Errorf("\nwant: %q\n got: %q", expectedOutput, actualOutput.String())
}
})
}
}
func joinLines(lines []string) string {
joined := strings.Join(lines, "\n")
if joined == "" {
return joined
}
return joined + "\n"
}
func TestNotifications(t *testing.T) {
tests := []struct {
name string
filename string
fs memfs
notifications map[string][]string
}{
{
name: "no notifications",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "nomatch.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: nil,
},
{
name: "file.md",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "file.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"file.md"},
},
},
{
name: "no leading slash",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "/file.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: nil,
},
{
name: "whitespace",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "\n\nfile.md @notify\n\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"file.md"},
},
},
{
name: "comments",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "#comment\n" +
"file.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"file.md"},
},
},
{
name: "*",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "* @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"CODENOTIFY", "file.md"},
},
},
{
name: "dir/*",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "dir/* @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"dir/file.md"},
},
},
{
name: "**",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "** @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"CODENOTIFY", "file.md", "dir/file.md", "dir/dir/file.md"},
},
},
{
name: "**/*", // same as **
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "**/* @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"CODENOTIFY", "file.md", "dir/file.md", "dir/dir/file.md"},
},
},
{
name: "**/file.md",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "**/file.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"file.md", "dir/file.md", "dir/dir/file.md"},
},
},
{
name: "dir/**",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "dir/** @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"dir/file.md", "dir/dir/file.md"},
},
},
{
name: "dir/", // same as "dir/**"
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "dir/ @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"dir/file.md", "dir/dir/file.md"},
},
},
{
name: "dir/**/file.md",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "dir/**/file.md @notify\n",
"file.md": "",
"dirfile.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"dir/file.md", "dir/dir/file.md"},
},
},
{
name: "multiple subscribers",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "* @alice @bob\n",
"file.md": "",
},
notifications: map[string][]string{
"@alice": {"CODENOTIFY", "file.md"},
"@bob": {"CODENOTIFY", "file.md"},
},
},
{
name: "..",
filename: "CODENOTIFY",
fs: memfs{
"dir/CODENOTIFY": "../* @alice @bob\n",
"file.md": "",
},
notifications: nil,
},
{
name: "multiple CODENOTIFY",
filename: "CODENOTIFY",
fs: memfs{
"CODENOTIFY": "\n" +
"* @rootany\n" +
"*.go @rootgo\n" +
"*.js @rootjs\n" +
"**/* @all\n" +
"**/*.go @allgo\n" +
"**/*.js @alljs\n",
"file.md": "",
"file.js": "",
"file.go": "",
"dir/CODENOTIFY": "\n" +
"* @dir/any\n" +
"*.go @dir/go\n" +
"*.js @dir/js\n" +
"**/* @dir/all\n" +
"**/*.go @dir/allgo\n" +
"**/*.js @dir/alljs\n",
"dir/file.md": "",
"dir/file.go": "",
"dir/file.js": "",
"dir/dir/CODENOTIFY": "\n" +
"* @dir/dir/any\n" +
"*.go @dir/dir/go\n" +
"*.js @dir/dir/js\n" +
"**/* @dir/dir/all\n" +
"**/*.go @dir/dir/allgo\n" +
"**/*.js @dir/dir/alljs\n",
"dir/dir/file.md": "",
"dir/dir/file.go": "",
"dir/dir/file.js": "",
},
notifications: map[string][]string{
"@all": {
"CODENOTIFY",
"file.md",
"file.js",
"file.go",
"dir/CODENOTIFY",
"dir/file.md",
"dir/file.go",
"dir/file.js",
"dir/dir/CODENOTIFY",
"dir/dir/file.md",
"dir/dir/file.go",
"dir/dir/file.js",
},
"@allgo": {
"file.go",
"dir/file.go",
"dir/dir/file.go",
},
"@alljs": {
"file.js",
"dir/file.js",
"dir/dir/file.js",
},
"@rootany": {
"CODENOTIFY",
"file.md",
"file.js",
"file.go",
},
"@rootgo": {
"file.go",
},
"@rootjs": {
"file.js",
},
"@dir/all": {
"dir/CODENOTIFY",
"dir/file.md",
"dir/file.go",
"dir/file.js",
"dir/dir/CODENOTIFY",
"dir/dir/file.md",
"dir/dir/file.go",
"dir/dir/file.js",
},
"@dir/allgo": {
"dir/file.go",
"dir/dir/file.go",
},
"@dir/alljs": {
"dir/file.js",
"dir/dir/file.js",
},
"@dir/any": {
"dir/CODENOTIFY",
"dir/file.md",
"dir/file.js",
"dir/file.go",
},
"@dir/go": {
"dir/file.go",
},
"@dir/js": {
"dir/file.js",
},
"@dir/dir/all": {
"dir/dir/CODENOTIFY",
"dir/dir/file.md",
"dir/dir/file.go",
"dir/dir/file.js",
},
"@dir/dir/allgo": {
"dir/dir/file.go",
},
"@dir/dir/alljs": {
"dir/dir/file.js",
},
"@dir/dir/any": {
"dir/dir/CODENOTIFY",
"dir/dir/file.md",
"dir/dir/file.js",
"dir/dir/file.go",
},
"@dir/dir/go": {
"dir/dir/file.go",
},
"@dir/dir/js": {
"dir/dir/file.js",
},
},
},
{
name: "no notifications for OWNERS",
filename: "OWNERS",
fs: memfs{
"CODENOTIFY": "file.md @notify\n",
"OWNERS": "nomatch.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: nil,
},
{
name: "file.md in OWNERS",
filename: "OWNERS",
fs: memfs{
"CODENOTIFY": "nomatch.md @notify\n",
"OWNERS": "file.md @notify\n",
"file.md": "",
"dir/file.md": "",
"dir/dir/file.md": "",
},
notifications: map[string][]string{
"@notify": {"file.md"},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
notifs, err := notifications(test.fs, test.fs.paths(), test.filename)
if err != nil {
t.Errorf("expected nil error; got %s", err)
}
subs := map[string]struct{}{}
for subscriber, actualfiles := range notifs {
subs[subscriber] = struct{}{}
expectedfiles := test.notifications[subscriber]
sort.Strings(expectedfiles)
sort.Strings(actualfiles)
if !reflect.DeepEqual(expectedfiles, actualfiles) {
t.Errorf("%s expected notifications for %v; got %v", subscriber, expectedfiles, actualfiles)
}
}
for subscriber, expectedfiles := range test.notifications {
if _, ok := subs[subscriber]; ok {
// avoid duplicate errors
continue
}
actualfiles := notifs[subscriber]
sort.Strings(expectedfiles)
sort.Strings(actualfiles)
if !reflect.DeepEqual(expectedfiles, actualfiles) {
t.Errorf("%s expected notifications for %v; got %v", subscriber, expectedfiles, actualfiles)
}
}
})
}
}
func TestIsRateLimitErr(t *testing.T) {
cases := []struct {
err error
expected bool
}{
{
err: fmt.Errorf("graphql: API rate limit exceeded for user ID 12345"),
expected: true,
}, {
err: nil,
expected: false,
}, {
err: fmt.Errorf("fake top error"),
expected: false,
}, {
err: fmt.Errorf("something something: API rate limit exceeded for user ID 12345"),
expected: true,
},
}
for _, tc := range cases {
if isRateLimitErr(tc.err) != tc.expected {
t.Errorf("expected %v got %v for %s", tc.expected, !tc.expected, tc.err)
}
}
}
// memfs is an in-memory implementation of the FS interface.
type memfs map[string]string
func (m memfs) paths() []string {
paths := []string{}
for path := range m {
paths = append(paths, path)
}
return paths
}
func (m memfs) Open(name string) (File, error) {
content, ok := m[name]
if !ok {
return nil, os.ErrNotExist
}
mf := memfile{
Buffer: bytes.NewBufferString(content),
}
return mf, nil
}