Skip to content

Commit

Permalink
Refactor the test code sample to support multiple files per sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ccojocar committed Sep 28, 2018
1 parent d3f1980 commit 64d58c2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 129 deletions.
14 changes: 7 additions & 7 deletions analyzer_test.go
Expand Up @@ -98,7 +98,7 @@ var _ = Describe("Analyzer", func() {

// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())

controlPackage := testutils.NewTestPackage()
Expand All @@ -114,7 +114,7 @@ var _ = Describe("Analyzer", func() {
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
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())

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

nosecPackage := testutils.NewTestPackage()
Expand All @@ -148,7 +148,7 @@ 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
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())

nosecPackage := testutils.NewTestPackage()
Expand All @@ -165,7 +165,7 @@ 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
source := sample.Code[0]
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())

nosecPackage := testutils.NewTestPackage()
Expand All @@ -181,7 +181,7 @@ var _ = Describe("Analyzer", func() {

It("should pass the build tags", func() {
sample := testutils.SampleCode601[0]
source := sample.Code
source := sample.Code[0]
analyzer.LoadRules(rules.Generate().Builders())
pkg := testutils.NewTestPackage()
defer pkg.Close()
Expand All @@ -197,7 +197,7 @@ var _ = Describe("Analyzer", func() {

// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code
source := sample.Code[0]

// overwrite nosec option
nosecIgnoreConfig := gosec.NewConfig()
Expand Down
2 changes: 1 addition & 1 deletion call_list_test.go
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("call list", func() {
// Create file to be scanned
pkg := testutils.NewTestPackage()
defer pkg.Close()
pkg.AddFile("md5.go", testutils.SampleCodeG401[0].Code)
pkg.AddFile("md5.go", testutils.SampleCodeG401[0].Code[0])

ctx := pkg.CreateContext("md5.go")

Expand Down
16 changes: 2 additions & 14 deletions rules/rules_test.go
Expand Up @@ -28,25 +28,13 @@ var _ = Describe("gosec rules", func() {
analyzer = gosec.NewAnalyzer(config, logger)
runner = func(rule string, samples []testutils.CodeSample) {
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, rule)).Builders())

supportingFiles := []string{}
for _, sample := range samples {
if sample.SupportingCode {
supportingFiles = append(supportingFiles, sample.Code)
}
}

for n, sample := range samples {
if sample.SupportingCode {
continue
}
analyzer.Reset()
pkg := testutils.NewTestPackage()
defer pkg.Close()
for n, supportingCode := range supportingFiles {
pkg.AddFile(fmt.Sprintf("supporting_sample_%d.go", n), supportingCode)
for i, code := range sample.Code {
pkg.AddFile(fmt.Sprintf("sample_%d_%d.go", n, i), code)
}
pkg.AddFile(fmt.Sprintf("sample_%d.go", n), sample.Code)
err := pkg.Build()
Expect(err).ShouldNot(HaveOccurred())
err = analyzer.Process(buildTags, pkg.Path)
Expand Down

0 comments on commit 64d58c2

Please sign in to comment.