diff --git a/examples/demo-empty-project/proj.json b/examples/demo-empty-project/proj.json index 2eac429f..8a7e8095 100644 --- a/examples/demo-empty-project/proj.json +++ b/examples/demo-empty-project/proj.json @@ -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" } \ No newline at end of file diff --git a/examples/demo-with-go-plugin/proj.json b/examples/demo-with-go-plugin/proj.json index b5665891..a707307e 100644 --- a/examples/demo-with-go-plugin/proj.json +++ b/examples/demo-with-go-plugin/proj.json @@ -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" } \ No newline at end of file diff --git a/examples/demo-with-py-plugin/proj.json b/examples/demo-with-py-plugin/proj.json index 8dba7a24..3a8afa50 100644 --- a/examples/demo-with-py-plugin/proj.json +++ b/examples/demo-with-py-plugin/proj.json @@ -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" } \ No newline at end of file diff --git a/examples/demo-without-plugin/proj.json b/examples/demo-without-plugin/proj.json index 6aafc4c4..8e3725e4 100644 --- a/examples/demo-without-plugin/proj.json +++ b/examples/demo-without-plugin/proj.json @@ -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" } \ No newline at end of file diff --git a/hrp/boomer_test.go b/hrp/boomer_test.go index 4edefa38..547a4618 100644 --- a/hrp/boomer_test.go +++ b/hrp/boomer_test.go @@ -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() } diff --git a/hrp/internal/build/main.go b/hrp/build.go similarity index 90% rename from hrp/internal/build/main.go rename to hrp/build.go index 45d1912e..65eded8d 100644 --- a/hrp/internal/build/main.go +++ b/hrp/build.go @@ -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 diff --git a/hrp/internal/build/main_test.go b/hrp/build_test.go similarity index 73% rename from hrp/internal/build/main_test.go rename to hrp/build_test.go index b05fd53e..c527b4ee 100644 --- a/hrp/internal/build/main_test.go +++ b/hrp/build_test.go @@ -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() } diff --git a/hrp/cmd/build.go b/hrp/cmd/build.go index da174bbc..3c8848e3 100644 --- a/hrp/cmd/build.go +++ b/hrp/cmd/build.go @@ -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) }, } diff --git a/hrp/internal/scaffold/main.go b/hrp/internal/scaffold/main.go index 501cc2ef..941ab14c 100644 --- a/hrp/internal/scaffold/main.go +++ b/hrp/internal/scaffold/main.go @@ -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") diff --git a/hrp/internal/scaffold/templates/.debugtalk_gen.py b/hrp/internal/scaffold/templates/.debugtalk_gen.py deleted file mode 100644 index 1bc989f2..00000000 --- a/hrp/internal/scaffold/templates/.debugtalk_gen.py +++ /dev/null @@ -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() diff --git a/hrp/internal/build/templates/debugtalkGoTemplate b/hrp/internal/scaffold/templates/build/debugtalkGoTemplate similarity index 100% rename from hrp/internal/build/templates/debugtalkGoTemplate rename to hrp/internal/scaffold/templates/build/debugtalkGoTemplate diff --git a/hrp/internal/build/templates/debugtalkPythonTemplate b/hrp/internal/scaffold/templates/build/debugtalkPythonTemplate similarity index 100% rename from hrp/internal/build/templates/debugtalkPythonTemplate rename to hrp/internal/scaffold/templates/build/debugtalkPythonTemplate diff --git a/hrp/internal/scaffold/templates/debugtalk.py b/hrp/internal/scaffold/templates/debugtalk.py deleted file mode 100644 index ea48ff48..00000000 --- a/hrp/internal/scaffold/templates/debugtalk.py +++ /dev/null @@ -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}" diff --git a/hrp/plugin.go b/hrp/plugin.go index 7d00540c..facb09ef 100644 --- a/hrp/plugin.go +++ b/hrp/plugin.go @@ -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 } diff --git a/hrp/plugin_test.go b/hrp/plugin_test.go index 26e74213..27cbaa77 100644 --- a/hrp/plugin_test.go +++ b/hrp/plugin_test.go @@ -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() } diff --git a/hrp/runner.go b/hrp/runner.go index 66bc6a9b..8290b1bf 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -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 { diff --git a/hrp/runner_test.go b/hrp/runner_test.go index 7437c8bc..9c169bc1 100644 --- a/hrp/runner_test.go +++ b/hrp/runner_test.go @@ -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() diff --git a/hrp/testcase_test.go b/hrp/testcase_test.go index eb6a69ae..c55642e1 100644 --- a/hrp/testcase_test.go +++ b/hrp/testcase_test.go @@ -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() }