From ed23792a6c7abd48a413f00541fd21d3faf9c9a7 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Sat, 25 Nov 2023 22:14:33 +0200 Subject: [PATCH] [#139] Find unformatted files in dir (Part 2) (#142) --- main.go | 8 +++++ reviser/dir.go | 31 +++++++++++++++++-- reviser/dir_test.go | 72 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 7b8576d..8ae40d8 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/reviser/dir.go b/reviser/dir.go index a967492..d24d30e 100644 --- a/reviser/dir.go +++ b/reviser/dir.go @@ -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 @@ -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 { @@ -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 diff --git a/reviser/dir_test.go b/reviser/dir_test.go index a2d265a..c97dd00 100644 --- a/reviser/dir_test.go +++ b/reviser/dir_test.go @@ -3,6 +3,7 @@ package reviser import ( "os" "path/filepath" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -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()) + }) + } +}