Skip to content

Commit 49728f7

Browse files
committed
internal/tdtest: make function detection more robust
Instead of detecting the package (which was already brittle), test the first argument of the closure of the second argument of Run. This allows other packages to wrap the tdtest.Run function, as long as they keep the same signature. This is necessary to handle errors in cuetest.Run. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: If8dea69244fec9111916df667b0a8c09dc85fa4d Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1167818 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 1763cea commit 49728f7

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

internal/tdtest/update.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,7 @@ func findFileAndPackage(path string, pkgs []*packages.Package) (*ast.File, *pack
204204
return nil, nil
205205
}
206206

207-
const (
208-
typeT = "*cuelang.org/go/internal/tdtest.T"
209-
tdtestParen = `("cuelang.org/go/internal/tdtest")`
210-
)
207+
const typeT = "*cuelang.org/go/internal/tdtest.T"
211208

212209
// findCalls finds all call expressions within a given block for functions
213210
// or methods defined within the tdtest package.
@@ -229,10 +226,19 @@ func (i *info) findCalls(block *ast.BlockStmt, names ...string) []*callInfo {
229226
info := i.testPkg.TypesInfo
230227
for _, name := range names {
231228
if sel.Sel.Name == name {
232-
if info.TypeOf(sel.X).String() == typeT {
233-
} else if ident, ok := sel.X.(*ast.Ident); !ok {
234-
return true // Run method.
235-
} else if id, ok := info.Uses[ident].(*types.PkgName); ok && strings.Contains(id.String(), tdtestParen) {
229+
receiver := info.TypeOf(sel.X).String()
230+
if receiver == typeT {
231+
// Method.
232+
} else if len(c.Args) == 3 {
233+
// Run function.
234+
fn := c.Args[2].(*ast.FuncLit)
235+
if len(fn.Type.Params.List) != 2 {
236+
return true
237+
}
238+
argType := info.TypeOf(fn.Type.Params.List[0].Type).String()
239+
if argType != typeT {
240+
return true
241+
}
236242
} else {
237243
return true
238244
}

0 commit comments

Comments
 (0)