mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-29 03:57:22 +08:00
change: ensure upload file path absolute in parser
This commit is contained in:
@@ -9,6 +9,7 @@ HttpRunner loader
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from httprunner.loader.check import is_testcase_path, is_testcases, validate_json_file
|
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.load import load_csv_file, load_builtin_functions
|
||||||
from httprunner.loader.buildup import load_cases, load_project_data
|
from httprunner.loader.buildup import load_cases, load_project_data
|
||||||
|
|
||||||
@@ -16,6 +17,7 @@ __all__ = [
|
|||||||
"is_testcase_path",
|
"is_testcase_path",
|
||||||
"is_testcases",
|
"is_testcases",
|
||||||
"validate_json_file",
|
"validate_json_file",
|
||||||
|
"get_pwd",
|
||||||
"load_csv_file",
|
"load_csv_file",
|
||||||
"load_builtin_functions",
|
"load_builtin_functions",
|
||||||
"load_project_data",
|
"load_project_data",
|
||||||
|
|||||||
@@ -334,7 +334,6 @@ def load_test_file(path):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
raw_content = load_file(path)
|
raw_content = load_file(path)
|
||||||
loaded_content = None
|
|
||||||
|
|
||||||
if isinstance(raw_content, dict):
|
if isinstance(raw_content, dict):
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ $ pip install requests_toolbelt filetype
|
|||||||
|
|
||||||
from httprunner.exceptions import ParamsError
|
from httprunner.exceptions import ParamsError
|
||||||
|
|
||||||
PWD = os.getcwd()
|
|
||||||
|
|
||||||
|
|
||||||
def prepare_upload_test(test_dict):
|
def prepare_upload_test(test_dict):
|
||||||
""" preprocess for upload test
|
""" preprocess for upload test
|
||||||
@@ -78,6 +76,18 @@ def prepare_upload_test(test_dict):
|
|||||||
|
|
||||||
params_list = []
|
params_list = []
|
||||||
for key, value in upload_json.items():
|
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
|
test_dict["variables"][key] = value
|
||||||
params_list.append("{}=${}".format(key, key))
|
params_list.append("{}=${}".format(key, key))
|
||||||
|
|
||||||
@@ -101,18 +111,12 @@ def multipart_encoder(**kwargs):
|
|||||||
fields_dict = {}
|
fields_dict = {}
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
|
|
||||||
if os.path.isabs(value):
|
if os.path.isfile(value):
|
||||||
_file_path = value
|
# value is file path to upload,
|
||||||
is_file = True
|
# it has been ensured to be absolute in prepare_upload_test
|
||||||
else:
|
filename = os.path.basename(value)
|
||||||
global PWD
|
with open(value, 'rb') as f:
|
||||||
_file_path = os.path.join(PWD, value)
|
mime_type = get_filetype(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)
|
|
||||||
fields_dict[key] = (filename, f.read(), mime_type)
|
fields_dict[key] = (filename, f.read(), mime_type)
|
||||||
else:
|
else:
|
||||||
fields_dict[key] = value
|
fields_dict[key] = value
|
||||||
|
|||||||
Reference in New Issue
Block a user