Skip to content

Commit

Permalink
Refactor the test for Go build errors
Browse files Browse the repository at this point in the history
Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
  • Loading branch information
ccojocar authored and Cosmin Cojocar committed May 1, 2019
1 parent 3af4ae9 commit 625718d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (gosec *Analyzer) parseErrors(pkg *packages.Package) error {
var err error
var line, column int
var errorMsg string
if len(infoErr) == 4 {
if len(infoErr) > 3 {
if line, err = strconv.Atoi(infoErr[1]); err != nil {
return fmt.Errorf("parsing line: %v", err)
}
Expand Down
25 changes: 8 additions & 17 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ var _ = Describe("Analyzer", func() {
})

It("should find errors when nosec is not in use", func() {

// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
Expand All @@ -117,7 +115,7 @@ var _ = Describe("Analyzer", func() {

})

It("should report for Golang errors and invalid files", func() {
It("should report Go build errors and invalid files", func() {
analyzer.LoadRules(rules.Generate().Builders())
pkg := testutils.NewTestPackage()
defer pkg.Close()
Expand All @@ -129,22 +127,17 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred())
err = analyzer.Process(buildTags, pkg.Path)
Expect(err).ShouldNot(HaveOccurred())
_, _, golangErrors := analyzer.Report()
keys := make([]string, len(golangErrors))
i := 0
for key := range golangErrors {
keys[i] = key
i++
_, _, errors := analyzer.Report()
Expect(len(errors)).To(Equal(1))
for _, ferr := range errors {
Expect(len(ferr)).To(Equal(1))
Expect(ferr[0].Line).To(Equal(4))
Expect(ferr[0].Column).To(Equal(5))
Expect(ferr[0].Err).Should(MatchRegexp(`expected declaration, found '}'`))
}
fileErr := golangErrors[keys[0]]
Expect(len(fileErr)).To(Equal(1))
Expect(fileErr[0].Line).To(Equal(4))
Expect(fileErr[0].Column).To(Equal(5))
Expect(fileErr[0].Err).Should(MatchRegexp(`expected declaration, found '}'`))
})

It("should not report errors when a nosec comment is present", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
Expand Down Expand Up @@ -180,7 +173,6 @@ var _ = Describe("Analyzer", func() {
})

It("should report errors when an exclude comment is present for a different rule", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
Expand All @@ -198,7 +190,6 @@ var _ = Describe("Analyzer", func() {
})

It("should not report errors when an exclude comment is present for multiple rules, including the correct rule", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
Expand Down

0 comments on commit 625718d

Please sign in to comment.