diff --git a/cmd/hz/generator/package_tpl.go b/cmd/hz/generator/package_tpl.go index 7f72b9b91..a03e27824 100644 --- a/cmd/hz/generator/package_tpl.go +++ b/cmd/hz/generator/package_tpl.go @@ -29,7 +29,7 @@ var ( idlClientName = "idl_client.go" // client of service for quick call insertPointNew = "//INSERT_POINT: DO NOT DELETE THIS LINE!" - insertPointPatternNew = `//INSERT_POINT\: DO NOT DELETE THIS LINE\!` + insertPointPatternNew = `//\s?INSERT_POINT\: DO NOT DELETE THIS LINE\!` ) var templateNameSet = map[string]string{ diff --git a/cmd/hz/generator/package_tpl_test.go b/cmd/hz/generator/package_tpl_test.go new file mode 100644 index 000000000..9252a5329 --- /dev/null +++ b/cmd/hz/generator/package_tpl_test.go @@ -0,0 +1,93 @@ +/* + * Copyright 2022 CloudWeGo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package generator + +import ( + "regexp" + "testing" +) + +func TestMatchInsertPointPatternNew(t *testing.T) { + reg := regexp.MustCompile(insertPointPatternNew) + + ValidPatternFunc := func(s string) bool { + subIndexReg := reg.FindSubmatchIndex([]byte(s)) + if len(subIndexReg) != 2 || subIndexReg[0] < 1 { + return false + } + return true + } + + type args struct { + name string + fileContent string + } + tests := []struct { + name string + args args + WantValid bool + }{ + {"TestInsertPointPatternNoSpace", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "demo/biz/router/admin" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + //INSERT_POINT: DO NOT DELETE THIS LINE! + admin.Register(r) +}`}, true}, + {"TestInsertPointPatternHasSpace", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "demo/biz/router/admin" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + // INSERT_POINT: DO NOT DELETE THIS LINE! + admin.Register(r) +}`}, true}, + {"TestInsertPointPatternWrongFormat", args{"test", `// Code generated by hertz generator. DO NOT EDIT. + +package router + +import ( + admin "demo/biz/router/admin" + "github.com/cloudwego/hertz/pkg/app/server" +) + +// GeneratedRegister registers routers generated by IDL. +func GeneratedRegister(r *server.Hertz) { + admin.Register(r) +}`}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := ValidPatternFunc(tt.args.fileContent); got != tt.WantValid { + t.Errorf("wrong format %s: insert-point '%s' not found", tt.args.fileContent, insertPointNew) + } + }) + } +} diff --git a/cmd/hz/generator/router.go b/cmd/hz/generator/router.go index 10f431657..42b186efe 100644 --- a/cmd/hz/generator/router.go +++ b/cmd/hz/generator/router.go @@ -327,7 +327,7 @@ func (pkgGen *HttpPackageGenerator) updateRegister(pkg, rDir, pkgName string) er subIndexReg := regRegisterV3.FindSubmatchIndex(file) if len(subIndexReg) != 2 || subIndexReg[0] < 1 { - return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointPatternNew) + return fmt.Errorf("wrong format %s: insert-point '%s' not found", string(file), insertPointNew) } bufReg := bytes.NewBuffer(nil)