relocate .env file loader

This commit is contained in:
debugtalk
2018-07-29 22:44:11 +08:00
parent 5738c368c8
commit 18d7b7ac45
3 changed files with 24 additions and 22 deletions

View File

@@ -134,3 +134,24 @@ def load_folder_files(folder_path, recursive=True):
break
return file_list
def load_dot_env_file(path):
""" load .env file and set to os.environ
"""
if not path:
path = os.path.join(os.getcwd(), ".env")
if not os.path.isfile(path):
logger.log_debug(".env file not exist: {}".format(path))
return
else:
if not os.path.isfile(path):
raise exceptions.FileNotFound("env file not exist: {}".format(path))
logger.log_info("Loading environment variables from {}".format(path))
with io.open(path, 'r', encoding='utf-8') as fp:
for line in fp:
variable, value = line.split("=")
variable = variable.strip()
os.environ[variable] = value.strip()
logger.log_debug("Loaded variable: {}".format(variable))

View File

@@ -4,12 +4,12 @@ import copy
import sys
import unittest
from httprunner import exceptions, logger, runner, testcase, utils
from httprunner import exceptions, loader, logger, runner, testcase, utils
from httprunner.compat import is_py3
from httprunner.report import (HtmlTestResult, get_platform, get_summary,
render_html_report)
from httprunner.testcase import TestcaseLoader
from httprunner.utils import load_dot_env_file, print_output
from httprunner.utils import print_output
class TestCase(unittest.TestCase):
@@ -212,7 +212,7 @@ class HttpRunner(object):
- dot_env_path: .env file path
"""
dot_env_path = kwargs.pop("dot_env_path", None)
load_dot_env_file(dot_env_path)
loader.load_dot_env_file(dot_env_path)
kwargs.setdefault("resultclass", HtmlTestResult)
self.runner = unittest.TextTestRunner(**kwargs)

View File

@@ -375,25 +375,6 @@ def create_scaffold(project_path):
logger.color_print(msg, "BLUE")
def load_dot_env_file(path):
""" load .env file and set to os.environ
"""
if not path:
path = os.path.join(os.getcwd(), ".env")
if not os.path.isfile(path):
logger.log_debug(".env file not exist: {}".format(path))
return
else:
if not os.path.isfile(path):
raise exceptions.FileNotFound("env file not exist: {}".format(path))
logger.log_info("Loading environment variables from {}".format(path))
with io.open(path, 'r', encoding='utf-8') as fp:
for line in fp:
variable, value = line.split("=")
variable = variable.strip()
os.environ[variable] = value.strip()
logger.log_debug("Loaded variable: {}".format(variable))
def validate_json_file(file_list):
""" validate JSON testset format