mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-06 00:09:37 +08:00
refactor loading .env file
This commit is contained in:
@@ -138,24 +138,31 @@ def load_folder_files(folder_path, recursive=True):
|
|||||||
|
|
||||||
|
|
||||||
def load_dot_env_file(path):
|
def load_dot_env_file(path):
|
||||||
""" load .env file and set to os.environ
|
""" load .env file
|
||||||
"""
|
"""
|
||||||
if not path:
|
if not path:
|
||||||
path = os.path.join(os.getcwd(), ".env")
|
path = os.path.join(os.getcwd(), ".env")
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
logger.log_debug(".env file not exist: {}".format(path))
|
logger.log_debug(".env file not exist: {}".format(path))
|
||||||
return
|
return {}
|
||||||
else:
|
else:
|
||||||
if not os.path.isfile(path):
|
if not os.path.isfile(path):
|
||||||
raise exceptions.FileNotFound("env file not exist: {}".format(path))
|
raise exceptions.FileNotFound("env file not exist: {}".format(path))
|
||||||
|
|
||||||
logger.log_info("Loading environment variables from {}".format(path))
|
logger.log_info("Loading environment variables from {}".format(path))
|
||||||
|
env_variables_mapping = {}
|
||||||
with io.open(path, 'r', encoding='utf-8') as fp:
|
with io.open(path, 'r', encoding='utf-8') as fp:
|
||||||
for line in fp:
|
for line in fp:
|
||||||
variable, value = line.split("=")
|
if "=" in line:
|
||||||
variable = variable.strip()
|
variable, value = line.split("=")
|
||||||
os.environ[variable] = value.strip()
|
elif ":" in line:
|
||||||
logger.log_debug("Loaded variable: {}".format(variable))
|
variable, value = line.split(":")
|
||||||
|
else:
|
||||||
|
raise exceptions.FileFormatError(".env format error")
|
||||||
|
|
||||||
|
env_variables_mapping[variable.strip()] = value.strip()
|
||||||
|
|
||||||
|
return env_variables_mapping
|
||||||
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|||||||
@@ -206,7 +206,7 @@ class HttpRunner(object):
|
|||||||
- dot_env_path: .env file path
|
- dot_env_path: .env file path
|
||||||
"""
|
"""
|
||||||
dot_env_path = kwargs.pop("dot_env_path", None)
|
dot_env_path = kwargs.pop("dot_env_path", None)
|
||||||
loader.load_dot_env_file(dot_env_path)
|
utils.set_os_environ(loader.load_dot_env_file(dot_env_path))
|
||||||
|
|
||||||
kwargs.setdefault("resultclass", HtmlTestResult)
|
kwargs.setdefault("resultclass", HtmlTestResult)
|
||||||
self.runner = unittest.TextTestRunner(**kwargs)
|
self.runner = unittest.TextTestRunner(**kwargs)
|
||||||
|
|||||||
@@ -42,6 +42,14 @@ def remove_prefix(text, prefix):
|
|||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
|
def set_os_environ(variables_mapping):
|
||||||
|
""" set variables mapping to os.environ
|
||||||
|
"""
|
||||||
|
for variable in variables_mapping:
|
||||||
|
os.environ[variable] = variables_mapping[variable]
|
||||||
|
logger.log_debug("Loaded variable: {}".format(variable))
|
||||||
|
|
||||||
|
|
||||||
def query_json(json_content, query, delimiter='.'):
|
def query_json(json_content, query, delimiter='.'):
|
||||||
""" Do an xpath-like query with json_content.
|
""" Do an xpath-like query with json_content.
|
||||||
@param (dict/list/string) json_content
|
@param (dict/list/string) json_content
|
||||||
|
|||||||
@@ -132,10 +132,9 @@ class TestFileLoader(unittest.TestCase):
|
|||||||
self.assertEqual([], files)
|
self.assertEqual([], files)
|
||||||
|
|
||||||
def test_load_dot_env_file(self):
|
def test_load_dot_env_file(self):
|
||||||
self.assertNotIn("PROJECT_KEY", os.environ)
|
env_variables_mapping = loader.load_dot_env_file("tests/data/test.env")
|
||||||
loader.load_dot_env_file("tests/data/test.env")
|
self.assertIn("PROJECT_KEY", env_variables_mapping)
|
||||||
self.assertIn("PROJECT_KEY", os.environ)
|
self.assertEqual(env_variables_mapping["UserName"], "debugtalk")
|
||||||
self.assertEqual(os.environ["UserName"], "debugtalk")
|
|
||||||
|
|
||||||
def test_load_env_path_not_exist(self):
|
def test_load_env_path_not_exist(self):
|
||||||
with self.assertRaises(exceptions.FileNotFound):
|
with self.assertRaises(exceptions.FileNotFound):
|
||||||
|
|||||||
@@ -17,6 +17,15 @@ class TestUtils(ApiServerUnittest):
|
|||||||
"/post/123"
|
"/post/123"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_set_os_environ(self):
|
||||||
|
self.assertNotIn("abc", os.environ)
|
||||||
|
variables_mapping = {
|
||||||
|
"abc": "123"
|
||||||
|
}
|
||||||
|
utils.set_os_environ(variables_mapping)
|
||||||
|
self.assertIn("abc", os.environ)
|
||||||
|
self.assertEqual(os.environ["abc"], "123")
|
||||||
|
|
||||||
def test_query_json(self):
|
def test_query_json(self):
|
||||||
json_content = {
|
json_content = {
|
||||||
"ids": [1, 2, 3, 4],
|
"ids": [1, 2, 3, 4],
|
||||||
|
|||||||
Reference in New Issue
Block a user