/
generator.go
180 lines (163 loc) · 4.76 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
package generator
import (
"context"
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"strconv"
"strings"
"golang.org/x/tools/go/packages"
)
const (
mockgenPackage = "go.uber.org/mock/mockgen"
)
type runnerFactory func(ctx context.Context, cmdExecutable string, cmdArgs ...string) Runner
type Generator struct {
UseGoRun bool
DryRun bool
MockSetName string
RestArgs []string
}
func (g *Generator) Generate(ctx context.Context, rf runnerFactory) error {
mockSet, externalPkg, err := g.findMockSet(".")
if err != nil {
return err
}
var cmdExecutable string
var cmdArgs []string
if g.UseGoRun {
cmdExecutable = "go"
cmdArgs = append(cmdArgs, "run", mockgenPackage)
} else {
cmdExecutable = "mockgen"
}
cmdArgs = append(cmdArgs, g.RestArgs...)
if externalPkg != "" {
cmdArgs = append(cmdArgs, externalPkg)
} else {
cmdArgs = append(cmdArgs, ".")
}
cmdArgs = append(cmdArgs, strings.Join(mockSet, ","))
if g.DryRun {
fmt.Printf("%s %s", cmdExecutable, strings.Join(cmdArgs, " "))
return nil
}
runner := rf(ctx, cmdExecutable, cmdArgs...)
if err := runner.Run(); err != nil {
return err
}
return nil
}
func (g *Generator) findMockSet(sourceDir string) ([]string, string, error) {
if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
return g.findMockSetFromPackage(sourceDir)
}
return g.findMockSetFromDirectory(sourceDir)
}
func (g *Generator) findMockSetFromPackage(packagePath string) ([]string, string, error) {
pp, err := packages.Load(&packages.Config{}, packagePath)
if err != nil {
return nil, "", err
}
pkg := pp[0]
return g.findMockSetFromDirectory(pkg.Module.Dir)
}
func (g *Generator) findMockSetFromDirectory(sourceDir string) ([]string, string, error) {
fset := token.NewFileSet()
parsed, err := parser.ParseDir(fset, sourceDir, nil, 0)
if err != nil {
return nil, "", fmt.Errorf("failed to parse: %w", err)
}
for _, p := range parsed {
for _, f := range p.Files {
mockSet, externalPkg, err := lookupFromScope(fset, f.Scope, g.MockSetName)
if err != nil {
if errors.Is(err, errMockSetNotFound) {
continue
}
return nil, "", err
}
if externalPkg != "" {
for _, imp := range f.Imports {
impPath, _ := strconv.Unquote(imp.Path.Value)
if imp.Name != nil && externalPkg == imp.Name.Name {
externalPkg = impPath
break
}
splittedImpPath := strings.Split(impPath, "/")
if externalPkg == splittedImpPath[len(splittedImpPath) - 1] {
externalPkg = impPath
break
}
}
}
return mockSet, externalPkg, nil
}
}
return nil, "", fmt.Errorf("mock set %s not found", g.MockSetName)
}
var errMockSetNotFound = errors.New("mock set notfound")
func lookupFromScope(fset *token.FileSet, s *ast.Scope, mockSetName string) ([]string, string, error) {
obj := s.Lookup(mockSetName)
if obj == nil {
return nil, "", errMockSetNotFound
}
if obj.Kind != ast.Var {
return nil, "", errMockSetNotFound
}
vs, ok := obj.Decl.(*ast.ValueSpec)
if !ok {
return nil, "", errMockSetNotFound
}
var externalPackageName string
var mockSet []string
for _, v := range vs.Values {
cmp, ok := v.(*ast.CompositeLit)
if !ok {
return nil, "", newInvalidMockSetError("mock set is not a slice", fset, v)
}
for _, v := range cmp.Elts {
funCall, ok := v.(*ast.CallExpr)
if !ok {
return nil, "", newInvalidMockSetError("mock set contains not a function call", fset, v)
}
// new(...)
newIdent, ok := funCall.Fun.(*ast.Ident)
if !ok {
return nil, "", newInvalidMockSetError("mock set contains not a new(...) function call", fset, v)
}
if newIdent.Name != "new" {
return nil, "", newInvalidMockSetError("mock set contains not a new(...) function call", fset, v)
}
if len(funCall.Args) != 1 {
return nil, "", newInvalidMockSetError("mock set contains not a new(...) function call", fset, v)
}
arg := funCall.Args[0]
switch arg := arg.(type) {
case *ast.Ident:
mockSet = append(mockSet, arg.Name)
case *ast.SelectorExpr:
x := arg.X
ident, ok := x.(*ast.Ident)
if !ok {
return nil, "", newInvalidMockSetError("not an external package selector", fset, arg)
}
if externalPackageName != "" && externalPackageName != ident.Name {
return nil, "", newInvalidMockSetError("mixing external package interfaces is not allowed", fset, arg)
}
externalPackageName = ident.Name
mockSet = append(mockSet, arg.Sel.Name)
default:
return nil, "", newInvalidMockSetError("not an interface is passed to new(...) function call", fset, arg)
}
}
}
return mockSet, externalPackageName, nil
}
func newInvalidMockSetError(msg string, fset *token.FileSet, e ast.Expr) error {
pos := fset.Position(e.Pos())
return fmt.Errorf("%s at file %s, line %d, column %d", msg, pos.Filename, pos.Line, pos.Column)
}