diff --git a/testdata/src/test/setenv_test.go b/testdata/src/test/setenv_test.go new file mode 100644 index 0000000..b5a6079 --- /dev/null +++ b/testdata/src/test/setenv_test.go @@ -0,0 +1,22 @@ +package test + +import ( + "testing" +) + +func Test_SetEnv_Func1(t *testing.T) { + teardown := setup("Test_Setenv_Func1") + t.Cleanup(teardown) + + t.Setenv("foo", "bar") + + t.Run("Setenv_Func1_Sub1", func(t *testing.T) { + call("Setenv_Func1_Sub1") + t.Parallel() + }) + + t.Run("Setenv_Func1_Sub2", func(t *testing.T) { + call("Setenv_Func1_Sub2") + t.Parallel() + }) +} diff --git a/tparallel.go b/tparallel.go index 3139e04..72cb3e7 100644 --- a/tparallel.go +++ b/tparallel.go @@ -37,31 +37,34 @@ func run(pass *analysis.Pass) (interface{}, error) { parallel, _ := p.(*types.Func) c, _, _ := types.LookupFieldOrMethod(testTyp, true, testPkg, "Cleanup") cleanup, _ := c.(*types.Func) + s, _, _ := types.LookupFieldOrMethod(testTyp, true, testPkg, "Setenv") + setenv, _ := s.(*types.Func) testMap := getTestMap(ssaanalyzer, testTyp) // ex. {Test1: [TestSub1, TestSub2], Test2: [TestSub1, TestSub2, TestSub3], ...} for top, subs := range testMap { if len(subs) == 0 { continue } + usesSetenv := ssafunc.IsCalled(top, setenv) isParallelTop := ssafunc.IsCalled(top, parallel) - isPararellSub := false + isParallelSub := false for _, sub := range subs { - isPararellSub = ssafunc.IsCalled(sub, parallel) - if isPararellSub { + isParallelSub = ssafunc.IsCalled(sub, parallel) + if isParallelSub { break } } if ssafunc.IsDeferCalled(top) { useCleanup := ssafunc.IsCalled(top, cleanup) - if isPararellSub && !useCleanup { + if isParallelSub && !useCleanup { pass.Reportf(top.Pos(), "%s should use t.Cleanup instead of defer", top.Name()) } } - if isParallelTop == isPararellSub { + if isParallelTop == isParallelSub { continue - } else if isPararellSub { + } else if isParallelSub && !usesSetenv { 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())