refactor: relocate build plugin

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