mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
fix: check if debugtalk.go contains main() function
This commit is contained in:
@@ -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()
|
||||||
|
}
|
||||||
+18
-5
@@ -35,7 +35,7 @@ var (
|
|||||||
regexGoFunctionName = regexFunctions{regexp.MustCompile(`(?m)^func ([a-zA-Z_]\w*)\(.*\)`)}
|
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
|
var functionNames []string
|
||||||
// find all function names
|
// find all function names
|
||||||
functionNameSlice := r.FindAllStringSubmatch(content, -1)
|
functionNameSlice := r.FindAllStringSubmatch(content, -1)
|
||||||
@@ -56,7 +56,11 @@ func (r *regexFunctions) findAllFunctionNames(content string) []string {
|
|||||||
} else if r == ®exGoFunctionName {
|
} else if r == ®exGoFunctionName {
|
||||||
// filter main and init function
|
// filter main and init function
|
||||||
for _, name := range functionNames {
|
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
|
continue
|
||||||
}
|
}
|
||||||
filteredFunctionNames = append(filteredFunctionNames, name)
|
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")
|
log.Info().Strs("functionNames", filteredFunctionNames).Msg("find all function names")
|
||||||
return filteredFunctionNames
|
return filteredFunctionNames, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type pluginTemplate struct {
|
type pluginTemplate struct {
|
||||||
@@ -167,14 +171,19 @@ func (pt *pluginTemplate) generateGo(output string) error {
|
|||||||
|
|
||||||
// buildGo builds debugtalk.go to debugtalk.bin
|
// buildGo builds debugtalk.go to debugtalk.bin
|
||||||
func buildGo(path string, output string) error {
|
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)
|
content, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to read file")
|
log.Error().Err(err).Msg("failed to read file")
|
||||||
return errors.Wrap(err, "read file failed")
|
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{
|
templateContent := &pluginTemplate{
|
||||||
|
path: path,
|
||||||
Version: version.VERSION,
|
Version: version.VERSION,
|
||||||
FunctionNames: functionNames,
|
FunctionNames: functionNames,
|
||||||
}
|
}
|
||||||
@@ -183,6 +192,7 @@ func buildGo(path string, output string) error {
|
|||||||
|
|
||||||
// buildPy completes funppy information in debugtalk.py
|
// buildPy completes funppy information in debugtalk.py
|
||||||
func buildPy(path string, output string) error {
|
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
|
// check the syntax of debugtalk.py
|
||||||
err := builtin.ExecCommand("python3", "-m", "py_compile", path)
|
err := builtin.ExecCommand("python3", "-m", "py_compile", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -194,7 +204,10 @@ func buildPy(path string, output string) error {
|
|||||||
log.Error().Err(err).Msg("failed to read file")
|
log.Error().Err(err).Msg("failed to read file")
|
||||||
return errors.Wrap(err, "read file failed")
|
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{
|
templateContent := &pluginTemplate{
|
||||||
path: path,
|
path: path,
|
||||||
|
|||||||
+24
-9
@@ -60,7 +60,10 @@ def __test_3(): # private function
|
|||||||
def Test5(): # exported function
|
def Test5(): # exported function
|
||||||
pass
|
pass
|
||||||
`
|
`
|
||||||
names := regexPyFunctionName.findAllFunctionNames(content)
|
names, err := regexPyFunctionName.findAllFunctionNames(content)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
if !assert.Contains(t, names, "test_1") {
|
if !assert.Contains(t, names, "test_1") {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
@@ -89,10 +92,6 @@ func testFunc2() { // exported function
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() { // private function
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() { // private function
|
func init() { // private function
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -105,16 +104,16 @@ func _Test3() { // exported function
|
|||||||
// return
|
// return
|
||||||
// }
|
// }
|
||||||
`
|
`
|
||||||
names := regexGoFunctionName.findAllFunctionNames(content)
|
names, err := regexGoFunctionName.findAllFunctionNames(content)
|
||||||
|
if !assert.Nil(t, err) {
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
if !assert.Contains(t, names, "Test1") {
|
if !assert.Contains(t, names, "Test1") {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
if !assert.Contains(t, names, "testFunc2") {
|
if !assert.Contains(t, names, "testFunc2") {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
if !assert.NotContains(t, names, "main") {
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
if !assert.NotContains(t, names, "init") {
|
if !assert.NotContains(t, names, "init") {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
@@ -126,3 +125,19 @@ func _Test3() { // exported function
|
|||||||
t.FailNow()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user