mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 00:39:34 +08:00
refactor: convert postman case
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -13,65 +12,86 @@ import (
|
||||
)
|
||||
|
||||
var convertCmd = &cobra.Command{
|
||||
Use: "convert $path...",
|
||||
Short: "convert to JSON/YAML/gotest/pytest testcases",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Use: "convert $path...",
|
||||
Short: "convert multiple source format to HttpRunner JSON/YAML/gotest/pytest cases",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
SilenceUsage: false,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: convertRun,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
caseConverter := convert.NewConverter(outputDir, profilePath)
|
||||
|
||||
var fromType convert.FromType
|
||||
if fromYAMLFlag {
|
||||
fromType = convert.FromTypeYAML
|
||||
} else if fromPostmanFlag {
|
||||
fromType = convert.FromTypePostman
|
||||
} else if fromHARFlag {
|
||||
fromType = convert.FromTypeHAR
|
||||
} else {
|
||||
fromType = convert.FromTypeJSON
|
||||
log.Info().Str("fromType", fromType.String()).Msg("set default")
|
||||
}
|
||||
|
||||
var outputType convert.OutputType
|
||||
if toYAMLFlag {
|
||||
outputType = convert.OutputTypeYAML
|
||||
} else if toPyTestFlag {
|
||||
packages := []string{
|
||||
fmt.Sprintf("httprunner==%s", version.HttpRunnerMinimumVersion),
|
||||
}
|
||||
_, err := myexec.EnsurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("python3 venv is not ready")
|
||||
return err
|
||||
}
|
||||
|
||||
outputType = convert.OutputTypePyTest
|
||||
} else {
|
||||
outputType = convert.OutputTypeJSON
|
||||
log.Info().Str("outputType", outputType.String()).Msg("set default")
|
||||
}
|
||||
|
||||
for _, arg := range args {
|
||||
if err := caseConverter.Convert(arg, fromType, outputType); err != nil {
|
||||
log.Error().Err(err).Str("path", arg).
|
||||
Str("outputType", outputType.String()).
|
||||
Msg("convert case failed")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
outputDir string
|
||||
profilePath string
|
||||
|
||||
fromJSONFlag bool
|
||||
fromYAMLFlag bool
|
||||
fromPostmanFlag bool
|
||||
fromHARFlag bool
|
||||
|
||||
toJSONFlag bool
|
||||
toYAMLFlag bool
|
||||
toGoTestFlag bool
|
||||
toPyTestFlag bool
|
||||
outputDir string
|
||||
profilePath string
|
||||
|
||||
outputType convert.OutputType
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(convertCmd)
|
||||
|
||||
convertCmd.Flags().BoolVar(&fromJSONFlag, "from-json", true, "load from json case format")
|
||||
convertCmd.Flags().BoolVar(&fromYAMLFlag, "from-yaml", false, "load from yaml case format")
|
||||
convertCmd.Flags().BoolVar(&fromHARFlag, "from-har", false, "load from HAR format")
|
||||
convertCmd.Flags().BoolVar(&fromPostmanFlag, "from-postman", false, "load from postman format")
|
||||
|
||||
convertCmd.Flags().BoolVar(&toJSONFlag, "to-json", true, "convert to JSON case scripts")
|
||||
convertCmd.Flags().BoolVar(&toYAMLFlag, "to-yaml", false, "convert to YAML case scripts")
|
||||
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(&outputDir, "output-dir", "d", "", "specify output directory")
|
||||
convertCmd.Flags().StringVarP(&profilePath, "profile", "p", "", "specify profile path to override headers and cookies")
|
||||
}
|
||||
|
||||
func convertRun(cmd *cobra.Command, args []string) error {
|
||||
var flagCount int
|
||||
if toJSONFlag {
|
||||
flagCount++
|
||||
}
|
||||
if toYAMLFlag {
|
||||
flagCount++
|
||||
outputType = convert.OutputTypeYAML
|
||||
}
|
||||
if toGoTestFlag {
|
||||
flagCount++
|
||||
outputType = convert.OutputTypeGoTest
|
||||
}
|
||||
if toPyTestFlag {
|
||||
flagCount++
|
||||
outputType = convert.OutputTypePyTest
|
||||
|
||||
packages := []string{
|
||||
fmt.Sprintf("httprunner==%s", version.HttpRunnerMinimumVersion),
|
||||
}
|
||||
_, err := myexec.EnsurePython3Venv(venv, packages...)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("python3 venv is not ready")
|
||||
return err
|
||||
}
|
||||
}
|
||||
if flagCount > 1 {
|
||||
return errors.New("please specify at most one conversion flag")
|
||||
}
|
||||
convert.Run(outputType, outputDir, profilePath, args)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/boomer"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/convert"
|
||||
)
|
||||
|
||||
var runCurlCmd = &cobra.Command{
|
||||
Use: "curl URLs",
|
||||
Short: "run API test with curl command",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
DisableFlagParsing: true,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
runner := makeHRPRunner()
|
||||
return runner.Run(makeCurlTestCase(args))
|
||||
},
|
||||
}
|
||||
|
||||
var boomCurlCmd = &cobra.Command{
|
||||
Use: "curl URLs",
|
||||
Short: "run load test with curl command",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
DisableFlagParsing: true,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
boomer.SetUlimit(10240)
|
||||
if !strings.EqualFold(logLevel, "DEBUG") {
|
||||
logLevel = "WARN" // disable info logs for load testing
|
||||
}
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
boomer, err := makeHRPBoomer()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
boomer.Run(makeCurlTestCase(args))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var convertCurlCmd = &cobra.Command{
|
||||
Use: "curl URLs",
|
||||
Short: "convert curl command to httprunner testcase",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
DisableFlagParsing: true,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
curlCommand := makeCurlCommand(args)
|
||||
return convertRun(cmd, []string{curlCommand})
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
runCmd.AddCommand(runCurlCmd)
|
||||
boomCmd.AddCommand(boomCurlCmd)
|
||||
convertCmd.AddCommand(convertCurlCmd)
|
||||
}
|
||||
|
||||
func makeCurlTestCase(args []string) *hrp.TestCase {
|
||||
curlCommand := makeCurlCommand(args)
|
||||
tCase, err := convert.LoadSingleCurlCase(curlCommand)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("convert curl command failed")
|
||||
os.Exit(1)
|
||||
}
|
||||
testCase, err := tCase.ToTestCase(env.RootDir)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("convert testcase to failed")
|
||||
os.Exit(1)
|
||||
}
|
||||
return testCase
|
||||
}
|
||||
|
||||
func makeCurlCommand(args []string) string {
|
||||
for i := 0; i < len(args); i++ {
|
||||
if !strings.HasPrefix(args[i], "-") {
|
||||
args[i] = fmt.Sprintf("\"%s\"", args[i])
|
||||
}
|
||||
}
|
||||
var curlCmd []string
|
||||
curlCmd = append(curlCmd, "curl")
|
||||
curlCmd = append(curlCmd, args...)
|
||||
return strings.Join(curlCmd, " ")
|
||||
}
|
||||
Reference in New Issue
Block a user