feat: add scaffold for plugin

This commit is contained in:
debugtalk
2022-01-17 20:24:42 +08:00
parent b84f33eee1
commit d5dff2dcdf
5 changed files with 72 additions and 7 deletions

View File

@@ -3,9 +3,9 @@
"name": "demo with complex mechanisms",
"base_url": "https://postman-echo.com",
"variables": {
"a": 12.3,
"a": "${sum(10, 2.3)}",
"b": 3.45,
"n": 5,
"n": "${sum_ints(1, 2, 2)}",
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}"
}

View File

@@ -2,9 +2,9 @@ config:
name: demo with complex mechanisms
base_url: https://postman-echo.com
variables:
a: 12.3
a: ${sum(10, 2.3)}
b: 3.45
"n": 5
"n": ${sum_ints(1, 2, 2)}
varFoo1: ${gen_random_string($n)}
varFoo2: ${max($a, $b)}
teststeps:

View File

@@ -6,8 +6,8 @@ var demoTestCase = &hrp.TestCase{
Config: hrp.NewConfig("demo with complex mechanisms").
SetBaseURL("https://postman-echo.com").
WithVariables(map[string]interface{}{ // global level variables
"n": 5,
"a": 12.3,
"n": "${sum_ints(1, 2, 2)}",
"a": "${sum(10, 2.3)}",
"b": 3.45,
"varFoo1": "${gen_random_string($n)}",
"varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function
@@ -56,6 +56,50 @@ var demoTestCase = &hrp.TestCase{
},
}
// debugtalk.go
var demoPlugin = `package main
import (
"fmt"
"github.com/httprunner/hrp/plugin"
)
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 main() {
plugin.Register("sum_ints", SumInts)
plugin.Register("sum_two_int", SumTwoInt)
plugin.Register("sum", Sum)
plugin.Serve()
}
`
// .gitignore
var demoIgnoreContent = `.env
reports/*
@@ -64,9 +108,13 @@ reports/*
.idea/
.DS_Store
output/
# plugin
debugtalk.bin
debugtalk.so
`
// .env
var demoEnvContent = `USERNAME=debugtalk
"PASSWORD=123456
PASSWORD=123456
`

View File

@@ -25,6 +25,7 @@ func TestGenDemoTestCase(t *testing.T) {
}
func Example_demo() {
demoTestCase.Config.ToStruct().Path = "../../examples/debugtalk.bin"
err := hrp.NewRunner(nil).Run(demoTestCase) // hrp.Run(demoTestCase)
fmt.Println(err)
// Output:

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"github.com/httprunner/hrp/internal/ga"
@@ -36,6 +37,9 @@ func CreateScaffold(projectName string) error {
if err := createFolder(path.Join(projectName, "testcases")); err != nil {
return err
}
if err := createFolder(path.Join(projectName, "plugin")); err != nil {
return err
}
if err := createFolder(path.Join(projectName, "reports")); err != nil {
return err
}
@@ -53,6 +57,18 @@ func CreateScaffold(projectName string) error {
return err
}
// create debugtalk.go
pluginFile := path.Join(projectName, "plugin", "debugtalk.go")
if err := createFile(pluginFile, demoPlugin); err != nil {
return err
}
cmd := exec.Command("go", "build",
"-o", path.Join(projectName, "debugtalk.bin"),
pluginFile)
if err := cmd.Run(); err != nil {
log.Error().Err(err).Msg("build plugin debugtalk.bin failed")
}
// create .gitignore
if err := createFile(path.Join(projectName, ".gitignore"), demoIgnoreContent); err != nil {
return err