Skip to content

Commit

Permalink
Refactor the rules tests to be able to configure the analyzer config …
Browse files Browse the repository at this point in the history
…per test sample

Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
  • Loading branch information
ccojocar committed Jun 25, 2019
1 parent 36a82ea commit ed9934f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 81 deletions.
5 changes: 5 additions & 0 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func NewAnalyzer(conf Config, tests bool, logger *log.Logger) *Analyzer {
}
}

// SetConfig upates the analyzer configuration
func (gosec *Analyzer) SetConfig(conf Config) {
gosec.config = conf
}

// LoadRules instantiates all the rules to be used when analyzing source
// packages
func (gosec *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder) {
Expand Down
15 changes: 4 additions & 11 deletions rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,13 @@ import (
"github.com/securego/gosec/testutils"
)

type option struct {
name gosec.GlobalOption
value string
}

var _ = Describe("gosec rules", func() {

var (
logger *log.Logger
config gosec.Config
analyzer *gosec.Analyzer
runner func(string, []testutils.CodeSample, ...option)
runner func(string, []testutils.CodeSample)
buildTags []string
tests bool
)
Expand All @@ -32,13 +27,11 @@ var _ = Describe("gosec rules", func() {
logger, _ = testutils.NewLogger()
config = gosec.NewConfig()
analyzer = gosec.NewAnalyzer(config, tests, logger)
runner = func(rule string, samples []testutils.CodeSample, options ...option) {
for _, o := range options {
config.SetGlobal(o.name, o.value)
}
runner = func(rule string, samples []testutils.CodeSample) {
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, rule)).Builders())
for n, sample := range samples {
analyzer.Reset()
analyzer.SetConfig(sample.Config)
pkg := testutils.NewTestPackage()
defer pkg.Close()
for i, code := range sample.Code {
Expand Down Expand Up @@ -75,7 +68,7 @@ var _ = Describe("gosec rules", func() {
})

It("should detect errors not being checked in audit mode", func() {
runner("G104", testutils.SampleCodeG104Audit, option{name: gosec.Audit, value: "enabled"})
runner("G104", testutils.SampleCodeG104Audit)
})

It("should detect of big.Exp function", func() {
Expand Down
Loading

0 comments on commit ed9934f

Please sign in to comment.