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

fix bug list-diff support dir mode #140

Closed
wants to merge 1 commit into from
Closed
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
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)
}
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, true, 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, true, args.excludes).isExcluded(args.testPath)
assert.Equal(tt, test.want, excluded)
})
}
Expand Down