forked from gotestyourself/gotest.tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.go
375 lines (330 loc) · 9.85 KB
/
migrate.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
package main
import (
"go/ast"
"go/token"
"go/types"
"log"
"path"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/loader"
)
const (
pkgTestifyAssert = "github.com/stretchr/testify/assert"
pkgGopkgTestifyAssert = "gopkg.in/stretchr/testify.v1/assert"
pkgTestifyRequire = "github.com/stretchr/testify/require"
pkgGopkgTestifyRequire = "gopkg.in/stretchr/testify.v1/require"
pkgAssert = "gotest.tools/assert"
pkgCmp = "gotest.tools/assert/cmp"
)
const (
funcNameAssert = "Assert"
funcNameCheck = "Check"
)
var allTestifyPks = []string{
pkgTestifyAssert,
pkgTestifyRequire,
pkgGopkgTestifyAssert,
pkgGopkgTestifyRequire,
}
type migration struct {
file *ast.File
fileset *token.FileSet
importNames importNames
pkgInfo *loader.PackageInfo
}
func migrateFile(migration migration) {
astutil.Apply(migration.file, nil, replaceCalls(migration))
updateImports(migration)
}
func updateImports(migration migration) {
for _, remove := range allTestifyPks {
astutil.DeleteImport(migration.fileset, migration.file, remove)
}
var alias string
if migration.importNames.assert != path.Base(pkgAssert) {
alias = migration.importNames.assert
}
astutil.AddNamedImport(migration.fileset, migration.file, alias, pkgAssert)
if migration.importNames.cmp != path.Base(pkgCmp) {
alias = migration.importNames.cmp
}
astutil.AddNamedImport(migration.fileset, migration.file, alias, pkgCmp)
}
type emptyNode struct{}
func (n emptyNode) Pos() token.Pos {
return 0
}
func (n emptyNode) End() token.Pos {
return 0
}
var removeNode = emptyNode{}
func replaceCalls(migration migration) func(cursor *astutil.Cursor) bool {
return func(cursor *astutil.Cursor) bool {
var newNode ast.Node
switch typed := cursor.Node().(type) {
case *ast.SelectorExpr:
newNode = getReplacementTestingT(typed, migration.importNames)
case *ast.CallExpr:
newNode = getReplacementAssertion(typed, migration)
case *ast.AssignStmt:
newNode = getReplacementAssignment(typed, migration)
}
switch newNode {
case nil:
case removeNode:
cursor.Delete()
default:
cursor.Replace(newNode)
}
return true
}
}
func getReplacementTestingT(selector *ast.SelectorExpr, names importNames) ast.Node {
xIdent, ok := selector.X.(*ast.Ident)
if !ok {
return nil
}
if selector.Sel.Name != "TestingT" || !names.matchesTestify(xIdent) {
return nil
}
return &ast.SelectorExpr{
X: &ast.Ident{Name: names.assert, NamePos: xIdent.NamePos},
Sel: selector.Sel,
}
}
func getReplacementAssertion(callExpr *ast.CallExpr, migration migration) ast.Node {
tcall, ok := newTestifyCallFromNode(callExpr, migration)
if !ok {
return nil
}
if len(tcall.expr.Args) < 2 {
return convertTestifySingleArgCall(tcall)
}
return convertTestifyAssertion(tcall, migration)
}
func getReplacementAssignment(assign *ast.AssignStmt, migration migration) ast.Node {
if isAssignmentFromAssertNew(assign, migration) {
return removeNode
}
return nil
}
func convertTestifySingleArgCall(tcall call) ast.Node {
switch tcall.selExpr.Sel.Name {
case "TestingT":
// handled as SelectorExpr
return nil
case "New":
// handled by getReplacementAssignment
return nil
default:
log.Printf("%s: skipping unknown selector", tcall.StringWithFileInfo())
return nil
}
}
// nolint: gocyclo
func convertTestifyAssertion(tcall call, migration migration) ast.Node {
imports := migration.importNames
switch tcall.selExpr.Sel.Name {
case "NoError", "NoErrorf":
return convertNoError(tcall, imports)
case "True", "Truef":
return convertTrue(tcall, imports)
case "False", "Falsef":
return convertFalse(tcall, imports)
case "Equal", "Equalf", "Exactly", "Exactlyf", "EqualValues", "EqualValuesf":
return convertEqual(tcall, migration)
case "Contains", "Containsf":
return convertTwoArgComparison(tcall, imports, "Contains")
case "Len", "Lenf":
return convertTwoArgComparison(tcall, imports, "Len")
case "Panics", "Panicsf":
return convertOneArgComparison(tcall, imports, "Panics")
case "EqualError", "EqualErrorf":
return convertEqualError(tcall, imports)
case "Error", "Errorf":
return convertError(tcall, imports)
case "Empty", "Emptyf":
return convertEmpty(tcall, imports)
case "Nil", "Nilf":
return convertNil(tcall, migration)
case "NotNil", "NotNilf":
return convertNegativeComparison(tcall, imports, &ast.Ident{Name: "nil"}, 2)
case "NotEqual", "NotEqualf":
return convertNegativeComparison(tcall, imports, tcall.arg(2), 3)
case "Fail", "Failf":
return convertFail(tcall, "Error")
case "FailNow", "FailNowf":
return convertFail(tcall, "Fatal")
case "NotEmpty", "NotEmptyf":
return convertNotEmpty(tcall, imports)
case "NotZero", "NotZerof":
zero := &ast.BasicLit{Kind: token.INT, Value: "0"}
return convertNegativeComparison(tcall, imports, zero, 2)
}
log.Printf("%s: skipping unsupported assertion", tcall.StringWithFileInfo())
return nil
}
func newCallExpr(x, sel string, args []ast.Expr) *ast.CallExpr {
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: x},
Sel: &ast.Ident{Name: sel},
},
Args: args,
}
}
func newCallExprArgs(t ast.Expr, cmp ast.Expr, extra ...ast.Expr) []ast.Expr {
return append(append([]ast.Expr{t}, cmp), extra...)
}
func newCallExprWithPosition(tcall call, imports importNames, args []ast.Expr) *ast.CallExpr {
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{
Name: imports.assert,
NamePos: tcall.xIdent.NamePos,
},
Sel: &ast.Ident{Name: tcall.assert},
},
Args: args,
}
}
func convertNoError(tcall call, imports importNames) ast.Node {
// use assert.NilError() for require.NoError()
if tcall.assert == funcNameAssert {
return newCallExprWithoutComparison(tcall, imports, "NilError")
}
// use assert.Check() for assert.NoError()
return newCallExprWithoutComparison(tcall, imports, "Check")
}
func convertEqualError(tcall call, imports importNames) ast.Node {
if tcall.assert == funcNameAssert {
return newCallExprWithoutComparison(tcall, imports, "Error")
}
return convertTwoArgComparison(tcall, imports, "Error")
}
func newCallExprWithoutComparison(tcall call, imports importNames, name string) ast.Node {
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{
Name: imports.assert,
NamePos: tcall.xIdent.NamePos,
},
Sel: &ast.Ident{Name: name},
},
Args: tcall.expr.Args,
}
}
func convertOneArgComparison(tcall call, imports importNames, cmpName string) ast.Node {
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
newCallExpr(imports.cmp, cmpName, []ast.Expr{tcall.arg(1)}),
tcall.extraArgs(2)...))
}
func convertTrue(tcall call, imports importNames) ast.Node {
return newCallExprWithPosition(tcall, imports, tcall.expr.Args)
}
func convertFalse(tcall call, imports importNames) ast.Node {
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
&ast.UnaryExpr{Op: token.NOT, X: tcall.arg(1)},
tcall.extraArgs(2)...))
}
func convertEqual(tcall call, migration migration) ast.Node {
imports := migration.importNames
hasExtraArgs := len(tcall.extraArgs(3)) > 0
cmpEqual := convertTwoArgComparison(tcall, imports, "Equal")
if tcall.assert == funcNameAssert {
cmpEqual = newCallExprWithoutComparison(tcall, imports, "Equal")
}
cmpDeepEqual := convertTwoArgComparison(tcall, imports, "DeepEqual")
if tcall.assert == funcNameAssert && !hasExtraArgs {
cmpDeepEqual = newCallExprWithoutComparison(tcall, imports, "DeepEqual")
}
gotype := walkForType(migration.pkgInfo, tcall.arg(1))
if isUnknownType(gotype) {
gotype = walkForType(migration.pkgInfo, tcall.arg(2))
}
if isUnknownType(gotype) {
return cmpDeepEqual
}
switch gotype.Underlying().(type) {
case *types.Basic:
return cmpEqual
default:
return cmpDeepEqual
}
}
func convertTwoArgComparison(tcall call, imports importNames, cmpName string) ast.Node {
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
newCallExpr(imports.cmp, cmpName, tcall.args(1, 3)),
tcall.extraArgs(3)...))
}
func convertError(tcall call, imports importNames) ast.Node {
cmpArgs := []ast.Expr{
tcall.arg(1),
&ast.BasicLit{Kind: token.STRING, Value: `""`}}
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
newCallExpr(imports.cmp, "ErrorContains", cmpArgs),
tcall.extraArgs(2)...))
}
func convertEmpty(tcall call, imports importNames) ast.Node {
cmpArgs := []ast.Expr{
tcall.arg(1),
&ast.BasicLit{Kind: token.INT, Value: "0"},
}
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
newCallExpr(imports.cmp, "Len", cmpArgs),
tcall.extraArgs(2)...))
}
func convertNil(tcall call, migration migration) ast.Node {
gotype := walkForType(migration.pkgInfo, tcall.arg(1))
if gotype != nil && gotype.String() == "error" {
return convertNoError(tcall, migration.importNames)
}
return convertOneArgComparison(tcall, migration.importNames, "Nil")
}
func convertNegativeComparison(
tcall call,
imports importNames,
right ast.Expr,
extra int,
) ast.Node {
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
&ast.BinaryExpr{X: tcall.arg(1), Op: token.NEQ, Y: right},
tcall.extraArgs(extra)...))
}
func convertFail(tcall call, selector string) ast.Node {
extraArgs := tcall.extraArgs(1)
if len(extraArgs) > 1 {
selector = selector + "f"
}
return &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: tcall.testingT(),
Sel: &ast.Ident{Name: selector},
},
Args: extraArgs,
}
}
func convertNotEmpty(tcall call, imports importNames) ast.Node {
lenExpr := &ast.CallExpr{
Fun: &ast.Ident{Name: "len"},
Args: tcall.args(1, 2),
}
zeroExpr := &ast.BasicLit{Kind: token.INT, Value: "0"}
return newCallExprWithPosition(tcall, imports,
newCallExprArgs(
tcall.testingT(),
&ast.BinaryExpr{X: lenExpr, Op: token.NEQ, Y: zeroExpr},
tcall.extraArgs(2)...))
}