Skip to content

Commit

Permalink
chore: add more tests & API Change (#22)
Browse files Browse the repository at this point in the history
* test: `Imports` 
* test: added `Violation` tests
* test: added golangci `(*Violation).Issue()` tests + changed API
  • Loading branch information
butuzov authored May 15, 2023
1 parent 8c8321f commit 85325bc
Show file tree
Hide file tree
Showing 15 changed files with 554 additions and 8 deletions.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ module github.com/butuzov/mirror

go 1.19

require golang.org/x/tools v0.8.0
require (
github.com/stretchr/testify v1.8.2
golang.org/x/tools v0.8.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
17 changes: 17 additions & 0 deletions go.sum
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=
94 changes: 94 additions & 0 deletions internal/checker/imports_test.go
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)
})
}
}
12 changes: 12 additions & 0 deletions internal/checker/testdata/imports_alias.txtar
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")
}
55 changes: 55 additions & 0 deletions internal/checker/testdata/imports_all.txtar
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")
}
12 changes: 12 additions & 0 deletions internal/checker/testdata/imports_dot.txtar
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")
}
22 changes: 22 additions & 0 deletions internal/checker/testdata/imports_many.txtar
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")
}
5 changes: 5 additions & 0 deletions internal/checker/testdata/imports_nothing.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foobar
-- a.go --
package testdata

func M() {}
12 changes: 12 additions & 0 deletions internal/checker/testdata/imports_regular.txtar
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")
}
12 changes: 12 additions & 0 deletions internal/checker/testdata/violations_new_lines.txtar
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"))
11 changes: 11 additions & 0 deletions internal/checker/testdata/violations_packages.txtar
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"))
12 changes: 12 additions & 0 deletions internal/checker/testdata/violations_simple.txtar
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"))
45 changes: 45 additions & 0 deletions internal/checker/testing_test.go
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
}
15 changes: 8 additions & 7 deletions internal/checker/violation.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,24 @@ type GolangIssue struct {
Original string
}

// GolangCI-lint related diagnostic
func (v *Violation) Issue(pass *analysis.Pass) GolangIssue {
// Issue inteanded to be used only with golangci-lint, bu you can use use it
// alongside Diagnostic if you wish.
func (v *Violation) Issue(fSet *token.FileSet) GolangIssue {
issue := GolangIssue{
Start: pass.Fset.Position(v.callExpr.Pos()),
End: pass.Fset.Position(v.callExpr.End()),
Start: fSet.Position(v.callExpr.Pos()),
End: fSet.Position(v.callExpr.End()),
Message: v.Message(),
}

// original expression (useful for debug & requied for replace)
var buf bytes.Buffer
printer.Fprint(&buf, pass.Fset, v.callExpr)
printer.Fprint(&buf, fSet, v.callExpr)
issue.Original = buf.String()

noNl := strings.IndexByte(issue.Original, '\n') < 0

if v.Type == Method && noNl {
fix := v.suggest(pass.Fset)
fix := v.suggest(fSet)
issue.InlineFix = string(fix)
}

Expand All @@ -175,7 +176,7 @@ func (v *Violation) Issue(pass *analysis.Pass) GolangIssue {

// Hooray! we don't need to change package and redo imports.
if v.Type == Function && v.AltPackage == v.Package && noNl {
fix := v.suggest(pass.Fset)
fix := v.suggest(fSet)
issue.InlineFix = string(fix)
}

Expand Down
Loading

0 comments on commit 85325bc

Please sign in to comment.