@@ -16,6 +16,7 @@ package gosec_test
1616
1717import (
1818 "errors"
19+ "fmt"
1920 "go/build"
2021 "log"
2122 "regexp"
@@ -783,16 +784,79 @@ var _ = Describe("Analyzer", func() {
783784 Expect (nosecIssues ).Should (BeEmpty ())
784785 })
785786
786- It ("should pass the build tags" , func () {
787+ It ("should not panic if a file can not compile" , func () {
788+ sample := testutils .SampleCodeCompilationFail [0 ]
789+ source := sample .Code [0 ]
790+ analyzer .LoadRules (rules .Generate (false ).RulesInfo ())
791+ pkg := testutils .NewTestPackage ()
792+ defer pkg .Close ()
793+
794+ pkg .AddFile ("main.go" , source )
795+ err := pkg .Build ()
796+ Expect (err ).ShouldNot (HaveOccurred ())
797+ err = analyzer .Process (buildTags , pkg .Path )
798+ Expect (err ).ShouldNot (HaveOccurred ())
799+ })
800+
801+ It ("should exclude a reportable file, if excluded by build tags" , func () {
802+ // file has a reportable security issue, but should only be flagged
803+ // to only being compiled in via a build flag.
804+ sample := testutils .SampleCodeG501BuildTag [0 ]
805+ source := sample .Code [0 ]
806+ analyzer .LoadRules (rules .Generate (false ).RulesInfo ())
807+ pkg := testutils .NewTestPackage ()
808+ defer pkg .Close ()
809+
810+ pkg .AddFile ("main.go" , source )
811+ err := pkg .Build ()
812+ Expect (err ).To (BeEquivalentTo (& build.NoGoError {Dir : pkg .Path })) // no files should be found for scanning.
813+ err = analyzer .Process (buildTags , pkg .Path )
814+ Expect (err ).ShouldNot (HaveOccurred ())
815+
816+ issues , _ , _ := analyzer .Report ()
817+ Expect (issues ).Should (BeEmpty ())
818+ })
819+
820+ It ("should attempt to analyse a file with build tags" , func () {
787821 sample := testutils .SampleCodeBuildTag [0 ]
788822 source := sample .Code [0 ]
789823 analyzer .LoadRules (rules .Generate (false ).RulesInfo ())
790824 pkg := testutils .NewTestPackage ()
791825 defer pkg .Close ()
792- pkg .AddFile ("tags.go" , source )
826+
827+ tags := []string {"tag" }
828+ pkg .AddFile ("main.go" , source )
829+ err := pkg .Build (testutils .WithBuildTags (tags ))
830+ Expect (err ).ShouldNot (HaveOccurred ())
831+ err = analyzer .Process (tags , pkg .Path )
832+ Expect (err ).ShouldNot (HaveOccurred ())
833+
834+ issues , _ , _ := analyzer .Report ()
835+ if len (issues ) != sample .Errors {
836+ fmt .Println (sample .Code )
837+ }
838+ Expect (issues ).Should (HaveLen (sample .Errors ))
839+ })
840+
841+ It ("should report issues from a file with build tags" , func () {
842+ sample := testutils .SampleCodeG501BuildTag [0 ]
843+ source := sample .Code [0 ]
844+ analyzer .LoadRules (rules .Generate (false ).RulesInfo ())
845+ pkg := testutils .NewTestPackage ()
846+ defer pkg .Close ()
847+
793848 tags := []string {"tag" }
794- err := analyzer .Process (tags , pkg .Path )
849+ pkg .AddFile ("main.go" , source )
850+ err := pkg .Build (testutils .WithBuildTags (tags ))
795851 Expect (err ).ShouldNot (HaveOccurred ())
852+ err = analyzer .Process (tags , pkg .Path )
853+ Expect (err ).ShouldNot (HaveOccurred ())
854+
855+ issues , _ , _ := analyzer .Report ()
856+ if len (issues ) != sample .Errors {
857+ fmt .Println (sample .Code )
858+ }
859+ Expect (issues ).Should (HaveLen (sample .Errors ))
796860 })
797861
798862 It ("should process an empty package with test file" , func () {
0 commit comments