From f12b9fe859d6644f1e85e14e07ddf9a2ca5993f5 Mon Sep 17 00:00:00 2001 From: "yuxuan.wang1" Date: Tue, 21 Jan 2025 19:35:13 +0800 Subject: [PATCH] fix: check nonExist err --- .../internal_pkg/generator/custom_template.go | 81 ++++++++++--------- tool/internal_pkg/generator/generator_test.go | 29 +++++++ .../internal_pkg/generator/test_dir/test0.txt | 0 .../generator/test_dir/test_dir1/test1.txt | 0 4 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 tool/internal_pkg/generator/test_dir/test0.txt create mode 100644 tool/internal_pkg/generator/test_dir/test_dir1/test1.txt diff --git a/tool/internal_pkg/generator/custom_template.go b/tool/internal_pkg/generator/custom_template.go index 933327e50f..7d2aabd1fb 100644 --- a/tool/internal_pkg/generator/custom_template.go +++ b/tool/internal_pkg/generator/custom_template.go @@ -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, @@ -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 @@ -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 { diff --git a/tool/internal_pkg/generator/generator_test.go b/tool/internal_pkg/generator/generator_test.go index 7b3d160a70..f10435a57b 100644 --- a/tool/internal_pkg/generator/generator_test.go +++ b/tool/internal_pkg/generator/generator_test.go @@ -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) + } +} diff --git a/tool/internal_pkg/generator/test_dir/test0.txt b/tool/internal_pkg/generator/test_dir/test0.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tool/internal_pkg/generator/test_dir/test_dir1/test1.txt b/tool/internal_pkg/generator/test_dir/test_dir1/test1.txt new file mode 100644 index 0000000000..e69de29bb2