From 9914737fc314e0433c408a99df55c7a9cae3b5c1 Mon Sep 17 00:00:00 2001 From: Marat Reymers Date: Sun, 10 Nov 2019 01:05:27 +0300 Subject: [PATCH] Add linter testpackage --- .golangci.example.yml | 3 +++ README.md | 6 +++++ go.mod | 3 ++- go.sum | 9 +++++++ pkg/config/config.go | 30 ++++++++++++++-------- pkg/golinters/testpackage.go | 23 +++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 +++++ test/testdata/testpackage_internal_test.go | 6 +++++ test/testdata/testpackage_test.go | 2 ++ 9 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 pkg/golinters/testpackage.go create mode 100644 test/testdata/testpackage_internal_test.go create mode 100644 test/testdata/testpackage_test.go diff --git a/.golangci.example.yml b/.golangci.example.yml index c48ec722..ed55227e 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -222,6 +222,9 @@ linters-settings: rowserrcheck: packages: - github.com/jmoiron/sqlx + testpackage: + # regexp pattern to skip files + skip-regexp: (export|internal)_test\.go 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/README.md b/README.md index ae63ad0b..48a0fd04 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,7 @@ prealloc: Finds slice declarations that could potentially be preallocated [fast: rowserrcheck: checks whether Err of rows is checked successfully [fast: true, auto-fix: false] scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false] stylecheck: Stylecheck is a replacement for golint [fast: true, auto-fix: false] +testpackage: linter that makes you use a separate _test package [fast: true, auto-fix: false] unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false] unparam: Reports unused function parameters [fast: true, auto-fix: false] whitespace: Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true] @@ -490,6 +491,7 @@ golangci-lint help linters - [gomnd](https://github.com/tommy-muehle/go-mnd) - An analyzer to detect magic numbers. - [gomodguard](https://github.com/ryancurrah/gomodguard) - Allow and block list linter for direct Go module dependencies. - [godot](https://github.com/tetafro/godot) - Check if comments end in a period +- [testpackage](https://github.com/maratori/testpackage) - linter that makes you use a separate _test package ## Configuration @@ -834,6 +836,9 @@ linters-settings: rowserrcheck: packages: - github.com/jmoiron/sqlx + testpackage: + # regexp pattern to skip files + skip-regexp: (export|internal)_test\.go 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: @@ -1280,6 +1285,7 @@ Thanks to developers and authors of used linters: - [tommy-muehle](https://github.com/tommy-muehle) - [ryancurrah](https://github.com/ryancurrah) - [tetafro](https://github.com/tetafro) +- [maratori](https://github.com/maratori) ## Changelog diff --git a/go.mod b/go.mod index 5fa498b3..d9512f62 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4 github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3 + github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0 github.com/mattn/go-colorable v0.1.4 github.com/mitchellh/go-homedir v1.1.0 @@ -46,7 +47,7 @@ require ( github.com/ultraware/whitespace v0.0.4 github.com/uudashr/gocognit v1.0.1 github.com/valyala/quicktemplate v1.2.0 - golang.org/x/tools v0.0.0-20200204192400-7124308813f3 + golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e gopkg.in/yaml.v2 v2.2.8 honnef.co/go/tools v0.0.1-2020.1.3 mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed diff --git a/go.sum b/go.sum index 7432d940..2e881724 100644 --- a/go.sum +++ b/go.sum @@ -165,6 +165,8 @@ github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQ github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= +github.com/maratori/testpackage v1.0.1 h1:QtJ5ZjqapShm0w5DosRjg0PRlSdAdlx+W6cCKoALdbQ= +github.com/maratori/testpackage v1.0.1/go.mod h1:ddKdw+XG0Phzhx8BFDTKgpWP4i7MpApTE5fXSKAqwDU= github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb h1:RHba4YImhrUVQDHUCe2BNSOz4tVy2yGyXhvYDvxGgeE= github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s= github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= @@ -293,6 +295,7 @@ github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOV github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= @@ -322,11 +325,13 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -364,10 +369,14 @@ golang.org/x/tools v0.0.0-20200102140908-9497f49d5709 h1:AfG1EmoRkFK24HWWLxSrRKN golang.org/x/tools v0.0.0-20200102140908-9497f49d5709/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/tools v0.0.0-20200204192400-7124308813f3 h1:Ms82wn6YK4ZycO6Bxyh0kxX3gFFVGo79CCuc52xgcys= golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e h1:3Dzrrxi54Io7Aoyb0PYLsI47K2TxkRQg+cqUn+m04do= +golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/pkg/config/config.go b/pkg/config/config.go index f8775521..0e55666a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -200,17 +200,18 @@ type LintersSettings struct { } `mapstructure:"blocked"` } - WSL WSLSettings - Lll LllSettings - Unparam UnparamSettings - Nakedret NakedretSettings - Prealloc PreallocSettings - Errcheck ErrcheckSettings - Gocritic GocriticSettings - Godox GodoxSettings - Dogsled DogsledSettings - Gocognit GocognitSettings - Godot GodotSettings + WSL WSLSettings + Lll LllSettings + Unparam UnparamSettings + Nakedret NakedretSettings + Prealloc PreallocSettings + Errcheck ErrcheckSettings + Gocritic GocriticSettings + Godox GodoxSettings + Dogsled DogsledSettings + Gocognit GocognitSettings + Godot GodotSettings + Testpackage TestpackageSettings Custom map[string]CustomLinterSettings } @@ -292,6 +293,10 @@ type GodotSettings struct { CheckAll bool `mapstructure:"check-all"` } +type TestpackageSettings struct { + SkipRegexp string `mapstructure:"skip-regexp"` +} + //nolint:gomnd var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ @@ -331,6 +336,9 @@ var defaultLintersSettings = LintersSettings{ ForceCuddleErrCheckAndAssign: false, ForceCaseTrailingWhitespaceLimit: 0, }, + Testpackage: TestpackageSettings{ + SkipRegexp: `(export|internal)_test\.go`, + }, } type CustomLinterSettings struct { diff --git a/pkg/golinters/testpackage.go b/pkg/golinters/testpackage.go new file mode 100644 index 00000000..1248e78f --- /dev/null +++ b/pkg/golinters/testpackage.go @@ -0,0 +1,23 @@ +package golinters + +import ( + "github.com/maratori/testpackage/pkg/testpackage" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewTestpackage(cfg *config.TestpackageSettings) *goanalysis.Linter { + var a = testpackage.NewAnalyzer() + var settings map[string]map[string]interface{} + if cfg != nil { + settings = map[string]map[string]interface{}{ + a.Name: { + testpackage.SkipRegexpFlagName: cfg.SkipRegexp, + }, + } + } + return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, settings). + WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 411bf830..a3e84f0c 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -86,8 +86,10 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) //nolint:funlen func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var govetCfg *config.GovetSettings + var testpackageCfg *config.TestpackageSettings if m.cfg != nil { govetCfg = &m.cfg.LintersSettings.Govet + testpackageCfg = &m.cfg.LintersSettings.Testpackage } const megacheckName = "megacheck" lcs := []*linter.Config{ @@ -254,6 +256,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { linter.NewConfig(golinters.NewGodot()). WithPresets(linter.PresetStyle). WithURL("https://github.com/tetafro/godot"), + linter.NewConfig(golinters.NewTestpackage(testpackageCfg)). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/maratori/testpackage"), } isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == "" diff --git a/test/testdata/testpackage_internal_test.go b/test/testdata/testpackage_internal_test.go new file mode 100644 index 00000000..2ddff77d --- /dev/null +++ b/test/testdata/testpackage_internal_test.go @@ -0,0 +1,6 @@ +//args: -Etestpackage -Egochecknoglobals +package testdata + +// Test expects at least one issue in the file. +// So we have to add global variable and enable gochecknoglobals. +var global = `global` // ERROR "`global` is a global variable" diff --git a/test/testdata/testpackage_test.go b/test/testdata/testpackage_test.go new file mode 100644 index 00000000..13deccb0 --- /dev/null +++ b/test/testdata/testpackage_test.go @@ -0,0 +1,2 @@ +//args: -Etestpackage +package testdata // ERROR "package should be `testdata_test` instead of `testdata`"