Skip to content

Commit

Permalink
fix: filed to fix directory when using ./... path (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
danteay authored Aug 26, 2023
1 parent 8ec65d1 commit 91b69df
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion pkg/astutil/testdata/testdata_with_deprecated_build_tag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build test
//go:build test
// +build test

package testdata

Expand Down
11 changes: 8 additions & 3 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions reviser/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 91b69df

Please sign in to comment.