Refactor the test for Go build errors

Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
Cosmin Cojocar 2019-04-30 12:17:44 +02:00 committed by Cosmin Cojocar
parent 3af4ae9ddb
commit 625718d294
2 changed files with 9 additions and 18 deletions

View file

@ -199,7 +199,7 @@ func (gosec *Analyzer) parseErrors(pkg *packages.Package) error {
var err error var err error
var line, column int var line, column int
var errorMsg string var errorMsg string
if len(infoErr) == 4 { if len(infoErr) > 3 {
if line, err = strconv.Atoi(infoErr[1]); err != nil { if line, err = strconv.Atoi(infoErr[1]); err != nil {
return fmt.Errorf("parsing line: %v", err) return fmt.Errorf("parsing line: %v", err)
} }

View file

@ -99,8 +99,6 @@ var _ = Describe("Analyzer", func() {
}) })
It("should find errors when nosec is not in use", func() { It("should find errors when nosec is not in use", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0] sample := testutils.SampleCodeG401[0]
source := sample.Code[0] source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders()) analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
@ -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()) analyzer.LoadRules(rules.Generate().Builders())
pkg := testutils.NewTestPackage() pkg := testutils.NewTestPackage()
defer pkg.Close() defer pkg.Close()
@ -129,22 +127,17 @@ var _ = Describe("Analyzer", func() {
Expect(err).ShouldNot(HaveOccurred()) Expect(err).ShouldNot(HaveOccurred())
err = analyzer.Process(buildTags, pkg.Path) err = analyzer.Process(buildTags, pkg.Path)
Expect(err).ShouldNot(HaveOccurred()) Expect(err).ShouldNot(HaveOccurred())
_, _, golangErrors := analyzer.Report() _, _, errors := analyzer.Report()
keys := make([]string, len(golangErrors)) Expect(len(errors)).To(Equal(1))
i := 0 for _, ferr := range errors {
for key := range golangErrors { Expect(len(ferr)).To(Equal(1))
keys[i] = key Expect(ferr[0].Line).To(Equal(4))
i++ 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() { It("should not report errors when a nosec comment is present", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0] sample := testutils.SampleCodeG401[0]
source := sample.Code[0] source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders()) analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
@ -180,7 +173,6 @@ var _ = Describe("Analyzer", func() {
}) })
It("should report errors when an exclude comment is present for a different rule", 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] sample := testutils.SampleCodeG401[0]
source := sample.Code[0] source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders()) analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
@ -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() { 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] sample := testutils.SampleCodeG401[0]
source := sample.Code[0] source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders()) analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())