Skip to content

Commit

Permalink
Merge pull request #21 from magnetikonline/test-workflow-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kunwardeep authored Jun 7, 2022
2 parents 705a44e + ac8eaea commit c61ab88
Show file tree
Hide file tree
Showing 84 changed files with 316 additions and 1,393 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/main.yml

This file was deleted.

25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test

on:
pull_request:
push:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v3
- name: Setup Golang
uses: actions/setup-go@v3
with:
go-version: ~1.17
- name: Get dependencies
run: go get -v -t -d ./...
- name: Build
run: go build -v
- name: Test
run: go test -v -race ./...
167 changes: 81 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,146 +1,141 @@
# paralleltest


[![Build Status](https://github.com/kunwardeep/paralleltest/workflows/CI/badge.svg)](https://github.com/kunwardeep/paralleltest/actions)

[![Test](https://github.com/kunwardeep/paralleltest/actions/workflows/test.yml/badge.svg)](https://github.com/kunwardeep/paralleltest/actions/workflows/test.yml)

The Go linter `paralleltest` checks that the t.Parallel gets called for the test method and for the range of test cases within the test.


## Usage

```
```sh
paralleltest ./...
```

## Examples

### Missing t.Parallel() in the test method
### Missing `t.Parallel()` in the test method

```go
// bad
func TestFunctionMissingCallToParallel(t *testing.T) {
}
}

// good
func TestFunctionMissingCallToParallel(t *testing.T) {
t.Parallel()
// ^ call to t.Parallel()
}
t.Parallel()
// ^ call to t.Parallel()
}
// Error displayed
// Function TestFunctionMissingCallToParallel missing the call to method parallel
```

### Missing t.Parallel() in the range method
### Missing `t.Parallel()` in the range method

```go
// bad
func TestFunctionRangeMissingCallToParallel(t *testing.T) {
t.Parallel()
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
testCases := []struct {
name string
}{{name: "foo"}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fmt.Println(tc.name)
})
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fmt.Println(tc.name)
})
}
}

// good
func TestFunctionRangeMissingCallToParallel(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// ^ call to t.Parallel()
fmt.Println(tc.name)
})
}
}
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// ^ call to t.Parallel()
fmt.Println(tc.name)
})
}
}
// Error displayed
// Range statement for test TestFunctionRangeMissingCallToParallel missing the call to method parallel in t.Run
```



### t.Parallel() is called in the range method but testcase variable not being used
### `t.Parallel()` is called in the range method but testcase variable not being used

```go
// bad
func TestFunctionRangeNotUsingRangeValueInTDotRun(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run("this is a test name", func(t *testing.T) {
// ^ call to tc.name missing
t.Parallel()
fmt.Println(tc.name)
})
}
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run("this is a test name", func(t *testing.T) {
// ^ call to tc.name missing
t.Parallel()
fmt.Println(tc.name)
})
}
}

// good
func TestFunctionRangeNotUsingRangeValueInTDotRun(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// ^ call to tc.name
t.Parallel()
fmt.Println(tc.name)
})
}
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// ^ call to tc.name
t.Parallel()
fmt.Println(tc.name)
})
}
}
// Error displayed
// Range statement for test TestFunctionRangeNotUsingRangeValueInTDotRun does not use range value in t.Run
```

### t.Parallel() is called in the range method and test case variable tc being used, but is not reinitialised (<a href="https://gist.github.com/kunwardeep/80c2e9f3d3256c894898bae82d9f75d0" target="_blank">More Info</a>)
### `t.Parallel()` is called in the range method and test case variable tc being used, but is not reinitialised (<a href="https://gist.github.com/kunwardeep/80c2e9f3d3256c894898bae82d9f75d0" target="_blank">More Info</a>)
```go
// bad
func TestFunctionRangeNotReInitialisingVariable(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fmt.Println(tc.name)
})
}
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fmt.Println(tc.name)
})
}
}

// good
func TestFunctionRangeNotReInitialisingVariable(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
tc:=tc
// ^ tc variable reinitialised
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fmt.Println(tc.name)
})
}
t.Parallel()

testCases := []struct {
name string
}{{name: "foo"}}
for _, tc := range testCases {
tc:=tc
// ^ tc variable reinitialised
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
fmt.Println(tc.name)
})
}
}
// Error displayed
// Range statement for test TestFunctionRangeNotReInitialisingVariable does not reinitialise the variable tc
Expand Down
78 changes: 0 additions & 78 deletions coverage.out

This file was deleted.

8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
module github.com/kunwardeep/paralleltest

go 1.14
go 1.17

require golang.org/x/tools v0.1.10

require (
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
Loading

0 comments on commit c61ab88

Please sign in to comment.