diff --git a/hrp/cmd/convert.go b/hrp/cmd/convert.go new file mode 100644 index 00000000..4c16b8ef --- /dev/null +++ b/hrp/cmd/convert.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "errors" + "os" + + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/hrp" +) + +var convertCmd = &cobra.Command{ + Use: "convert $path...", + Short: "convert JSON/YAML testcases to pytest/gotest scripts", + Args: cobra.ExactValidArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { + setLogLevel(logLevel) + }, + RunE: func(cmd *cobra.Command, args []string) error { + if !pytestFlag && !gotestFlag { + return errors.New("please specify convertion type") + } + + var err error + if gotestFlag { + err = hrp.Convert2TestScripts("gotest", args...) + } else { + err = hrp.Convert2TestScripts("pytest", args...) + } + if err != nil { + log.Error().Err(err).Msg("convert test scripts failed") + os.Exit(1) + } + return nil + }, +} + +var ( + pytestFlag bool + gotestFlag bool +) + +func init() { + rootCmd.AddCommand(convertCmd) + convertCmd.Flags().BoolVar(&pytestFlag, "pytest", true, "convert to pytest scripts") + convertCmd.Flags().BoolVar(&gotestFlag, "gotest", false, "convert to gotest scripts (TODO)") +} diff --git a/hrp/convert.go b/hrp/convert.go new file mode 100644 index 00000000..2194dd51 --- /dev/null +++ b/hrp/convert.go @@ -0,0 +1,62 @@ +package hrp + +import ( + "os/exec" + "strings" + + "github.com/pkg/errors" + "github.com/rs/zerolog/log" + + "github.com/httprunner/httprunner/hrp/internal/builtin" +) + +func Convert2TestScripts(destType string, paths ...string) error { + if destType == "gotest" { + return convert2GoTestScripts(paths...) + } else { + return convert2PyTestScripts(paths...) + } +} + +func convert2PyTestScripts(paths ...string) error { + python3, err := builtin.EnsurePython3Venv("httprunner") + if err != nil { + return errors.Wrap(err, "ensure python venv failed") + } + + args := append([]string{"-m", "httprunner", "make"}, paths...) + cmd := exec.Command(python3, args...) + log.Info().Str("cmd", cmd.String()).Msg("convert to pytest scripts") + + output, err := cmd.CombinedOutput() + if err != nil { + return errors.Wrap(err, "pytest running failed") + } + out := strings.TrimSpace(string(output)) + println(out) + + log.Info().Msg("convert to pytest scripts successfully") + return nil +} + +func convert2GoTestScripts(paths ...string) error { + log.Warn().Msg("convert to gotest scripts is not supported yet") + // 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/httprunner/cli.py b/httprunner/cli.py index 2257ba97..87b16229 100644 --- a/httprunner/cli.py +++ b/httprunner/cli.py @@ -108,13 +108,5 @@ def main(): main_make(args.testcase_path) -def main_make_alias(): - """ command alias - hmake = httprunner make - """ - sys.argv.insert(1, "make") - main() - - if __name__ == "__main__": main() diff --git a/pyproject.toml b/pyproject.toml index bdd45f2b..9f3f74a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,6 @@ coverage = "^4.5.4" [tool.poetry.scripts] httprunner = "httprunner.cli:main" -hmake = "httprunner.cli:main_make_alias" [build-system] requires = ["poetry>=1.0.0"]