refactor: simplify testcase converter

This commit is contained in:
debugtalk
2022-06-27 00:25:32 +08:00
parent 2caf5d5159
commit 700cdfd815
10 changed files with 228 additions and 670 deletions

View File

@@ -1,78 +1,29 @@
package convert
import (
"reflect"
"github.com/pkg/errors"
"github.com/httprunner/httprunner/v4/hrp"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
)
func NewConverterJSON(converter *TCaseConverter) *ConverterJSON {
return &ConverterJSON{
converter: converter,
}
}
type ConverterJSON struct {
converter *TCaseConverter
}
func (c *ConverterJSON) Struct() *TCaseConverter {
return c.converter
}
func (c *ConverterJSON) ToJSON() (string, error) {
testCase, err := c.makeTestCase()
func LoadJSONCase(path string) (*hrp.TCase, error) {
// load json case file
caseJSON := new(hrp.TCase)
err := builtin.LoadFile(path, caseJSON)
if err != nil {
return "", err
return nil, errors.Wrap(err, "load json file failed")
}
jsonPath := c.converter.genOutputPath(suffixJSON)
err = builtin.Dump2JSON(testCase, jsonPath)
if err != nil {
return "", err
if reflect.ValueOf(*caseJSON).IsZero() {
return nil, errors.New("invalid json file")
}
return jsonPath, nil
}
func (c *ConverterJSON) ToYAML() (string, error) {
testCase, err := c.makeTestCase()
if err != nil {
return "", err
}
yamlPath := c.converter.genOutputPath(suffixYAML)
err = builtin.Dump2YAML(testCase, yamlPath)
if err != nil {
return "", err
}
return yamlPath, nil
}
func (c *ConverterJSON) ToGoTest() (string, error) {
// TODO implement me
return "", errors.New("convert from json testcase to gotest scripts is not supported yet")
}
func (c *ConverterJSON) ToPyTest() (string, error) {
return convertToPyTest(c)
}
func (c *ConverterJSON) MakePyTestScript() (string, error) {
args := append([]string{"make"}, c.converter.InputPath)
err := builtin.ExecPython3Command("httprunner", args...)
if err != nil {
return "", err
}
return c.converter.genOutputPath(suffixPyTest), nil
}
func (c *ConverterJSON) makeTestCase() (*hrp.TCase, error) {
tCase, err := makeTestCaseFromJSONYAML(c)
// convert json case to TCase
err = caseJSON.MakeCompat()
if err != nil {
return nil, err
}
err = tCase.MakeCompat()
if err != nil {
return nil, err
}
return tCase, nil
return caseJSON, nil
}