mirror of
https://github.com/securego/gosec.git
synced 2024-12-26 04:25:52 +00:00
Refactor AppendError to check for build.NoGoError (#1273)
This commit is contained in:
parent
9a2d74ffe0
commit
36c81ed69b
2 changed files with 8 additions and 2 deletions
|
@ -16,6 +16,7 @@
|
||||||
package gosec
|
package gosec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"go/build"
|
"go/build"
|
||||||
|
@ -543,7 +544,8 @@ func (gosec *Analyzer) ParseErrors(pkg *packages.Package) error {
|
||||||
// AppendError appends an error to the file errors
|
// AppendError appends an error to the file errors
|
||||||
func (gosec *Analyzer) AppendError(file string, err error) {
|
func (gosec *Analyzer) AppendError(file string, err error) {
|
||||||
// Do not report the error for empty packages (e.g. files excluded from build with a tag)
|
// Do not report the error for empty packages (e.g. files excluded from build with a tag)
|
||||||
if strings.Contains(err.Error(), "no buildable Go source files in") {
|
var noGoErr *build.NoGoError
|
||||||
|
if errors.As(err, &noGoErr) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
errors := make([]Error, 0)
|
errors := make([]Error, 0)
|
||||||
|
|
|
@ -16,6 +16,7 @@ package gosec_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"go/build"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -1311,7 +1312,10 @@ var _ = Describe("Analyzer", func() {
|
||||||
|
|
||||||
Context("when appending errors", func() {
|
Context("when appending errors", func() {
|
||||||
It("should skip error for non-buildable packages", func() {
|
It("should skip error for non-buildable packages", func() {
|
||||||
analyzer.AppendError("test", errors.New(`loading file from package "pkg/test": no buildable Go source files in pkg/test`))
|
err := &build.NoGoError{
|
||||||
|
Dir: "pkg/test",
|
||||||
|
}
|
||||||
|
analyzer.AppendError("test", err)
|
||||||
_, _, errors := analyzer.Report()
|
_, _, errors := analyzer.Report()
|
||||||
Expect(errors).To(BeEmpty())
|
Expect(errors).To(BeEmpty())
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue