Skip to content

Commit 4ae8c95

Browse files
cosmincojocargcmurphy
authored andcommitted
Add an option for Go build tags (#201)
* Add an option for Go build tags * Update README with a section for Go build tags
1 parent 7790709 commit 4ae8c95

File tree

6 files changed

+60
-20
lines changed

6 files changed

+60
-20
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ can do the following:
103103
```
104104
$ gas -nosec=true ./...
105105
```
106+
#### Build tags
107+
108+
Gas is able to pass your [Go build tags](https://golang.org/pkg/go/build/) to the analyzer.
109+
They can be provided as a comma separated list as follows:
110+
111+
```
112+
$ gas -tag debug,ignore ./...
113+
```
106114

107115
### Output formats
108116

analyzer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ func (gas *Analyzer) LoadRules(ruleDefinitions map[string]RuleBuilder) {
9797
}
9898

9999
// Process kicks off the analysis process for a given package
100-
func (gas *Analyzer) Process(packagePaths ...string) error {
100+
func (gas *Analyzer) Process(buildTags []string, packagePaths ...string) error {
101+
ctx := build.Default
102+
ctx.BuildTags = append(ctx.BuildTags, buildTags...)
101103
packageConfig := loader.Config{
102-
Build: &build.Default,
104+
Build: &ctx,
103105
ParserMode: parser.ParseComments,
104106
AllowErrors: true,
105107
}

analyzer_test.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ import (
1717
var _ = Describe("Analyzer", func() {
1818

1919
var (
20-
analyzer *gas.Analyzer
21-
logger *log.Logger
20+
analyzer *gas.Analyzer
21+
logger *log.Logger
22+
buildTags []string
2223
)
2324
BeforeEach(func() {
2425
logger, _ = testutils.NewLogger()
@@ -32,7 +33,7 @@ var _ = Describe("Analyzer", func() {
3233
dir, err := ioutil.TempDir("", "empty")
3334
defer os.RemoveAll(dir)
3435
Expect(err).ShouldNot(HaveOccurred())
35-
err = analyzer.Process(dir)
36+
err = analyzer.Process(buildTags, dir)
3637
Expect(err).Should(HaveOccurred())
3738
Expect(err.Error()).Should(MatchRegexp("no buildable Go source files"))
3839
})
@@ -44,7 +45,7 @@ var _ = Describe("Analyzer", func() {
4445
pkg.AddFile("wonky.go", `func main(){ println("forgot the package")}`)
4546
pkg.Build()
4647

47-
err := analyzer.Process(pkg.Path)
48+
err := analyzer.Process(buildTags, pkg.Path)
4849
Expect(err).Should(HaveOccurred())
4950
Expect(err.Error()).Should(MatchRegexp(`expected 'package'`))
5051

@@ -65,7 +66,7 @@ var _ = Describe("Analyzer", func() {
6566
println("package has two files!")
6667
}`)
6768
pkg.Build()
68-
err := analyzer.Process(pkg.Path)
69+
err := analyzer.Process(buildTags, pkg.Path)
6970
Expect(err).ShouldNot(HaveOccurred())
7071
_, metrics := analyzer.Report()
7172
Expect(metrics.NumFiles).To(Equal(2))
@@ -87,7 +88,7 @@ var _ = Describe("Analyzer", func() {
8788
}`)
8889
pkg1.Build()
8990
pkg2.Build()
90-
err := analyzer.Process(pkg1.Path, pkg2.Path)
91+
err := analyzer.Process(buildTags, pkg1.Path, pkg2.Path)
9192
Expect(err).ShouldNot(HaveOccurred())
9293
_, metrics := analyzer.Report()
9394
Expect(metrics.NumFiles).To(Equal(2))
@@ -104,7 +105,7 @@ var _ = Describe("Analyzer", func() {
104105
defer controlPackage.Close()
105106
controlPackage.AddFile("md5.go", source)
106107
controlPackage.Build()
107-
analyzer.Process(controlPackage.Path)
108+
analyzer.Process(buildTags, controlPackage.Path)
108109
controlIssues, _ := analyzer.Report()
109110
Expect(controlIssues).Should(HaveLen(sample.Errors))
110111

@@ -122,7 +123,7 @@ var _ = Describe("Analyzer", func() {
122123
nosecPackage.AddFile("md5.go", nosecSource)
123124
nosecPackage.Build()
124125

125-
analyzer.Process(nosecPackage.Path)
126+
analyzer.Process(buildTags, nosecPackage.Path)
126127
nosecIssues, _ := analyzer.Report()
127128
Expect(nosecIssues).Should(BeEmpty())
128129
})
@@ -139,7 +140,7 @@ var _ = Describe("Analyzer", func() {
139140
nosecPackage.AddFile("md5.go", nosecSource)
140141
nosecPackage.Build()
141142

142-
analyzer.Process(nosecPackage.Path)
143+
analyzer.Process(buildTags, nosecPackage.Path)
143144
nosecIssues, _ := analyzer.Report()
144145
Expect(nosecIssues).Should(BeEmpty())
145146
})
@@ -156,7 +157,7 @@ var _ = Describe("Analyzer", func() {
156157
nosecPackage.AddFile("md5.go", nosecSource)
157158
nosecPackage.Build()
158159

159-
analyzer.Process(nosecPackage.Path)
160+
analyzer.Process(buildTags, nosecPackage.Path)
160161
nosecIssues, _ := analyzer.Report()
161162
Expect(nosecIssues).Should(HaveLen(sample.Errors))
162163
})
@@ -173,10 +174,23 @@ var _ = Describe("Analyzer", func() {
173174
nosecPackage.AddFile("md5.go", nosecSource)
174175
nosecPackage.Build()
175176

176-
analyzer.Process(nosecPackage.Path)
177+
analyzer.Process(buildTags, nosecPackage.Path)
177178
nosecIssues, _ := analyzer.Report()
178179
Expect(nosecIssues).Should(BeEmpty())
179180
})
181+
182+
It("should pass the build tags", func() {
183+
sample := testutils.SampleCode601[0]
184+
source := sample.Code
185+
analyzer.LoadRules(rules.Generate().Builders())
186+
pkg := testutils.NewTestPackage()
187+
defer pkg.Close()
188+
pkg.AddFile("tags.go", source)
189+
190+
buildTags = append(buildTags, "test")
191+
err := analyzer.Process(buildTags, pkg.Path)
192+
Expect(err).Should(HaveOccurred())
193+
})
180194
})
181195

182196
It("should be possible to overwrite nosec comments, and report issues", func() {
@@ -197,7 +211,7 @@ var _ = Describe("Analyzer", func() {
197211
nosecPackage.AddFile("md5.go", nosecSource)
198212
nosecPackage.Build()
199213

200-
customAnalyzer.Process(nosecPackage.Path)
214+
customAnalyzer.Process(buildTags, nosecPackage.Path)
201215
nosecIssues, _ := customAnalyzer.Report()
202216
Expect(nosecIssues).Should(HaveLen(sample.Errors))
203217

cmd/gas/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ var (
9090
// sort the issues by severity
9191
flagSortIssues = flag.Bool("sort", true, "Sort issues by severity")
9292

93+
// go build tags
94+
flagBuildTags = flag.String("tags", "", "Comma separated list of build tags")
95+
9396
logger *log.Logger
9497
)
9598

@@ -313,7 +316,11 @@ func main() {
313316
packages = append(packages, resolvePackage(pkg, gopaths))
314317
}
315318

316-
if err := analyzer.Process(packages...); err != nil {
319+
var buildTags []string
320+
if *flagBuildTags != "" {
321+
buildTags = strings.Split(*flagBuildTags, ",")
322+
}
323+
if err := analyzer.Process(buildTags, packages...); err != nil {
317324
logger.Fatal(err)
318325
}
319326

rules/rules_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import (
1515
var _ = Describe("gas rules", func() {
1616

1717
var (
18-
logger *log.Logger
19-
config gas.Config
20-
analyzer *gas.Analyzer
21-
runner func(string, []testutils.CodeSample)
18+
logger *log.Logger
19+
config gas.Config
20+
analyzer *gas.Analyzer
21+
runner func(string, []testutils.CodeSample)
22+
buildTags []string
2223
)
2324

2425
BeforeEach(func() {
@@ -34,7 +35,7 @@ var _ = Describe("gas rules", func() {
3435
pkg.AddFile(fmt.Sprintf("sample_%d.go", n), sample.Code)
3536
err := pkg.Build()
3637
Expect(err).ShouldNot(HaveOccurred())
37-
err = analyzer.Process(pkg.Path)
38+
err = analyzer.Process(buildTags, pkg.Path)
3839
Expect(err).ShouldNot(HaveOccurred())
3940
issues, _ := analyzer.Report()
4041
if len(issues) != sample.Errors {

testutils/source.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,5 +720,13 @@ import (
720720
)
721721
func main() {
722722
cgi.Serve(http.FileServer(http.Dir("/usr/share/doc")))
723+
}`, 1}}
724+
// SampleCode601 - Go build tags
725+
SampleCode601 = []CodeSample{{`
726+
// +build test
727+
728+
package main
729+
func main() {
730+
fmt.Println("no package imported error")
723731
}`, 1}}
724732
)

0 commit comments

Comments
 (0)