From 780ec65636e35caadb08ee8087c5315c42045d9c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 12 Dec 2019 21:14:16 +0800 Subject: [PATCH] change: ensure upload file path absolute in parser --- httprunner/loader/__init__.py | 2 ++ httprunner/loader/buildup.py | 1 - httprunner/plugins/uploader/__init__.py | 32 ++++++++++++++----------- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/httprunner/loader/__init__.py b/httprunner/loader/__init__.py index 99bbdf3c..46e18ad5 100644 --- a/httprunner/loader/__init__.py +++ b/httprunner/loader/__init__.py @@ -9,6 +9,7 @@ HttpRunner loader """ from httprunner.loader.check import is_testcase_path, is_testcases, validate_json_file +from httprunner.loader.locate import get_project_working_directory as get_pwd from httprunner.loader.load import load_csv_file, load_builtin_functions from httprunner.loader.buildup import load_cases, load_project_data @@ -16,6 +17,7 @@ __all__ = [ "is_testcase_path", "is_testcases", "validate_json_file", + "get_pwd", "load_csv_file", "load_builtin_functions", "load_project_data", diff --git a/httprunner/loader/buildup.py b/httprunner/loader/buildup.py index 1a1096c2..d0c6a642 100644 --- a/httprunner/loader/buildup.py +++ b/httprunner/loader/buildup.py @@ -334,7 +334,6 @@ def load_test_file(path): """ raw_content = load_file(path) - loaded_content = None if isinstance(raw_content, dict): diff --git a/httprunner/plugins/uploader/__init__.py b/httprunner/plugins/uploader/__init__.py index 17bcf5f7..bcacd072 100644 --- a/httprunner/plugins/uploader/__init__.py +++ b/httprunner/plugins/uploader/__init__.py @@ -40,8 +40,6 @@ $ pip install requests_toolbelt filetype from httprunner.exceptions import ParamsError -PWD = os.getcwd() - def prepare_upload_test(test_dict): """ preprocess for upload test @@ -78,6 +76,18 @@ def prepare_upload_test(test_dict): params_list = [] for key, value in upload_json.items(): + + if os.path.isabs(value) and os.path.isfile(value): + # value is absolute file path, do nothing + pass + else: + # value is not absolute file path, check if it is relative file path + from httprunner.loader import get_pwd + _file_path = os.path.join(get_pwd(), value) + if os.path.isfile(_file_path): + # value is relative file path, convert it to be absolute path + value = _file_path + test_dict["variables"][key] = value params_list.append("{}=${}".format(key, key)) @@ -101,18 +111,12 @@ def multipart_encoder(**kwargs): fields_dict = {} for key, value in kwargs.items(): - if os.path.isabs(value): - _file_path = value - is_file = True - else: - global PWD - _file_path = os.path.join(PWD, value) - is_file = os.path.isfile(_file_path) - - if is_file: - filename = os.path.basename(_file_path) - with open(_file_path, 'rb') as f: - mime_type = get_filetype(_file_path) + if os.path.isfile(value): + # value is file path to upload, + # it has been ensured to be absolute in prepare_upload_test + filename = os.path.basename(value) + with open(value, 'rb') as f: + mime_type = get_filetype(value) fields_dict[key] = (filename, f.read(), mime_type) else: fields_dict[key] = value