diff --git a/main.go b/main.go index e558016..d73ec14 100644 --- a/main.go +++ b/main.go @@ -269,7 +269,7 @@ func main() { if _, ok := reviser.IsDir(originPath); ok { err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...) if err != nil { - log.Fatalf("Failed to fix directory: %+v\n", err) + log.Fatalf("Failed to fix directory %s: %+v\n", originPath, err) } return } diff --git a/pkg/astutil/astutil.go b/pkg/astutil/astutil.go index 0157bc4..95fadc4 100644 --- a/pkg/astutil/astutil.go +++ b/pkg/astutil/astutil.go @@ -64,7 +64,8 @@ func UsesImport(f *ast.File, packageImports PackageImports, importPath string) b } // LoadPackageDependencies will return all package's imports with it names: -// key - package(ex.: github/pkg/errors), value - name(ex.: errors) +// +// key - package(ex.: github/pkg/errors), value - name(ex.: errors) func LoadPackageDependencies(dir, buildTag string) (PackageImports, error) { cfg := &packages.Config{ Dir: dir, diff --git a/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go index 408d81c..26f5261 100644 --- a/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go +++ b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go @@ -1,4 +1,5 @@ -//+build test +//go:build test +// +build test package testdata diff --git a/reviser/dir.go b/reviser/dir.go index 21d0c55..8653fa3 100644 --- a/reviser/dir.go +++ b/reviser/dir.go @@ -34,12 +34,17 @@ type SourceDir struct { } func NewSourceDir(projectName string, path string, isRecursive bool, excludes string) *SourceDir { - if path == recursivePath { - isRecursive = true - } patterns := make([]string, 0) + // get the absolute path absPath, err := filepath.Abs(path) + + // if path is recursive, then we need to remove the "/..." suffix + if path == recursivePath { + isRecursive = true + absPath = strings.TrimSuffix(absPath, "/...") + } + if err == nil { segs := strings.Split(excludes, ",") for _, seg := range segs { diff --git a/reviser/dir_test.go b/reviser/dir_test.go index cb0a43b..1356309 100644 --- a/reviser/dir_test.go +++ b/reviser/dir_test.go @@ -10,6 +10,16 @@ import ( const sep = string(os.PathSeparator) +func TestNewSourceDir(t *testing.T) { + t.Run("should generate source dir from recursive path", func(tt *testing.T) { + dir := NewSourceDir("project", recursivePath, false, "") + assert.Equal(tt, "project", dir.projectName) + assert.NotContains(tt, dir.dir, "/...") + assert.Equal(tt, true, dir.isRecursive) + assert.Equal(tt, 0, len(dir.excludePatterns)) + }) +} + func TestSourceDir_Fix(t *testing.T) { testFile := "testdata/dir/dir1/file1.go"