mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-06 23:11:21 +08:00
fix #1308: load .env file as environment variables
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
**go version**
|
**go version**
|
||||||
|
|
||||||
|
- fix #1308: load `.env` file as environment variables
|
||||||
- fix #1309: locate plugin file upward recursively until system root dir
|
- fix #1309: locate plugin file upward recursively until system root dir
|
||||||
|
|
||||||
## v4.1.0-beta (2022-05-21)
|
## v4.1.0-beta (2022-05-21)
|
||||||
|
|||||||
6
examples/demo-with-go-plugin/proj.json
Normal file
6
examples/demo-with-go-plugin/proj.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"project_name": "demo-with-go-plugin",
|
||||||
|
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-go-plugin",
|
||||||
|
"create_time": "2022-05-24T11:10:10.127839+08:00",
|
||||||
|
"hrp_version": "v4.1.0-beta"
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
{
|
{
|
||||||
"name": "get with params",
|
"name": "get with params",
|
||||||
"variables": {
|
"variables": {
|
||||||
"foo1": "bar11",
|
"foo1": "${ENV(USERNAME)}",
|
||||||
"foo2": "bar21",
|
"foo2": "bar21",
|
||||||
"sum_v": "${sum_two_int(1, 2)}"
|
"sum_v": "${sum_two_int(1, 2)}"
|
||||||
},
|
},
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
{
|
{
|
||||||
"eq": [
|
"eq": [
|
||||||
"body.args.foo1",
|
"body.args.foo1",
|
||||||
"bar11"
|
"debugtalk"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ teststeps:
|
|||||||
-
|
-
|
||||||
name: get with params
|
name: get with params
|
||||||
variables:
|
variables:
|
||||||
foo1: bar11
|
foo1: ${ENV(USERNAME)}
|
||||||
foo2: bar21
|
foo2: bar21
|
||||||
sum_v: "${sum_two_int(1, 2)}"
|
sum_v: "${sum_two_int(1, 2)}"
|
||||||
request:
|
request:
|
||||||
@@ -29,7 +29,7 @@ teststeps:
|
|||||||
foo3: "body.args.foo2"
|
foo3: "body.args.foo2"
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- eq: ["body.args.foo1", "bar11"]
|
- eq: ["body.args.foo1", "debugtalk"]
|
||||||
- eq: ["body.args.sum_v", "3"]
|
- eq: ["body.args.sum_v", "3"]
|
||||||
- eq: ["body.args.foo2", "bar21"]
|
- eq: ["body.args.foo2", "bar21"]
|
||||||
-
|
-
|
||||||
|
|||||||
6
examples/demo-with-py-plugin/proj.json
Normal file
6
examples/demo-with-py-plugin/proj.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"project_name": "demo-with-py-plugin",
|
||||||
|
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-with-py-plugin",
|
||||||
|
"create_time": "2022-05-24T11:10:15.544763+08:00",
|
||||||
|
"hrp_version": "v4.1.0-beta"
|
||||||
|
}
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
{
|
{
|
||||||
"name": "get with params",
|
"name": "get with params",
|
||||||
"variables": {
|
"variables": {
|
||||||
"foo1": "bar11",
|
"foo1": "${ENV(USERNAME)}",
|
||||||
"foo2": "bar21",
|
"foo2": "bar21",
|
||||||
"sum_v": "${sum_two_int(1, 2)}"
|
"sum_v": "${sum_two_int(1, 2)}"
|
||||||
},
|
},
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
{
|
{
|
||||||
"eq": [
|
"eq": [
|
||||||
"body.args.foo1",
|
"body.args.foo1",
|
||||||
"bar11"
|
"debugtalk"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ teststeps:
|
|||||||
-
|
-
|
||||||
name: get with params
|
name: get with params
|
||||||
variables:
|
variables:
|
||||||
foo1: bar11
|
foo1: ${ENV(USERNAME)}
|
||||||
foo2: bar21
|
foo2: bar21
|
||||||
sum_v: "${sum_two_int(1, 2)}"
|
sum_v: "${sum_two_int(1, 2)}"
|
||||||
request:
|
request:
|
||||||
@@ -29,7 +29,7 @@ teststeps:
|
|||||||
foo3: "body.args.foo2"
|
foo3: "body.args.foo2"
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- eq: ["body.args.foo1", "bar11"]
|
- eq: ["body.args.foo1", "debugtalk"]
|
||||||
- eq: ["body.args.sum_v", "3"]
|
- eq: ["body.args.sum_v", "3"]
|
||||||
- eq: ["body.args.foo2", "bar21"]
|
- eq: ["body.args.foo2", "bar21"]
|
||||||
-
|
-
|
||||||
|
|||||||
6
examples/demo-without-plugin/proj.json
Normal file
6
examples/demo-without-plugin/proj.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"project_name": "demo-without-plugin",
|
||||||
|
"project_path": "/Users/debugtalk/MyProjects/HttpRunner-dev/httprunner/examples/demo-without-plugin",
|
||||||
|
"create_time": "2022-05-24T11:10:16.993462+08:00",
|
||||||
|
"hrp_version": "v4.1.0-beta"
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
func NewConfig(name string) *TConfig {
|
func NewConfig(name string) *TConfig {
|
||||||
return &TConfig{
|
return &TConfig{
|
||||||
Name: name,
|
Name: name,
|
||||||
|
Env: make(map[string]string),
|
||||||
Variables: make(map[string]interface{}),
|
Variables: make(map[string]interface{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,6 +22,7 @@ type TConfig struct {
|
|||||||
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"`
|
||||||
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"`
|
||||||
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
|
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
|
||||||
|
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"`
|
||||||
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
|
||||||
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
Parameters map[string]interface{} `json:"parameters,omitempty" yaml:"parameters,omitempty"`
|
||||||
ParametersSetting *TParamsConfig `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
|
ParametersSetting *TParamsConfig `json:"parameters_setting,omitempty" yaml:"parameters_setting,omitempty"`
|
||||||
|
|||||||
@@ -294,12 +294,45 @@ func LoadFile(path string, structObj interface{}) (err error) {
|
|||||||
err = decoder.Decode(structObj)
|
err = decoder.Decode(structObj)
|
||||||
case ".yaml", ".yml":
|
case ".yaml", ".yml":
|
||||||
err = yaml.Unmarshal(file, structObj)
|
err = yaml.Unmarshal(file, structObj)
|
||||||
|
case ".env":
|
||||||
|
err = parseEnvContent(file, structObj)
|
||||||
default:
|
default:
|
||||||
err = ErrUnsupportedFileExt
|
err = ErrUnsupportedFileExt
|
||||||
}
|
}
|
||||||
return err
|
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)
|
||||||
|
}
|
||||||
|
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{} {
|
func loadFromCSV(path string) []map[string]interface{} {
|
||||||
log.Info().Str("path", path).Msg("load csv file")
|
log.Info().Str("path", path).Msg("load csv file")
|
||||||
file, err := readFile(path)
|
file, err := readFile(path)
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ func CreateScaffold(projectName string, pluginType PluginType, force bool) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
projectInfo := &ProjectInfo{
|
projectInfo := &ProjectInfo{
|
||||||
ProjectName: projectName,
|
ProjectName: filepath.Base(projectName),
|
||||||
ProjectPath: projectPath,
|
ProjectPath: projectPath,
|
||||||
CreateTime: time.Now(),
|
CreateTime: time.Now(),
|
||||||
Version: version.VERSION,
|
Version: version.VERSION,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
{
|
{
|
||||||
"name": "get with params",
|
"name": "get with params",
|
||||||
"variables": {
|
"variables": {
|
||||||
"foo1": "bar11",
|
"foo1": "${ENV(USERNAME)}",
|
||||||
"foo2": "bar21",
|
"foo2": "bar21",
|
||||||
"sum_v": "${sum_two_int(1, 2)}"
|
"sum_v": "${sum_two_int(1, 2)}"
|
||||||
},
|
},
|
||||||
@@ -46,7 +46,7 @@
|
|||||||
{
|
{
|
||||||
"eq": [
|
"eq": [
|
||||||
"body.args.foo1",
|
"body.args.foo1",
|
||||||
"bar11"
|
"debugtalk"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ teststeps:
|
|||||||
-
|
-
|
||||||
name: get with params
|
name: get with params
|
||||||
variables:
|
variables:
|
||||||
foo1: bar11
|
foo1: ${ENV(USERNAME)}
|
||||||
foo2: bar21
|
foo2: bar21
|
||||||
sum_v: "${sum_two_int(1, 2)}"
|
sum_v: "${sum_two_int(1, 2)}"
|
||||||
request:
|
request:
|
||||||
@@ -29,7 +29,7 @@ teststeps:
|
|||||||
foo3: "body.args.foo2"
|
foo3: "body.args.foo2"
|
||||||
validate:
|
validate:
|
||||||
- eq: ["status_code", 200]
|
- eq: ["status_code", 200]
|
||||||
- eq: ["body.args.foo1", "bar11"]
|
- eq: ["body.args.foo1", "debugtalk"]
|
||||||
- eq: ["body.args.sum_v", "3"]
|
- eq: ["body.args.sum_v", "3"]
|
||||||
- eq: ["body.args.foo2", "bar21"]
|
- eq: ["body.args.foo2", "bar21"]
|
||||||
-
|
-
|
||||||
|
|||||||
@@ -80,6 +80,16 @@ func (path *TestCasePath) ToTestCase() (*TestCase, error) {
|
|||||||
return nil, errors.Wrap(err, "failed to get project root dir")
|
return nil, errors.Wrap(err, "failed to get project root dir")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// load .env file
|
||||||
|
dotEnvPath := filepath.Join(projectRootDir, ".env")
|
||||||
|
if builtin.IsFilePathExists(dotEnvPath) {
|
||||||
|
testCase.Config.Env = make(map[string]string)
|
||||||
|
err = builtin.LoadFile(dotEnvPath, testCase.Config.Env)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to load .env file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, step := range tc.TestSteps {
|
for _, step := range tc.TestSteps {
|
||||||
if step.API != nil {
|
if step.API != nil {
|
||||||
apiPath, ok := step.API.(string)
|
apiPath, ok := step.API.(string)
|
||||||
|
|||||||
Reference in New Issue
Block a user