-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain_test.go
More file actions
954 lines (855 loc) · 22.7 KB
/
main_test.go
File metadata and controls
954 lines (855 loc) · 22.7 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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/g-rath/osv-detector/internal/cachedregexp"
"github.com/gkampitakis/go-snaps/snaps"
)
func TestMain(m *testing.M) {
m.Run()
dirty, err := snaps.Clean(m, snaps.CleanOpts{Sort: true})
if err != nil {
//nolint:forbidigo
fmt.Println("Error cleaning snaps:", err)
os.Exit(1)
}
if dirty {
//nolint:forbidigo
fmt.Println("Some snapshots were outdated.")
os.Exit(1)
}
}
type cliTestCase struct {
name string
args []string
exit int
around func(t *testing.T) func()
}
func testCli(t *testing.T, tc cliTestCase) {
t.Helper()
stdoutBuffer := &bytes.Buffer{}
stderrBuffer := &bytes.Buffer{}
ec := run(tc.args, stdoutBuffer, stderrBuffer)
stdout := normalizeSnapshot(t, stdoutBuffer.String())
stderr := normalizeSnapshot(t, stderrBuffer.String())
if ec != tc.exit {
t.Errorf("cli exited with code %d, not %d", ec, tc.exit)
}
snaps.MatchSnapshot(t, stdout)
snaps.MatchSnapshot(t, stderr)
}
func TestRun(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
{
name: "",
args: []string{},
exit: 127,
},
{
name: "",
args: []string{"--version"},
exit: 0,
},
{
name: "",
args: []string{"--parse-as", "my-file"},
exit: 127,
},
// only the files in the given directories are checked (no recursion)
{
name: "",
args: []string{filepath.FromSlash("./testdata/")},
exit: 128,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_EmptyDirExitCode(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// no paths should return standard error exit code
{
name: "",
args: []string{},
exit: 127,
},
// one directory without any lockfiles should result in "no lockfiles in directories" exit code
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-none")},
exit: 128,
},
// two directories without any lockfiles should return "no lockfiles in directories" exit code
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-none"), filepath.FromSlash("./testdata/")},
exit: 128,
},
// a path to an unknown lockfile should return standard error exit code
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-none/my-file.txt")},
exit: 127,
},
// mix and match of directory without any lockfiles and a path to an unknown lockfile should return standard exit code
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-none/my-file.txt"), filepath.FromSlash("./testdata/")},
exit: 127,
},
// when the directory does not exist, the exit code should not be for "no lockfiles in directories"
{
name: "",
args: []string{filepath.FromSlash("./testdata/does/not/exist")},
exit: 127,
// "file not found" message is different on Windows vs other OSs
},
// an empty directory + a directory that does not exist should return standard exit code
{
name: "",
args: []string{filepath.FromSlash("./testdata/does/not/exist"), filepath.FromSlash("./testdata/locks-none")},
exit: 127,
// "file not found" message is different on Windows vs other OSs
},
// when there are no parsable lockfiles in the directory + --json should give sensible json
{
name: "",
args: []string{"--json", filepath.FromSlash("./testdata/locks-none")},
exit: 128,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_ListPackages(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
{
name: "",
args: []string{"--list-packages", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
{
name: "",
args: []string{"--list-packages", filepath.FromSlash("./testdata/locks-many")},
exit: 0,
},
{
name: "",
args: []string{"--list-packages", filepath.FromSlash("./testdata/locks-empty")},
exit: 0,
},
// json results in non-json output going to stderr
{
name: "",
args: []string{"--list-packages", "--json", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_Lockfile(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-many")},
exit: 0,
},
{
name: "",
args: []string{filepath.FromSlash("./testdata/locks-empty")},
exit: 0,
},
// parse-as + known vulnerability exits with error code 1
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json")},
exit: 1,
},
// json results in non-json output going to stderr
{
name: "",
args: []string{"--json", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_Lockfile_AbsolutePath(t *testing.T) {
t.Parallel()
testdataDir, err := filepath.Abs("./testdata")
if err != nil {
t.Fatal(err)
}
tests := []cliTestCase{
{
name: "",
args: []string{filepath.Join(testdataDir, "locks-one")},
exit: 0,
},
{
name: "",
args: []string{filepath.Join(testdataDir, "locks-many")},
exit: 0,
},
{
name: "",
args: []string{filepath.Join(testdataDir, "locks-empty")},
exit: 0,
},
// parse-as + known vulnerability exits with error code 1
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.Join(testdataDir, "locks-insecure/my-package-lock.json")},
exit: 1,
},
// json results in non-json output going to stderr
{
name: "",
args: []string{"--json", filepath.Join(testdataDir, "locks-one")},
exit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_DBs(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
{
name: "",
args: []string{"--use-dbs=false", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
{
name: "",
args: []string{"--use-api", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_ParseAsSpecific(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// when there is just a ":", it defaults as empty
{
name: "",
args: []string{filepath.FromSlash(":./testdata/locks-insecure/composer.lock")},
exit: 0,
},
// ":" can be used as an escape (no test though because it's invalid on Windows)
{
name: "",
args: []string{filepath.FromSlash(":./testdata/locks-insecure/my:file")},
exit: 127,
},
// when a path to a file is given, parse-as is applied to that file
{
name: "",
args: []string{filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json")},
exit: 1,
},
// when a path to a directory is given, parse-as is applied to all files in the directory
{
name: "",
args: []string{filepath.FromSlash("package-lock.json:./testdata/locks-insecure")},
exit: 1,
},
// files that error on parsing don't stop parsable files from being checked
{
name: "",
args: []string{filepath.FromSlash("package-lock.json:./testdata/locks-empty")},
exit: 127,
},
// files that error on parsing don't stop parsable files from being checked
{
name: "",
args: []string{filepath.FromSlash("package-lock.json:./testdata/locks-empty"), filepath.FromSlash("package-lock.json:./testdata/locks-insecure")},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_ParseAsGlobal(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// when a path to a file is given, parse-as is applied to that file
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json")},
exit: 1,
},
// when a path to a directory is given, parse-as is applied to all files in the directory
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("./testdata/locks-insecure")},
exit: 1,
},
// files that error on parsing don't stop parsable files from being checked
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("./testdata/locks-empty")},
exit: 127,
},
// files that error on parsing don't stop parsable files from being checked
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("./testdata/locks-empty"), filepath.FromSlash("./testdata/locks-insecure")},
exit: 127,
},
// specific parse-as takes precedence over global parse-as
{
name: "",
args: []string{"--parse-as", "package-lock.json", filepath.FromSlash("Gemfile.lock:./testdata/locks-empty"), filepath.FromSlash("./testdata/locks-insecure")},
exit: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_ParseAs_CsvRow(t *testing.T) {
t.Parallel()
// these tests use "--no-config" in case the repo ever has a
// default config (which can be useful during development)
tests := []cliTestCase{
{
name: "",
args: []string{
"--no-config",
"--parse-as", "csv-row",
"NuGet,,Yarp.ReverseProxy,",
},
exit: 1,
},
{
name: "",
args: []string{
"--no-config",
"--parse-as", "csv-row",
"NuGet,,Yarp.ReverseProxy,",
"npm,,@typescript-eslint/types,5.13.0",
},
exit: 1,
},
{
name: "",
args: []string{
"--no-config",
"--parse-as", "csv-row",
"NuGet,,",
"npm,,@typescript-eslint/types,5.13.0",
},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_ParseAs_CsvFile(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
{
name: "",
args: []string{"--parse-as", "csv-file", filepath.FromSlash("./testdata/csvs-files/two-rows.csv")},
exit: 1,
},
{
name: "",
args: []string{"--parse-as", "csv-file", filepath.FromSlash("./testdata/csvs-files/not-a-csv.xml")},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_Configs(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// when given a path to a single lockfile, the local config should be used
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-one/yarn.lock")},
exit: 0,
},
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-two/yarn.lock")},
exit: 0,
},
// when given a path to a directory, the local config should be used for all lockfiles
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-one")},
exit: 0,
},
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-two")},
exit: 0,
},
// local configs should be applied based on directory of each lockfile
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-one/yarn.lock"), filepath.FromSlash("./testdata/locks-many")},
exit: 0,
},
// invalid databases should be skipped
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-extra-dbs/yarn.lock")},
exit: 127,
},
// databases from configs are ignored if "--no-config-databases" is passed...
{
name: "",
args: []string{
"--no-config-databases",
filepath.FromSlash("./testdata/configs-extra-dbs/yarn.lock"),
},
exit: 0,
},
// ...but it does still use the built-in databases
{
name: "",
args: []string{
"--config", filepath.FromSlash("./testdata/configs-extra-dbs/.osv-detector.yaml"),
"--no-config-databases",
filepath.FromSlash("./testdata/locks-many/yarn.lock"),
},
exit: 0,
},
// when a global config is provided, any local configs should be ignored
{
name: "",
args: []string{"--config", filepath.FromSlash("testdata/my-config.yml"), filepath.FromSlash("./testdata/configs-one/yarn.lock")},
exit: 0,
},
{
name: "",
args: []string{"--config", filepath.FromSlash("testdata/my-config.yml"), filepath.FromSlash("./testdata/configs-two")},
exit: 0,
},
{
name: "",
args: []string{"--config", filepath.FromSlash("testdata/my-config.yml"), filepath.FromSlash("./testdata/configs-one/yarn.lock"), filepath.FromSlash("./testdata/locks-many")},
exit: 0,
},
// when a local config is invalid, none of the lockfiles in that directory should
// be checked (as the results could be different due to e.g. missing ignores)
{
name: "",
args: []string{filepath.FromSlash("./testdata/configs-invalid"), filepath.FromSlash("./testdata/locks-one")},
exit: 127,
},
// when a global config is invalid, none of the lockfiles should be checked
// (as the results could be different due to e.g. missing ignores)
{
name: "",
args: []string{
"--config", filepath.FromSlash("./testdata/configs-invalid/.osv-detector.yaml"),
filepath.FromSlash("./testdata/configs-invalid"),
filepath.FromSlash("./testdata/locks-one"),
filepath.FromSlash("./testdata/locks-many"),
},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_Ignores(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// no ignore count is printed if there is nothing ignored
{
name: "",
args: []string{"--ignore", "GHSA-1234", "--ignore", "GHSA-5678", filepath.FromSlash("./testdata/locks-one")},
exit: 0,
},
{
name: "",
args: []string{
"--ignore", "GHSA-whgm-jr23-g3j9",
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
},
// the ignored count reflects the number of vulnerabilities ignored,
// not the number of ignores that were provided
{
name: "",
args: []string{
"--ignore", "GHSA-whgm-jr23-g3j9",
"--ignore", "GHSA-whgm-jr23-1234",
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
},
// ignores passed by flags are _merged_ with those specified in configs by default
{
name: "",
args: []string{
"--config", filepath.FromSlash("./testdata/my-config.yml"),
"--ignore", "GHSA-1234",
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
},
// ignores from configs are ignored if "--no-config-ignores" is passed
{
name: "",
args: []string{
"--no-config-ignores",
"--config", filepath.FromSlash("./testdata/my-config.yml"),
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 1,
},
// ignores passed by flags are still respected with "--no-config-ignores"
{
name: "",
args: []string{
"--no-config-ignores",
"--config", filepath.FromSlash("./testdata/my-config.yml"),
"--ignore", "GHSA-whgm-jr23-g3j9",
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
},
// ignores passed by flags are ignored with those specified in configs
{
name: "",
args: []string{
"--config", filepath.FromSlash("./testdata/my-config.yml"),
"--ignore", "GHSA-1234",
"--parse-as", "package-lock.json",
filepath.FromSlash("./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func setupConfigForUpdating(t *testing.T, path string, initial string) func() {
t.Helper()
err := os.WriteFile(path, []byte(initial), 0600)
if err != nil {
t.Fatalf("could not create test file: %v", err)
}
return func() {
t.Helper()
// ensure that we always try to remove the file
defer func() {
if err = os.Remove(path); err != nil {
// this will typically fail on Windows due to processes,
// so we just treat it as a warning instead of an error
t.Logf("could not remove test file: %v", err)
}
}()
content, err := os.ReadFile(path)
if err != nil {
t.Fatalf("could not read test config file: %v", err)
}
snaps.MatchSnapshot(t, string(content))
}
}
func TestRun_UpdatingConfigIgnores(t *testing.T) {
t.Parallel()
tests := []cliTestCase{
// when there is no existing config, nothing should be updated
{
name: "",
args: []string{"--update-config-ignores", filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json")},
exit: 1,
},
// when given an explicit config, that should be updated
{
name: "",
args: []string{
"--update-config-ignores",
"--config", "testdata/existing-config.yml",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json"),
},
exit: 1,
around: func(t *testing.T) func() {
t.Helper()
return setupConfigForUpdating(t, "testdata/existing-config.yml", "")
},
},
// when there are existing ignores
{
name: "",
args: []string{
"--update-config-ignores",
"--config", "testdata/existing-config-with-ignores.yml",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json"),
},
exit: 0,
around: func(t *testing.T) func() {
t.Helper()
return setupConfigForUpdating(t,
"testdata/existing-config-with-ignores.yml",
"ignore: [GHSA-whgm-jr23-g3j9]",
)
},
},
// when there are existing ignores but told to ignore those
{
name: "",
args: []string{
"--update-config-ignores",
"--no-config-ignores",
"--config", "testdata/existing-config-with-ignored-ignores.yml",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json"),
},
exit: 1,
around: func(t *testing.T) func() {
t.Helper()
return setupConfigForUpdating(t,
"testdata/existing-config-with-ignored-ignores.yml",
"ignore: [GHSA-whgm-jr23-g3j9]",
)
},
},
// when there are many lockfiles with one config
{
name: "",
args: []string{
"--update-config-ignores",
"--config", "testdata/existing-config-with-many-lockfiles.yml",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure/my-package-lock.json"),
filepath.FromSlash("package-lock.json:./testdata/locks-insecure-many/my-package-lock.json"),
filepath.FromSlash("package-lock.json:./testdata/locks-insecure-nested/my-package-lock.json"),
filepath.FromSlash("composer.lock:./testdata/locks-insecure-nested/nested/my-composer-lock.json"),
},
exit: 1,
around: func(t *testing.T) func() {
t.Helper()
return setupConfigForUpdating(t,
"testdata/existing-config-with-many-lockfiles.yml",
"ignore: [GHSA-whgm-jr23-g3j9]",
)
},
},
// when there are multiple implicit configs, it updates the right ones
{
name: "",
args: []string{
"--update-config-ignores",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure-nested/my-package-lock.json"),
filepath.FromSlash("composer.lock:./testdata/locks-insecure-nested/nested/my-composer-lock.json"),
},
exit: 1,
around: func(t *testing.T) func() {
t.Helper()
cleanupConfig1 := setupConfigForUpdating(t,
"testdata/locks-insecure-nested/.osv-detector.yml",
"ignore: []",
)
cleanupConfig2 := setupConfigForUpdating(t,
"testdata/locks-insecure-nested/nested/.osv-detector.yml",
"ignore: []",
)
return func() {
cleanupConfig1()
cleanupConfig2()
}
},
},
// when there are existing ignores, it updates them and removes patched ones
{
name: "",
args: []string{
"--update-config-ignores",
filepath.FromSlash("package-lock.json:./testdata/locks-insecure-many/my-package-lock.json"),
},
exit: 1,
around: func(t *testing.T) func() {
t.Helper()
return setupConfigForUpdating(t,
"testdata/locks-insecure-many/.osv-detector.yml",
"ignore: [GHSA-7p7h-4mm5-852v, GHSA-93q8-gq69-wqmw, GHSA-67hx-6x53-jw92]",
)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if tt.around != nil {
teardown := tt.around(t)
defer teardown()
}
testCli(t, tt)
})
}
}
func TestRun_EndToEnd(t *testing.T) {
t.Parallel()
if os.Getenv("TEST_ACCEPTANCE") != "true" {
snaps.Skip(t, "Skipping acceptance tests")
}
e2eTestdataDir := "./testdata/locks-e2e"
files, err := os.ReadDir(e2eTestdataDir)
if err != nil {
t.Fatalf("%v", err)
}
tests := make([]cliTestCase, 0, len(files)/2)
re := cachedregexp.MustCompile(`\d+-(.*)`)
for _, f := range files {
if strings.HasSuffix(f.Name(), ".out.txt") {
continue
}
if f.IsDir() {
t.Errorf("unexpected directory in e2e tests")
continue
}
matches := re.FindStringSubmatch(f.Name())
if matches == nil {
t.Errorf("could not determine parser for %s", f.Name())
continue
}
parseAs := matches[1]
fp := filepath.FromSlash(filepath.Join(e2eTestdataDir, f.Name()))
tests = append(tests, cliTestCase{
args: []string{parseAs + ":" + fp},
exit: 1,
})
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
testCli(t, tt)
})
}
}
func TestRun_APIError(t *testing.T) {
t.Parallel()
tests := []struct{ handler http.HandlerFunc }{
{
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte("{}"))
},
},
{
handler: func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("<html></html>"))
},
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
t.Parallel()
//nolint:usetesting // we need to customize the directory name to replace in snapshots
p, err := os.MkdirTemp("", "osv-detector-test-*")
if err != nil {
t.Fatalf("could not create test directory: %v", err)
}
// ensure the test directory is removed when we're done testing
t.Cleanup(func() {
_ = os.RemoveAll(p)
})
// create a file for scanning
err = os.WriteFile(filepath.Join(p, "Gemfile.lock"), []byte(`
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
`), 0600)
if err != nil {
t.Fatal(err)
}
// setup a fake api server
ts := httptest.NewServer(tt.handler)
t.Cleanup(ts.Close)
// create a config file setting up our api server
err = os.WriteFile(filepath.Join(p, ".osv-detector.yml"), []byte(`
extra-databases:
- url: `+ts.URL,
), 0600)
if err != nil {
t.Fatal(err)
}
// run the cli in our tmp directory
testCli(t, cliTestCase{
name: "",
args: []string{p},
exit: 127,
})
})
}
}