mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 22:44:05 +08:00
change: load_project_meta only once by default
This commit is contained in:
@@ -124,9 +124,10 @@ def multipart_encoder(**kwargs) -> MultipartEncoder:
|
|||||||
is_exists_file = os.path.isfile(value)
|
is_exists_file = os.path.isfile(value)
|
||||||
else:
|
else:
|
||||||
# value is not absolute file path, check if it is relative file path
|
# value is not absolute file path, check if it is relative file path
|
||||||
from httprunner.loader import project_working_directory
|
from httprunner.loader import load_project_meta
|
||||||
|
project_meta = load_project_meta(os.getcwd())
|
||||||
|
|
||||||
_file_path = os.path.join(project_working_directory, value)
|
_file_path = os.path.join(project_meta.PWD, value)
|
||||||
is_exists_file = os.path.isfile(_file_path)
|
is_exists_file = os.path.isfile(_file_path)
|
||||||
|
|
||||||
if is_exists_file:
|
if is_exists_file:
|
||||||
|
|||||||
@@ -23,8 +23,7 @@ except AttributeError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
project_meta_cached_mapping: Dict[Text, ProjectMeta] = {}
|
project_meta: Union[ProjectMeta, None] = None
|
||||||
project_working_directory: Union[Text, None] = None
|
|
||||||
|
|
||||||
|
|
||||||
def _load_yaml_file(yaml_file: Text) -> Dict:
|
def _load_yaml_file(yaml_file: Text) -> Dict:
|
||||||
@@ -174,12 +173,12 @@ def load_csv_file(csv_file: Text) -> List[Dict]:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
if not os.path.isabs(csv_file):
|
if not os.path.isabs(csv_file):
|
||||||
global project_working_directory
|
global project_meta
|
||||||
if project_working_directory is None:
|
if project_meta is None:
|
||||||
raise exceptions.MyBaseFailure("load_project_meta() has not been called!")
|
raise exceptions.MyBaseFailure("load_project_meta() has not been called!")
|
||||||
|
|
||||||
# make compatible with Windows/Linux
|
# make compatible with Windows/Linux
|
||||||
csv_file = os.path.join(project_working_directory, *csv_file.split("/"))
|
csv_file = os.path.join(project_meta.PWD, *csv_file.split("/"))
|
||||||
|
|
||||||
if not os.path.isfile(csv_file):
|
if not os.path.isfile(csv_file):
|
||||||
# file path not exist
|
# file path not exist
|
||||||
@@ -327,13 +326,8 @@ def locate_debugtalk_py(start_path: Text) -> Text:
|
|||||||
return debugtalk_path
|
return debugtalk_path
|
||||||
|
|
||||||
|
|
||||||
def init_project_working_directory(test_path: Text) -> Tuple[Text, Text]:
|
def locate_project_working_directory(test_path: Text) -> Tuple[Text, Text]:
|
||||||
""" this should be called at startup
|
""" locate debugtalk.py path as project working directory
|
||||||
|
|
||||||
run test file:
|
|
||||||
run_path -> load_cases -> load_project_data -> init_project_working_directory
|
|
||||||
or run passed in data structure:
|
|
||||||
run -> init_project_working_directory
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
test_path: specified testfile path
|
test_path: specified testfile path
|
||||||
@@ -359,7 +353,6 @@ def init_project_working_directory(test_path: Text) -> Tuple[Text, Text]:
|
|||||||
# locate debugtalk.py file
|
# locate debugtalk.py file
|
||||||
debugtalk_path = locate_debugtalk_py(test_path)
|
debugtalk_path = locate_debugtalk_py(test_path)
|
||||||
|
|
||||||
global project_working_directory
|
|
||||||
if debugtalk_path:
|
if debugtalk_path:
|
||||||
# The folder contains debugtalk.py will be treated as PWD.
|
# The folder contains debugtalk.py will be treated as PWD.
|
||||||
project_working_directory = os.path.dirname(debugtalk_path)
|
project_working_directory = os.path.dirname(debugtalk_path)
|
||||||
@@ -367,20 +360,9 @@ def init_project_working_directory(test_path: Text) -> Tuple[Text, Text]:
|
|||||||
# debugtalk.py not found, use os.getcwd() as PWD.
|
# debugtalk.py not found, use os.getcwd() as PWD.
|
||||||
project_working_directory = os.getcwd()
|
project_working_directory = os.getcwd()
|
||||||
|
|
||||||
# add PWD to sys.path
|
|
||||||
sys.path.insert(0, project_working_directory)
|
|
||||||
|
|
||||||
return debugtalk_path, project_working_directory
|
return debugtalk_path, project_working_directory
|
||||||
|
|
||||||
|
|
||||||
def get_project_working_directory(test_path: Text) -> Text:
|
|
||||||
global project_working_directory
|
|
||||||
if not project_working_directory:
|
|
||||||
init_project_working_directory(test_path)
|
|
||||||
|
|
||||||
return project_working_directory
|
|
||||||
|
|
||||||
|
|
||||||
def load_debugtalk_functions() -> Dict[Text, Callable]:
|
def load_debugtalk_functions() -> Dict[Text, Callable]:
|
||||||
""" load project debugtalk.py module functions
|
""" load project debugtalk.py module functions
|
||||||
debugtalk.py should be located in project working directory.
|
debugtalk.py should be located in project working directory.
|
||||||
@@ -398,30 +380,36 @@ def load_debugtalk_functions() -> Dict[Text, Callable]:
|
|||||||
return load_module_functions(imported_module)
|
return load_module_functions(imported_module)
|
||||||
|
|
||||||
|
|
||||||
def load_project_meta(test_path: Text) -> ProjectMeta:
|
def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta:
|
||||||
""" load api, testcases, .env, debugtalk.py functions.
|
""" load api, testcases, .env, debugtalk.py functions.
|
||||||
api/testcases folder is relative to project_working_directory
|
api/testcases folder is relative to project_working_directory
|
||||||
|
by default, project_meta will be loaded only once, unless set reload to true.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
test_path (str): test file/folder path, locate pwd from this path.
|
test_path (str): test file/folder path, locate pwd from this path.
|
||||||
|
reload: reload project meta if set true, default to false
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
project loaded api/testcases definitions,
|
project loaded api/testcases definitions,
|
||||||
environments and debugtalk.py functions.
|
environments and debugtalk.py functions.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
global project_meta
|
||||||
|
if project_meta and (not reload):
|
||||||
|
return project_meta
|
||||||
|
|
||||||
project_meta = ProjectMeta()
|
project_meta = ProjectMeta()
|
||||||
|
|
||||||
if not test_path:
|
if not test_path:
|
||||||
return project_meta
|
return project_meta
|
||||||
|
|
||||||
if test_path in project_meta_cached_mapping:
|
debugtalk_path, project_working_directory = locate_project_working_directory(
|
||||||
return project_meta_cached_mapping[test_path]
|
|
||||||
|
|
||||||
debugtalk_path, project_working_directory = init_project_working_directory(
|
|
||||||
test_path
|
test_path
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# add PWD to sys.path
|
||||||
|
sys.path.insert(0, project_working_directory)
|
||||||
|
|
||||||
# load .env file
|
# load .env file
|
||||||
# NOTICE:
|
# NOTICE:
|
||||||
# environment variable maybe loaded in debugtalk.py
|
# environment variable maybe loaded in debugtalk.py
|
||||||
@@ -442,5 +430,4 @@ def load_project_meta(test_path: Text) -> ProjectMeta:
|
|||||||
len(project_working_directory) + 1 :
|
len(project_working_directory) + 1 :
|
||||||
]
|
]
|
||||||
|
|
||||||
project_meta_cached_mapping[test_path] = project_meta
|
|
||||||
return project_meta
|
return project_meta
|
||||||
|
|||||||
Reference in New Issue
Block a user