mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 07:57:27 +08:00
refactor: hrp build
This commit is contained in:
+28
-27
@@ -5,7 +5,6 @@ import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -31,6 +30,11 @@ const (
|
||||
regexGoFunctionContent = `func [\s\S]*?\n}`
|
||||
)
|
||||
|
||||
const (
|
||||
genDebugTalkGo = "debugtalk_gen.go"
|
||||
genDebugTalkPy = "debugtalk_gen.py"
|
||||
)
|
||||
|
||||
//go:embed templates/debugtalkPythonTemplate
|
||||
var pyTemplate string
|
||||
|
||||
@@ -55,7 +59,7 @@ type Regexps struct {
|
||||
}
|
||||
|
||||
func (t *TemplateContent) parseGoContent(path string) error {
|
||||
log.Info().Msg(fmt.Sprintf("start to parse %v", path))
|
||||
log.Info().Str("path", path).Msg("start to parse debugtalk.go")
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -108,7 +112,7 @@ func (t *TemplateContent) parseGoContent(path string) error {
|
||||
func (t *TemplateContent) parsePyContent(path string) error {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %s\n", err)
|
||||
log.Error().Err(err).Str("path", path).Msg("failed to open file")
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
@@ -191,38 +195,34 @@ func buildGo(path string, output string) error {
|
||||
},
|
||||
}
|
||||
|
||||
// create temp dir for building
|
||||
tempDir, err := ioutil.TempDir("", "hrp_build")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pluginDir := filepath.Dir(path)
|
||||
|
||||
// check go sdk in tempDir
|
||||
if err := builtin.ExecCommandInDir(exec.Command("go", "version"), tempDir); err != nil {
|
||||
if err := builtin.ExecCommandInDir(exec.Command("go", "version"), pluginDir); err != nil {
|
||||
return errors.Wrap(err, "go sdk not installed")
|
||||
}
|
||||
|
||||
// create pluginDir
|
||||
pluginDir := filepath.Join(tempDir, "plugin")
|
||||
if err := builtin.CreateFolder(pluginDir); err != nil {
|
||||
return err
|
||||
}
|
||||
// parse debugtalk.go in pluginDir
|
||||
err = templateContent.parseGoContent(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// generate debugtalk.go in pluginDir
|
||||
err = templateContent.genDebugTalk(filepath.Join(pluginDir, "debugtalk.go"), goTemplate)
|
||||
err := templateContent.parseGoContent(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// create go mod
|
||||
if err := builtin.ExecCommandInDir(exec.Command("go", "mod", "init", "plugin"), pluginDir); err != nil {
|
||||
// generate debugtalk.go in pluginDir
|
||||
err = templateContent.genDebugTalk(filepath.Join(pluginDir, genDebugTalkGo), goTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// create go mod if not exists
|
||||
goModFile := filepath.Join(pluginDir, "go.mod")
|
||||
if !builtin.IsFilePathExists(goModFile) {
|
||||
err = builtin.ExecCommandInDir(exec.Command("go", "mod", "init", "plugin"), pluginDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// download plugin dependency
|
||||
// funplugin version should be locked
|
||||
funplugin := fmt.Sprintf("github.com/httprunner/funplugin@%s", shared.Version)
|
||||
@@ -242,10 +242,11 @@ func buildGo(path string, output string) error {
|
||||
}
|
||||
|
||||
// build plugin debugtalk.bin
|
||||
if err := builtin.ExecCommandInDir(exec.Command("go", "build", "-o", outputPath, "debugtalk.go"), pluginDir); err != nil {
|
||||
cmd := exec.Command("go", "build", "-o", outputPath, genDebugTalkGo, filepath.Base(path))
|
||||
if err := builtin.ExecCommandInDir(cmd, pluginDir); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info().Msg(fmt.Sprintf("build %s to %s successfully", path, outputPath))
|
||||
log.Info().Str("output", outputPath).Str("plugin", path).Msg("build plugin successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -266,9 +267,9 @@ func buildPy(path string, output string) error {
|
||||
// generate debugtalk.py
|
||||
if output == "" {
|
||||
dir, _ := os.Getwd()
|
||||
output = filepath.Join(dir, "debugtalk_gen.py")
|
||||
output = filepath.Join(dir, genDebugTalkPy)
|
||||
} else if builtin.IsFolderPathExists(output) {
|
||||
output = filepath.Join(output, "debugtalk_gen.py")
|
||||
output = filepath.Join(output, genDebugTalkPy)
|
||||
}
|
||||
err = templateContent.genDebugTalk(output, pyTemplate)
|
||||
if err != nil {
|
||||
@@ -295,7 +296,7 @@ func Run(arg string, output string) (err error) {
|
||||
return errors.New("type error, expected .py or .go")
|
||||
}
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg(fmt.Sprintf("failed to build %s", arg))
|
||||
log.Error().Err(err).Str("arg", arg).Msg("build plugin failed")
|
||||
os.Exit(1)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -4,23 +4,24 @@ import (
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
err := Run("plugin/debugtalk.go", "./debugtalk_gen.bin")
|
||||
err := Run("../scaffold/templates/plugin/debugtalk.go", "./debugtalk.bin")
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
err = Run("plugin/debugtalk.py", "./debugtalk_gen.py")
|
||||
genDebugTalkPy := "../scaffold/templates/plugin/debugtalk_gen.py"
|
||||
err = Run("../scaffold/templates/plugin/debugtalk.py", genDebugTalkPy)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
contentBytes, err := builtin.ReadFile("./debugtalk_gen.py")
|
||||
contentBytes, err := builtin.ReadFile(genDebugTalkPy)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package noplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func SumTwoInt(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
func SumInts(args ...int) int {
|
||||
var sum int
|
||||
for _, arg := range args {
|
||||
sum += arg
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func Sum(args ...interface{}) (interface{}, error) {
|
||||
var sum float64
|
||||
for _, arg := range args {
|
||||
switch v := arg.(type) {
|
||||
case int:
|
||||
sum += float64(v)
|
||||
case float64:
|
||||
sum += v
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected type: %T", arg)
|
||||
}
|
||||
}
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func SetupHookExample(args string) string {
|
||||
return fmt.Sprintf("step name: %v, setup...", args)
|
||||
}
|
||||
|
||||
func TeardownHookExample(args string) string {
|
||||
return fmt.Sprintf("step name: %v, teardown...", args)
|
||||
}
|
||||
|
||||
func GetVersion() string {
|
||||
return "v0.4"
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import logging
|
||||
import time
|
||||
from typing import List
|
||||
|
||||
|
||||
def get_version():
|
||||
return "v0.4"
|
||||
|
||||
|
||||
def sleep(n_secs):
|
||||
time.sleep(n_secs)
|
||||
|
||||
|
||||
def sum(*args):
|
||||
result = 0
|
||||
for arg in args:
|
||||
result += arg
|
||||
return result
|
||||
|
||||
|
||||
def sum_ints(*args: List[int]) -> int:
|
||||
result = 0
|
||||
for arg in args:
|
||||
result += arg
|
||||
return result
|
||||
|
||||
|
||||
def sum_two_int(a: int, b: int) -> int:
|
||||
return a + b
|
||||
|
||||
|
||||
def sum_two_string(a: str, b: str) -> str:
|
||||
return a + b
|
||||
|
||||
|
||||
def sum_strings(*args: List[str]) -> str:
|
||||
result = ""
|
||||
for arg in args:
|
||||
result += arg
|
||||
return result
|
||||
|
||||
|
||||
def concatenate(*args: List[str]) -> str:
|
||||
result = ""
|
||||
for arg in args:
|
||||
result += str(arg)
|
||||
return result
|
||||
|
||||
|
||||
def setup_hook_example(name):
|
||||
logging.warning("setup_hook_example")
|
||||
return f"setup_hook_example: {name}"
|
||||
|
||||
|
||||
def teardown_hook_example(name):
|
||||
logging.warning("teardown_hook_example")
|
||||
return f"teardown_hook_example: {name}"
|
||||
@@ -1,15 +1,10 @@
|
||||
// NOTE: Generated By hrp {{ .Version }}, DO NOT EDIT!
|
||||
package main
|
||||
|
||||
import (
|
||||
{{- range $import := .Imports }}
|
||||
{{ $import -}}
|
||||
{{ end }}
|
||||
"github.com/httprunner/funplugin/fungo"
|
||||
)
|
||||
|
||||
{{ range $function := .Functions }}
|
||||
{{ $function }}
|
||||
{{ end }}
|
||||
|
||||
func main() {
|
||||
{{- range $functionName := .FunctionNames }}
|
||||
fungo.Register("{{ $functionName }}", {{ $functionName }})
|
||||
|
||||
Reference in New Issue
Block a user