Skip to content

Commit

Permalink
Check unvendored version of package paths for ignore parameter
Browse files Browse the repository at this point in the history
Fixes #102
  • Loading branch information
nmiyake committed Sep 15, 2016
1 parent be8bde4 commit 6202bbd
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
22 changes: 20 additions & 2 deletions internal/errcheck/errcheck.go
Expand Up @@ -10,14 +10,13 @@ import (
"go/ast"
"go/build"
"go/token"
"go/types"
"os"
"regexp"
"sort"
"strings"
"sync"

"go/types"

"golang.org/x/tools/go/loader"
)

Expand Down Expand Up @@ -220,12 +219,31 @@ func (v *visitor) ignoreCall(call *ast.CallExpr) bool {
if re, ok := v.ignore[pkg.Path()]; ok {
return re.MatchString(id.Name)
}

// if current package being considered is vendored, check to see if it should be ignored based
// on the unvendored path.
if nonVendoredPkg, ok := nonVendoredPkgPath(pkg.Path()); ok {
if re, ok := v.ignore[nonVendoredPkg]; ok {
return re.MatchString(id.Name)
}
}
}
}

return false
}

// nonVendoredPkgPath returns the unvendored version of the provided package path (or returns the provided path if it
// does not represent a vendored path). The second return value is true if the provided package was vendored, false
// otherwise.
func nonVendoredPkgPath(pkgPath string) (string, bool) {
lastVendorIndex := strings.LastIndex(pkgPath, "/vendor/")
if lastVendorIndex == -1 {
return pkgPath, false
}
return pkgPath[lastVendorIndex+len("/vendor/"):], true
}

// errorsByArg returns a slice s such that
// len(s) == number of return types of call
// s[i] == true iff return type at position i from left is an error type
Expand Down
55 changes: 54 additions & 1 deletion internal/errcheck/errcheck_test.go
Expand Up @@ -5,10 +5,14 @@ import (
"go/build"
"go/parser"
"go/token"
"regexp"
"testing"
)

const testPackage = "github.com/kisielk/errcheck/testdata"
const (
testPackage = "github.com/kisielk/errcheck/testdata"
testVendorPackage = "github.com/kisielk/errcheck/testvendor"
)

var (
uncheckedMarkers map[marker]bool
Expand Down Expand Up @@ -83,6 +87,55 @@ func TestAll(t *testing.T) {
test(t, CheckAsserts|CheckBlank)
}

func TestIgnore(t *testing.T) {
cases := []struct {
ignore map[string]*regexp.Regexp
numExpectedErrs int
}{
// basic case has one error
{
ignore: nil,
numExpectedErrs: 1,
},
// ignoring vendored import works
{
ignore: map[string]*regexp.Regexp{
"github.com/kisielk/errcheck/testvendor/vendor/github.com/testlog": regexp.MustCompile("Info"),
},
},
// non-vendored path ignores vendored import
{
ignore: map[string]*regexp.Regexp{
"github.com/testlog": regexp.MustCompile("Info"),
},
},
}

for i, currCase := range cases {
checker := &Checker{
Ignore: currCase.ignore,
}
err := checker.CheckPackages(testVendorPackage)

if currCase.numExpectedErrs == 0 {
if err != nil {
t.Errorf("Case %d: expected no errors, but got: %v", i, err)
}
continue
}

uerr, ok := err.(*UncheckedErrors)
if !ok {
t.Errorf("Case %d: wrong error type returned", i)
continue
}

if currCase.numExpectedErrs != len(uerr.Errors) {
t.Errorf("Case %d:\nExpected: %d errors\nActual: %d errors", i, currCase.numExpectedErrs, len(uerr.Errors))
}
}
}

func test(t *testing.T, f flags) {
var (
asserts bool = f&CheckAsserts != 0
Expand Down
10 changes: 10 additions & 0 deletions testvendor/main.go
@@ -0,0 +1,10 @@
package main

import (
"github.com/testlog"
)

func main() {
// returns an error that is not checked
testlog.Info()
}
5 changes: 5 additions & 0 deletions testvendor/vendor/github.com/testlog/testlog.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6202bbd

Please sign in to comment.