Skip to content

Commit

Permalink
dir mode support list-diff flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericwww committed Nov 22, 2023
1 parent b2651e7 commit 94f7d60
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 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() {
close(deprecatedMessagesCh)

if _, ok := reviser.IsDir(originPath); ok {
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, excludes).Fix(options...)
err := reviser.NewSourceDir(originProjectName, originPath, *isRecursive, *listFileName, excludes).Fix(options...)
if err != nil {
log.Fatalf("Failed to fix directory %s: %+v\n", originPath, err)
}
Expand Down
23 changes: 14 additions & 9 deletions reviser/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ var (

// SourceDir to validate and fix import
type SourceDir struct {
projectName string
dir string
isRecursive bool
excludePatterns []string // see filepath.Match
projectName string
dir string
isRecursive bool
shouldListFileName bool
excludePatterns []string // see filepath.Match
}

func NewSourceDir(projectName string, path string, isRecursive bool, excludes string) *SourceDir {
func NewSourceDir(projectName string, path string, isRecursive, shouldListFileName bool, excludes string) *SourceDir {
patterns := make([]string, 0)

// get the absolute path
Expand Down Expand Up @@ -62,10 +63,11 @@ func NewSourceDir(projectName string, path string, isRecursive bool, excludes st
}
}
return &SourceDir{
projectName: projectName,
dir: absPath,
isRecursive: isRecursive,
excludePatterns: patterns,
projectName: projectName,
dir: absPath,
isRecursive: isRecursive,
shouldListFileName: shouldListFileName,
excludePatterns: patterns,
}
}

Expand Down Expand Up @@ -97,6 +99,9 @@ func (d *SourceDir) walk(options ...SourceFileOption) fs.WalkDirFunc {
return fmt.Errorf("failed to fix: %w", err)
}
if hasChange {
if d.shouldListFileName {
fmt.Println(path)
}

Check warning on line 104 in reviser/dir.go

View check run for this annotation

Codecov / codecov/patch

reviser/dir.go#L103-L104

Added lines #L103 - L104 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)
}
Expand Down
7 changes: 4 additions & 3 deletions reviser/dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ 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, "")
dir := NewSourceDir("project", recursivePath, false, false, "")
assert.Equal(tt, "project", dir.projectName)
assert.NotContains(tt, dir.dir, "/...")
assert.Equal(tt, true, dir.isRecursive)
assert.Equal(tt, false, dir.shouldListFileName)
assert.Equal(tt, 0, len(dir.excludePatterns))
})
}
Expand Down Expand Up @@ -99,7 +100,7 @@ func main() {

exec(tt, func(ttt *testing.T) error {
// executing SourceDir.Fix
err := NewSourceDir(test.args.project, test.args.path, true, test.args.excludes).Fix()
err := NewSourceDir(test.args.project, test.args.path, true, false, test.args.excludes).Fix()
assert.NoError(tt, err)
// read new content
content, err := os.ReadFile(testFile)
Expand Down Expand Up @@ -158,7 +159,7 @@ func TestSourceDir_IsExcluded(t *testing.T) {
for _, test := range tests {
args := test.args
t.Run(test.name, func(tt *testing.T) {
excluded := NewSourceDir(args.project, args.path, true, args.excludes).isExcluded(args.testPath)
excluded := NewSourceDir(args.project, args.path, true, false, args.excludes).isExcluded(args.testPath)
assert.Equal(tt, test.want, excluded)
})
}
Expand Down

0 comments on commit 94f7d60

Please sign in to comment.