@@ -5,6 +5,12 @@ import (
55 "go/build"
66 "go/parser"
77 "go/token"
8+ "io/ioutil"
9+ "os"
10+ "path"
11+ "regexp"
12+ "runtime"
13+ "strings"
814 "testing"
915)
1016
@@ -83,6 +89,93 @@ func TestAll(t *testing.T) {
8389 test (t , CheckAsserts | CheckBlank )
8490}
8591
92+ const testVendorMain = `
93+ package main
94+
95+ import "github.com/testlog"
96+
97+ func main() {
98+ // returns an error that is not checked
99+ testlog.Info()
100+ }`
101+ const testLog = `
102+ package testlog
103+
104+ func Info() error {
105+ return nil
106+ }`
107+
108+ func TestIgnore (t * testing.T ) {
109+ if strings .HasPrefix (runtime .Version (), "go1.5" ) && os .Getenv ("GO15VENDOREXPERIMENT" ) != "1" {
110+ // skip tests if running in go1.5 and vendoring is not enabled
111+ t .SkipNow ()
112+ }
113+
114+ // copy testvendor directory into current directory for test
115+ testVendorDir , err := ioutil .TempDir ("." , "testvendor" )
116+ if err != nil {
117+ t .Fatalf ("unable to create testvendor directory: %v" , err )
118+ }
119+ defer os .RemoveAll (testVendorDir )
120+
121+ if err := ioutil .WriteFile (path .Join (testVendorDir , "main.go" ), []byte (testVendorMain ), 0755 ); err != nil {
122+ t .Fatalf ("Failed to write testvendor main: %v" , err )
123+ }
124+ if err := os .MkdirAll (path .Join (testVendorDir , "vendor/github.com/testlog" ), 0755 ); err != nil {
125+ t .Fatalf ("MkdirAll failed: %v" , err )
126+ }
127+ if err := ioutil .WriteFile (path .Join (testVendorDir , "vendor/github.com/testlog/testlog.go" ), []byte (testLog ), 0755 ); err != nil {
128+ t .Fatalf ("Failed to write testlog: %v" , err )
129+ }
130+
131+ cases := []struct {
132+ ignore map [string ]* regexp.Regexp
133+ numExpectedErrs int
134+ }{
135+ // basic case has one error
136+ {
137+ ignore : nil ,
138+ numExpectedErrs : 1 ,
139+ },
140+ // ignoring vendored import works
141+ {
142+ ignore : map [string ]* regexp.Regexp {
143+ path .Join ("github.com/kisielk/errcheck/internal/errcheck" , testVendorDir , "vendor/github.com/testlog" ): regexp .MustCompile ("Info" ),
144+ },
145+ },
146+ // non-vendored path ignores vendored import
147+ {
148+ ignore : map [string ]* regexp.Regexp {
149+ "github.com/testlog" : regexp .MustCompile ("Info" ),
150+ },
151+ },
152+ }
153+
154+ for i , currCase := range cases {
155+ checker := & Checker {
156+ Ignore : currCase .ignore ,
157+ }
158+ err := checker .CheckPackages (path .Join ("github.com/kisielk/errcheck/internal/errcheck" , testVendorDir ))
159+
160+ if currCase .numExpectedErrs == 0 {
161+ if err != nil {
162+ t .Errorf ("Case %d: expected no errors, but got: %v" , i , err )
163+ }
164+ continue
165+ }
166+
167+ uerr , ok := err .(* UncheckedErrors )
168+ if ! ok {
169+ t .Errorf ("Case %d: wrong error type returned" , i )
170+ continue
171+ }
172+
173+ if currCase .numExpectedErrs != len (uerr .Errors ) {
174+ t .Errorf ("Case %d:\n Expected: %d errors\n Actual: %d errors" , i , currCase .numExpectedErrs , len (uerr .Errors ))
175+ }
176+ }
177+ }
178+
86179func test (t * testing.T , f flags ) {
87180 var (
88181 asserts bool = f & CheckAsserts != 0
0 commit comments