|
| 1 | +package checkers |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "go/ast" |
| 6 | + |
| 7 | + "golang.org/x/tools/go/analysis" |
| 8 | + "golang.org/x/tools/go/ast/inspector" |
| 9 | + |
| 10 | + "github.com/Antonboom/testifylint/internal/analysisutil" |
| 11 | + "github.com/Antonboom/testifylint/internal/testify" |
| 12 | +) |
| 13 | + |
| 14 | +// SuiteMethodSignature warns, when |
| 15 | +// - suite's test methods have arguments or returning values; |
| 16 | +// - suite's custom (user) methods conflicts with (shadows/overrides) suite functionality methods. |
| 17 | +type SuiteMethodSignature struct{} |
| 18 | + |
| 19 | +// NewSuiteMethodSignature constructs SuiteMethodSignature checker. |
| 20 | +func NewSuiteMethodSignature() SuiteMethodSignature { return SuiteMethodSignature{} } |
| 21 | +func (SuiteMethodSignature) Name() string { return "suite-method-signature" } |
| 22 | + |
| 23 | +func (checker SuiteMethodSignature) Check(pass *analysis.Pass, inspector *inspector.Inspector) (diags []analysis.Diagnostic) { |
| 24 | + inspector.Preorder([]ast.Node{(*ast.FuncDecl)(nil)}, func(node ast.Node) { |
| 25 | + fd := node.(*ast.FuncDecl) |
| 26 | + if !isSuiteMethod(pass, fd) { |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + methodName := fd.Name.Name |
| 31 | + rcv := fd.Recv.List[0] |
| 32 | + |
| 33 | + if isSuiteTestMethod(methodName) { |
| 34 | + if fd.Type.Params.NumFields() > 0 || fd.Type.Results.NumFields() > 0 { |
| 35 | + const msg = "test method should not have any arguments or returning values" |
| 36 | + diags = append(diags, *newDiagnostic(checker.Name(), fd, msg)) |
| 37 | + return |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + iface, ok := suiteMethodToInterface[methodName] |
| 42 | + if !ok { |
| 43 | + return |
| 44 | + } |
| 45 | + ifaceObj := analysisutil.ObjectOf(pass.Pkg, testify.SuitePkgPath, iface) |
| 46 | + if ifaceObj == nil { |
| 47 | + return |
| 48 | + } |
| 49 | + if !implements(pass, rcv.Type, ifaceObj) { |
| 50 | + msg := fmt.Sprintf("method conflicts with %s.%s interface", testify.SuitePkgName, iface) |
| 51 | + diags = append(diags, *newDiagnostic(checker.Name(), fd, msg)) |
| 52 | + } |
| 53 | + }) |
| 54 | + return diags |
| 55 | +} |
| 56 | + |
| 57 | +// https://github.com/stretchr/testify/blob/master/suite/interfaces.go |
| 58 | +var suiteMethodToInterface = map[string]string{ |
| 59 | + // NOTE(a.telyshev): We ignore suite.TestingSuite interface, because |
| 60 | + // - suite.Run will not work for suite-types that do not implement it; |
| 61 | + // - this interface has several methods, which may cause a false positive for one of the methods in the suite-type. |
| 62 | + "SetupSuite": "SetupAllSuite", |
| 63 | + "SetupTest": "SetupTestSuite", |
| 64 | + "TearDownSuite": "TearDownAllSuite", |
| 65 | + "TearDownTest": "TearDownTestSuite", |
| 66 | + "BeforeTest": "BeforeTest", |
| 67 | + "AfterTest": "AfterTest", |
| 68 | + "HandleStats": "WithStats", |
| 69 | + "SetupSubTest": "SetupSubTest", |
| 70 | + "TearDownSubTest": "TearDownSubTest", |
| 71 | +} |
0 commit comments