Skip to content

Commit 2cc6838

Browse files
author
Cosmin Cojocar
authored
Merge pull request #248 from ccojocar/code-samples-multiple-files
Refactor the test code sample to support multiple files per sample
2 parents d3f1980 + 64d58c2 commit 2cc6838

File tree

4 files changed

+105
-129
lines changed

4 files changed

+105
-129
lines changed

analyzer_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ var _ = Describe("Analyzer", func() {
9898

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

104104
controlPackage := testutils.NewTestPackage()
@@ -114,7 +114,7 @@ var _ = Describe("Analyzer", func() {
114114
It("should not report errors when a nosec comment is present", func() {
115115
// Rule for MD5 weak crypto usage
116116
sample := testutils.SampleCodeG401[0]
117-
source := sample.Code
117+
source := sample.Code[0]
118118
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
119119

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

137137
nosecPackage := testutils.NewTestPackage()
@@ -148,7 +148,7 @@ var _ = Describe("Analyzer", func() {
148148
It("should report errors when an exclude comment is present for a different rule", func() {
149149
// Rule for MD5 weak crypto usage
150150
sample := testutils.SampleCodeG401[0]
151-
source := sample.Code
151+
source := sample.Code[0]
152152
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, "G401")).Builders())
153153

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

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

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

198198
// Rule for MD5 weak crypto usage
199199
sample := testutils.SampleCodeG401[0]
200-
source := sample.Code
200+
source := sample.Code[0]
201201

202202
// overwrite nosec option
203203
nosecIgnoreConfig := gosec.NewConfig()

call_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var _ = Describe("call list", func() {
6161
// Create file to be scanned
6262
pkg := testutils.NewTestPackage()
6363
defer pkg.Close()
64-
pkg.AddFile("md5.go", testutils.SampleCodeG401[0].Code)
64+
pkg.AddFile("md5.go", testutils.SampleCodeG401[0].Code[0])
6565

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

rules/rules_test.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,13 @@ var _ = Describe("gosec rules", func() {
2828
analyzer = gosec.NewAnalyzer(config, logger)
2929
runner = func(rule string, samples []testutils.CodeSample) {
3030
analyzer.LoadRules(rules.Generate(rules.NewRuleFilter(false, rule)).Builders())
31-
32-
supportingFiles := []string{}
33-
for _, sample := range samples {
34-
if sample.SupportingCode {
35-
supportingFiles = append(supportingFiles, sample.Code)
36-
}
37-
}
38-
3931
for n, sample := range samples {
40-
if sample.SupportingCode {
41-
continue
42-
}
4332
analyzer.Reset()
4433
pkg := testutils.NewTestPackage()
4534
defer pkg.Close()
46-
for n, supportingCode := range supportingFiles {
47-
pkg.AddFile(fmt.Sprintf("supporting_sample_%d.go", n), supportingCode)
35+
for i, code := range sample.Code {
36+
pkg.AddFile(fmt.Sprintf("sample_%d_%d.go", n, i), code)
4837
}
49-
pkg.AddFile(fmt.Sprintf("sample_%d.go", n), sample.Code)
5038
err := pkg.Build()
5139
Expect(err).ShouldNot(HaveOccurred())
5240
err = analyzer.Process(buildTags, pkg.Path)

0 commit comments

Comments
 (0)