Skip to content

Commit

Permalink
fix: check nonExist err
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Jan 21, 2025
1 parent eac7ee6 commit f12b9fe
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 36 deletions.
81 changes: 45 additions & 36 deletions tool/internal_pkg/generator/custom_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,27 +101,17 @@ func (c *customGenerator) loopGenerate(tpl *Template) error {
continue
}
// using scanDir to substitute the default directory
if tpl.UpdateBehavior.ScanDirRecursively {
fileName := filepath.Base(filePath)
dirPath := filepath.Dir(filePath)
var found bool
if err = filepath.Walk(dirPath, func(path string, info fs.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !info.IsDir() && info.Name() == fileName {
found = true
filePath = path
return nil
}
return nil
}); err != nil {
return err
}
if found && updateType(tpl.UpdateBehavior.Type) == skip {
scanPath, scanErr := scanDir(filePath, tpl.UpdateBehavior)
if scanErr != nil {
return scanErr
}
if scanPath != "" {
if updateType(tpl.UpdateBehavior.Type) == skip {
continue
}
filePath = scanPath
}

task := &Task{
Name: path.Base(renderPath),
Path: filePath,
Expand All @@ -136,6 +126,36 @@ func (c *customGenerator) loopGenerate(tpl *Template) error {
return nil
}

// scanDir scans the directory of the filePath recursively and returns the path of the file with the same name.
func scanDir(filePath string, behavior *Update) (res string, err error) {
if behavior == nil || !behavior.ScanDirRecursively {
return "", nil
}
fileName := filepath.Base(filePath)
dirPath := filepath.Dir(filePath)
var found bool
if err := filepath.Walk(dirPath, func(path string, info fs.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !info.IsDir() && info.Name() == fileName {
found = true
res = path
return nil
}
return nil
}); err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
if found {
return res, nil
}
return "", nil
}

func (c *customGenerator) commonGenerate(tpl *Template) error {
// Use all services including base service.
tmp := c.pkg.Methods
Expand All @@ -158,26 +178,15 @@ func (c *customGenerator) commonGenerate(tpl *Template) error {
return nil
}
// using scanDir to substitute the default directory
if tpl.UpdateBehavior.ScanDirRecursively {
fileName := filepath.Base(filePath)
dirPath := filepath.Dir(filePath)
var found bool
if err = filepath.Walk(dirPath, func(path string, info fs.FileInfo, walkErr error) error {
if walkErr != nil {
return walkErr
}
if !info.IsDir() && info.Name() == fileName {
found = true
filePath = path
return nil
}
return nil
}); err != nil {
return err
}
if found && updateType(tpl.UpdateBehavior.Type) == skip {
scanPath, scanErr := scanDir(filePath, tpl.UpdateBehavior)
if scanErr != nil {
return scanErr
}
if scanPath != "" {
if updateType(tpl.UpdateBehavior.Type) == skip {
return nil
}
filePath = scanPath
}
var f *File
if update && updateType(tpl.UpdateBehavior.Type) == incrementalUpdate {
Expand Down
29 changes: 29 additions & 0 deletions tool/internal_pkg/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,32 @@ func Test_needCallOpt(t *testing.T) {
test.Assert(t, needCallOpt(pkg))
})
}

func Test_scanDir(t *testing.T) {
testcases := []struct {
filePath string
update *Update
expectPath string
expectErr error
}{
{
filePath: "./test_dir/test1.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
expectPath: "./test_dir/test_dir1/test1.txt",
},
{
filePath: "./test_dir/test2.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
},
{
filePath: "./non_exist_dir/test3.txt",
update: &Update{ScanDirRecursively: true, Type: "skip"},
},
}

for _, tc := range testcases {
res, err := scanDir(tc.filePath, tc.update)
test.Assert(t, err == tc.expectErr, err)
test.Assert(t, res == tc.expectPath, res)
}
}
Empty file.
Empty file.

0 comments on commit f12b9fe

Please sign in to comment.