Skip to content

Commit 48361b9

Browse files
authored
Merge pull request #852 from maratori/add-testpackage
Add linter testpackage
2 parents aed4806 + 9914737 commit 48361b9

File tree

9 files changed

+76
-12
lines changed

9 files changed

+76
-12
lines changed

.golangci.example.yml

+3
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ linters-settings:
224224
rowserrcheck:
225225
packages:
226226
- github.com/jmoiron/sqlx
227+
testpackage:
228+
# regexp pattern to skip files
229+
skip-regexp: (export|internal)_test\.go
227230
unparam:
228231
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
229232
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ prealloc: Finds slice declarations that could potentially be preallocated [fast:
233233
rowserrcheck: checks whether Err of rows is checked successfully [fast: true, auto-fix: false]
234234
scopelint: Scopelint checks for unpinned variables in go programs [fast: true, auto-fix: false]
235235
stylecheck: Stylecheck is a replacement for golint [fast: true, auto-fix: false]
236+
testpackage: linter that makes you use a separate _test package [fast: true, auto-fix: false]
236237
unconvert: Remove unnecessary type conversions [fast: true, auto-fix: false]
237238
unparam: Reports unused function parameters [fast: true, auto-fix: false]
238239
whitespace: Tool for detection of leading and trailing whitespace [fast: true, auto-fix: true]
@@ -490,6 +491,7 @@ golangci-lint help linters
490491
- [gomnd](https://github.com/tommy-muehle/go-mnd) - An analyzer to detect magic numbers.
491492
- [gomodguard](https://github.com/ryancurrah/gomodguard) - Allow and block list linter for direct Go module dependencies.
492493
- [godot](https://github.com/tetafro/godot) - Check if comments end in a period
494+
- [testpackage](https://github.com/maratori/testpackage) - linter that makes you use a separate _test package
493495
494496
## Configuration
495497
@@ -836,6 +838,9 @@ linters-settings:
836838
rowserrcheck:
837839
packages:
838840
- github.com/jmoiron/sqlx
841+
testpackage:
842+
# regexp pattern to skip files
843+
skip-regexp: (export|internal)_test\.go
839844
unparam:
840845
# Inspect exported functions, default is false. Set to true if no external program/library imports your code.
841846
# XXX: if you enable this setting, unparam will report a lot of false-positives in text editors:
@@ -1282,6 +1287,7 @@ Thanks to developers and authors of used linters:
12821287
- [tommy-muehle](https://github.com/tommy-muehle)
12831288
- [ryancurrah](https://github.com/ryancurrah)
12841289
- [tetafro](https://github.com/tetafro)
1290+
- [maratori](https://github.com/maratori)
12851291
12861292
## Changelog
12871293

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
2626
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
2727
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
28+
github.com/maratori/testpackage v1.0.1
2829
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb // v1.0
2930
github.com/mattn/go-colorable v0.1.4
3031
github.com/mitchellh/go-homedir v1.1.0
@@ -46,7 +47,7 @@ require (
4647
github.com/ultraware/whitespace v0.0.4
4748
github.com/uudashr/gocognit v1.0.1
4849
github.com/valyala/quicktemplate v1.2.0
49-
golang.org/x/tools v0.0.0-20200204192400-7124308813f3
50+
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e
5051
gopkg.in/yaml.v2 v2.2.8
5152
honnef.co/go/tools v0.0.1-2020.1.3
5253
mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed

go.sum

+9
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQ
165165
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
166166
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
167167
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
168+
github.com/maratori/testpackage v1.0.1 h1:QtJ5ZjqapShm0w5DosRjg0PRlSdAdlx+W6cCKoALdbQ=
169+
github.com/maratori/testpackage v1.0.1/go.mod h1:ddKdw+XG0Phzhx8BFDTKgpWP4i7MpApTE5fXSKAqwDU=
168170
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb h1:RHba4YImhrUVQDHUCe2BNSOz4tVy2yGyXhvYDvxGgeE=
169171
github.com/matoous/godox v0.0.0-20190911065817-5d6d842e92eb/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
170172
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
293295
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
294296
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
295297
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
298+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
296299
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
297300
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
298301
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
322325
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
323326
golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g=
324327
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
328+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
325329
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
326330
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
327331
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
328332
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
329333
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
334+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
330335
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
331336
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
332337
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
364369
golang.org/x/tools v0.0.0-20200102140908-9497f49d5709/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
365370
golang.org/x/tools v0.0.0-20200204192400-7124308813f3 h1:Ms82wn6YK4ZycO6Bxyh0kxX3gFFVGo79CCuc52xgcys=
366371
golang.org/x/tools v0.0.0-20200204192400-7124308813f3/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
372+
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e h1:3Dzrrxi54Io7Aoyb0PYLsI47K2TxkRQg+cqUn+m04do=
373+
golang.org/x/tools v0.0.0-20200422022333-3d57cf2e726e/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
367374
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=
368375
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
369376
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
370377
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
378+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
379+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
371380
google.golang.org/appengine v1.1.0 h1:igQkv0AAhEIvTEpD5LIpAfav2eeVO9HBTjvKHVJPRSs=
372381
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
373382
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=

pkg/config/config.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,18 @@ type LintersSettings struct {
200200
} `mapstructure:"blocked"`
201201
}
202202

203-
WSL WSLSettings
204-
Lll LllSettings
205-
Unparam UnparamSettings
206-
Nakedret NakedretSettings
207-
Prealloc PreallocSettings
208-
Errcheck ErrcheckSettings
209-
Gocritic GocriticSettings
210-
Godox GodoxSettings
211-
Dogsled DogsledSettings
212-
Gocognit GocognitSettings
213-
Godot GodotSettings
203+
WSL WSLSettings
204+
Lll LllSettings
205+
Unparam UnparamSettings
206+
Nakedret NakedretSettings
207+
Prealloc PreallocSettings
208+
Errcheck ErrcheckSettings
209+
Gocritic GocriticSettings
210+
Godox GodoxSettings
211+
Dogsled DogsledSettings
212+
Gocognit GocognitSettings
213+
Godot GodotSettings
214+
Testpackage TestpackageSettings
214215

215216
Custom map[string]CustomLinterSettings
216217
}
@@ -292,6 +293,10 @@ type GodotSettings struct {
292293
CheckAll bool `mapstructure:"check-all"`
293294
}
294295

296+
type TestpackageSettings struct {
297+
SkipRegexp string `mapstructure:"skip-regexp"`
298+
}
299+
295300
//nolint:gomnd
296301
var defaultLintersSettings = LintersSettings{
297302
Lll: LllSettings{
@@ -331,6 +336,9 @@ var defaultLintersSettings = LintersSettings{
331336
ForceCuddleErrCheckAndAssign: false,
332337
ForceCaseTrailingWhitespaceLimit: 0,
333338
},
339+
Testpackage: TestpackageSettings{
340+
SkipRegexp: `(export|internal)_test\.go`,
341+
},
334342
}
335343

336344
type CustomLinterSettings struct {

pkg/golinters/testpackage.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package golinters
2+
3+
import (
4+
"github.com/maratori/testpackage/pkg/testpackage"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/config"
8+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
9+
)
10+
11+
func NewTestpackage(cfg *config.TestpackageSettings) *goanalysis.Linter {
12+
var a = testpackage.NewAnalyzer()
13+
var settings map[string]map[string]interface{}
14+
if cfg != nil {
15+
settings = map[string]map[string]interface{}{
16+
a.Name: {
17+
testpackage.SkipRegexpFlagName: cfg.SkipRegexp,
18+
},
19+
}
20+
}
21+
return goanalysis.NewLinter(a.Name, a.Doc, []*analysis.Analyzer{a}, settings).
22+
WithLoadMode(goanalysis.LoadModeSyntax)
23+
}

pkg/lint/lintersdb/manager.go

+6
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config)
8686
//nolint:funlen
8787
func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
8888
var govetCfg *config.GovetSettings
89+
var testpackageCfg *config.TestpackageSettings
8990
if m.cfg != nil {
9091
govetCfg = &m.cfg.LintersSettings.Govet
92+
testpackageCfg = &m.cfg.LintersSettings.Testpackage
9193
}
9294
const megacheckName = "megacheck"
9395
lcs := []*linter.Config{
@@ -254,6 +256,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
254256
linter.NewConfig(golinters.NewGodot()).
255257
WithPresets(linter.PresetStyle).
256258
WithURL("https://github.com/tetafro/godot"),
259+
linter.NewConfig(golinters.NewTestpackage(testpackageCfg)).
260+
WithPresets(linter.PresetStyle).
261+
WithLoadForGoAnalysis().
262+
WithURL("https://github.com/maratori/testpackage"),
257263
}
258264

259265
isLocalRun := os.Getenv("GOLANGCI_COM_RUN") == ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//args: -Etestpackage -Egochecknoglobals
2+
package testdata
3+
4+
// Test expects at least one issue in the file.
5+
// So we have to add global variable and enable gochecknoglobals.
6+
var global = `global` // ERROR "`global` is a global variable"

test/testdata/testpackage_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//args: -Etestpackage
2+
package testdata // ERROR "package should be `testdata_test` instead of `testdata`"

0 commit comments

Comments
 (0)