From 6837e6af3ff1d8edbb3b9e113a95604560a6018c Mon Sep 17 00:00:00 2001 From: sivchari Date: Thu, 16 Sep 2021 23:46:45 +0900 Subject: [PATCH] update checking target and execution condition --- .golangci.example.yml | 3 ++ go.mod | 2 +- go.sum | 4 +- pkg/config/linters_settings.go | 1 + pkg/golinters/tenv.go | 1 + test/testdata/configs/tenv_all_force.yml | 4 ++ .../configs/{tenv.yml => tenv_force.yml} | 0 test/testdata/tenv_all_force_test.go | 41 +++++++++++++++++++ test/testdata/tenv_force_test.go | 22 ++++++++-- test/testdata/tenv_test.go | 28 +++++++++---- 10 files changed, 93 insertions(+), 13 deletions(-) create mode 100644 test/testdata/configs/tenv_all_force.yml rename test/testdata/configs/{tenv.yml => tenv_force.yml} (100%) create mode 100644 test/testdata/tenv_all_force_test.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 30465f80e52c..fe218e93b6f3 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -616,6 +616,9 @@ linters-settings: tenv: # The following configurations enable checks prior to Go 1.17. force: false + # The all option will run against all method in test file. + # By default, only methods that take *testing.T, *testing.B, and testing.TB as arguments are checked. + all: false unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. diff --git a/go.mod b/go.mod index 1fcc7109990e..4a1ab91ed56f 100644 --- a/go.mod +++ b/go.mod @@ -68,7 +68,7 @@ require ( github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c github.com/shirou/gopsutil/v3 v3.21.8 github.com/sirupsen/logrus v1.8.1 - github.com/sivchari/tenv v1.0.5 + github.com/sivchari/tenv v1.1.6 github.com/sonatard/noctx v0.0.1 github.com/sourcegraph/go-diff v0.6.1 github.com/spf13/cobra v1.2.1 diff --git a/go.sum b/go.sum index a28a578393e1..e6ca72944a9c 100644 --- a/go.sum +++ b/go.sum @@ -630,8 +630,8 @@ github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrf github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= -github.com/sivchari/tenv v1.0.5 h1:4Mc4FyAb1MfYwqxnTZ7c6N4oCECDlV8sTHjuk8C/C84= -github.com/sivchari/tenv v1.0.5/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.1.6 h1:MQLDdDZwfr3IEmNFdN9VFKStp02RTEZqa0G5zlIcc1c= +github.com/sivchari/tenv v1.1.6/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index b74d7f99ed4d..6e64e18c4612 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -456,6 +456,7 @@ type ThelperSettings struct { type TenvSettings struct { Force bool `mapstructure:"force"` + All bool `mapstructure:"all"` } type UnparamSettings struct { diff --git a/pkg/golinters/tenv.go b/pkg/golinters/tenv.go index 98dadbca710a..41313ef46f6e 100644 --- a/pkg/golinters/tenv.go +++ b/pkg/golinters/tenv.go @@ -20,6 +20,7 @@ func NewTenv(settings *config.TenvSettings) *goanalysis.Linter { cfg = map[string]map[string]interface{}{ a.Name: { tenv.F: settings.Force, + tenv.A: settings.All, }, } } diff --git a/test/testdata/configs/tenv_all_force.yml b/test/testdata/configs/tenv_all_force.yml new file mode 100644 index 000000000000..29fd3dc2d043 --- /dev/null +++ b/test/testdata/configs/tenv_all_force.yml @@ -0,0 +1,4 @@ +linters-settings: + tenv: + force: true + all: true diff --git a/test/testdata/configs/tenv.yml b/test/testdata/configs/tenv_force.yml similarity index 100% rename from test/testdata/configs/tenv.yml rename to test/testdata/configs/tenv_force.yml diff --git a/test/testdata/tenv_all_force_test.go b/test/testdata/tenv_all_force_test.go new file mode 100644 index 000000000000..772356e9e440 --- /dev/null +++ b/test/testdata/tenv_all_force_test.go @@ -0,0 +1,41 @@ +// args: -Etenv +// config_path: testdata/configs/tenv_all_force.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + e = os.Setenv("a", "b") // ERROR "variable e is not using testing.Setenv" +) + +func setup() { + os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv" + err := os.Setenv("a", "b") // ERROR "func setup is not using testing.Setenv" + if err != nil { + _ = err + } +} + +func TestF(t *testing.T) { + os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv" + _ = err + } +} + +func testTB(tb testing.TB) { + os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv" + _ = err + } +} diff --git a/test/testdata/tenv_force_test.go b/test/testdata/tenv_force_test.go index 558d1f24c728..01ee5dda1e58 100644 --- a/test/testdata/tenv_force_test.go +++ b/test/testdata/tenv_force_test.go @@ -1,8 +1,10 @@ // args: -Etenv +// config_path: testdata/configs/tenv_force.yml package testdata import ( "os" + "testing" ) var ( @@ -17,9 +19,23 @@ func setup() { } } -func TestF() { - os.Setenv("a", "b") // OK - if err := os.Setenv("a", "b"); err != nil { // OK +func TestF(t *testing.T) { + os.Setenv("a", "b") // ERROR "func TestF is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using testing.Setenv" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + os.Setenv("a", "b") // ERROR "func BenchmarkF is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func BenchmarkF is not using testing.Setenv" + _ = err + } +} + +func testTB(tb testing.TB) { + os.Setenv("a", "b") // ERROR "func testTB is not using testing.Setenv" + if err := os.Setenv("a", "b"); err != nil { // ERROR "func testTB is not using testing.Setenv" _ = err } } diff --git a/test/testdata/tenv_test.go b/test/testdata/tenv_test.go index bd8c0e947dda..023ea68ed02d 100644 --- a/test/testdata/tenv_test.go +++ b/test/testdata/tenv_test.go @@ -1,26 +1,40 @@ // args: -Etenv -// config_path: testdata/configs/tenv.yml package testdata import ( "os" + "testing" ) var ( - e = os.Setenv("a", "b") // ERROR "variable e is not using t.Setenv" + e = os.Setenv("a", "b") // OK ) func setup() { - os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv" - err := os.Setenv("a", "b") // ERROR "func setup is not using t.Setenv" + os.Setenv("a", "b") // OK + err := os.Setenv("a", "b") // OK if err != nil { _ = err } } -func TestF() { - os.Setenv("a", "b") // ERROR "func TestF is not using t.Setenv" - if err := os.Setenv("a", "b"); err != nil { // ERROR "func TestF is not using t.Setenv" +func TestF(t *testing.T) { + os.Setenv("a", "b") // OK + if err := os.Setenv("a", "b"); err != nil { // OK + _ = err + } +} + +func BenchmarkF(b *testing.B) { + os.Setenv("a", "b") // OK + if err := os.Setenv("a", "b"); err != nil { // OK + _ = err + } +} + +func testTB(tb testing.TB) { + os.Setenv("a", "b") // OK + if err := os.Setenv("a", "b"); err != nil { // OK _ = err } }