fix: check if debugtalk.go contains main() function

This commit is contained in:
debugtalk
2022-06-14 23:17:34 +08:00
parent 797e0ea56f
commit ec345d9e53
3 changed files with 58 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
// NOTE: Generated By hrp v4.1.3, DO NOT EDIT!
package main
import (
"github.com/httprunner/funplugin/fungo"
)
func main() {
fungo.Register("SumTwoInt", SumTwoInt)
fungo.Register("SumInts", SumInts)
fungo.Register("Sum", Sum)
fungo.Register("SetupHookExample", SetupHookExample)
fungo.Register("TeardownHookExample", TeardownHookExample)
fungo.Register("GetUserAgent", GetUserAgent)
fungo.Serve()
}

View File

@@ -35,7 +35,7 @@ var (
regexGoFunctionName = regexFunctions{regexp.MustCompile(`(?m)^func ([a-zA-Z_]\w*)\(.*\)`)}
)
func (r *regexFunctions) findAllFunctionNames(content string) []string {
func (r *regexFunctions) findAllFunctionNames(content string) ([]string, error) {
var functionNames []string
// find all function names
functionNameSlice := r.FindAllStringSubmatch(content, -1)
@@ -56,7 +56,11 @@ func (r *regexFunctions) findAllFunctionNames(content string) []string {
} else if r == &regexGoFunctionName {
// filter main and init function
for _, name := range functionNames {
if name == "main" || name == "init" {
if name == "main" {
log.Warn().Msg("plugin debugtalk.go should not define main() function !!!")
return nil, errors.New("debugtalk.go should not contain main() function")
}
if name == "init" {
continue
}
filteredFunctionNames = append(filteredFunctionNames, name)
@@ -64,7 +68,7 @@ func (r *regexFunctions) findAllFunctionNames(content string) []string {
}
log.Info().Strs("functionNames", filteredFunctionNames).Msg("find all function names")
return filteredFunctionNames
return filteredFunctionNames, nil
}
type pluginTemplate struct {
@@ -167,14 +171,19 @@ func (pt *pluginTemplate) generateGo(output string) error {
// buildGo builds debugtalk.go to debugtalk.bin
func buildGo(path string, output string) error {
log.Info().Str("path", path).Str("output", output).Msg("start to build go plugin")
content, err := os.ReadFile(path)
if err != nil {
log.Error().Err(err).Msg("failed to read file")
return errors.Wrap(err, "read file failed")
}
functionNames := regexGoFunctionName.findAllFunctionNames(string(content))
functionNames, err := regexGoFunctionName.findAllFunctionNames(string(content))
if err != nil {
return err
}
templateContent := &pluginTemplate{
path: path,
Version: version.VERSION,
FunctionNames: functionNames,
}
@@ -183,6 +192,7 @@ func buildGo(path string, output string) error {
// buildPy completes funppy information in debugtalk.py
func buildPy(path string, output string) error {
log.Info().Str("path", path).Str("output", output).Msg("start to prepare python plugin")
// check the syntax of debugtalk.py
err := builtin.ExecCommand("python3", "-m", "py_compile", path)
if err != nil {
@@ -194,7 +204,10 @@ func buildPy(path string, output string) error {
log.Error().Err(err).Msg("failed to read file")
return errors.Wrap(err, "read file failed")
}
functionNames := regexPyFunctionName.findAllFunctionNames(string(content))
functionNames, err := regexPyFunctionName.findAllFunctionNames(string(content))
if err != nil {
return err
}
templateContent := &pluginTemplate{
path: path,

View File

@@ -60,7 +60,10 @@ def __test_3(): # private function
def Test5(): # exported function
pass
`
names := regexPyFunctionName.findAllFunctionNames(content)
names, err := regexPyFunctionName.findAllFunctionNames(content)
if !assert.Nil(t, err) {
t.FailNow()
}
if !assert.Contains(t, names, "test_1") {
t.FailNow()
}
@@ -89,10 +92,6 @@ func testFunc2() { // exported function
return
}
func main() { // private function
return
}
func init() { // private function
return
}
@@ -105,16 +104,16 @@ func _Test3() { // exported function
// return
// }
`
names := regexGoFunctionName.findAllFunctionNames(content)
names, err := regexGoFunctionName.findAllFunctionNames(content)
if !assert.Nil(t, err) {
t.FailNow()
}
if !assert.Contains(t, names, "Test1") {
t.FailNow()
}
if !assert.Contains(t, names, "testFunc2") {
t.FailNow()
}
if !assert.NotContains(t, names, "main") {
t.FailNow()
}
if !assert.NotContains(t, names, "init") {
t.FailNow()
}
@@ -126,3 +125,19 @@ func _Test3() { // exported function
t.FailNow()
}
}
func TestFindAllGoFunctionNamesAbnormal(t *testing.T) {
content := `
func init() { // private function
return
}
func main() { // should not define main() function
return
}
`
_, err := regexGoFunctionName.findAllFunctionNames(content)
if !assert.NotNil(t, err) {
t.FailNow()
}
}