Skip to content

Commit

Permalink
feat: new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 27, 2024
1 parent 5433335 commit 4287499
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 44 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ go 1.22.0

require (
github.com/ettle/strcase v0.2.0
github.com/golangci/modinfo v0.3.2
github.com/hashicorp/go-immutable-radix/v2 v2.0.0
github.com/hashicorp/go-immutable-radix/v2 v2.1.0
golang.org/x/tools v0.27.0
)

require (
github.com/hashicorp/golang-lru/v2 v2.0.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.9.0 // indirect
)
18 changes: 4 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
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/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q=
github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A=
github.com/golangci/modinfo v0.3.2 h1:vtlDk+LU5eIpeg3Ljf/YawlDsB/RunSWYfv/b93thgs=
github.com/golangci/modinfo v0.3.2/go.mod h1:AMKyptKcDrNIz9z/KJyED82pJAboTub2Vgt9vwxZhGE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/go-immutable-radix/v2 v2.0.0 h1:nq9lQ5I71Heg2lRb2/+szuIWKY3Y73d8YKyXyN91WzU=
github.com/hashicorp/go-immutable-radix/v2 v2.0.0/go.mod h1:hgdqLXA4f6NIjRVisM1TJ9aOJVNRqKZj+xDGF6m7PBw=
github.com/hashicorp/go-immutable-radix/v2 v2.1.0 h1:CUW5RYIcysz+D3B+l1mDeXrQ7fUvGGCwJfdASSzbrfo=
github.com/hashicorp/go-immutable-radix/v2 v2.1.0/go.mod h1:hgdqLXA4f6NIjRVisM1TJ9aOJVNRqKZj+xDGF6m7PBw=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru/v2 v2.0.0 h1:Lf+9eD8m5pncvHAOCQj49GSN6aQI8XGfI5OpXNkoWaA=
github.com/hashicorp/golang-lru/v2 v2.0.0/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
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/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
golang.org/x/exp v0.0.0-20221215174704-0915cd710c24 h1:6w3iSY8IIkp5OQtbYj8NeuKG1jS9d+kYaubXqsoOiQ8=
golang.org/x/exp v0.0.0-20221215174704-0915cd710c24/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
Expand All @@ -24,5 +16,3 @@ golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13 changes: 3 additions & 10 deletions tagliatelle.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"

"github.com/ettle/strcase"
"github.com/golangci/modinfo"
iradix "github.com/hashicorp/go-immutable-radix/v2"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
Expand Down Expand Up @@ -53,17 +52,11 @@ func New(config Config) *analysis.Analyzer {
},
Requires: []*analysis.Analyzer{
inspect.Analyzer,
modinfo.Analyzer,
},
}
}

func run(pass *analysis.Pass, config Config) (interface{}, error) {
info, err := modinfo.FindModuleFromPass(pass)
if err != nil {
return nil, err
}

isp, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
if !ok {
return nil, errors.New("missing inspect analyser")
Expand All @@ -73,7 +66,7 @@ func run(pass *analysis.Pass, config Config) (interface{}, error) {
(*ast.StructType)(nil),
}

r := createRadixTree(config, info)
r := createRadixTree(config, pass.Module.Path)

isp.Preorder(nodeFilter, func(n ast.Node) {
node, ok := n.(*ast.StructType)
Expand Down Expand Up @@ -259,7 +252,7 @@ func toHeader(s string) string {
return strcase.ToCase(s, strcase.TitleCase, '-')
}

func createRadixTree(config Config, info modinfo.ModInfo) *iradix.Tree[Base] {
func createRadixTree(config Config, modPath string) *iradix.Tree[Base] {
r := iradix.New[Base]()

defaultRule := Base{
Expand All @@ -284,7 +277,7 @@ func createRadixTree(config Config, info modinfo.ModInfo) *iradix.Tree[Base] {
c.Rules[k] = v
}

r, _, _ = r.Insert([]byte(path.Join(info.Path, override.Package)), c)
r, _, _ = r.Insert([]byte(path.Join(modPath, override.Package)), c)
}

return r
Expand Down
28 changes: 17 additions & 11 deletions tagliatelle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestAnalyzer(t *testing.T) {
}{
{
desc: "simple",
dir: "example.com/one",
patterns: []string{"example.com/one"},
dir: "one",
patterns: []string{"example.com/fake/one"},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand All @@ -43,8 +43,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "with non-applicable overrides",
dir: "example.com/one",
patterns: []string{"example.com/one/..."},
dir: "one",
patterns: []string{"example.com/fake/one/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand Down Expand Up @@ -76,8 +76,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "with applicable overrides",
dir: "example.com/two",
patterns: []string{"example.com/two/..."},
dir: "two",
patterns: []string{"example.com/fake/two/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand Down Expand Up @@ -109,8 +109,8 @@ func TestAnalyzer(t *testing.T) {
},
{
desc: "ignore",
dir: "example.com/three",
patterns: []string{"example.com/three/..."},
dir: "three",
patterns: []string{"example.com/fake/three/..."},
cfg: tagliatelle.Config{
Base: tagliatelle.Base{
Rules: map[string]string{
Expand Down Expand Up @@ -138,6 +138,8 @@ func TestAnalyzer(t *testing.T) {
},
}

os.Setenv("GOPROXY", "off")

Check failure on line 141 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Main Process

Error return value of `os.Setenv` is not checked (errcheck)

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
runWithSuggestedFixes(t, tagliatelle.New(test.cfg), test.dir, test.patterns...)
Expand All @@ -155,12 +157,16 @@ func runWithSuggestedFixes(t *testing.T, a *analysis.Analyzer, dir string, patte

defer func() { _ = os.Chdir(wd) }()

testdata := analysistest.TestData()
tempDir := t.TempDir()

// Needs to be run outside testdata.
err = os.CopyFS(tempDir, os.DirFS(filepath.Join(analysistest.TestData(), "src", "example.com")))

Check failure on line 163 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Go (oldstable, ubuntu-latest)

undefined: os.CopyFS

Check failure on line 163 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Go (oldstable, macos-latest)

undefined: os.CopyFS

Check failure on line 163 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Main Process

ineffectual assignment to err (ineffassign)

Check failure on line 163 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Go (oldstable, ubuntu-latest)

undefined: os.CopyFS

Check failure on line 163 in tagliatelle_test.go

View workflow job for this annotation

GitHub Actions / Go (oldstable, macos-latest)

undefined: os.CopyFS

// NOTE: analysistest does not yet support modules;
// see https://github.com/golang/go/issues/37054 for details.

err = os.Chdir(filepath.Join(testdata, "src", filepath.FromSlash(dir)))
srcPath := filepath.Join(tempDir, filepath.FromSlash(dir))
err = os.Chdir(srcPath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -171,5 +177,5 @@ func runWithSuggestedFixes(t *testing.T, a *analysis.Analyzer, dir string, patte
t.Fatal(err)
}

return analysistest.RunWithSuggestedFixes(t, testdata, a, patterns...)
return analysistest.Run(t, srcPath, a, patterns...)
}
4 changes: 2 additions & 2 deletions testdata/src/example.com/one/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module example.com/one
module example.com/fake/one

go 1.19
go 1.22.0
4 changes: 2 additions & 2 deletions testdata/src/example.com/three/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module example.com/three
module example.com/fake/three

go 1.19
go 1.22.0
4 changes: 2 additions & 2 deletions testdata/src/example.com/two/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module example.com/two
module example.com/fake/two

go 1.19
go 1.22.0

0 comments on commit 4287499

Please sign in to comment.