aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/moricho/tparallel/tparallel.go
blob: 3139e0425df35dfac35b403d63b0a62c7c8c5255 (plain)
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
package tparallel

import (
	"go/types"

	"github.com/gostaticanalysis/analysisutil"
	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/passes/buildssa"

	"github.com/moricho/tparallel/pkg/ssafunc"
)

const doc = "tparallel detects inappropriate usage of t.Parallel() method in your Go test codes."

// Analyzer analyzes Go test codes whether they use t.Parallel() appropriately
// by using SSA (Single Static Assignment)
var Analyzer = &analysis.Analyzer{
	Name: "tparallel",
	Doc:  doc,
	Run:  run,
	Requires: []*analysis.Analyzer{
		buildssa.Analyzer,
	},
}

func run(pass *analysis.Pass) (interface{}, error) {
	ssaanalyzer := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA)

	obj := analysisutil.ObjectOf(pass, "testing", "T")
	if obj == nil {
		// skip checking
		return nil, nil
	}
	testTyp, testPkg := obj.Type(), obj.Pkg()

	p, _, _ := types.LookupFieldOrMethod(testTyp, true, testPkg, "Parallel")
	parallel, _ := p.(*types.Func)
	c, _, _ := types.LookupFieldOrMethod(testTyp, true, testPkg, "Cleanup")
	cleanup, _ := c.(*types.Func)

	testMap := getTestMap(ssaanalyzer, testTyp) // ex. {Test1: [TestSub1, TestSub2], Test2: [TestSub1, TestSub2, TestSub3], ...}
	for top, subs := range testMap {
		if len(subs) == 0 {
			continue
		}
		isParallelTop := ssafunc.IsCalled(top, parallel)
		isPararellSub := false
		for _, sub := range subs {
			isPararellSub = ssafunc.IsCalled(sub, parallel)
			if isPararellSub {
				break
			}
		}

		if ssafunc.IsDeferCalled(top) {
			useCleanup := ssafunc.IsCalled(top, cleanup)
			if isPararellSub && !useCleanup {
				pass.Reportf(top.Pos(), "%s should use t.Cleanup instead of defer", top.Name())
			}
		}

		if isParallelTop == isPararellSub {
			continue
		} else if isPararellSub {
			pass.Reportf(top.Pos(), "%s should call t.Parallel on the top level as well as its subtests", top.Name())
		} else if isParallelTop {
			pass.Reportf(top.Pos(), "%s's subtests should call t.Parallel", top.Name())
		}
	}

	return nil, nil
}