Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions testdata/src/test/setenv_test.go
Original file line number Diff line number Diff line change
@@ -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()
})
}
15 changes: 9 additions & 6 deletions tparallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down