Skip to content

Commit

Permalink
[#139] Find unformatted files in dir (Part 2) (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
incu6us committed Nov 25, 2023
1 parent dfb617c commit ed23792
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 4 deletions.
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ func main() {
close(deprecatedMessagesCh)

if _, ok := reviser.IsDir(originPath); ok {
if *listFileName {
unformattedFiles, err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Find(options...)
if err != nil {
log.Fatalf("Failed to find unformatted files %s: %+v\n", originPath, err)
}
fmt.Printf("%s\n", unformattedFiles.String())
return
}
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...)
if err != nil {
log.Fatalf("Failed to fix directory %s: %+v\n", originPath, err)
Expand Down
31 changes: 29 additions & 2 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (d *SourceDir) Fix(options ...SourceFileOption) error {
}

// Find collection of bad formatted paths
func (d *SourceDir) Find(options ...SourceFileOption) ([]string, error) {
func (d *SourceDir) Find(options ...SourceFileOption) (*UnformattedCollection, error) {
var (
ok bool
badFormattedCollection []string
Expand All @@ -121,7 +121,7 @@ func (d *SourceDir) Find(options ...SourceFileOption) ([]string, error) {
return nil, fmt.Errorf("failed to walk dif: %w", err)
}

return badFormattedCollection, nil
return newUnformattedCollection(badFormattedCollection), nil
}

func (d *SourceDir) walk(callback walkCallbackFunc, options ...SourceFileOption) fs.WalkDirFunc {
Expand Down Expand Up @@ -159,6 +159,33 @@ func (d *SourceDir) isExcluded(path string) bool {
return false
}

type UnformattedCollection struct {
list []string
}

func newUnformattedCollection(list []string) *UnformattedCollection {
return &UnformattedCollection{
list: list,
}
}

func (c *UnformattedCollection) List() []string {
list := make([]string, len(c.list))
copy(list, c.list)
return list
}

func (c *UnformattedCollection) String() string {
var builder strings.Builder
for i, file := range c.list {
builder.WriteString(file)
if len(c.list)-1 > i {
builder.WriteString("\n")
}
}
return builder.String()
}

func IsDir(path string) (string, bool) {
if path == recursivePath || slices.Contains(currentPaths, path) {
var err error
Expand Down
72 changes: 70 additions & 2 deletions reviser/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package reviser
import (
"os"
"path/filepath"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -226,11 +227,78 @@ func main() {
assert.NoError(tt, err)
var want []string
for _, w := range test.want {
want = append(want, rootPath+"/"+w)
want = append(want, filepath.Join(rootPath, w))
}
assert.Equal(tt, want, files)
assert.Equal(tt, want, files.List())
return nil
})
})
}
}

func TestUnformattedCollection_List(t *testing.T) {
tests := []struct {
name string
init func(t *testing.T) *UnformattedCollection
inspect func(r *UnformattedCollection, t *testing.T) //inspects receiver after test run

want1 []string
}{
{
name: "sucess",
init: func(t *testing.T) *UnformattedCollection {
return newUnformattedCollection([]string{"1", "2"})
},
inspect: func(r *UnformattedCollection, t *testing.T) {

},
want1: []string{"1", "2"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
receiver := tt.init(t)
got1 := receiver.List()

if tt.inspect != nil {
tt.inspect(receiver, t)
}

if !reflect.DeepEqual(got1, tt.want1) {
t.Errorf("UnformattedCollection.List got1 = %v, want1: %v", got1, tt.want1)
}
})
}
}

func TestUnformattedCollection_String(t *testing.T) {
tests := []struct {
name string
init func(t *testing.T) *UnformattedCollection
inspect func(r *UnformattedCollection, t *testing.T) //inspects receiver after test run
want string
}{
{
name: "success",
init: func(t *testing.T) *UnformattedCollection {
return newUnformattedCollection([]string{"1", "2"})
},
inspect: func(r *UnformattedCollection, t *testing.T) {

},
want: `1
2`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
receiver := tt.init(t)
if tt.inspect != nil {
tt.inspect(receiver, t)
}
assert.Equal(t, tt.want, receiver.String())
})
}
}

0 comments on commit ed23792

Please sign in to comment.