From 813284110eb0ebd700b18257cb591a653ab2e57e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 23 Apr 2022 23:56:47 +0800 Subject: [PATCH] refactor: relocate convert --- hrp/boomer.go | 2 +- hrp/cmd/convert.go | 6 +- hrp/convert.go | 59 --------------- hrp/internal/convert/main.go | 118 +++++++++++++++++++++++++++++ hrp/internal/convert/testcase.tmpl | 38 ++++++++++ hrp/runner.go | 2 +- hrp/runner_test.go | 8 +- hrp/testcase.go | 2 +- 8 files changed, 166 insertions(+), 69 deletions(-) delete mode 100644 hrp/convert.go create mode 100644 hrp/internal/convert/main.go create mode 100644 hrp/internal/convert/testcase.tmpl diff --git a/hrp/boomer.go b/hrp/boomer.go index d639f981..271f00d7 100644 --- a/hrp/boomer.go +++ b/hrp/boomer.go @@ -46,7 +46,7 @@ func (b *HRPBoomer) Run(testcases ...ITestCase) { var taskSlice []*boomer.Task // load all testcases - testCases, err := loadTestCases(testcases...) + testCases, err := LoadTestCases(testcases...) if err != nil { log.Error().Err(err).Msg("failed to load testcases") os.Exit(1) diff --git a/hrp/cmd/convert.go b/hrp/cmd/convert.go index 4c16b8ef..7ec89187 100644 --- a/hrp/cmd/convert.go +++ b/hrp/cmd/convert.go @@ -7,7 +7,7 @@ import ( "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/httprunner/httprunner/hrp" + "github.com/httprunner/httprunner/hrp/internal/convert" ) var convertCmd = &cobra.Command{ @@ -24,9 +24,9 @@ var convertCmd = &cobra.Command{ var err error if gotestFlag { - err = hrp.Convert2TestScripts("gotest", args...) + err = convert.Convert2TestScripts("gotest", args...) } else { - err = hrp.Convert2TestScripts("pytest", args...) + err = convert.Convert2TestScripts("pytest", args...) } if err != nil { log.Error().Err(err).Msg("convert test scripts failed") diff --git a/hrp/convert.go b/hrp/convert.go deleted file mode 100644 index 92f424f8..00000000 --- a/hrp/convert.go +++ /dev/null @@ -1,59 +0,0 @@ -package hrp - -import ( - "github.com/rs/zerolog/log" - - "github.com/httprunner/httprunner/hrp/internal/builtin" - "github.com/httprunner/httprunner/hrp/internal/sdk" -) - -func Convert2TestScripts(destType string, paths ...string) error { - if destType == "gotest" { - return convert2GoTestScripts(paths...) - } else { - return convert2PyTestScripts(paths...) - } -} - -func convert2PyTestScripts(paths ...string) error { - sdk.SendEvent(sdk.EventTracking{ - Category: "ConvertTests", - Action: "hrp convert --pytest", - }) - - python3, err := builtin.EnsurePython3Venv("httprunner") - if err != nil { - return err - } - - args := append([]string{"-m", "httprunner", "make"}, paths...) - return builtin.ExecCommand(python3, args...) -} - -func convert2GoTestScripts(paths ...string) error { - log.Warn().Msg("convert to gotest scripts is not supported yet") - - sdk.SendEvent(sdk.EventTracking{ - Category: "ConvertTests", - Action: "hrp convert --gotest", - }) - - // report event - // sdk.SendEvent(sdk.EventTracking{ - // Category: "Convert", - // Action: fmt.Sprintf("hrp convert to %s", destType), - // }) - - // var testCasePaths []ITestCase - // for _, path := range paths { - // testCasePath := TestCasePath(path) - // testCasePaths = append(testCasePaths, &testCasePath) - // } - - // _, err := loadTestCases(testCasePaths...) - // if err != nil { - // log.Error().Err(err).Msg("failed to load testcases") - // return err - // } - return nil -} diff --git a/hrp/internal/convert/main.go b/hrp/internal/convert/main.go new file mode 100644 index 00000000..a52a0fab --- /dev/null +++ b/hrp/internal/convert/main.go @@ -0,0 +1,118 @@ +package convert + +import ( + _ "embed" + "fmt" + "os" + + "github.com/rs/zerolog/log" + + "github.com/httprunner/httprunner/hrp" + "github.com/httprunner/httprunner/hrp/internal/builtin" + "github.com/httprunner/httprunner/hrp/internal/sdk" +) + +func Convert2TestScripts(destType string, paths ...string) error { + // report event + sdk.SendEvent(sdk.EventTracking{ + Category: "ConvertTests", + Action: fmt.Sprintf("hrp convert --%s", destType), + }) + + if destType == "gotest" { + return convert2GoTestScripts(paths...) + } else { + // default to pytest + return convert2PyTestScripts(paths...) + } +} + +func convert2PyTestScripts(paths ...string) error { + python3, err := builtin.EnsurePython3Venv("httprunner") + if err != nil { + return err + } + + args := append([]string{"-m", "httprunner", "make"}, paths...) + return builtin.ExecCommand(python3, args...) +} + +func convert2GoTestScripts(paths ...string) error { + log.Warn().Msg("convert to gotest scripts is not supported yet") + os.Exit(1) + + // TODO + var testCasePaths []hrp.ITestCase + for _, path := range paths { + testCasePath := hrp.TestCasePath(path) + testCasePaths = append(testCasePaths, &testCasePath) + } + + testCases, err := hrp.LoadTestCases(testCasePaths...) + if err != nil { + log.Error().Err(err).Msg("failed to load testcases") + return err + } + + var pytestPaths []string + for _, testCase := range testCases { + tc := testCase.ToTCase() + converter := CaseConverter{ + TCase: tc, + } + pytestPath, err := converter.ToPyTest() + if err != nil { + log.Error().Err(err). + Str("originPath", tc.Config.Path). + Msg("convert to pytest failed") + continue + } + log.Info(). + Str("pytestPath", pytestPath). + Str("originPath", tc.Config.Path). + Msg("convert to pytest success") + pytestPaths = append(pytestPaths, pytestPath) + } + + // format pytest scripts with black + python3, err := builtin.EnsurePython3Venv("black") + if err != nil { + return err + } + args := append([]string{"-m", "black"}, pytestPaths...) + return builtin.ExecCommand(python3, args...) +} + +//go:embed testcase.tmpl +var testcaseTemplate string + +type CaseConverter struct { + *hrp.TCase +} + +func (c *CaseConverter) ToPyTest() (string, error) { + script := convertConfig(c.TCase.Config) + println(script) + return script, nil +} + +func (c *CaseConverter) ToGoTest() (string, error) { + return "", nil +} + +func convertConfig(config *hrp.TConfig) string { + script := fmt.Sprintf("Config('%s')", config.Name) + + if config.Variables != nil { + script += fmt.Sprintf(".variables(**{%v})", config.Variables) + } + if config.BaseURL != "" { + script += fmt.Sprintf(".base_url('%s')", config.BaseURL) + } + if config.Export != nil { + script += fmt.Sprintf(".export(*%v)", config.Export) + } + script += fmt.Sprintf(".verify(%v)", config.Verify) + + return script +} diff --git a/hrp/internal/convert/testcase.tmpl b/hrp/internal/convert/testcase.tmpl new file mode 100644 index 00000000..783a1bc3 --- /dev/null +++ b/hrp/internal/convert/testcase.tmpl @@ -0,0 +1,38 @@ +# NOTE: Generated By HttpRunner v{{ version }} +# FROM: {{ testcase_path }} + +{% if imports_list and diff_levels > 0 %} +import sys +from pathlib import Path +sys.path.insert(0, str(Path(__file__){% for _ in range(diff_levels) %}.parent{% endfor %})) +{% endif %} + +{% if parameters %} +import pytest +from httprunner import Parameters +{% endif %} + +from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase +{% for import_str in imports_list %} +{{ import_str }} +{% endfor %} + +class {{ class_name }}(HttpRunner): + + {% if parameters %} + @pytest.mark.parametrize("param", Parameters({{parameters}})) + def test_start(self, param): + super().test_start(param) + {% endif %} + + config = {{ config_chain_style }} + + teststeps = [ + {% for step_chain_style in teststeps_chain_style %} + {{ step_chain_style }}, + {% endfor %} + ] + + +if __name__ == "__main__": + {{ class_name }}().test_start() diff --git a/hrp/runner.go b/hrp/runner.go index 057bdabd..d21b8beb 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -151,7 +151,7 @@ func (r *HRPRunner) Run(testcases ...ITestCase) error { s := newOutSummary() // load all testcases - testCases, err := loadTestCases(testcases...) + testCases, err := LoadTestCases(testcases...) if err != nil { log.Error().Err(err).Msg("failed to load testcases") return err diff --git a/hrp/runner_test.go b/hrp/runner_test.go index 54c82f05..25eaca68 100644 --- a/hrp/runner_test.go +++ b/hrp/runner_test.go @@ -203,7 +203,7 @@ func TestRunCaseWithRefAPI(t *testing.T) { func TestLoadTestCases(t *testing.T) { // load test cases from folder path tc := TestCasePath("../examples/demo-with-py-plugin/testcases/") - testCases, err := loadTestCases(&tc) + testCases, err := LoadTestCases(&tc) if !assert.Nil(t, err) { t.Fatal() } @@ -213,7 +213,7 @@ func TestLoadTestCases(t *testing.T) { // load test cases from folder path, including sub folders tc = TestCasePath("../examples/demo-with-py-plugin/") - testCases, err = loadTestCases(&tc) + testCases, err = LoadTestCases(&tc) if !assert.Nil(t, err) { t.Fatal() } @@ -223,7 +223,7 @@ func TestLoadTestCases(t *testing.T) { // load test cases from single file path tc = demoTestCaseWithPluginJSONPath - testCases, err = loadTestCases(&tc) + testCases, err = LoadTestCases(&tc) if !assert.Nil(t, err) { t.Fatal() } @@ -235,7 +235,7 @@ func TestLoadTestCases(t *testing.T) { testcase := &TestCase{ Config: NewConfig("TestCase").SetWeight(3), } - testCases, err = loadTestCases(testcase) + testCases, err = LoadTestCases(testcase) if !assert.Nil(t, err) { t.Fatal() } diff --git a/hrp/testcase.go b/hrp/testcase.go index 815480d7..ce6a8106 100644 --- a/hrp/testcase.go +++ b/hrp/testcase.go @@ -240,7 +240,7 @@ func convertCheckExpr(checkExpr string) string { return strings.Join(checkItems, ".") } -func loadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { +func LoadTestCases(iTestCases ...ITestCase) ([]*TestCase, error) { testCases := make([]*TestCase, 0) for _, iTestCase := range iTestCases {