mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
feat: dump2YAML
This commit is contained in:
31
convert.go
31
convert.go
@@ -1,10 +1,13 @@
|
||||
package httpboomer
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func (tc *TestCase) toStruct() *TCase {
|
||||
@@ -33,3 +36,31 @@ func (tc *TestCase) dump2JSON(path string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tc *TestCase) dump2YAML(path string) error {
|
||||
path, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
log.Printf("convert absolute path error: %v, path: %v", err, path)
|
||||
return err
|
||||
}
|
||||
log.Printf("dump testcase to yaml path: %s", path)
|
||||
|
||||
// init yaml encoder
|
||||
buffer := new(bytes.Buffer)
|
||||
encoder := yaml.NewEncoder(buffer)
|
||||
encoder.SetIndent(4)
|
||||
|
||||
// encode
|
||||
tcStruct := tc.toStruct()
|
||||
err = encoder.Encode(tcStruct)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(path, buffer.Bytes(), 0644)
|
||||
if err != nil {
|
||||
log.Printf("dump yaml path error: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user