@@ -93,7 +93,8 @@ func TestWhitelist(t *testing.T) {
93
93
94
94
}
95
95
96
- const testVendorMain = `
96
+ func TestIgnore (t * testing.T ) {
97
+ const testVendorMain = `
97
98
package main
98
99
99
100
import "github.com/testlog"
@@ -102,14 +103,13 @@ const testVendorMain = `
102
103
// returns an error that is not checked
103
104
testlog.Info()
104
105
}`
105
- const testLog = `
106
+ const testLog = `
106
107
package testlog
107
108
108
109
func Info() error {
109
110
return nil
110
111
}`
111
112
112
- func TestIgnore (t * testing.T ) {
113
113
if strings .HasPrefix (runtime .Version (), "go1.5" ) && os .Getenv ("GO15VENDOREXPERIMENT" ) != "1" {
114
114
// skip tests if running in go1.5 and vendoring is not enabled
115
115
t .SkipNow ()
@@ -179,6 +179,86 @@ func TestIgnore(t *testing.T) {
179
179
}
180
180
}
181
181
182
+ func TestWithoutGeneratedCode (t * testing.T ) {
183
+ const testVendorMain = `
184
+ // Code generated by protoc-gen-go. DO NOT EDIT.
185
+ package main
186
+
187
+ import "github.com/testlog"
188
+
189
+ func main() {
190
+ // returns an error that is not checked
191
+ testlog.Info()
192
+ }`
193
+ const testLog = `
194
+ package testlog
195
+
196
+ func Info() error {
197
+ return nil
198
+ }`
199
+
200
+ if strings .HasPrefix (runtime .Version (), "go1.5" ) && os .Getenv ("GO15VENDOREXPERIMENT" ) != "1" {
201
+ // skip tests if running in go1.5 and vendoring is not enabled
202
+ t .SkipNow ()
203
+ }
204
+
205
+ // copy testvendor directory into current directory for test
206
+ testVendorDir , err := ioutil .TempDir ("." , "testvendor" )
207
+ if err != nil {
208
+ t .Fatalf ("unable to create testvendor directory: %v" , err )
209
+ }
210
+ defer os .RemoveAll (testVendorDir )
211
+
212
+ if err := ioutil .WriteFile (path .Join (testVendorDir , "main.go" ), []byte (testVendorMain ), 0755 ); err != nil {
213
+ t .Fatalf ("Failed to write testvendor main: %v" , err )
214
+ }
215
+ if err := os .MkdirAll (path .Join (testVendorDir , "vendor/github.com/testlog" ), 0755 ); err != nil {
216
+ t .Fatalf ("MkdirAll failed: %v" , err )
217
+ }
218
+ if err := ioutil .WriteFile (path .Join (testVendorDir , "vendor/github.com/testlog/testlog.go" ), []byte (testLog ), 0755 ); err != nil {
219
+ t .Fatalf ("Failed to write testlog: %v" , err )
220
+ }
221
+
222
+ cases := []struct {
223
+ withoutGeneratedCode bool
224
+ numExpectedErrs int
225
+ }{
226
+ // basic case has one error
227
+ {
228
+ withoutGeneratedCode : false ,
229
+ numExpectedErrs : 1 ,
230
+ },
231
+ // ignoring vendored import works
232
+ {
233
+ withoutGeneratedCode : true ,
234
+ numExpectedErrs : 0 ,
235
+ },
236
+ }
237
+
238
+ for i , currCase := range cases {
239
+ checker := NewChecker ()
240
+ checker .WithoutGeneratedCode = currCase .withoutGeneratedCode
241
+ err := checker .CheckPackages (path .Join ("github.com/kisielk/errcheck/internal/errcheck" , testVendorDir ))
242
+
243
+ if currCase .numExpectedErrs == 0 {
244
+ if err != nil {
245
+ t .Errorf ("Case %d: expected no errors, but got: %v" , i , err )
246
+ }
247
+ continue
248
+ }
249
+
250
+ uerr , ok := err .(* UncheckedErrors )
251
+ if ! ok {
252
+ t .Errorf ("Case %d: wrong error type returned" , i )
253
+ continue
254
+ }
255
+
256
+ if currCase .numExpectedErrs != len (uerr .Errors ) {
257
+ t .Errorf ("Case %d:\n Expected: %d errors\n Actual: %d errors" , i , currCase .numExpectedErrs , len (uerr .Errors ))
258
+ }
259
+ }
260
+ }
261
+
182
262
func test (t * testing.T , f flags ) {
183
263
var (
184
264
asserts bool = f & CheckAsserts != 0
0 commit comments