1
1
package gosec_test
2
2
3
3
import (
4
+ "errors"
4
5
"io/ioutil"
5
6
"log"
6
7
"os"
@@ -30,27 +31,31 @@ var _ = Describe("Analyzer", func() {
30
31
31
32
Context ("when processing a package" , func () {
32
33
33
- It ("should return an error if the package contains no Go files" , func () {
34
+ It ("should not report an error if the package contains no Go files" , func () {
34
35
analyzer .LoadRules (rules .Generate ().Builders ())
35
36
dir , err := ioutil .TempDir ("" , "empty" )
36
37
defer os .RemoveAll (dir )
37
38
Expect (err ).ShouldNot (HaveOccurred ())
38
39
err = analyzer .Process (buildTags , dir )
39
- Expect (err ).Should (HaveOccurred ())
40
- Expect (err .Error ()).Should (MatchRegexp ("no buildable Go source files" ))
40
+ Expect (err ).ShouldNot (HaveOccurred ())
41
+ _ , _ , errors := analyzer .Report ()
42
+ Expect (len (errors )).To (Equal (0 ))
41
43
})
42
44
43
- It ("should return an error if the package fails to build" , func () {
45
+ It ("should report an error if the package fails to build" , func () {
44
46
analyzer .LoadRules (rules .Generate ().Builders ())
45
47
pkg := testutils .NewTestPackage ()
46
48
defer pkg .Close ()
47
49
pkg .AddFile ("wonky.go" , `func main(){ println("forgot the package")}` )
48
50
err := pkg .Build ()
49
51
Expect (err ).Should (HaveOccurred ())
50
52
err = analyzer .Process (buildTags , pkg .Path )
51
- Expect (err ).Should (HaveOccurred ())
52
- Expect (err .Error ()).Should (MatchRegexp (`expected 'package'` ))
53
-
53
+ Expect (err ).ShouldNot (HaveOccurred ())
54
+ _ , _ , errors := analyzer .Report ()
55
+ Expect (len (errors )).To (Equal (1 ))
56
+ for _ , ferr := range errors {
57
+ Expect (len (ferr )).To (Equal (1 ))
58
+ }
54
59
})
55
60
56
61
It ("should be able to analyze multiple Go files" , func () {
@@ -216,9 +221,9 @@ var _ = Describe("Analyzer", func() {
216
221
pkg := testutils .NewTestPackage ()
217
222
defer pkg .Close ()
218
223
pkg .AddFile ("tags.go" , source )
219
- buildTags = append ( buildTags , "test" )
220
- err := analyzer .Process (buildTags , pkg .Path )
221
- Expect (err ).Should (HaveOccurred ())
224
+ tags := [] string { "tag" }
225
+ err := analyzer .Process (tags , pkg .Path )
226
+ Expect (err ).ShouldNot (HaveOccurred ())
222
227
})
223
228
224
229
It ("should process an empty package with test file" , func () {
@@ -236,14 +241,6 @@ var _ = Describe("Analyzer", func() {
236
241
Expect (err ).ShouldNot (HaveOccurred ())
237
242
})
238
243
239
- It ("should report an error when the package is empty" , func () {
240
- analyzer .LoadRules (rules .Generate ().Builders ())
241
- pkg := testutils .NewTestPackage ()
242
- defer pkg .Close ()
243
- err := analyzer .Process (buildTags , pkg .Path )
244
- Expect (err ).Should (HaveOccurred ())
245
- })
246
-
247
244
It ("should be possible to overwrite nosec comments, and report issues" , func () {
248
245
// Rule for MD5 weak crypto usage
249
246
sample := testutils .SampleCodeG401 [0 ]
@@ -416,4 +413,31 @@ var _ = Describe("Analyzer", func() {
416
413
}
417
414
})
418
415
})
416
+
417
+ Context ("when appending errors" , func () {
418
+ It ("should skip error for non-buildable packages" , func () {
419
+ analyzer .AppendError ("test" , errors .New (`loading file from package "pkg/test": no buildable Go source files in pkg/test` ))
420
+ _ , _ , errors := analyzer .Report ()
421
+ Expect (len (errors )).To (Equal (0 ))
422
+ })
423
+
424
+ It ("should add a new error" , func () {
425
+ pkg := & packages.Package {
426
+ Errors : []packages.Error {
427
+ packages.Error {
428
+ Pos : "file:1:2" ,
429
+ Msg : "build error" ,
430
+ },
431
+ },
432
+ }
433
+ err := analyzer .ParseErrors (pkg )
434
+ Expect (err ).ShouldNot (HaveOccurred ())
435
+ analyzer .AppendError ("file" , errors .New ("file build error" ))
436
+ _ , _ , errors := analyzer .Report ()
437
+ Expect (len (errors )).To (Equal (1 ))
438
+ for _ , ferr := range errors {
439
+ Expect (len (ferr )).To (Equal (2 ))
440
+ }
441
+ })
442
+ })
419
443
})
0 commit comments