-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add more tests & API Change (#22)
* test: `Imports` * test: added `Violation` tests * test: added golangci `(*Violation).Issue()` tests + changed API
- Loading branch information
Showing
15 changed files
with
554 additions
and
8 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
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 |
---|---|---|
@@ -1,7 +1,24 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk= | ||
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= | ||
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= | ||
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y= | ||
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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,94 @@ | ||
package checker | ||
|
||
import ( | ||
"go/token" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
func TestImports(t *testing.T) { | ||
testData := []struct { | ||
txtarPath string | ||
importsLen int | ||
hasImports map[string]string | ||
}{ | ||
{ | ||
txtarPath: "testdata/imports_nothing.txtar", | ||
importsLen: 0, | ||
}, | ||
{ | ||
txtarPath: "testdata/imports_dot.txtar", | ||
importsLen: 1, | ||
hasImports: map[string]string{".": "strings"}, | ||
}, | ||
{ | ||
txtarPath: "testdata/imports_alias.txtar", | ||
importsLen: 1, | ||
hasImports: map[string]string{"foo": "strings"}, | ||
}, | ||
{ | ||
txtarPath: "testdata/imports_regular.txtar", | ||
importsLen: 1, | ||
hasImports: map[string]string{"strings": "strings"}, | ||
}, | ||
{ | ||
txtarPath: "testdata/imports_all.txtar", | ||
importsLen: 22, | ||
hasImports: map[string]string{ | ||
"strings": "strings", | ||
".": "strings", | ||
"foo1": "strings", | ||
"foo2": "strings", | ||
"foo3": "strings", | ||
"foo4": "strings", | ||
"foo5": "strings", | ||
"foo6": "strings", | ||
"foo7": "strings", | ||
"foo8": "strings", | ||
"foo9": "strings", | ||
"foo10": "strings", | ||
"foo11": "strings", | ||
"foo12": "strings", | ||
"foo13": "strings", | ||
"foo14": "strings", | ||
"foo15": "strings", | ||
"foo16": "strings", | ||
"foo17": "strings", | ||
"foo18": "strings", | ||
"foo19": "strings", | ||
"foo20": "strings", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testData { | ||
test := test | ||
t.Run(test.txtarPath, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
fset := token.NewFileSet() | ||
ar, err := Txtar(t, fset, test.txtarPath) | ||
|
||
assert.Nil(t, err) | ||
assert.Len(t, ar, 1) | ||
|
||
ins := inspector.New(ar) | ||
testImports := Load(fset, ins) | ||
|
||
// assert | ||
assert.Len(t, testImports["a.go"], test.importsLen) | ||
|
||
for k, v := range test.hasImports { | ||
str, ok := testImports.Lookup("a.go", k) | ||
assert.True(t, ok, "Import `%s` not found", k) | ||
assert.Equal(t, v, str, "Wrong package found want(%s) vs got(%s)", v, str) | ||
} | ||
|
||
// test if lookup produce fail | ||
str, ok := testImports.Lookup("a.go", "foobar") | ||
assert.False(t, ok, "found somethig enexpected %s", str) | ||
}) | ||
} | ||
} |
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,12 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
foo "strings" | ||
) | ||
|
||
func M() { | ||
foo.EqualFold("string", "STRING") | ||
} |
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,55 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
foo1 "strings" | ||
foo2 "strings" | ||
foo3 "strings" | ||
foo4 "strings" | ||
foo5 "strings" | ||
foo6 "strings" | ||
foo7 "strings" | ||
foo8 "strings" | ||
foo9 "strings" | ||
foo10 "strings" | ||
foo11 "strings" | ||
foo12 "strings" | ||
foo13 "strings" | ||
foo14 "strings" | ||
foo15 "strings" | ||
foo16 "strings" | ||
foo17 "strings" | ||
foo18 "strings" | ||
foo19 "strings" | ||
foo20 "strings" | ||
. "strings" | ||
"strings" | ||
) | ||
|
||
func M() { | ||
foo1.EqualFold("string", "STRING") | ||
foo2.EqualFold("string", "STRING") | ||
foo3.EqualFold("string", "STRING") | ||
foo4.EqualFold("string", "STRING") | ||
foo5.EqualFold("string", "STRING") | ||
foo6.EqualFold("string", "STRING") | ||
foo7.EqualFold("string", "STRING") | ||
foo8.EqualFold("string", "STRING") | ||
foo9.EqualFold("string", "STRING") | ||
foo10.EqualFold("string", "STRING") | ||
foo11.EqualFold("string", "STRING") | ||
foo12.EqualFold("string", "STRING") | ||
foo13.EqualFold("string", "STRING") | ||
foo14.EqualFold("string", "STRING") | ||
foo15.EqualFold("string", "STRING") | ||
foo16.EqualFold("string", "STRING") | ||
foo17.EqualFold("string", "STRING") | ||
foo18.EqualFold("string", "STRING") | ||
foo19.EqualFold("string", "STRING") | ||
foo20.EqualFold("string", "STRING") | ||
|
||
EqualFold("string", "STRING") | ||
strings.EqualFold("string", "STRING") | ||
} |
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,12 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
. "strings" | ||
) | ||
|
||
func M() { | ||
EqualFold("string", "STRING") | ||
} |
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,22 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
foo1 "strings" | ||
foo2 "strings" | ||
foo3 "strings" | ||
foo4 "strings" | ||
. "strings" | ||
"strings" | ||
) | ||
|
||
func M() { | ||
foo1.EqualFold("string", "STRING") | ||
foo2.EqualFold("string", "STRING") | ||
foo3.EqualFold("string", "STRING") | ||
foo4.EqualFold("string", "STRING") | ||
EqualFold("string", "STRING") | ||
strings.EqualFold("string", "STRING") | ||
} |
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 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
func M() {} |
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,12 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
func M() { | ||
strings.EqualFold("string", "STRING") | ||
} |
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,12 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
"unicode/utf8" | ||
) | ||
|
||
// nothing as fix expected | ||
var foo = utf8.ValidString( | ||
string("foo")) |
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 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
"unicode/utf8" | ||
) | ||
|
||
// nothing as fix expected | ||
var foo = utf8.ValidString(string("foo")) |
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,12 @@ | ||
foobar | ||
-- a.go -- | ||
package testdata | ||
|
||
|
||
import ( | ||
"unicode/utf8" | ||
) | ||
|
||
// we are only trying to match selector by description | ||
// and we specially set | ||
var foo = utf8.ValidString(string("foo")) |
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,45 @@ | ||
package checker | ||
|
||
import ( | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"path" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"golang.org/x/tools/txtar" | ||
) | ||
|
||
func ParseExprFrom(t *testing.T, fset *token.FileSet, src any) ast.Expr { | ||
astExpr, err := parser.ParseExprFrom(fset, "a.go", src, parser.AllErrors) | ||
assert.NoError(t, err) | ||
return astExpr | ||
} | ||
|
||
func Txtar(t *testing.T, fset *token.FileSet, txtarPath string) (files []*ast.File, err error) { | ||
t.Helper() | ||
|
||
ar, err := txtar.ParseFile(txtarPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
files = make([]*ast.File, 0, len(ar.Files)) | ||
for i := range ar.Files { | ||
file := ar.Files[i] | ||
if !strings.HasSuffix(ar.Files[i].Name, ".go") { | ||
continue | ||
} | ||
|
||
f, err := parser.ParseFile(fset, | ||
path.Base(file.Name), file.Data, parser.AllErrors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
files = append(files, f) | ||
} | ||
|
||
return files, 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
Oops, something went wrong.