Skip to content

Commit 2922fd5

Browse files
authored
Merge pull request #1 from maratori/analyzer
Add testpackage analyzer
2 parents 8c8b84f + 3ec9ff7 commit 2922fd5

File tree

12 files changed

+111
-0
lines changed

12 files changed

+111
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea/
2+
.DS_Store
3+
*.out

.travis.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
language: go
2+
go:
3+
- "1.11.x"
4+
- "1.12.x"
5+
env:
6+
global:
7+
- GO111MODULE=on
8+
matrix:
9+
include:
10+
- go: "1.13.x"
11+
#install:
12+
# - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0
13+
# - golangci-lint --version
14+
script:
15+
- go test -race -coverpkg ./... -coverprofile=coverage.out ./...
16+
17+
#- golangci-lint run
18+
19+
- echo "Check that go.mod and go.sum are tidy"
20+
- go mod tidy
21+
- if [[ `git status --porcelain go.mod` ]]; then git diff -- go.mod ; echo "go.mod is outdated, please run go mod tidy" ; exit 1; fi
22+
- if [[ `git status --porcelain go.sum` ]]; then git diff -- go.sum ; echo "go.sum is outdated, please run go mod tidy" ; exit 1; fi
23+
after_success:
24+
- bash <(curl -s https://codecov.io/bash)
25+
script:
26+
- go test -race ./...

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/maratori/testpackage
2+
3+
go 1.13
4+
5+
require golang.org/x/tools v0.0.0-20191101200257-8dbcdeb83d3f

go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
3+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
4+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
5+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6+
golang.org/x/tools v0.0.0-20191101200257-8dbcdeb83d3f h1:+QO45yvqhfD79HVNFPAgvstYLFye8zA+rd0mHFsGV9s=
7+
golang.org/x/tools v0.0.0-20191101200257-8dbcdeb83d3f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
8+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

main.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"golang.org/x/tools/go/analysis/singlechecker"
5+
6+
"github.com/maratori/testpackage/pkg/testpackage"
7+
)
8+
9+
func main() {
10+
singlechecker.Main(testpackage.Analyzer)
11+
}

pkg/testpackage/testdata/bad/bad.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bad
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bad // want `package should be "bad_test" instead of "bad"`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bad_test

pkg/testpackage/testdata/good/good.go

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package good
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package good_test

pkg/testpackage/testpackage.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package testpackage
2+
3+
import (
4+
"strings"
5+
6+
"golang.org/x/tools/go/analysis"
7+
)
8+
9+
// Analyzer that make you use a separate _test package
10+
var Analyzer = &analysis.Analyzer{
11+
Name: "testpackage",
12+
Doc: "linter that make you use a separate _test package",
13+
Run: run,
14+
}
15+
16+
func run(pass *analysis.Pass) (interface{}, error) {
17+
for _, f := range pass.Files {
18+
fileName := pass.Fset.Position(f.Pos()).Filename
19+
if strings.HasSuffix(fileName, "_test.go") {
20+
packageName := f.Name.Name
21+
if !strings.HasSuffix(packageName, "_test") {
22+
pass.Reportf(f.Pos(), `package should be "%s_test" instead of "%s"`, packageName, packageName)
23+
}
24+
}
25+
}
26+
return nil, nil
27+
}

pkg/testpackage/testpackage_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package testpackage_test
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"golang.org/x/tools/go/analysis/analysistest"
8+
9+
"github.com/maratori/testpackage/pkg/testpackage"
10+
)
11+
12+
func TestAnalyzer_Good(t *testing.T) {
13+
testdata, err := filepath.Abs("testdata/good")
14+
if err != nil {
15+
t.FailNow()
16+
}
17+
analysistest.Run(t, testdata, testpackage.Analyzer)
18+
}
19+
20+
func TestAnalyzer_Bad(t *testing.T) {
21+
testdata, err := filepath.Abs("testdata/bad")
22+
if err != nil {
23+
t.FailNow()
24+
}
25+
analysistest.Run(t, testdata, testpackage.Analyzer)
26+
}

0 commit comments

Comments
 (0)