Skip to content

Commit

Permalink
Find unformatted files in dir (Part 1) (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us committed Nov 25, 2023
1 parent b2651e7 commit dfb617c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
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 @@ import (
"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 @@ func (d *SourceDir) Fix(options ...SourceFileOption) error {
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
}
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
}
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
}
err := filepath.WalkDir(d.dir, d.walk(
func(hasChanged bool, path string, content []byte) error {
if !hasChanged {
return nil
}
badFormattedCollection = append(badFormattedCollection, path)
return nil
},
options...,
))
if err != nil {
return nil, fmt.Errorf("failed to walk dif: %w", err)
}

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 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
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

0 comments on commit dfb617c

Please sign in to comment.