-
Notifications
You must be signed in to change notification settings - Fork 396
/
generator.go
648 lines (554 loc) · 16.4 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package mockery
import (
"bytes"
"errors"
"fmt"
"go/ast"
"go/build"
"go/types"
"io"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"unicode"
"golang.org/x/tools/imports"
)
var invalidIdentifierChar = regexp.MustCompile("[^[:digit:][:alpha:]_]")
func getGoPathSrc() string {
return filepath.Join(filepath.SplitList(build.Default.GOPATH)[0], "src")
}
func stripChars(str, chr string) string {
return strings.Map(func(r rune) rune {
if strings.IndexRune(chr, r) < 0 {
return r
}
return -1
}, str)
}
// Generator is responsible for generating the string containing
// imports and the mock struct that will later be written out as file.
type Generator struct {
buf bytes.Buffer
ip bool
iface *Interface
pkg string
localPackageName *string
importsWerePopulated bool
localizationCache map[string]string
packagePathToName map[string]string
nameToPackagePath map[string]string
packageRoots []string
}
// NewGenerator builds a Generator.
func NewGenerator(iface *Interface, pkg string, inPackage bool) *Generator {
var roots []string
for _, root := range filepath.SplitList(build.Default.GOPATH) {
roots = append(roots, filepath.Join(root, "src"))
}
g := &Generator{
iface: iface,
pkg: pkg,
ip: inPackage,
localizationCache: make(map[string]string),
packagePathToName: make(map[string]string),
nameToPackagePath: make(map[string]string),
packageRoots: roots,
}
g.addPackageImportWithName("github.com/stretchr/testify/mock", "mock")
return g
}
func (g *Generator) populateImports() {
if g.importsWerePopulated {
return
}
for i := 0; i < g.iface.Type.NumMethods(); i++ {
fn := g.iface.Type.Method(i)
ftype := fn.Type().(*types.Signature)
g.addImportsFromTuple(ftype.Params())
g.addImportsFromTuple(ftype.Results())
g.renderType(g.iface.NamedType)
}
}
func (g *Generator) addImportsFromTuple(list *types.Tuple) {
for i := 0; i < list.Len(); i++ {
// We use renderType here because we need to recursively
// resolve any types to make sure that all named types that
// will appear in the interface file are known
g.renderType(list.At(i).Type())
}
}
func (g *Generator) addPackageImport(pkg *types.Package) string {
return g.addPackageImportWithName(pkg.Path(), pkg.Name())
}
func (g *Generator) addPackageImportWithName(path, name string) string {
path = g.getLocalizedPath(path)
if existingName, pathExists := g.packagePathToName[path]; pathExists {
return existingName
}
nonConflictingName := g.getNonConflictingName(path, name)
g.packagePathToName[path] = nonConflictingName
g.nameToPackagePath[nonConflictingName] = path
return nonConflictingName
}
func (g *Generator) getNonConflictingName(path, name string) string {
if !g.importNameExists(name) {
return name
}
// The path will always contain '/' because it is enforced in getLocalizedPath
// regardless of OS.
directories := strings.Split(path, "/")
cleanedDirectories := make([]string, 0, len(directories))
for _, directory := range directories {
cleaned := invalidIdentifierChar.ReplaceAllString(directory, "_")
cleanedDirectories = append(cleanedDirectories, cleaned)
}
numDirectories := len(cleanedDirectories)
var prospectiveName string
for i := 1; i <= numDirectories; i++ {
prospectiveName = strings.Join(cleanedDirectories[numDirectories-i:], "")
if !g.importNameExists(prospectiveName) {
return prospectiveName
}
}
// Try adding numbers to the given name
i := 2
for {
prospectiveName = fmt.Sprintf("%v%d", name, i)
if !g.importNameExists(prospectiveName) {
return prospectiveName
}
i++
}
}
func (g *Generator) importNameExists(name string) bool {
_, nameExists := g.nameToPackagePath[name]
return nameExists
}
func (g *Generator) getLocalizedPathFromPackage(pkg *types.Package) string {
return g.getLocalizedPath(pkg.Path())
}
func calculateImport(set []string, path string) string {
for _, root := range set {
if strings.HasPrefix(path, root) {
packagePath, err := filepath.Rel(root, path)
if err == nil {
return packagePath
} else {
log.Printf("Unable to localize path %v, %v", path, err)
}
}
}
return path
}
// TODO(@IvanMalison): Is there not a better way to get the actual
// import path of a package?
func (g *Generator) getLocalizedPath(path string) string {
if strings.HasSuffix(path, ".go") {
path, _ = filepath.Split(path)
}
if localized, ok := g.localizationCache[path]; ok {
return localized
}
directories := strings.Split(path, string(filepath.Separator))
numDirectories := len(directories)
vendorIndex := -1
for i := 1; i <= numDirectories; i++ {
dir := directories[numDirectories-i]
if dir == "vendor" {
vendorIndex = numDirectories - i
break
}
}
toReturn := path
if vendorIndex >= 0 {
toReturn = filepath.Join(directories[vendorIndex+1:]...)
} else if filepath.IsAbs(path) {
toReturn = calculateImport(g.packageRoots, path)
}
// Enforce '/' slashes for import paths in every OS.
toReturn = filepath.ToSlash(toReturn)
g.localizationCache[path] = toReturn
return toReturn
}
func (g *Generator) mockName() string {
if g.ip {
if ast.IsExported(g.iface.Name) {
return "Mock" + g.iface.Name
}
first := true
return "mock" + strings.Map(func(r rune) rune {
if first {
first = false
return unicode.ToUpper(r)
}
return r
}, g.iface.Name)
}
return g.iface.Name
}
func (g *Generator) unescapedImportPath(imp *ast.ImportSpec) string {
return strings.Replace(imp.Path.Value, "\"", "", -1)
}
func (g *Generator) getImportStringFromSpec(imp *ast.ImportSpec) string {
if name, ok := g.packagePathToName[g.unescapedImportPath(imp)]; ok {
return fmt.Sprintf("import %s %s\n", name, imp.Path.Value)
}
return fmt.Sprintf("import %s\n", imp.Path.Value)
}
func (g *Generator) sortedImportNames() (importNames []string) {
for name := range g.nameToPackagePath {
importNames = append(importNames, name)
}
sort.Strings(importNames)
return
}
func (g *Generator) generateImports() {
pkgPath := g.nameToPackagePath[g.iface.Pkg.Name()]
// Sort by import name so that we get a deterministic order
for _, name := range g.sortedImportNames() {
path := g.nameToPackagePath[name]
if g.ip && path == pkgPath {
continue
}
g.printf("import %s \"%s\"\n", name, path)
}
}
// GeneratePrologue generates the prologue of the mock.
func (g *Generator) GeneratePrologue(pkg string) {
g.populateImports()
if g.ip {
g.printf("package %s\n\n", g.iface.Pkg.Name())
} else {
g.printf("package %v\n\n", pkg)
}
g.generateImports()
g.printf("\n")
}
// GeneratePrologueNote adds a note after the prologue to the output
// string.
func (g *Generator) GeneratePrologueNote(note string) {
g.printf("// Code generated by mockery v%s. DO NOT EDIT.\n", SemVer)
if note != "" {
g.printf("\n")
for _, n := range strings.Split(note, "\\n") {
g.printf("// %s\n", n)
}
}
g.printf("\n")
}
// ErrNotInterface is returned when the given type is not an interface
// type.
var ErrNotInterface = errors.New("expression not an interface")
func (g *Generator) printf(s string, vals ...interface{}) {
fmt.Fprintf(&g.buf, s, vals...)
}
var builtinTypes = map[string]bool{
"ComplexType": true,
"FloatType": true,
"IntegerType": true,
"Type": true,
"Type1": true,
"bool": true,
"byte": true,
"complex128": true,
"complex64": true,
"error": true,
"float32": true,
"float64": true,
"int": true,
"int16": true,
"int32": true,
"int64": true,
"int8": true,
"rune": true,
"string": true,
"uint": true,
"uint16": true,
"uint32": true,
"uint64": true,
"uint8": true,
"uintptr": true,
}
type namer interface {
Name() string
}
func (g *Generator) renderType(typ types.Type) string {
switch t := typ.(type) {
case *types.Named:
o := t.Obj()
if o.Pkg() == nil || o.Pkg().Name() == "main" || (g.ip && o.Pkg() == g.iface.Pkg) {
return o.Name()
}
return g.addPackageImport(o.Pkg()) + "." + o.Name()
case *types.Basic:
return t.Name()
case *types.Pointer:
return "*" + g.renderType(t.Elem())
case *types.Slice:
return "[]" + g.renderType(t.Elem())
case *types.Array:
return fmt.Sprintf("[%d]%s", t.Len(), g.renderType(t.Elem()))
case *types.Signature:
switch t.Results().Len() {
case 0:
return fmt.Sprintf(
"func(%s)",
g.renderTypeTuple(t.Params()),
)
case 1:
return fmt.Sprintf(
"func(%s) %s",
g.renderTypeTuple(t.Params()),
g.renderType(t.Results().At(0).Type()),
)
default:
return fmt.Sprintf(
"func(%s)(%s)",
g.renderTypeTuple(t.Params()),
g.renderTypeTuple(t.Results()),
)
}
case *types.Map:
kt := g.renderType(t.Key())
vt := g.renderType(t.Elem())
return fmt.Sprintf("map[%s]%s", kt, vt)
case *types.Chan:
switch t.Dir() {
case types.SendRecv:
return "chan " + g.renderType(t.Elem())
case types.RecvOnly:
return "<-chan " + g.renderType(t.Elem())
default:
return "chan<- " + g.renderType(t.Elem())
}
case *types.Struct:
var fields []string
for i := 0; i < t.NumFields(); i++ {
f := t.Field(i)
if f.Anonymous() {
fields = append(fields, g.renderType(f.Type()))
} else {
fields = append(fields, fmt.Sprintf("%s %s", f.Name(), g.renderType(f.Type())))
}
}
return fmt.Sprintf("struct{%s}", strings.Join(fields, ";"))
case *types.Interface:
if t.NumMethods() != 0 {
panic("Unable to mock inline interfaces with methods")
}
return "interface{}"
case namer:
return t.Name()
default:
panic(fmt.Sprintf("un-namable type: %#v (%T)", t, t))
}
}
func (g *Generator) renderTypeTuple(tup *types.Tuple) string {
var parts []string
for i := 0; i < tup.Len(); i++ {
v := tup.At(i)
parts = append(parts, g.renderType(v.Type()))
}
return strings.Join(parts, " , ")
}
func isNillable(typ types.Type) bool {
switch t := typ.(type) {
case *types.Pointer, *types.Array, *types.Map, *types.Interface, *types.Signature, *types.Chan, *types.Slice:
return true
case *types.Named:
return isNillable(t.Underlying())
}
return false
}
type paramList struct {
Names []string
Types []string
Params []string
Nilable []bool
Variadic bool
}
func (g *Generator) genList(list *types.Tuple, variadic bool) *paramList {
var params paramList
if list == nil {
return ¶ms
}
for i := 0; i < list.Len(); i++ {
v := list.At(i)
ts := g.renderType(v.Type())
if variadic && i == list.Len()-1 {
t := v.Type()
switch t := t.(type) {
case *types.Slice:
params.Variadic = true
ts = "..." + g.renderType(t.Elem())
default:
panic("bad variadic type!")
}
}
pname := v.Name()
if g.nameCollides(pname) || pname == "" {
pname = fmt.Sprintf("_a%d", i)
}
params.Names = append(params.Names, pname)
params.Types = append(params.Types, ts)
params.Params = append(params.Params, fmt.Sprintf("%s %s", pname, ts))
params.Nilable = append(params.Nilable, isNillable(v.Type()))
}
return ¶ms
}
func (g *Generator) nameCollides(pname string) bool {
if pname == g.pkg {
return true
}
return g.importNameExists(pname)
}
// ErrNotSetup is returned when the generator is not configured.
var ErrNotSetup = errors.New("not setup")
// Generate builds a string that constitutes a valid go source file
// containing the mock of the relevant interface.
func (g *Generator) Generate() error {
g.populateImports()
if g.iface == nil {
return ErrNotSetup
}
g.printf(
"// %s is an autogenerated mock type for the %s type\n", g.mockName(),
g.iface.Name,
)
g.printf(
"type %s struct {\n\tmock.Mock\n}\n\n", g.mockName(),
)
for i := 0; i < g.iface.Type.NumMethods(); i++ {
fn := g.iface.Type.Method(i)
ftype := fn.Type().(*types.Signature)
fname := fn.Name()
params := g.genList(ftype.Params(), ftype.Variadic())
returns := g.genList(ftype.Results(), false)
if len(params.Names) == 0 {
g.printf("// %s provides a mock function with given fields:\n", fname)
} else {
g.printf(
"// %s provides a mock function with given fields: %s\n", fname,
strings.Join(params.Names, ", "),
)
}
g.printf(
"func (_m *%s) %s(%s) ", g.mockName(), fname,
strings.Join(params.Params, ", "),
)
switch len(returns.Types) {
case 0:
g.printf("{\n")
case 1:
g.printf("%s {\n", returns.Types[0])
default:
g.printf("(%s) {\n", strings.Join(returns.Types, ", "))
}
var formattedParamNames string
for i, name := range params.Names {
if i > 0 {
formattedParamNames += ", "
}
paramType := params.Types[i]
// for variable args, move the ... to the end.
if strings.Index(paramType, "...") == 0 {
name += "..."
}
formattedParamNames += name
}
called := g.generateCalled(params, formattedParamNames) // _m.Called invocation string
if len(returns.Types) > 0 {
g.printf("\tret := %s\n\n", called)
var (
ret []string
)
for idx, typ := range returns.Types {
g.printf("\tvar r%d %s\n", idx, typ)
g.printf("\tif rf, ok := ret.Get(%d).(func(%s) %s); ok {\n",
idx, strings.Join(params.Types, ", "), typ)
g.printf("\t\tr%d = rf(%s)\n", idx, formattedParamNames)
g.printf("\t} else {\n")
if typ == "error" {
g.printf("\t\tr%d = ret.Error(%d)\n", idx, idx)
} else if returns.Nilable[idx] {
g.printf("\t\tif ret.Get(%d) != nil {\n", idx)
g.printf("\t\t\tr%d = ret.Get(%d).(%s)\n", idx, idx, typ)
g.printf("\t\t}\n")
} else {
g.printf("\t\tr%d = ret.Get(%d).(%s)\n", idx, idx, typ)
}
g.printf("\t}\n\n")
ret = append(ret, fmt.Sprintf("r%d", idx))
}
g.printf("\treturn %s\n", strings.Join(ret, ", "))
} else {
g.printf("\t%s\n", called)
}
g.printf("}\n")
}
return nil
}
// generateCalled returns the Mock.Called invocation string and, if necessary, prints the
// steps to prepare its argument list.
//
// It is separate from Generate to avoid cyclomatic complexity through early return statements.
func (g *Generator) generateCalled(list *paramList, formattedParamNames string) string {
namesLen := len(list.Names)
if namesLen == 0 {
return "_m.Called()"
}
if !list.Variadic {
return "_m.Called(" + formattedParamNames + ")"
}
var variadicArgsName string
variadicName := list.Names[namesLen-1]
variadicIface := strings.Contains(list.Types[namesLen-1], "interface{}")
if variadicIface {
// Variadic is already of the interface{} type, so we don't need special handling.
variadicArgsName = variadicName
} else {
// Define _va to avoid "cannot use t (type T) as type []interface {} in append" error
// whenever the variadic type is non-interface{}.
g.printf("\t_va := make([]interface{}, len(%s))\n", variadicName)
g.printf("\tfor _i := range %s {\n\t\t_va[_i] = %s[_i]\n\t}\n", variadicName, variadicName)
variadicArgsName = "_va"
}
// _ca will hold all arguments we'll mirror into Called, one argument per distinct value
// passed to the method.
//
// For example, if the second argument is variadic and consists of three values,
// a total of 4 arguments will be passed to Called. The alternative is to
// pass a total of 2 arguments where the second is a slice with those 3 values from
// the variadic argument. But the alternative is less accessible because it requires
// building a []interface{} before calling Mock methods like On and AssertCalled for
// the variadic argument, and creates incompatibility issues with the diff algorithm
// in github.com/stretchr/testify/mock.
//
// This mirroring will allow argument lists for methods like On and AssertCalled to
// always resemble the expected calls they describe and retain compatibility.
//
// It's okay for us to use the interface{} type, regardless of the actual types, because
// Called receives only interface{} anyway.
g.printf("\tvar _ca []interface{}\n")
if namesLen > 1 {
nonVariadicParamNames := formattedParamNames[0:strings.LastIndex(formattedParamNames, ",")]
g.printf("\t_ca = append(_ca, %s)\n", nonVariadicParamNames)
}
g.printf("\t_ca = append(_ca, %s...)\n", variadicArgsName)
return "_m.Called(_ca...)"
}
func (g *Generator) Write(w io.Writer) error {
opt := &imports.Options{Comments: true}
theBytes := g.buf.Bytes()
res, err := imports.Process("mock.go", theBytes, opt)
if err != nil {
line := "--------------------------------------------------------------------------------------------"
fmt.Fprintf(os.Stderr, "Between the lines is the file (mock.go) mockery generated in-memory but detected as invalid:\n%s\n%s\n%s\n", line, g.buf.String(), line)
return err
}
w.Write(res)
return nil
}