mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-28 02:51:42 +08:00
refactor: relocate build plugin
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"project_name": "demo-empty-project",
|
||||
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-empty-project",
|
||||
"create_time": "2022-05-29T00:38:43.685282+08:00",
|
||||
"create_time": "2022-05-29T11:29:24.797507+08:00",
|
||||
"hrp_version": "v4.1.0"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"project_name": "demo-with-go-plugin",
|
||||
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-go-plugin",
|
||||
"create_time": "2022-05-29T00:38:42.384694+08:00",
|
||||
"create_time": "2022-05-29T11:29:23.237043+08:00",
|
||||
"hrp_version": "v4.1.0"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"project_name": "demo-with-py-plugin",
|
||||
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-py-plugin",
|
||||
"create_time": "2022-05-29T00:38:42.523325+08:00",
|
||||
"create_time": "2022-05-29T11:29:23.370656+08:00",
|
||||
"hrp_version": "v4.1.0"
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"project_name": "demo-without-plugin",
|
||||
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-without-plugin",
|
||||
"create_time": "2022-05-29T00:38:43.556711+08:00",
|
||||
"create_time": "2022-05-29T11:29:24.659348+08:00",
|
||||
"hrp_version": "v4.1.0"
|
||||
}
|
||||
@@ -25,10 +25,10 @@ func TestBoomerStandaloneRun(t *testing.T) {
|
||||
NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3")}),
|
||||
},
|
||||
}
|
||||
testcase2 := &demoTestCaseWithPluginJSONPath
|
||||
testcase2 := TestCasePath(demoTestCaseWithPluginJSONPath)
|
||||
|
||||
b := NewBoomer(2, 1)
|
||||
go b.Run(testcase1, testcase2)
|
||||
go b.Run(testcase1, &testcase2)
|
||||
time.Sleep(5 * time.Second)
|
||||
b.Quit()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package build
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -30,15 +30,10 @@ const (
|
||||
regexGoFunctionContent = `func [\s\S]*?\n}`
|
||||
)
|
||||
|
||||
const (
|
||||
genDebugTalkGo = "debugtalk_gen.go"
|
||||
genDebugTalkPy = ".debugtalk_gen.py"
|
||||
)
|
||||
|
||||
//go:embed templates/debugtalkPythonTemplate
|
||||
//go:embed internal/scaffold/templates/build/debugtalkPythonTemplate
|
||||
var pyTemplate string
|
||||
|
||||
//go:embed templates/debugtalkGoTemplate
|
||||
//go:embed internal/scaffold/templates/build/debugtalkGoTemplate
|
||||
var goTemplate string
|
||||
|
||||
type TemplateContent struct {
|
||||
@@ -209,7 +204,7 @@ func buildGo(path string, output string) error {
|
||||
}
|
||||
|
||||
// generate debugtalk.go in pluginDir
|
||||
err = templateContent.genDebugTalk(filepath.Join(pluginDir, genDebugTalkGo), goTemplate)
|
||||
err = templateContent.genDebugTalk(filepath.Join(pluginDir, PluginGoSourceGenFile), goTemplate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -232,9 +227,9 @@ func buildGo(path string, output string) error {
|
||||
|
||||
if output == "" {
|
||||
dir, _ := os.Getwd()
|
||||
output = filepath.Join(dir, "debugtalk.bin")
|
||||
output = filepath.Join(dir, PluginHashicorpGoBuiltFile)
|
||||
} else if builtin.IsFolderPathExists(output) {
|
||||
output = filepath.Join(output, "debugtalk.bin")
|
||||
output = filepath.Join(output, PluginHashicorpGoBuiltFile)
|
||||
}
|
||||
outputPath, err := filepath.Abs(output)
|
||||
if err != nil {
|
||||
@@ -242,7 +237,7 @@ func buildGo(path string, output string) error {
|
||||
}
|
||||
|
||||
// build plugin debugtalk.bin
|
||||
cmd := exec.Command("go", "build", "-o", outputPath, genDebugTalkGo, filepath.Base(path))
|
||||
cmd := exec.Command("go", "build", "-o", outputPath, PluginGoSourceGenFile, filepath.Base(path))
|
||||
if err := builtin.ExecCommandInDir(cmd, pluginDir); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -267,9 +262,9 @@ func buildPy(path string, output string) error {
|
||||
// generate debugtalk.py
|
||||
if output == "" {
|
||||
dir, _ := os.Getwd()
|
||||
output = filepath.Join(dir, genDebugTalkPy)
|
||||
output = filepath.Join(dir, PluginPySourceGenFile)
|
||||
} else if builtin.IsFolderPathExists(output) {
|
||||
output = filepath.Join(output, genDebugTalkPy)
|
||||
output = filepath.Join(output, PluginPySourceGenFile)
|
||||
}
|
||||
err = templateContent.genDebugTalk(output, pyTemplate)
|
||||
if err != nil {
|
||||
@@ -285,18 +280,18 @@ func buildPy(path string, output string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func Run(arg string, output string) (err error) {
|
||||
ext := filepath.Ext(arg)
|
||||
func BuildPlugin(path string, output string) (err error) {
|
||||
ext := filepath.Ext(path)
|
||||
switch ext {
|
||||
case ".py":
|
||||
err = buildPy(arg, output)
|
||||
err = buildPy(path, output)
|
||||
case ".go":
|
||||
err = buildGo(arg, output)
|
||||
err = buildGo(path, output)
|
||||
default:
|
||||
return errors.New("type error, expected .py or .go")
|
||||
}
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("arg", arg).Msg("build plugin failed")
|
||||
log.Error().Err(err).Str("arg", path).Msg("build plugin failed")
|
||||
os.Exit(1)
|
||||
}
|
||||
return nil
|
||||
@@ -1,6 +1,7 @@
|
||||
package build
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
@@ -10,13 +11,13 @@ import (
|
||||
)
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
err := Run("../scaffold/templates/plugin/debugtalk.go", "./debugtalk.bin")
|
||||
err := BuildPlugin(tmpl("plugin/debugtalk.go"), "./debugtalk.bin")
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
genDebugTalkPyPath := "../scaffold/templates/plugin/" + genDebugTalkPy
|
||||
err = Run("../scaffold/templates/plugin/debugtalk.py", genDebugTalkPyPath)
|
||||
genDebugTalkPyPath := filepath.Join(tmpl("plugin/"), PluginPySourceGenFile)
|
||||
err = BuildPlugin(tmpl("plugin/debugtalk.py"), genDebugTalkPyPath)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package cmd
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/build"
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
)
|
||||
|
||||
var buildCmd = &cobra.Command{
|
||||
@@ -17,7 +17,7 @@ var buildCmd = &cobra.Command{
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return build.Run(args[0], output)
|
||||
return hrp.BuildPlugin(args[0], output)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/version"
|
||||
@@ -193,7 +194,7 @@ func createGoPlugin(projectName string) error {
|
||||
return err
|
||||
}
|
||||
err := CopyFile("templates/plugin/debugtalk.go",
|
||||
filepath.Join(projectName, "plugin", "debugtalk.go"))
|
||||
filepath.Join(projectName, "plugin", hrp.PluginGoSourceFile))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "copy debugtalk.go failed")
|
||||
}
|
||||
@@ -205,7 +206,7 @@ func createPythonPlugin(projectName string) error {
|
||||
log.Info().Msg("start to create hashicorp python plugin")
|
||||
|
||||
// create debugtalk.py
|
||||
pluginFile := filepath.Join(projectName, "debugtalk.py")
|
||||
pluginFile := filepath.Join(projectName, hrp.PluginPySourceFile)
|
||||
err := CopyFile("templates/plugin/debugtalk.py", pluginFile)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "copy file failed")
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
# NOTE: Generated By hrp v4.1.0, DO NOT EDIT!
|
||||
|
||||
import logging
|
||||
import time
|
||||
import funppy
|
||||
|
||||
from typing import List
|
||||
|
||||
|
||||
def get_user_agent():
|
||||
return "hrp/funppy"
|
||||
|
||||
|
||||
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}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
funppy.register("get_user_agent", get_user_agent)
|
||||
funppy.register("sleep", sleep)
|
||||
funppy.register("sum", sum)
|
||||
funppy.register("sum_ints", sum_ints)
|
||||
funppy.register("sum_two_int", sum_two_int)
|
||||
funppy.register("sum_two_string", sum_two_string)
|
||||
funppy.register("sum_strings", sum_strings)
|
||||
funppy.register("concatenate", concatenate)
|
||||
funppy.register("setup_hook_example", setup_hook_example)
|
||||
funppy.register("teardown_hook_example", teardown_hook_example)
|
||||
funppy.serve()
|
||||
@@ -1,57 +0,0 @@
|
||||
import logging
|
||||
import time
|
||||
from typing import List
|
||||
|
||||
|
||||
def get_user_agent():
|
||||
return "hrp/funppy"
|
||||
|
||||
|
||||
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}"
|
||||
@@ -10,18 +10,20 @@ import (
|
||||
"github.com/httprunner/funplugin"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/build"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
|
||||
)
|
||||
|
||||
const (
|
||||
goPluginFile = "debugtalk.so" // built from go plugin
|
||||
hashicorpGoPluginFile = "debugtalk.bin" // built from hashicorp go plugin
|
||||
hashicorpPyPluginFile = ".debugtalk_gen.py" // used for hashicorp python plugin, automatically generated by HRP
|
||||
debugtalkPyFile = "debugtalk.py" // write by user
|
||||
projectInfoFile = "proj.json" // used for ensuring root project
|
||||
PluginGoBuiltFile = "debugtalk.so" // built from go official plugin
|
||||
PluginHashicorpGoBuiltFile = "debugtalk.bin" // built from hashicorp go plugin
|
||||
PluginGoSourceFile = "debugtalk.go" // golang function plugin source file
|
||||
PluginGoSourceGenFile = "debugtalk_gen.go" // generated for hashicorp go plugin
|
||||
PluginPySourceFile = "debugtalk.py" // python function plugin source file
|
||||
PluginPySourceGenFile = ".debugtalk_gen.py" // generated for hashicorp python plugin
|
||||
)
|
||||
|
||||
const projectInfoFile = "proj.json" // used for ensuring root project
|
||||
|
||||
func initPlugin(path string, logOn bool) (plugin funplugin.IPlugin, err error) {
|
||||
// plugin file not found
|
||||
if path == "" {
|
||||
@@ -64,16 +66,16 @@ func initPlugin(path string, logOn bool) (plugin funplugin.IPlugin, err error) {
|
||||
func locatePlugin(path string) (pluginPath string, err error) {
|
||||
// priority: hashicorp plugin (debugtalk.bin > debugtalk.py) > go plugin (debugtalk.so)
|
||||
|
||||
pluginPath, err = locateFile(path, hashicorpGoPluginFile)
|
||||
pluginPath, err = locateFile(path, PluginHashicorpGoBuiltFile)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
pluginPath, err = locateFile(path, debugtalkPyFile)
|
||||
pluginPath, err = locateFile(path, PluginPySourceFile)
|
||||
if err == nil {
|
||||
// register funppy plugin
|
||||
genPyPluginPath := filepath.Join(filepath.Dir(pluginPath), hashicorpPyPluginFile)
|
||||
err = build.Run(pluginPath, genPyPluginPath)
|
||||
genPyPluginPath := filepath.Join(filepath.Dir(pluginPath), PluginPySourceGenFile)
|
||||
err = BuildPlugin(pluginPath, genPyPluginPath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", pluginPath).Msg("build plugin failed")
|
||||
return
|
||||
@@ -81,7 +83,7 @@ func locatePlugin(path string) (pluginPath string, err error) {
|
||||
return genPyPluginPath, nil
|
||||
}
|
||||
|
||||
pluginPath, err = locateFile(path, goPluginFile)
|
||||
pluginPath, err = locateFile(path, PluginGoBuiltFile)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,36 +8,36 @@ import (
|
||||
|
||||
func TestLocateFile(t *testing.T) {
|
||||
// specify target file path
|
||||
_, err := locateFile(templatesDir+"plugin/debugtalk.go", "debugtalk.go")
|
||||
_, err := locateFile(tmpl("plugin/debugtalk.go"), PluginGoSourceFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
// specify path with the same dir
|
||||
_, err = locateFile(templatesDir+"plugin/debugtalk.py", "debugtalk.go")
|
||||
_, err = locateFile(tmpl("plugin/debugtalk.py"), PluginGoSourceFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
// specify target file path dir
|
||||
_, err = locateFile(templatesDir+"plugin/", "debugtalk.go")
|
||||
_, err = locateFile(tmpl("plugin/"), PluginGoSourceFile)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
// specify wrong path
|
||||
_, err = locateFile(".", "debugtalk.go")
|
||||
_, err = locateFile(".", PluginGoSourceFile)
|
||||
if !assert.Error(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
_, err = locateFile("/abc", "debugtalk.go")
|
||||
_, err = locateFile("/abc", PluginGoSourceFile)
|
||||
if !assert.Error(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocatePythonPlugin(t *testing.T) {
|
||||
_, err := locatePlugin(templatesDir + "plugin/debugtalk.py")
|
||||
_, err := locatePlugin(tmpl("plugin/debugtalk.py"))
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
@@ -47,7 +47,7 @@ func TestLocateGoPlugin(t *testing.T) {
|
||||
buildHashicorpGoPlugin()
|
||||
defer removeHashicorpGoPlugin()
|
||||
|
||||
_, err := locatePlugin(templatesDir + "debugtalk.bin")
|
||||
_, err := locatePlugin(tmpl("debugtalk.bin"))
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
@@ -166,6 +166,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
||||
return err
|
||||
}
|
||||
|
||||
var runErr error
|
||||
// run testcase one by one
|
||||
for _, testcase := range testCases {
|
||||
sessionRunner, err := r.NewSessionRunner(testcase)
|
||||
@@ -185,6 +186,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
||||
s.appendCaseSummary(caseSummary)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("[Run] run testcase failed")
|
||||
runErr = err
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -206,7 +208,8 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
return runErr
|
||||
}
|
||||
|
||||
// NewSessionRunner creates a new session runner for testcase.
|
||||
@@ -236,8 +239,10 @@ func (r *HRPRunner) newCaseRunner(testcase *TestCase) (*testCaseRunner, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "init plugin failed")
|
||||
}
|
||||
runner.parser.plugin = plugin
|
||||
runner.rootDir = filepath.Dir(plugin.Path())
|
||||
if plugin != nil {
|
||||
runner.parser.plugin = plugin
|
||||
runner.rootDir = filepath.Dir(plugin.Path())
|
||||
}
|
||||
|
||||
// parse testcase config
|
||||
if err := runner.parseConfig(); err != nil {
|
||||
|
||||
@@ -1,20 +1,18 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/build"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/scaffold"
|
||||
)
|
||||
|
||||
func buildHashicorpGoPlugin() {
|
||||
log.Info().Msg("[init] build hashicorp go plugin")
|
||||
err := build.Run(templatesDir+"plugin/debugtalk.go", templatesDir+"debugtalk.bin")
|
||||
err := BuildPlugin(tmpl("plugin/debugtalk.go"), tmpl("debugtalk.bin"))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("build hashicorp go plugin failed")
|
||||
os.Exit(1)
|
||||
@@ -23,15 +21,15 @@ func buildHashicorpGoPlugin() {
|
||||
|
||||
func removeHashicorpGoPlugin() {
|
||||
log.Info().Msg("[teardown] remove hashicorp go plugin")
|
||||
os.Remove(templatesDir + "debugtalk.bin")
|
||||
os.Remove(tmpl("debugtalk.bin"))
|
||||
}
|
||||
|
||||
func buildHashicorpPyPlugin() {
|
||||
log.Info().Msg("[init] prepare hashicorp python plugin")
|
||||
pluginFile := templatesDir + "debugtalk.py"
|
||||
err := scaffold.CopyFile("templates/plugin/debugtalk.py", pluginFile)
|
||||
src, _ := ioutil.ReadFile(tmpl("plugin/debugtalk.py"))
|
||||
err := ioutil.WriteFile(tmpl("debugtalk.py"), src, 0o644)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("build hashicorp python plugin failed")
|
||||
log.Error().Err(err).Msg("copy hashicorp python plugin failed")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -39,7 +37,8 @@ func buildHashicorpPyPlugin() {
|
||||
func removeHashicorpPyPlugin() {
|
||||
log.Info().Msg("[teardown] remove hashicorp python plugin")
|
||||
// on v4.1^, running case will generate .debugtalk_gen.py used by python plugin
|
||||
os.Remove(templatesDir + hashicorpPyPluginFile)
|
||||
os.Remove(tmpl(PluginPySourceFile))
|
||||
os.Remove(tmpl(PluginPySourceGenFile))
|
||||
}
|
||||
|
||||
func TestRunCaseWithGoPlugin(t *testing.T) {
|
||||
@@ -57,6 +56,7 @@ func TestRunCaseWithPythonPlugin(t *testing.T) {
|
||||
}
|
||||
|
||||
func assertRunTestCases(t *testing.T) {
|
||||
refCase := TestCasePath(demoTestCaseWithPluginJSONPath)
|
||||
testcase1 := &TestCase{
|
||||
Config: NewConfig("TestCase1").
|
||||
SetBaseURL("http://httpbin.org"),
|
||||
@@ -83,7 +83,7 @@ func assertRunTestCases(t *testing.T) {
|
||||
},
|
||||
},
|
||||
),
|
||||
NewStep("testcase1-step4").CallRefCase(&demoTestCaseWithPluginJSONPath),
|
||||
NewStep("testcase1-step4").CallRefCase(&refCase),
|
||||
},
|
||||
}
|
||||
testcase2 := &TestCase{
|
||||
@@ -160,7 +160,8 @@ func TestRunCaseWithPluginJSON(t *testing.T) {
|
||||
buildHashicorpGoPlugin()
|
||||
defer removeHashicorpGoPlugin()
|
||||
|
||||
err := NewRunner(nil).Run(&demoTestCaseWithPluginJSONPath) // hrp.Run(testCase)
|
||||
testCase := TestCasePath(demoTestCaseWithPluginJSONPath)
|
||||
err := NewRunner(nil).Run(&testCase) // hrp.Run(testCase)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
@@ -170,7 +171,8 @@ func TestRunCaseWithPluginYAML(t *testing.T) {
|
||||
buildHashicorpGoPlugin()
|
||||
defer removeHashicorpGoPlugin()
|
||||
|
||||
err := NewRunner(nil).Run(&demoTestCaseWithPluginYAMLPath) // hrp.Run(testCase)
|
||||
testCase := TestCasePath(demoTestCaseWithPluginYAMLPath)
|
||||
err := NewRunner(nil).Run(&testCase) // hrp.Run(testCase)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
@@ -180,16 +182,18 @@ func TestRunCaseWithRefAPI(t *testing.T) {
|
||||
buildHashicorpGoPlugin()
|
||||
defer removeHashicorpGoPlugin()
|
||||
|
||||
err := NewRunner(nil).Run(&demoTestCaseWithRefAPIPath)
|
||||
testCase := TestCasePath(demoTestCaseWithRefAPIPath)
|
||||
err := NewRunner(nil).Run(&testCase)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
refAPI := APIPath(demoAPIGETPath)
|
||||
testcase := &TestCase{
|
||||
Config: NewConfig("TestCase").
|
||||
SetBaseURL("https://postman-echo.com"),
|
||||
TestSteps: []IStep{
|
||||
NewStep("run referenced api").CallRefAPI(&demoAPIGETPath),
|
||||
NewStep("run referenced api").CallRefAPI(&refAPI),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -222,7 +226,7 @@ func TestLoadTestCases(t *testing.T) {
|
||||
}
|
||||
|
||||
// load test cases from single file path
|
||||
tc = demoTestCaseWithPluginJSONPath
|
||||
tc = TestCasePath(demoTestCaseWithPluginJSONPath)
|
||||
testCases, err = LoadTestCases(&tc)
|
||||
if !assert.Nil(t, err) {
|
||||
t.Fatal()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package hrp
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -9,17 +10,21 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
templatesDir = "internal/scaffold/templates/"
|
||||
hrpExamplesDir = "../examples/hrp"
|
||||
)
|
||||
|
||||
// tmpl returns template file path
|
||||
func tmpl(relativePath string) string {
|
||||
return filepath.Join("internal/scaffold/templates/", relativePath)
|
||||
}
|
||||
|
||||
var (
|
||||
demoTestCaseWithPluginJSONPath TestCasePath = templatesDir + "testcases/demo_with_funplugin.json"
|
||||
demoTestCaseWithPluginYAMLPath TestCasePath = templatesDir + "testcases/demo_with_funplugin.yaml"
|
||||
demoTestCaseWithoutPluginJSONPath TestCasePath = templatesDir + "testcases/demo_without_funplugin.json"
|
||||
demoTestCaseWithoutPluginYAMLPath TestCasePath = templatesDir + "testcases/demo_without_funplugin.yaml"
|
||||
demoTestCaseWithRefAPIPath TestCasePath = templatesDir + "testcases/demo_ref_api.json"
|
||||
demoAPIGETPath APIPath = templatesDir + "/api/get.yml"
|
||||
demoTestCaseWithPluginJSONPath = tmpl("testcases/demo_with_funplugin.json")
|
||||
demoTestCaseWithPluginYAMLPath = tmpl("testcases/demo_with_funplugin.yaml")
|
||||
demoTestCaseWithoutPluginJSONPath = tmpl("testcases/demo_without_funplugin.json")
|
||||
demoTestCaseWithoutPluginYAMLPath = tmpl("testcases/demo_without_funplugin.yaml")
|
||||
demoTestCaseWithRefAPIPath = tmpl("testcases/demo_ref_api.json")
|
||||
demoAPIGETPath = tmpl("/api/get.yml")
|
||||
)
|
||||
|
||||
var demoTestCaseWithThinkTimePath TestCasePath = hrpExamplesDir + "/think_time_test.json"
|
||||
@@ -152,21 +157,21 @@ var demoTestCaseWithoutPlugin = &TestCase{
|
||||
|
||||
func TestGenDemoTestCase(t *testing.T) {
|
||||
tCase := demoTestCaseWithPlugin.ToTCase()
|
||||
err := builtin.Dump2JSON(tCase, demoTestCaseWithPluginJSONPath.GetPath())
|
||||
err := builtin.Dump2JSON(tCase, demoTestCaseWithPluginJSONPath)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
err = builtin.Dump2YAML(tCase, demoTestCaseWithPluginYAMLPath.GetPath())
|
||||
err = builtin.Dump2YAML(tCase, demoTestCaseWithPluginYAMLPath)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
tCase = demoTestCaseWithoutPlugin.ToTCase()
|
||||
err = builtin.Dump2JSON(tCase, demoTestCaseWithoutPluginJSONPath.GetPath())
|
||||
err = builtin.Dump2JSON(tCase, demoTestCaseWithoutPluginJSONPath)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
err = builtin.Dump2YAML(tCase, demoTestCaseWithoutPluginYAMLPath.GetPath())
|
||||
err = builtin.Dump2YAML(tCase, demoTestCaseWithoutPluginYAMLPath)
|
||||
if err != nil {
|
||||
t.Fatal()
|
||||
}
|
||||
@@ -175,11 +180,11 @@ func TestGenDemoTestCase(t *testing.T) {
|
||||
func TestLoadCase(t *testing.T) {
|
||||
tcJSON := &TCase{}
|
||||
tcYAML := &TCase{}
|
||||
err := builtin.LoadFile(demoTestCaseWithPluginJSONPath.GetPath(), tcJSON)
|
||||
err := builtin.LoadFile(demoTestCaseWithPluginJSONPath, tcJSON)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
err = builtin.LoadFile(demoTestCaseWithPluginYAMLPath.GetPath(), tcYAML)
|
||||
err = builtin.LoadFile(demoTestCaseWithPluginYAMLPath, tcYAML)
|
||||
if !assert.NoError(t, err) {
|
||||
t.Fatal()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user