Skip to content

Commit

Permalink
Merge pull request #5 from sho-hata/fix/missing-call-all-tests
Browse files Browse the repository at this point in the history
Fix/missing call all tests
  • Loading branch information
sho-hata authored Aug 14, 2022
2 parents 24da3dc + 942ae25 commit c8ddd46
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ wip

## Installation
```
go install github.com/sho-hata/tparagen/cmd/tparagen@latest
go install -v github.com/sho-hata/tparagen/cmd/tparagen@latest
```


Expand Down
6 changes: 5 additions & 1 deletion process.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Process(filename string, src []byte) ([]byte, error) {
funcHasParallelMethod,
rangeStatementOverTestCasesExists,
rangeStatementHasParallelMethod,
insertedSubtest,
loopVarReInitialised bool

loopVariableUsedInRun *string
Expand All @@ -51,7 +52,9 @@ func Process(filename string, src []byte) ([]byte, error) {
case *ast.ExprStmt:
ast.Inspect(v, func(n ast.Node) bool {
// Check if the test method is calling t.Parallel
if !funcHasParallelMethod {
// If t.Parallel() is inserted once in a subtest in subsequent processing, `funcHasParallelmethod` becomes true.
// Therefore, also check the flag indicating whether the subtest has already been inserted or not.
if !funcHasParallelMethod && !insertedSubtest {
funcHasParallelMethod = methodParallelIsCalledInTestFunction(n, testVar)
}

Expand Down Expand Up @@ -80,6 +83,7 @@ func Process(filename string, src []byte) ([]byte, error) {
if fun, ok := funcArg.(*ast.FuncLit); ok {
tpStmt := buildTParallelStmt(fun.Body.Lbrace)
fun.Body.List = append([]ast.Stmt{tpStmt}, fun.Body.List...)
insertedSubtest = true
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions process_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tparagen

import (
"fmt"
"reflect"
"testing"
)
Expand Down Expand Up @@ -111,6 +112,38 @@ func TestFunctionOneTestRunMissingCallToParallel(t *testing.T) {
fmt.Println("1")
})
}
`,
},
{
testCase: "missing called t.Parallel in all tests not range",
src: `package t
import "testing"
func TestFunctionRunMissingCallAllTestsToParallelNotRange(t *testing.T) {
t.Run("1", func(x *testing.T) {
fmt.Println("1")
})
t.Run("2", func(t *testing.T) {
fmt.Println("2")
})
}
`,
want: `package t
import "testing"
func TestFunctionRunMissingCallAllTestsToParallelNotRange(t *testing.T) {
t.Parallel()
t.Run("1", func(x *testing.T) {
t.Parallel()
fmt.Println("1")
})
t.Run("2", func(t *testing.T) {
t.Parallel()
fmt.Println("2")
})
}
`,
},
{
Expand Down Expand Up @@ -362,6 +395,9 @@ func TestFunctionRangeMissingCallToParallel(t *testing.T) {
tt := tt
t.Run(tt.testCase, func(t *testing.T) {
t.Parallel()
if tt.testCase == "missing called t.Parallel in all tests" {
fmt.Print()
}
got, err := Process("./testdata/t/t_test.go", []byte(tt.src))
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 9 additions & 0 deletions testdata/t/t_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ func TestFunctionSecondOneTestRunMissingCallToParallel(t *testing.T) {
})
}

func TestFunctionRunMissingCallAllTestsToParallelNotRange(t *testing.T) {
t.Run("1", func(x *testing.T) {
fmt.Println("1")
})
t.Run("2", func(t *testing.T) {
fmt.Println("2")
})
}

func TestFunctionMissingCallToParallelAndRangeNotUsingRangeValueInTDotRun(t *testing.T) {
testCases := []struct {
name string
Expand Down

0 comments on commit c8ddd46

Please sign in to comment.