refactor: hrp convert

This commit is contained in:
buyuxiang
2022-05-24 13:36:34 +08:00
parent 85e5b26ee3
commit ff9df1a251
37 changed files with 2245 additions and 626 deletions

View File

@@ -2,48 +2,61 @@ package cmd
import (
"errors"
"os"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/convert/case2script"
"github.com/httprunner/httprunner/v4/hrp/internal/convert"
)
var convertCmd = &cobra.Command{
Use: "convert $path...",
Short: "convert JSON/YAML testcases to pytest/gotest scripts",
Args: cobra.ExactValidArgs(1),
Short: "convert external cases to JSON/YAML/gotest/pytest testcases",
Args: cobra.MinimumNArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
setLogLevel(logLevel)
},
RunE: func(cmd *cobra.Command, args []string) error {
// TODO: integrate har2case, postman2case, etc. in convert command (forward compatibility)
if !pytestFlag && !gotestFlag {
return errors.New("please specify convertion type")
var flagCount int
var outputType convert.OutputType
if toJSONFlag {
flagCount++
}
var err error
if gotestFlag {
err = case2script.Convert2TestScripts("gotest", args...)
} else {
err = case2script.Convert2TestScripts("pytest", args...)
if toYAMLFlag {
flagCount++
outputType = convert.OutputTypeYAML
}
if err != nil {
log.Error().Err(err).Msg("convert test scripts failed")
os.Exit(1)
if toGoTestFlag {
flagCount++
outputType = convert.OutputTypeGoTest
}
if toPyTestFlag {
flagCount++
outputType = convert.OutputTypePyTest
}
if flagCount > 1 {
return errors.New("please specify at most one conversion flag")
}
iCaseConverters := convert.LoadConverters(outputType, outputDir, profilePath, args)
convert.Run(iCaseConverters)
return nil
},
}
var (
pytestFlag bool
gotestFlag bool
toJSONFlag bool
toYAMLFlag bool
toGoTestFlag bool
toPyTestFlag bool
outputDir string
profilePath string
)
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)")
convertCmd.Flags().BoolVar(&toPyTestFlag, "to-pytest", false, "convert to pytest scripts")
convertCmd.Flags().BoolVar(&toGoTestFlag, "to-gotest", false, "convert to gotest scripts (TODO)")
convertCmd.Flags().BoolVar(&toJSONFlag, "to-json", false, "convert to JSON scripts (default)")
convertCmd.Flags().BoolVar(&toYAMLFlag, "to-yaml", false, "convert to YAML scripts")
convertCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
convertCmd.Flags().StringVarP(&profilePath, "profile", "p", "", "specify profile path to override headers (except for auto-generated headers) and cookies")
}