-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from maratori/analyzer
Add testpackage analyzer
- Loading branch information
Showing
12 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea/ | ||
.DS_Store | ||
*.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bad |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bad // want `package should be "bad_test" instead of "bad"` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bad_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package good |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package good_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |