From 3615c9daa0e752fc844dd01ae7eccf3eee169968 Mon Sep 17 00:00:00 2001 From: sivchari Date: Tue, 21 Sep 2021 20:21:18 +0900 Subject: [PATCH 01/12] fix conflict --- .golangci.example.yml | 4 ++++ go.mod | 1 + go.sum | 21 ++++++++++++++++++++ pkg/config/linters_settings.go | 5 +++++ pkg/golinters/tenv.go | 33 ++++++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 7 +++++++ test/testdata/configs/tenv.yml | 3 +++ test/testdata/tenv_force_test.go | 25 ++++++++++++++++++++++++ test/testdata/tenv_test.go | 26 +++++++++++++++++++++++++ 9 files changed, 125 insertions(+) create mode 100644 pkg/golinters/tenv.go create mode 100644 test/testdata/configs/tenv.yml create mode 100644 test/testdata/tenv_force_test.go create mode 100644 test/testdata/tenv_test.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 496cb5e485b2..badea3878a2b 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -627,6 +627,10 @@ linters-settings: name: true begin: true + tenv: + # The following configurations enable checks prior to Go 1.17. + f: false + unparam: # Inspect exported functions, default is false. Set to true if no external program/library imports your code. # XXX: if you enable this setting, unparam will report a lot of false-positives in text editors: diff --git a/go.mod b/go.mod index 449fb85dc3df..bb9d6b01ddc1 100644 --- a/go.mod +++ b/go.mod @@ -69,6 +69,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.2 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 8733e21d36bc..244b80102ecf 100644 --- a/go.sum +++ b/go.sum @@ -350,6 +350,8 @@ github.com/gostaticanalysis/forcetypeassert v0.0.0-20200621232751-01d4955beaa5 h github.com/gostaticanalysis/forcetypeassert v0.0.0-20200621232751-01d4955beaa5/go.mod h1:qZEedyP/sY1lTGV1uJ3VhWZ2mqag3IkWsDHVbplHXak= github.com/gostaticanalysis/nilerr v0.1.1 h1:ThE+hJP0fEp4zWLkWHWcRyI2Od0p7DlgYG3Uqrmrcpk= github.com/gostaticanalysis/nilerr v0.1.1/go.mod h1:wZYb6YI5YAxxq0i1+VJbY0s2YONW0HU0GPE3+5PWN4A= +github.com/gostaticanalysis/testutil v0.4.0 h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoISdUv3PPQgHY= +github.com/gostaticanalysis/testutil v0.4.0/go.mod h1:bLIoPefWXrRi/ssLFWX1dx7Repi5x3CuviD3dgAZaBU= github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= @@ -379,6 +381,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI= +github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -414,6 +418,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/jonboulle/clockwork v0.2.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= +github.com/josharian/txtarfs v0.0.0-20210218200122-0702f000015a h1:8NZHLa6Gp0hW6xJ0c3F1Kse7dJw30fOcDzHuF9sLbnE= +github.com/josharian/txtarfs v0.0.0-20210218200122-0702f000015a/go.mod h1:izVPOvVRsHiKkeGCT6tYBNWyDVuzj9wAaBb5R9qamfw= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -565,6 +571,14 @@ github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1y github.com/onsi/gomega v1.13.0 h1:7lLHu94wT9Ij0o6EWWclhu0aOh32VxhkwEJvzuWPeak= github.com/onsi/gomega v1.13.0/go.mod h1:lRk9szgn8TxENtWd0Tp4c3wjlRfMTMH27I+3Je41yGY= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/otiai10/copy v1.2.0 h1:HvG945u96iNadPoG2/Ja2+AUJeW5YuFQMixq9yirC+k= +github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw= +github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE= +github.com/otiai10/curr v1.0.0 h1:TJIWdbX0B+kpNagQrjgq8bCMrbhiuX73M2XwgtDMoOI= +github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs= +github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo= +github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc= +github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= @@ -652,6 +666,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.2 h1:5t8caLMocyfH37rLM66WCSBAEfyZTzvwkLhQ6ufgWWw= +github.com/sivchari/tenv v1.0.2/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -701,6 +717,10 @@ github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b h1:HxLVTlqcHhFAz3nWUcuvpH7WuOMv8LQoCWmruLfFH2U= github.com/tdakkota/asciicheck v0.0.0-20200416200610-e657995f937b/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM= +github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA= +github.com/tenntenn/modver v1.0.1/go.mod h1:bePIyQPb7UeioSRkw3Q0XeMhYZSMx9B8ePqg6SAMGH0= +github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpRQGxTSkNYKJ51yaw6ChIqO+Je8UqsTKN/cDag= +github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY= github.com/tetafro/godot v1.4.11 h1:BVoBIqAf/2QdbFmSwAWnaIqDivZdOV0ZRwEm6jivLKw= github.com/tetafro/godot v1.4.11/go.mod h1:LR3CJpxDVGlYOWn3ZZg1PgNZdTUvzsZWu8xaEohUpn8= github.com/timakin/bodyclose v0.0.0-20200424151742-cb6215831a94 h1:ig99OeTyDwQWhPe2iw9lwfQVF1KB3Q4fpP3X7/2VBG8= @@ -1088,6 +1108,7 @@ golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee/go.mod h1:emZCQorbCU4vsT4f golang.org/x/tools v0.0.0-20210105154028-b0ab187a4818/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.1-0.20210205202024-ef80cdb6ec6d/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 9ef297f9c35f..f2e331b3e457 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -132,6 +132,7 @@ type LintersSettings struct { Tagliatelle TagliatelleSettings Testpackage TestpackageSettings Thelper ThelperSettings + Tenv TenvSettings Unparam UnparamSettings Unused StaticCheckSettings Varcheck VarCheckSettings @@ -463,6 +464,10 @@ type ThelperSettings struct { } `mapstructure:"tb"` } +type TenvSettings struct { + Force bool `mapstructure:"f"` +} + type UnparamSettings struct { CheckExported bool `mapstructure:"check-exported"` Algo string diff --git a/pkg/golinters/tenv.go b/pkg/golinters/tenv.go new file mode 100644 index 000000000000..7cd72a5ed488 --- /dev/null +++ b/pkg/golinters/tenv.go @@ -0,0 +1,33 @@ +package golinters + +import ( + "github.com/sivchari/tenv" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewTenv(settings *config.TenvSettings) *goanalysis.Linter { + a := tenv.Analyzer + + analyzers := []*analysis.Analyzer{ + a, + } + + var cfg map[string]map[string]interface{} + if settings != nil { + cfg = map[string]map[string]interface{}{ + a.Name: { + tenv.F: settings.Force, + }, + } + } + + return goanalysis.NewLinter( + a.Name, + "tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17", + analyzers, + cfg, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index ecc2efec97e3..7b3347ec78c5 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -116,6 +116,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var staticcheckCfg *config.StaticCheckSettings var stylecheckCfg *config.StaticCheckSettings var tagliatelleCfg *config.TagliatelleSettings + var tenvCfg *config.TenvSettings var testpackageCfg *config.TestpackageSettings var thelperCfg *config.ThelperSettings var unusedCfg *config.StaticCheckSettings @@ -140,6 +141,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { staticcheckCfg = &m.cfg.LintersSettings.Staticcheck stylecheckCfg = &m.cfg.LintersSettings.Stylecheck tagliatelleCfg = &m.cfg.LintersSettings.Tagliatelle + tenvCfg = &m.cfg.LintersSettings.Tenv testpackageCfg = &m.cfg.LintersSettings.Testpackage thelperCfg = &m.cfg.LintersSettings.Thelper unusedCfg = &m.cfg.LintersSettings.Unused @@ -522,6 +524,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithLoadForGoAnalysis(). WithURL("https://github.com/Antonboom/nilnil"). WithSince("v1.43.0"), + linter.NewConfig(golinters.NewTenv(tenvCfg)). + WithSince("v1.43.0"). + WithPresets(linter.PresetPerformance, linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/sivchari/tenv"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). diff --git a/test/testdata/configs/tenv.yml b/test/testdata/configs/tenv.yml new file mode 100644 index 000000000000..e46377d141fa --- /dev/null +++ b/test/testdata/configs/tenv.yml @@ -0,0 +1,3 @@ +linters-settings: + tenv: + f: true diff --git a/test/testdata/tenv_force_test.go b/test/testdata/tenv_force_test.go new file mode 100644 index 000000000000..558d1f24c728 --- /dev/null +++ b/test/testdata/tenv_force_test.go @@ -0,0 +1,25 @@ +// args: -Etenv +package testdata + +import ( + "os" +) + +var ( + e = os.Setenv("a", "b") // OK +) + +func setup() { + os.Setenv("a", "b") // OK + err := os.Setenv("a", "b") // OK + if err != nil { + _ = err + } +} + +func TestF() { + os.Setenv("a", "b") // OK + if err := os.Setenv("a", "b"); err != nil { // OK + _ = err + } +} diff --git a/test/testdata/tenv_test.go b/test/testdata/tenv_test.go new file mode 100644 index 000000000000..bd8c0e947dda --- /dev/null +++ b/test/testdata/tenv_test.go @@ -0,0 +1,26 @@ +// args: -Etenv +// config_path: testdata/configs/tenv.yml +package testdata + +import ( + "os" +) + +var ( + e = os.Setenv("a", "b") // ERROR "variable e is not using t.Setenv" +) + +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" + 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" + _ = err + } +} From 16920db711c7735370984f9b80b13139a2c66cbb Mon Sep 17 00:00:00 2001 From: sivchari Date: Tue, 21 Sep 2021 20:22:52 +0900 Subject: [PATCH 02/12] fix conflict --- .golangci.example.yml | 2 +- go.mod | 2 +- go.sum | 3 +++ pkg/config/linters_settings.go | 2 +- test/testdata/configs/tenv.yml | 2 +- 5 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index badea3878a2b..f03d878610c2 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -629,7 +629,7 @@ linters-settings: tenv: # The following configurations enable checks prior to Go 1.17. - f: false + force: 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 bb9d6b01ddc1..d9155fa197f9 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.2 + github.com/sivchari/tenv v1.0.3 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 244b80102ecf..5196fe11b6c5 100644 --- a/go.sum +++ b/go.sum @@ -668,6 +668,9 @@ 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.2 h1:5t8caLMocyfH37rLM66WCSBAEfyZTzvwkLhQ6ufgWWw= github.com/sivchari/tenv v1.0.2/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.0.3 h1:cMsN53QjWu6hujLypG+kPPb6lriIGyq3tDPB6HCRfhs= +github.com/sivchari/tenv v1.0.3/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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index f2e331b3e457..01383ab6d59c 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -465,7 +465,7 @@ type ThelperSettings struct { } type TenvSettings struct { - Force bool `mapstructure:"f"` + Force bool `mapstructure:"force"` } type UnparamSettings struct { diff --git a/test/testdata/configs/tenv.yml b/test/testdata/configs/tenv.yml index e46377d141fa..7ec079ba9581 100644 --- a/test/testdata/configs/tenv.yml +++ b/test/testdata/configs/tenv.yml @@ -1,3 +1,3 @@ linters-settings: tenv: - f: true + force: true From 925096c7b758c67c27e247a0ce9b4146cb267937 Mon Sep 17 00:00:00 2001 From: sivchari Date: Tue, 7 Sep 2021 16:28:24 +0900 Subject: [PATCH 03/12] fix conflict --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index 5196fe11b6c5..15cdc7e3ee25 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,6 @@ 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.2 h1:5t8caLMocyfH37rLM66WCSBAEfyZTzvwkLhQ6ufgWWw= -github.com/sivchari/tenv v1.0.2/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= github.com/sivchari/tenv v1.0.3 h1:cMsN53QjWu6hujLypG+kPPb6lriIGyq3tDPB6HCRfhs= github.com/sivchari/tenv v1.0.3/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= From 98580f1170b0ccb91487b3c9027dc5bbfd4ed9d1 Mon Sep 17 00:00:00 2001 From: sivchari Date: Wed, 8 Sep 2021 15:25:56 +0900 Subject: [PATCH 04/12] fix docs --- pkg/golinters/tenv.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/golinters/tenv.go b/pkg/golinters/tenv.go index 7cd72a5ed488..98dadbca710a 100644 --- a/pkg/golinters/tenv.go +++ b/pkg/golinters/tenv.go @@ -26,7 +26,7 @@ func NewTenv(settings *config.TenvSettings) *goanalysis.Linter { return goanalysis.NewLinter( a.Name, - "tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17", + a.Doc, analyzers, cfg, ).WithLoadMode(goanalysis.LoadModeSyntax) From 249fba188254ef2963661c15374c206413c63f96 Mon Sep 17 00:00:00 2001 From: sivchari Date: Thu, 16 Sep 2021 15:01:06 +0900 Subject: [PATCH 05/12] update version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index d9155fa197f9..2f73053d9d7c 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.3 + github.com/sivchari/tenv v1.0.4 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 15cdc7e3ee25..ec2fb9259a60 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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.3 h1:cMsN53QjWu6hujLypG+kPPb6lriIGyq3tDPB6HCRfhs= -github.com/sivchari/tenv v1.0.3/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.0.4 h1:EQl5jOQdO1J0AmMreaqIQepK2SYfuKqr+bbDAstn33g= +github.com/sivchari/tenv v1.0.4/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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= From d0cbceb6fca9caf48c5a25557379b1969b9727c6 Mon Sep 17 00:00:00 2001 From: sivchari Date: Thu, 16 Sep 2021 15:40:52 +0900 Subject: [PATCH 06/12] fix --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2f73053d9d7c..f0b1f0cf331c 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.4 + github.com/sivchari/tenv v1.0.5 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 ec2fb9259a60..73168676d60b 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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.4 h1:EQl5jOQdO1J0AmMreaqIQepK2SYfuKqr+bbDAstn33g= -github.com/sivchari/tenv v1.0.4/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +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/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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= From 682dc62d2ffe24c2f02679d0793e5cfef583fa7c Mon Sep 17 00:00:00 2001 From: sivchari Date: Thu, 16 Sep 2021 23:46:45 +0900 Subject: [PATCH 07/12] 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 f03d878610c2..0ec409cbef1b 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -630,6 +630,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 f0b1f0cf331c..50b8acf9e9d2 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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 73168676d60b..62038e582370 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 01383ab6d59c..e49e36377bb0 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -466,6 +466,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 } } From ecb3d45460ee7fb2e976459596524be9088e47a4 Mon Sep 17 00:00:00 2001 From: sivchari Date: Tue, 21 Sep 2021 20:23:43 +0900 Subject: [PATCH 08/12] fix conflict --- .golangci.example.yml | 2 - go.mod | 2 +- go.sum | 4 +- pkg/config/linters_settings.go | 3 +- pkg/golinters/tenv.go | 1 - .../{tenv_all_force.yml => tenv_all.yml} | 1 - test/testdata/configs/tenv_force.yml | 3 -- ...env_all_force_test.go => tenv_all_test.go} | 2 +- test/testdata/tenv_force_test.go | 41 ------------------- test/testdata/tenv_test.go | 12 +++--- 10 files changed, 11 insertions(+), 60 deletions(-) rename test/testdata/configs/{tenv_all_force.yml => tenv_all.yml} (71%) delete mode 100644 test/testdata/configs/tenv_force.yml rename test/testdata/{tenv_all_force_test.go => tenv_all_test.go} (95%) delete mode 100644 test/testdata/tenv_force_test.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 0ec409cbef1b..505b414ecfa8 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -628,8 +628,6 @@ linters-settings: begin: true 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 diff --git a/go.mod b/go.mod index 50b8acf9e9d2..bb601bdd64a6 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.1.6 + github.com/sivchari/tenv v1.2.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 62038e582370..c80d67359c78 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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.1.6 h1:MQLDdDZwfr3IEmNFdN9VFKStp02RTEZqa0G5zlIcc1c= -github.com/sivchari/tenv v1.1.6/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.2.6 h1:OveRTnUsGqP65AaPBggmsdThsy5EpQcYakaTntauRAw= +github.com/sivchari/tenv v1.2.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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index e49e36377bb0..c32f2570706a 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -465,8 +465,7 @@ type ThelperSettings struct { } type TenvSettings struct { - Force bool `mapstructure:"force"` - All bool `mapstructure:"all"` + All bool `mapstructure:"all"` } type UnparamSettings struct { diff --git a/pkg/golinters/tenv.go b/pkg/golinters/tenv.go index 41313ef46f6e..8e3e9c61f601 100644 --- a/pkg/golinters/tenv.go +++ b/pkg/golinters/tenv.go @@ -19,7 +19,6 @@ func NewTenv(settings *config.TenvSettings) *goanalysis.Linter { if settings != nil { 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.yml similarity index 71% rename from test/testdata/configs/tenv_all_force.yml rename to test/testdata/configs/tenv_all.yml index 29fd3dc2d043..f0ae4fd226c4 100644 --- a/test/testdata/configs/tenv_all_force.yml +++ b/test/testdata/configs/tenv_all.yml @@ -1,4 +1,3 @@ linters-settings: tenv: - force: true all: true diff --git a/test/testdata/configs/tenv_force.yml b/test/testdata/configs/tenv_force.yml deleted file mode 100644 index 7ec079ba9581..000000000000 --- a/test/testdata/configs/tenv_force.yml +++ /dev/null @@ -1,3 +0,0 @@ -linters-settings: - tenv: - force: true diff --git a/test/testdata/tenv_all_force_test.go b/test/testdata/tenv_all_test.go similarity index 95% rename from test/testdata/tenv_all_force_test.go rename to test/testdata/tenv_all_test.go index 772356e9e440..babdd134a180 100644 --- a/test/testdata/tenv_all_force_test.go +++ b/test/testdata/tenv_all_test.go @@ -1,5 +1,5 @@ // args: -Etenv -// config_path: testdata/configs/tenv_all_force.yml +// config_path: testdata/configs/tenv_all.yml package testdata import ( diff --git a/test/testdata/tenv_force_test.go b/test/testdata/tenv_force_test.go deleted file mode 100644 index 01ee5dda1e58..000000000000 --- a/test/testdata/tenv_force_test.go +++ /dev/null @@ -1,41 +0,0 @@ -// args: -Etenv -// config_path: testdata/configs/tenv_force.yml -package testdata - -import ( - "os" - "testing" -) - -var ( - e = os.Setenv("a", "b") // OK -) - -func setup() { - os.Setenv("a", "b") // OK - err := os.Setenv("a", "b") // OK - 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_test.go b/test/testdata/tenv_test.go index 023ea68ed02d..d4f6c90d2d91 100644 --- a/test/testdata/tenv_test.go +++ b/test/testdata/tenv_test.go @@ -19,22 +19,22 @@ func setup() { } func TestF(t *testing.T) { - os.Setenv("a", "b") // OK - if err := os.Setenv("a", "b"); err != nil { // OK + 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") // OK - if err := os.Setenv("a", "b"); err != nil { // OK + 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") // OK - if err := os.Setenv("a", "b"); err != nil { // OK + 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 } } From 6e25817d0a47f3a604e9a876440169c0db216bcd Mon Sep 17 00:00:00 2001 From: sivchari Date: Sun, 19 Sep 2021 04:32:17 +0900 Subject: [PATCH 09/12] improve report message and fix option description --- .golangci.example.yml | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- test/testdata/tenv_all_test.go | 27 +++++++++++++++++---------- test/testdata/tenv_test.go | 14 +++++++------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/.golangci.example.yml b/.golangci.example.yml index 505b414ecfa8..d8cd938dbcde 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -628,8 +628,8 @@ linters-settings: begin: true tenv: - # 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. + # The option `all` will run against whole test files (`_test.go`) regardless of method/function signatures. + # By default, only methods that take `*testing.T`, `*testing.B`, and `testing.TB` as arguments are checked. all: false unparam: diff --git a/go.mod b/go.mod index bb601bdd64a6..fbb91708f885 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.2.6 + github.com/sivchari/tenv v1.3.7 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 c80d67359c78..5267f103d754 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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.2.6 h1:OveRTnUsGqP65AaPBggmsdThsy5EpQcYakaTntauRAw= -github.com/sivchari/tenv v1.2.6/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.3.7 h1:wG/vbmhRllWFZTw18FSOwdyBn4g6XeCr3LnmG++Y8bQ= +github.com/sivchari/tenv v1.3.7/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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= diff --git a/test/testdata/tenv_all_test.go b/test/testdata/tenv_all_test.go index babdd134a180..c870c29ba0dd 100644 --- a/test/testdata/tenv_all_test.go +++ b/test/testdata/tenv_all_test.go @@ -8,34 +8,41 @@ import ( ) var ( - e = os.Setenv("a", "b") // ERROR "variable e is not using testing.Setenv" + e = os.Setenv("a", "b") // never seen ) 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 { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in setup" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in setup" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `testing\\.Setenv\\(\\)` in setup" _ = 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" _ = 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" _ = 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" _ = err } } diff --git a/test/testdata/tenv_test.go b/test/testdata/tenv_test.go index d4f6c90d2d91..b5e478d54649 100644 --- a/test/testdata/tenv_test.go +++ b/test/testdata/tenv_test.go @@ -7,7 +7,7 @@ import ( ) var ( - e = os.Setenv("a", "b") // OK + e = os.Setenv("a", "b") // never seen ) func setup() { @@ -19,22 +19,22 @@ func setup() { } 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" _ = 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" _ = 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" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" _ = err } } From 90109834fbac9af2541635e6aae9eb8c802f7a0f Mon Sep 17 00:00:00 2001 From: sivchari Date: Sun, 19 Sep 2021 14:39:19 +0900 Subject: [PATCH 10/12] update linter --- go.mod | 2 +- go.sum | 4 ++-- test/testdata/tenv.go | 48 +++++++++++++++++++++++++++++++++++++++ test/testdata/tenv_all.go | 48 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 test/testdata/tenv.go create mode 100644 test/testdata/tenv_all.go diff --git a/go.mod b/go.mod index fbb91708f885..1189ca90a29c 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,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.3.7 + github.com/sivchari/tenv v1.4.7 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 5267f103d754..915ff19e46eb 100644 --- a/go.sum +++ b/go.sum @@ -666,8 +666,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.3.7 h1:wG/vbmhRllWFZTw18FSOwdyBn4g6XeCr3LnmG++Y8bQ= -github.com/sivchari/tenv v1.3.7/go.mod h1:5nF+bITvkebQVanjU6IuMbvIot/7ReNsUV7I5NbprB0= +github.com/sivchari/tenv v1.4.7 h1:FdTpgRlTue5eb5nXIYgS/lyVXSjugU8UUVDwhP1NLU8= +github.com/sivchari/tenv v1.4.7/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/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= diff --git a/test/testdata/tenv.go b/test/testdata/tenv.go new file mode 100644 index 000000000000..45bebd129ba1 --- /dev/null +++ b/test/testdata/tenv.go @@ -0,0 +1,48 @@ +// args: -Etenv +// config_path: testdata/configs/tenv_all.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + e = os.Setenv("a", "b") // never seen +) + +func setup() { + os.Setenv("a", "b") // never seen + err := os.Setenv("a", "b") // never seen + _ = err + if err := os.Setenv("a", "b"); err != nil { // never seen + _ = err + } +} + +func TestF(t *testing.T) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err + } +} + +func testTB(tb testing.TB) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err + } +} diff --git a/test/testdata/tenv_all.go b/test/testdata/tenv_all.go new file mode 100644 index 000000000000..45bebd129ba1 --- /dev/null +++ b/test/testdata/tenv_all.go @@ -0,0 +1,48 @@ +// args: -Etenv +// config_path: testdata/configs/tenv_all.yml +package testdata + +import ( + "os" + "testing" +) + +var ( + e = os.Setenv("a", "b") // never seen +) + +func setup() { + os.Setenv("a", "b") // never seen + err := os.Setenv("a", "b") // never seen + _ = err + if err := os.Setenv("a", "b"); err != nil { // never seen + _ = err + } +} + +func TestF(t *testing.T) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err + } +} + +func BenchmarkF(b *testing.B) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err + } +} + +func testTB(tb testing.TB) { + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err + if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err + } +} From 5489b92b725fed66133b10e6a0188042d0a4e48e Mon Sep 17 00:00:00 2001 From: sivchari Date: Sun, 19 Sep 2021 14:43:10 +0900 Subject: [PATCH 11/12] add test case --- test/testdata/tenv_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/testdata/tenv_test.go b/test/testdata/tenv_test.go index b5e478d54649..03977d482d8c 100644 --- a/test/testdata/tenv_test.go +++ b/test/testdata/tenv_test.go @@ -13,27 +13,34 @@ var ( func setup() { os.Setenv("a", "b") // OK err := os.Setenv("a", "b") // OK - if err != nil { + _ = err + if err := os.Setenv("a", "b"); err != nil { // OK _ = err } } func TestF(t *testing.T) { - os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" + _ = err if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `t\\.Setenv\\(\\)` in TestF" _ = err } } func BenchmarkF(b *testing.B) { - os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" + _ = err if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `b\\.Setenv\\(\\)` in BenchmarkF" _ = err } } func testTB(tb testing.TB) { - os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + err := os.Setenv("a", "b") // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" + _ = err if err := os.Setenv("a", "b"); err != nil { // ERROR "os\\.Setenv\\(\\) can be replaced by `tb\\.Setenv\\(\\)` in testTB" _ = err } From 020ab6d865943f917996875edca52c52a50ddaff Mon Sep 17 00:00:00 2001 From: sivchari Date: Thu, 23 Sep 2021 02:33:11 +0900 Subject: [PATCH 12/12] fix manager --- pkg/lint/lintersdb/manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 7b3347ec78c5..e8fd7dd0e626 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -526,7 +526,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithSince("v1.43.0"), linter.NewConfig(golinters.NewTenv(tenvCfg)). WithSince("v1.43.0"). - WithPresets(linter.PresetPerformance, linter.PresetStyle). + WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). WithURL("https://github.com/sivchari/tenv"),