Skip to content

Commit 6837e6a

Browse files
committed
update checking target and execution condition
1 parent 0e0ab7a commit 6837e6a

10 files changed

+93
-13
lines changed

.golangci.example.yml

+3
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ linters-settings:
616616
tenv:
617617
# The following configurations enable checks prior to Go 1.17.
618618
force: false
619+
# The all option will run against all method in test file.
620+
# By default, only methods that take *testing.T, *testing.B, and testing.TB as arguments are checked.
621+
all: false
619622

620623
unparam:
621624
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ require (
6868
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c
6969
github.com/shirou/gopsutil/v3 v3.21.8
7070
github.com/sirupsen/logrus v1.8.1
71-
github.com/sivchari/tenv v1.0.5
71+
github.com/sivchari/tenv v1.1.6
7272
github.com/sonatard/noctx v0.0.1
7373
github.com/sourcegraph/go-diff v0.6.1
7474
github.com/spf13/cobra v1.2.1

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ type ThelperSettings struct {
456456

457457
type TenvSettings struct {
458458
Force bool `mapstructure:"force"`
459+
All bool `mapstructure:"all"`
459460
}
460461

461462
type UnparamSettings struct {

pkg/golinters/tenv.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func NewTenv(settings *config.TenvSettings) *goanalysis.Linter {
2020
cfg = map[string]map[string]interface{}{
2121
a.Name: {
2222
tenv.F: settings.Force,
23+
tenv.A: settings.All,
2324
},
2425
}
2526
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
linters-settings:
2+
tenv:
3+
force: true
4+
all: true
File renamed without changes.

test/testdata/tenv_all_force_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// args: -Etenv
2+
// config_path: testdata/configs/tenv_all_force.yml
3+
package testdata
4+
5+
import (
6+
"os"
7+
"testing"
8+
)
9+
10+
var (
11+
e = os.Setenv("a", "b") // ERROR "variable e is not using testing.Setenv"
12+
)
13+
14+
func setup() {
15+
os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv"
16+
err := os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv"
17+
if err != nil {
18+
_ = err
19+
}
20+
}
21+
22+
func TestF(t *testing.T) {
23+
os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv"
24+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv"
25+
_ = err
26+
}
27+
}
28+
29+
func BenchmarkF(b *testing.B) {
30+
os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv"
31+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv"
32+
_ = err
33+
}
34+
}
35+
36+
func testTB(tb testing.TB) {
37+
os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv"
38+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv"
39+
_ = err
40+
}
41+
}

test/testdata/tenv_force_test.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// args: -Etenv
2+
// config_path: testdata/configs/tenv_force.yml
23
package testdata
34

45
import (
56
"os"
7+
"testing"
68
)
79

810
var (
@@ -17,9 +19,23 @@ func setup() {
1719
}
1820
}
1921

20-
func TestF() {
21-
os.Setenv("a", "b") // OK
22-
if err := os.Setenv("a", "b"); err != nil { // OK
22+
func TestF(t *testing.T) {
23+
os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv"
24+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv"
25+
_ = err
26+
}
27+
}
28+
29+
func BenchmarkF(b *testing.B) {
30+
os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv"
31+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv"
32+
_ = err
33+
}
34+
}
35+
36+
func testTB(tb testing.TB) {
37+
os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv"
38+
if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv"
2339
_ = err
2440
}
2541
}

test/testdata/tenv_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
// args: -Etenv
2-
// config_path: testdata/configs/tenv.yml
32
package testdata
43

54
import (
65
"os"
6+
"testing"
77
)
88

99
var (
10-
e = os.Setenv("a", "b") // ERROR "variable e is not using t.Setenv"
10+
e = os.Setenv("a", "b") // OK
1111
)
1212

1313
func setup() {
14-
os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
15-
err := os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv"
14+
os.Setenv("a", "b") // OK
15+
err := os.Setenv("a", "b") // OK
1616
if err != nil {
1717
_ = err
1818
}
1919
}
2020

21-
func TestF() {
22-
os.Setenv("a", "b") // ERROR "func TestF is not using t.Setenv"
23-
if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using t.Setenv"
21+
func TestF(t *testing.T) {
22+
os.Setenv("a", "b") // OK
23+
if err := os.Setenv("a", "b"); err != nil { // OK
24+
_ = err
25+
}
26+
}
27+
28+
func BenchmarkF(b *testing.B) {
29+
os.Setenv("a", "b") // OK
30+
if err := os.Setenv("a", "b"); err != nil { // OK
31+
_ = err
32+
}
33+
}
34+
35+
func testTB(tb testing.TB) {
36+
os.Setenv("a", "b") // OK
37+
if err := os.Setenv("a", "b"); err != nil { // OK
2438
_ = err
2539
}
2640
}

0 commit comments

Comments
 (0)