Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testpackage analyzer #1

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
.DS_Store
*.out
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: go
go:
- "1.11.x"
- "1.12.x"
env:
global:
- GO111MODULE=on
matrix:
include:
- go: "1.13.x"
#install:
# - curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.21.0
# - golangci-lint --version
script:
- go test -race -coverpkg ./... -coverprofile=coverage.out ./...

#- golangci-lint run

- echo "Check that go.mod and go.sum are tidy"
- go mod tidy
- if [[ `git status --porcelain go.mod` ]]; then git diff -- go.mod ; echo "go.mod is outdated, please run go mod tidy" ; exit 1; fi
- if [[ `git status --porcelain go.sum` ]]; then git diff -- go.sum ; echo "go.sum is outdated, please run go mod tidy" ; exit 1; fi
after_success:
- bash <(curl -s https://codecov.io/bash)
script:
- go test -race ./...
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/maratori/testpackage

go 1.13

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

import (
"golang.org/x/tools/go/analysis/singlechecker"

"github.com/maratori/testpackage/pkg/testpackage"
)

func main() {
singlechecker.Main(testpackage.Analyzer)
}
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/bad/bad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bad
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/bad/bad_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bad // want `package should be "bad_test" instead of "bad"`
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/bad/good_inside_bad_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package bad_test
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/good/good.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package good
1 change: 1 addition & 0 deletions pkg/testpackage/testdata/good/good_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package good_test
27 changes: 27 additions & 0 deletions pkg/testpackage/testpackage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package testpackage

import (
"strings"

"golang.org/x/tools/go/analysis"
)

// Analyzer that make you use a separate _test package
var Analyzer = &analysis.Analyzer{
Name: "testpackage",
Doc: "linter that make you use a separate _test package",
Run: run,
}

func run(pass *analysis.Pass) (interface{}, error) {
for _, f := range pass.Files {
fileName := pass.Fset.Position(f.Pos()).Filename
if strings.HasSuffix(fileName, "_test.go") {
packageName := f.Name.Name
if !strings.HasSuffix(packageName, "_test") {
pass.Reportf(f.Pos(), `package should be "%s_test" instead of "%s"`, packageName, packageName)
}
}
}
return nil, nil
}
26 changes: 26 additions & 0 deletions pkg/testpackage/testpackage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package testpackage_test

import (
"path/filepath"
"testing"

"golang.org/x/tools/go/analysis/analysistest"

"github.com/maratori/testpackage/pkg/testpackage"
)

func TestAnalyzer_Good(t *testing.T) {
testdata, err := filepath.Abs("testdata/good")
if err != nil {
t.FailNow()
}
analysistest.Run(t, testdata, testpackage.Analyzer)
}

func TestAnalyzer_Bad(t *testing.T) {
testdata, err := filepath.Abs("testdata/bad")
if err != nil {
t.FailNow()
}
analysistest.Run(t, testdata, testpackage.Analyzer)
}