add unittest; add --patch options

This commit is contained in:
buyuxiang
2022-05-17 16:03:45 +08:00
parent dd9f8878b2
commit 76bf2309ed
18 changed files with 604 additions and 248 deletions

View File

@@ -7,7 +7,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/convert"
"github.com/httprunner/httprunner/v4/hrp/internal/convert/case2script"
)
var convertCmd = &cobra.Command{
@@ -18,15 +18,16 @@ var convertCmd = &cobra.Command{
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 err error
if gotestFlag {
err = convert.Convert2TestScripts("gotest", args...)
err = case2script.Convert2TestScripts("gotest", args...)
} else {
err = convert.Convert2TestScripts("pytest", args...)
err = case2script.Convert2TestScripts("pytest", args...)
}
if err != nil {
log.Error().Err(err).Msg("convert test scripts failed")

View File

@@ -6,7 +6,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/har2case"
"github.com/httprunner/httprunner/v4/hrp/internal/convert/har2case"
)
// har2caseCmd represents the har2case command
@@ -22,7 +22,7 @@ var har2caseCmd = &cobra.Command{
var outputFiles []string
for _, arg := range args {
// must choose one
if !genYAMLFlag && !genJSONFlag {
if !har2caseGenYAMLFlag && !har2caseGenJSONFlag {
return errors.New("please select convert format type")
}
var outputPath string
@@ -31,17 +31,22 @@ var har2caseCmd = &cobra.Command{
har := har2case.NewHAR(arg)
// specify output dir
if outputDir != "" {
har.SetOutputDir(outputDir)
if har2caseOutputDir != "" {
har.SetOutputDir(har2caseOutputDir)
}
// specify profile
if profilePath != "" {
har.SetProfile(profilePath)
if har2caseProfilePath != "" {
har.SetProfile(har2caseProfilePath)
}
// specify profile
if har2casePatchPath != "" {
har.SetPatch(har2casePatchPath)
}
// generate json/yaml files
if genYAMLFlag {
if har2caseGenYAMLFlag {
outputPath, err = har.GenYAML()
} else {
outputPath, err = har.GenJSON() // default
@@ -57,16 +62,18 @@ var har2caseCmd = &cobra.Command{
}
var (
genJSONFlag bool
genYAMLFlag bool
outputDir string
profilePath string
har2caseGenJSONFlag bool
har2caseGenYAMLFlag bool
har2caseOutputDir string
har2caseProfilePath string
har2casePatchPath string
)
func init() {
rootCmd.AddCommand(har2caseCmd)
har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", true, "convert to JSON format")
har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to YAML format")
har2caseCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
har2caseCmd.Flags().StringVarP(&profilePath, "profile", "p", "", "specify profile path to override headers and cookies")
har2caseCmd.Flags().BoolVarP(&har2caseGenJSONFlag, "to-json", "j", true, "convert to JSON format")
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")
}

View File

@@ -6,7 +6,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/internal/postman2case"
"github.com/httprunner/httprunner/v4/hrp/internal/convert/postman2case"
)
// postman2caseCmd represents the postman2case command
@@ -22,24 +22,34 @@ var postman2caseCmd = &cobra.Command{
var outputFiles []string
for _, arg := range args {
// must choose one
if !postman2JSONFlag && !postman2YAMLFlag {
if !postman2caseGenJSONFlag && !postman2caseGenYAMLFlag {
return errors.New("please select convert format type")
}
var outputPath string
var err error
postman := postman2case.NewCollection(arg)
collection := postman2case.NewCollection(arg)
// specify output dir
if postman2Dir != "" {
postman.SetOutputDir(postman2Dir)
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 genYAMLFlag {
outputPath, err = postman.GenYAML()
if postman2caseGenYAMLFlag {
outputPath, err = collection.GenYAML()
} else {
outputPath, err = postman.GenJSON() // default
outputPath, err = collection.GenJSON() // default
}
if err != nil {
return err
@@ -52,14 +62,18 @@ var postman2caseCmd = &cobra.Command{
}
var (
postman2JSONFlag bool
postman2YAMLFlag bool
postman2Dir string
postman2caseGenJSONFlag bool
postman2caseGenYAMLFlag bool
postman2caseOutputDir string
postman2caseProfilePath string
postman2casePatchPath string
)
func init() {
rootCmd.AddCommand(postman2caseCmd)
postman2caseCmd.Flags().BoolVarP(&postman2JSONFlag, "to-json", "j", true, "convert to JSON format")
postman2caseCmd.Flags().BoolVarP(&postman2YAMLFlag, "to-yaml", "y", false, "convert to YAML format")
postman2caseCmd.Flags().StringVarP(&postman2Dir, "output-dir", "d", "", "specify output directory, default to the same dir with postman collection file")
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")
}