Merge branch 'master' of github.com:httprunner/httprunner

This commit is contained in:
buyuxiang
2022-05-25 15:43:19 +08:00
33 changed files with 160 additions and 44 deletions

View File

@@ -295,12 +295,44 @@ func LoadFile(path string, structObj interface{}) (err error) {
err = decoder.Decode(structObj)
case ".yaml", ".yml":
err = yaml.Unmarshal(file, structObj)
case ".env":
err = parseEnvContent(file, structObj)
default:
err = ErrUnsupportedFileExt
}
return err
}
func parseEnvContent(file []byte, obj interface{}) error {
envMap := obj.(map[string]string)
lines := strings.Split(string(file), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
// empty line or comment line
continue
}
var kv []string
if strings.Contains(line, "=") {
kv = strings.SplitN(line, "=", 2)
} else if strings.Contains(line, ":") {
kv = strings.SplitN(line, ":", 2)
}
if len(kv) != 2 {
return errors.New(".env format error")
}
key := strings.TrimSpace(kv[0])
value := strings.TrimSpace(kv[1])
envMap[key] = value
// set env
log.Info().Str("key", key).Msg("set env")
os.Setenv(key, value)
}
return nil
}
func loadFromCSV(path string) []map[string]interface{} {
log.Info().Str("path", path).Msg("load csv file")
file, err := readFile(path)

View File

@@ -104,7 +104,7 @@ func CreateScaffold(projectName string, pluginType PluginType, force bool) error
}
projectInfo := &ProjectInfo{
ProjectName: projectName,
ProjectName: filepath.Base(projectName),
ProjectPath: projectPath,
CreateTime: time.Now(),
Version: version.VERSION,

View File

@@ -1,2 +1,3 @@
base_url=https://postman-echo.com
USERNAME=debugtalk
PASSWORD=123456

View File

@@ -1,4 +1,3 @@
.env
reports/
*.so
.vscode/

View File

@@ -17,7 +17,7 @@
{
"name": "get with params",
"variables": {
"foo1": "bar11",
"foo1": "${ENV(USERNAME)}",
"foo2": "bar21",
"sum_v": "${sum_two_int(1, 2)}"
},
@@ -46,7 +46,7 @@
{
"eq": [
"body.args.foo1",
"bar11"
"debugtalk"
]
},
{

View File

@@ -5,7 +5,6 @@ config:
foo2: config_bar2
expect_foo1: config_bar1
expect_foo2: config_bar2
base_url: "https://postman-echo.com"
verify: False
export: ["foo3"]
@@ -13,12 +12,12 @@ teststeps:
-
name: get with params
variables:
foo1: bar11
foo1: ${ENV(USERNAME)}
foo2: bar21
sum_v: "${sum_two_int(1, 2)}"
request:
method: GET
url: /get
url: $base_url/get
params:
foo1: $foo1
foo2: $foo2
@@ -29,7 +28,7 @@ teststeps:
foo3: "body.args.foo2"
validate:
- eq: ["status_code", 200]
- eq: ["body.args.foo1", "bar11"]
- eq: ["body.args.foo1", "debugtalk"]
- eq: ["body.args.sum_v", "3"]
- eq: ["body.args.foo2", "bar21"]
-
@@ -39,7 +38,7 @@ teststeps:
foo3: "bar32"
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "text/plain"
@@ -53,7 +52,7 @@ teststeps:
foo2: bar23
request:
method: POST
url: /post
url: $base_url/post
headers:
User-Agent: funplugin/${get_version()}
Content-Type: "application/x-www-form-urlencoded"