diff --git a/.gitignore b/.gitignore index 1c8cc1fb0..b5318f47e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,6 +14,7 @@ bin/ cmd/gf/main cmd/gf/gf temp/ +example/log go.work go.work.sum !cmd/gf/go.work diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go index 4491a3fcb..562dc98e4 100644 --- a/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go @@ -73,3 +73,46 @@ func Test_Gen_Service_Default(t *testing.T) { } }) } + +// https://github.com/gogf/gf/issues/3328 +func Test_Issue3328(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + path = gfile.Temp(guid.S()) + dstFolder = path + filepath.FromSlash("/service") + apiFolder = gtest.DataPath("issue", "3328", "logic") + logicGoPath = apiFolder + filepath.FromSlash("/logic.go") + in = genservice.CGenServiceInput{ + SrcFolder: apiFolder, + DstFolder: dstFolder, + DstFileNameCase: "Snake", + WatchFile: "", + StPattern: "", + Packages: nil, + ImportPrefix: "", + Clear: false, + } + ) + gfile.Remove(logicGoPath) + defer gfile.Remove(logicGoPath) + + err := gutil.FillStructWithDefault(&in) + t.AssertNil(err) + + err = gfile.Mkdir(path) + t.AssertNil(err) + defer gfile.Remove(path) + + _, err = genservice.CGenService{}.Service(ctx, in) + t.AssertNil(err) + + files, err := gfile.ScanDir(apiFolder, "*", true) + for _, file := range files { + if file == logicGoPath { + if gfile.IsDir(logicGoPath) { + t.Fatalf("%s should not is folder", logicGoPath) + } + } + } + }) +} diff --git a/cmd/gf/internal/cmd/genservice/genservice.go b/cmd/gf/internal/cmd/genservice/genservice.go index 5b9ae7efc..392cd4308 100644 --- a/cmd/gf/internal/cmd/genservice/genservice.go +++ b/cmd/gf/internal/cmd/genservice/genservice.go @@ -289,7 +289,7 @@ func (c CGenService) checkAndUpdateMain(srcFolder string) (err error) { var ( logicPackageName = gstr.ToLower(gfile.Basename(srcFolder)) logicFilePath = gfile.Join(srcFolder, logicPackageName+".go") - importPath = utils.GetImportPath(logicFilePath) + importPath = utils.GetImportPath(srcFolder) importStr = fmt.Sprintf(`_ "%s"`, importPath) mainFilePath = gfile.Join(gfile.Dir(gfile.Dir(gfile.Dir(logicFilePath))), "main.go") mainFileContent = gfile.GetContents(mainFilePath) diff --git a/cmd/gf/internal/cmd/testdata/issue/3328/logic/.gitkeep b/cmd/gf/internal/cmd/testdata/issue/3328/logic/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/cmd/gf/internal/utility/utils/utils.go b/cmd/gf/internal/utility/utils/utils.go index 66625079a..94047db19 100644 --- a/cmd/gf/internal/utility/utils/utils.go +++ b/cmd/gf/internal/utility/utils/utils.go @@ -75,28 +75,24 @@ func ReplaceGeneratedContentGFV2(folderPath string) (err error) { }, folderPath, "*.go", true) } -// GetImportPath calculates and returns the golang import path for given `filePath`. +// GetImportPath calculates and returns the golang import path for given `dirPath`. // Note that it needs a `go.mod` in current working directory or parent directories to detect the path. -func GetImportPath(filePath string) string { +func GetImportPath(dirPath string) string { // If `filePath` does not exist, create it firstly to find the import path. - var realPath = gfile.RealPath(filePath) + var realPath = gfile.RealPath(dirPath) if realPath == "" { - _ = gfile.Mkdir(filePath) - realPath = gfile.RealPath(filePath) + _ = gfile.Mkdir(dirPath) + realPath = gfile.RealPath(dirPath) } var ( newDir = gfile.Dir(realPath) oldDir string - suffix string + suffix = gfile.Basename(dirPath) goModName = "go.mod" goModPath string importPath string ) - - if gfile.IsDir(filePath) { - suffix = gfile.Basename(filePath) - } for { goModPath = gfile.Join(newDir, goModName) if gfile.Exists(goModPath) { diff --git a/test/gtest/gtest_util.go b/test/gtest/gtest_util.go index 9d30df10c..49ec4b269 100644 --- a/test/gtest/gtest_util.go +++ b/test/gtest/gtest_util.go @@ -364,11 +364,11 @@ func AssertNil(value interface{}) { // which will be joined with current system separator and returned with the path. func DataPath(names ...string) string { _, path, _ := gdebug.CallerWithFilter([]string{pathFilterKey}) - path = filepath.Dir(path) + string(filepath.Separator) + "testdata" + path = filepath.Dir(path) + "/testdata" for _, name := range names { - path += string(filepath.Separator) + name + path += "/" + name } - return path + return filepath.FromSlash(path) } // DataContent retrieves and returns the file content for specified testdata path of current package