mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-07 07:21:23 +08:00
feat: specify output directory
This commit is contained in:
@@ -26,10 +26,20 @@ type HAR struct {
|
|||||||
path string
|
path string
|
||||||
filterStr string
|
filterStr string
|
||||||
excludeStr string
|
excludeStr string
|
||||||
|
outputDir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *HAR) SetOutputDir(dir string) {
|
||||||
|
h.outputDir = dir
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HAR) GenJSON() (jsonPath string, err error) {
|
func (h *HAR) GenJSON() (jsonPath string, err error) {
|
||||||
jsonPath = getFilenameWithoutExtension(h.path) + ".json"
|
jsonFile := getFilenameWithoutExtension(h.path) + ".json"
|
||||||
|
if h.outputDir != "" {
|
||||||
|
jsonPath = filepath.Join(h.outputDir, jsonFile)
|
||||||
|
} else {
|
||||||
|
jsonPath = filepath.Join(filepath.Dir(h.path), jsonFile)
|
||||||
|
}
|
||||||
|
|
||||||
tCase, err := h.makeTestCase()
|
tCase, err := h.makeTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -40,7 +50,12 @@ func (h *HAR) GenJSON() (jsonPath string, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *HAR) GenYAML() (yamlPath string, err error) {
|
func (h *HAR) GenYAML() (yamlPath string, err error) {
|
||||||
yamlPath = getFilenameWithoutExtension(h.path) + ".yaml"
|
yamlFile := getFilenameWithoutExtension(h.path) + ".yaml"
|
||||||
|
if h.outputDir != "" {
|
||||||
|
yamlPath = filepath.Join(h.outputDir, yamlFile)
|
||||||
|
} else {
|
||||||
|
yamlPath = filepath.Join(filepath.Dir(h.path), yamlFile)
|
||||||
|
}
|
||||||
|
|
||||||
tCase, err := h.makeTestCase()
|
tCase, err := h.makeTestCase()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -292,6 +307,7 @@ func (s *TStep) makeValidate(entry *Entry) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getFilenameWithoutExtension(path string) string {
|
func getFilenameWithoutExtension(path string) string {
|
||||||
ext := filepath.Ext(path)
|
base := filepath.Base(path)
|
||||||
return path[0 : len(path)-len(ext)]
|
ext := filepath.Ext(base)
|
||||||
|
return base[0 : len(base)-len(ext)]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,15 @@ package har2case
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
var harPath string
|
var (
|
||||||
|
harPath = "../examples/har/demo.har"
|
||||||
func TestMain(m *testing.M) {
|
harPath2 = "../examples/har/postman-echo.har"
|
||||||
harPath = "../examples/har/demo.har"
|
)
|
||||||
|
|
||||||
// run all tests
|
|
||||||
code := m.Run()
|
|
||||||
defer os.Exit(code)
|
|
||||||
|
|
||||||
// teardown
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenJSON(t *testing.T) {
|
func TestGenJSON(t *testing.T) {
|
||||||
jsonPath, err := NewHAR(harPath).GenJSON()
|
jsonPath, err := NewHAR(harPath).GenJSON()
|
||||||
@@ -31,6 +23,17 @@ func TestGenJSON(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenYAML(t *testing.T) {
|
||||||
|
yamlPath, err := NewHAR(harPath2).GenYAML()
|
||||||
|
log.Printf("yamlPath: %v, err: %v", yamlPath, err)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
if !assert.NotEmpty(t, yamlPath) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestLoadHAR(t *testing.T) {
|
func TestLoadHAR(t *testing.T) {
|
||||||
har := NewHAR(harPath)
|
har := NewHAR(harPath)
|
||||||
h, err := har.load()
|
h, err := har.load()
|
||||||
@@ -108,3 +111,10 @@ func TestMakeTestCase(t *testing.T) {
|
|||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetFilenameWithoutExtension(t *testing.T) {
|
||||||
|
filename := getFilenameWithoutExtension("examples/har/postman-echo.har")
|
||||||
|
if !assert.Equal(t, "postman-echo", filename) {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
// har2caseCmd represents the har2case command
|
// har2caseCmd represents the har2case command
|
||||||
var har2caseCmd = &cobra.Command{
|
var har2caseCmd = &cobra.Command{
|
||||||
Use: "har2case path...",
|
Use: "har2case harPath...",
|
||||||
Short: "Convert HAR to json/yaml testcase files",
|
Short: "Convert HAR to json/yaml testcase files",
|
||||||
Long: `Convert HAR to json/yaml testcase files`,
|
Long: `Convert HAR to json/yaml testcase files`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
@@ -19,10 +19,19 @@ var har2caseCmd = &cobra.Command{
|
|||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
var outputPath string
|
var outputPath string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
har := har2case.NewHAR(arg)
|
||||||
|
|
||||||
|
// specify output dir
|
||||||
|
if outputDir != "" {
|
||||||
|
har.SetOutputDir(outputDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate json/yaml files
|
||||||
if genYAMLFlag {
|
if genYAMLFlag {
|
||||||
outputPath, err = har2case.NewHAR(arg).GenYAML()
|
outputPath, err = har.GenYAML()
|
||||||
} else {
|
} else {
|
||||||
outputPath, err = har2case.NewHAR(arg).GenJSON()
|
outputPath, err = har.GenJSON()
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -37,10 +46,12 @@ var har2caseCmd = &cobra.Command{
|
|||||||
var (
|
var (
|
||||||
genJSONFlag bool
|
genJSONFlag bool
|
||||||
genYAMLFlag bool
|
genYAMLFlag bool
|
||||||
|
outputDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.AddCommand(har2caseCmd)
|
RootCmd.AddCommand(har2caseCmd)
|
||||||
har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", false, "convert to JSON format (default)")
|
har2caseCmd.Flags().BoolVarP(&genJSONFlag, "to-json", "j", false, "convert to JSON format (default)")
|
||||||
har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to JSON format")
|
har2caseCmd.Flags().BoolVarP(&genYAMLFlag, "to-yaml", "y", false, "convert to JSON format")
|
||||||
|
har2caseCmd.Flags().StringVarP(&outputDir, "output-dir", "d", "", "specify output directory, default to the same dir with har file")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user