Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find unformatted files in dir #141

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"golang.org/x/exp/slices"
)

type walkCallbackFunc = func(hasChanged bool, path string, content []byte) error

const (
goExtension = ".go"
recursivePath = "./..."
Expand Down Expand Up @@ -75,15 +77,54 @@
if !ok {
return ErrPathIsNotDir
}
err := filepath.WalkDir(d.dir, d.walk(options...))
err := filepath.WalkDir(d.dir, d.walk(
func(hasChanged bool, path string, content []byte) error {
if !hasChanged {
return nil
}

Check warning on line 84 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L83-L84

Added lines #L83 - L84 were not covered by tests
if err := os.WriteFile(path, content, 0o644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v\n", path, err)
return err
}

Check warning on line 88 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L86-L88

Added lines #L86 - L88 were not covered by tests
return nil
},
options...,
))
if err != nil {
return fmt.Errorf("failed to walk dif: %w", err)
}

return nil
}

func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
// Find collection of bad formatted paths
func (d *SourceDir) Find(options ...SourceFileOption) ([]string, error) {
var (
ok bool
badFormattedCollection []string
)
d.dir, ok = IsDir(d.dir)
if !ok {
return nil, ErrPathIsNotDir
}

Check warning on line 109 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L108-L109

Added lines #L108 - L109 were not covered by tests
err := filepath.WalkDir(d.dir, d.walk(
func(hasChanged bool, path string, content []byte) error {
if !hasChanged {
return nil
}

Check warning on line 114 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L113-L114

Added lines #L113 - L114 were not covered by tests
badFormattedCollection = append(badFormattedCollection, path)
return nil
},
options...,
))
if err != nil {
return nil, fmt.Errorf("failed to walk dif: %w", err)
}

Check warning on line 122 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L121-L122

Added lines #L121 - L122 were not covered by tests

return badFormattedCollection, nil
}

func (d *SourceDir) walk(callback walkCallbackFunc, options ...SourceFileOption) fs.WalkDirFunc {
return func(path string, dirEntry fs.DirEntry, err error) error {
if !d.isRecursive && dirEntry.IsDir() && filepath.Base(d.dir) != dirEntry.Name() {
return filepath.SkipDir
Expand All @@ -96,11 +137,7 @@
if err != nil {
return fmt.Errorf("failed to fix: %w", err)
}
if hasChange {
if err := os.WriteFile(path, content, 0o644); err != nil {
log.Fatalf("failed to write fixed result to file(%s): %+v\n", path, err)
}
}
return callback(hasChange, path, content)
}
return nil
}
Expand Down
73 changes: 72 additions & 1 deletion reviser/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func main() {
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {

exec(tt, func(ttt *testing.T) error {
// executing SourceDir.Fix
err := NewSourceDir(test.args.project, test.args.path, true, test.args.excludes).Fix()
Expand Down Expand Up @@ -163,3 +162,75 @@ func TestSourceDir_IsExcluded(t *testing.T) {
})
}
}

func TestSourceDir_Find(t *testing.T) {
testFile := "testdata/dir/dir1/file1.go"

originContent := `package dir1
import (
"strings"

"fmt"
)
func main() {
fmt.Println(strings.ToLower("Hello World!"))
}
`
exec := func(tt *testing.T, fn func(*testing.T) error) {
// create test file
err := os.MkdirAll(filepath.Dir(testFile), os.ModePerm)
assert.NoError(tt, err)
err = os.WriteFile(testFile, []byte(originContent), os.ModePerm)
assert.NoError(tt, err)

// exec test func
err = fn(tt)
assert.NoError(tt, err)

// remove test file
err = os.Remove(testFile)
assert.NoError(tt, err)
}
var sortedContent string
exec(t, func(tt *testing.T) error {
sortedData, changed, err := NewSourceFile("testdata", testFile).Fix()
assert.NoError(tt, err)
sortedContent = string(sortedData)
assert.Equal(tt, true, changed)
assert.NotEqual(tt, originContent, sortedContent)
return nil
})

type args struct {
project string
path string
excludes string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "found-unformatted",
args: args{project: "testdata", path: "testdata/dir", excludes: "dir1" + sep + "test.go"},
want: []string{"testdata/dir/dir1/file1.go"},
},
}
for _, test := range tests {
t.Run(test.name, func(tt *testing.T) {
exec(tt, func(ttt *testing.T) error {
files, err := NewSourceDir(test.args.project, test.args.path, true, test.args.excludes).Find()
assert.NoError(tt, err)
rootPath, err := os.Getwd()
assert.NoError(tt, err)
var want []string
for _, w := range test.want {
want = append(want, rootPath+"/"+w)
}
assert.Equal(tt, want, files)
return nil
})
})
}
}
4 changes: 3 additions & 1 deletion reviser/file_option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package reviser

import "strings"
import (
"strings"
)

// SourceFileOption is an int alias for options
type SourceFileOption func(f *SourceFile) error
Expand Down
Loading