Implement #4442 , goctl generate unit test files for api handler and logic (#4443)

This commit is contained in:
Devin
2025-01-22 14:52:49 +08:00
committed by GitHub
parent bf883101d7
commit 6138f85470
10 changed files with 338 additions and 6 deletions

View File

@@ -38,7 +38,8 @@ var (
// VarStringBranch describes the branch.
VarStringBranch string
// VarStringStyle describes the style of output files.
VarStringStyle string
VarStringStyle string
VarBoolWithTest bool
)
// GoCommand gen go project files from command line
@@ -49,6 +50,7 @@ func GoCommand(_ *cobra.Command, _ []string) error {
home := VarStringHome
remote := VarStringRemote
branch := VarStringBranch
withTest := VarBoolWithTest
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@@ -66,11 +68,11 @@ func GoCommand(_ *cobra.Command, _ []string) error {
return errors.New("missing -dir")
}
return DoGenProject(apiFile, dir, namingStyle)
return DoGenProject(apiFile, dir, namingStyle, withTest)
}
// DoGenProject gen go project files with api file
func DoGenProject(apiFile, dir, style string) error {
func DoGenProject(apiFile, dir, style string, withTest bool) error {
api, err := parser.Parse(apiFile)
if err != nil {
return err
@@ -100,6 +102,10 @@ func DoGenProject(apiFile, dir, style string) error {
logx.Must(genHandlers(dir, rootPkg, cfg, api))
logx.Must(genLogic(dir, rootPkg, cfg, api))
logx.Must(genMiddleware(dir, cfg, api))
if withTest {
logx.Must(genHandlersTest(dir, rootPkg, cfg, api))
logx.Must(genLogicTest(dir, rootPkg, cfg, api))
}
if err := backupAndSweep(apiFile); err != nil {
return err