mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-02 22:39:42 +08:00
refactor: hrp convert
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
@@ -40,11 +40,6 @@ var har2caseCmd = &cobra.Command{
|
||||
har.SetProfile(har2caseProfilePath)
|
||||
}
|
||||
|
||||
// specify profile
|
||||
if har2casePatchPath != "" {
|
||||
har.SetPatch(har2casePatchPath)
|
||||
}
|
||||
|
||||
// generate json/yaml files
|
||||
if har2caseGenYAMLFlag {
|
||||
outputPath, err = har.GenYAML()
|
||||
@@ -66,7 +61,6 @@ var (
|
||||
har2caseGenYAMLFlag bool
|
||||
har2caseOutputDir string
|
||||
har2caseProfilePath string
|
||||
har2casePatchPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -75,5 +69,4 @@ func init() {
|
||||
har2caseCmd.Flags().BoolVarP(&har2caseGenYAMLFlag, "to-yaml", "y", false, "convert to YAML format")
|
||||
har2caseCmd.Flags().StringVarP(&har2caseOutputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
|
||||
har2caseCmd.Flags().StringVarP(&har2caseProfilePath, "profile", "p", "", "specify profile path to override headers and cookies")
|
||||
har2caseCmd.Flags().StringVarP(&har2casePatchPath, "patch", "r", "", "specify the path of the file used to replace headers and cookies")
|
||||
}
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/convert/postman2case"
|
||||
)
|
||||
|
||||
// postman2caseCmd represents the postman2case command
|
||||
var postman2caseCmd = &cobra.Command{
|
||||
Use: "postman2case $postman_path...",
|
||||
Short: "convert postman collection to json/yaml testcase files",
|
||||
Long: `convert postman collection to json/yaml testcase files`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var outputFiles []string
|
||||
for _, arg := range args {
|
||||
// must choose one
|
||||
if !postman2caseGenJSONFlag && !postman2caseGenYAMLFlag {
|
||||
return errors.New("please select convert format type")
|
||||
}
|
||||
var outputPath string
|
||||
var err error
|
||||
|
||||
collection := postman2case.NewCollection(arg)
|
||||
|
||||
// specify output dir
|
||||
if postman2caseOutputDir != "" {
|
||||
collection.SetOutputDir(postman2caseOutputDir)
|
||||
}
|
||||
|
||||
// specify profile path
|
||||
if postman2caseProfilePath != "" {
|
||||
collection.SetProfile(postman2caseProfilePath)
|
||||
}
|
||||
|
||||
// specify patch path
|
||||
if postman2casePatchPath != "" {
|
||||
collection.SetPatch(postman2casePatchPath)
|
||||
}
|
||||
|
||||
// generate json/yaml files
|
||||
if postman2caseGenYAMLFlag {
|
||||
outputPath, err = collection.GenYAML()
|
||||
} else {
|
||||
outputPath, err = collection.GenJSON() // default
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
outputFiles = append(outputFiles, outputPath)
|
||||
}
|
||||
log.Info().Strs("output", outputFiles).Msg("convert testcase success")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
postman2caseGenJSONFlag bool
|
||||
postman2caseGenYAMLFlag bool
|
||||
postman2caseOutputDir string
|
||||
postman2caseProfilePath string
|
||||
postman2casePatchPath string
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(postman2caseCmd)
|
||||
postman2caseCmd.Flags().BoolVarP(&postman2caseGenJSONFlag, "to-json", "j", true, "convert to JSON format")
|
||||
postman2caseCmd.Flags().BoolVarP(&postman2caseGenYAMLFlag, "to-yaml", "y", false, "convert to YAML format")
|
||||
postman2caseCmd.Flags().StringVarP(&postman2caseOutputDir, "output-dir", "d", "", "specify output directory, default to the same dir with postman collection file")
|
||||
postman2caseCmd.Flags().StringVarP(&postman2caseProfilePath, "profile", "p", "", "specify profile path to override original headers (except for Content-Type) and cookies")
|
||||
postman2caseCmd.Flags().StringVarP(&postman2casePatchPath, "patch", "r", "", "specify patch path to create or update headers and cookies")
|
||||
}
|
||||
Reference in New Issue
Block a user