mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-20 20:11:58 +08:00
refactor: remove unused code
This commit is contained in:
@@ -1,15 +1,11 @@
|
||||
# encoding: utf-8
|
||||
|
||||
import collections
|
||||
import copy
|
||||
import io
|
||||
import itertools
|
||||
import json
|
||||
import os.path
|
||||
from typing import Union
|
||||
import platform
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from httprunner import __version__
|
||||
from httprunner import exceptions
|
||||
|
||||
|
||||
@@ -83,42 +79,6 @@ def lower_dict_keys(origin_dict):
|
||||
return {key.lower(): value for key, value in origin_dict.items()}
|
||||
|
||||
|
||||
def deepcopy_dict(data):
|
||||
""" deepcopy dict data, ignore file object (_io.BufferedReader)
|
||||
|
||||
Args:
|
||||
data (dict): dict data structure
|
||||
{
|
||||
'a': 1,
|
||||
'b': [2, 4],
|
||||
'c': lambda x: x+1,
|
||||
'd': open('LICENSE'),
|
||||
'f': {
|
||||
'f1': {'a1': 2},
|
||||
'f2': io.open('LICENSE', 'rb'),
|
||||
}
|
||||
}
|
||||
|
||||
Returns:
|
||||
dict: deep copied dict data, with file object unchanged.
|
||||
|
||||
"""
|
||||
try:
|
||||
return copy.deepcopy(data)
|
||||
except TypeError:
|
||||
copied_data = {}
|
||||
for key, value in data.items():
|
||||
if isinstance(value, dict):
|
||||
copied_data[key] = deepcopy_dict(value)
|
||||
else:
|
||||
try:
|
||||
copied_data[key] = copy.deepcopy(value)
|
||||
except TypeError:
|
||||
copied_data[key] = value
|
||||
|
||||
return copied_data
|
||||
|
||||
|
||||
def print_info(info_mapping):
|
||||
""" print info in mapping.
|
||||
|
||||
@@ -164,47 +124,6 @@ def print_info(info_mapping):
|
||||
logger.info(content)
|
||||
|
||||
|
||||
def gen_cartesian_product(*args):
|
||||
""" generate cartesian product for lists
|
||||
|
||||
Args:
|
||||
args (list of list): lists to be generated with cartesian product
|
||||
|
||||
Returns:
|
||||
list: cartesian product in list
|
||||
|
||||
Examples:
|
||||
|
||||
>>> arg1 = [{"a": 1}, {"a": 2}]
|
||||
>>> arg2 = [{"x": 111, "y": 112}, {"x": 121, "y": 122}]
|
||||
>>> args = [arg1, arg2]
|
||||
>>> gen_cartesian_product(*args)
|
||||
>>> # same as below
|
||||
>>> gen_cartesian_product(arg1, arg2)
|
||||
[
|
||||
{'a': 1, 'x': 111, 'y': 112},
|
||||
{'a': 1, 'x': 121, 'y': 122},
|
||||
{'a': 2, 'x': 111, 'y': 112},
|
||||
{'a': 2, 'x': 121, 'y': 122}
|
||||
]
|
||||
|
||||
"""
|
||||
if not args:
|
||||
return []
|
||||
elif len(args) == 1:
|
||||
return args[0]
|
||||
|
||||
product_list = []
|
||||
for product_item_tuple in itertools.product(*args):
|
||||
product_item_dict = {}
|
||||
for item in product_item_tuple:
|
||||
product_item_dict.update(item)
|
||||
|
||||
product_list.append(product_item_dict)
|
||||
|
||||
return product_list
|
||||
|
||||
|
||||
def omit_long_data(body, omit_len=512):
|
||||
""" omit too long str/bytes
|
||||
"""
|
||||
@@ -224,64 +143,11 @@ def omit_long_data(body, omit_len=512):
|
||||
return omitted_body + appendix_str
|
||||
|
||||
|
||||
def dump_json_file(json_data: Union[dict, list], json_file_abs_path: str) -> None:
|
||||
""" dump json data to file
|
||||
"""
|
||||
|
||||
class PythonObjectEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
try:
|
||||
return super().default(self, obj)
|
||||
except TypeError:
|
||||
return str(obj)
|
||||
|
||||
file_foder_path = os.path.dirname(json_file_abs_path)
|
||||
if not os.path.isdir(file_foder_path):
|
||||
os.makedirs(file_foder_path)
|
||||
|
||||
try:
|
||||
with io.open(json_file_abs_path, "w", encoding="utf-8") as outfile:
|
||||
json.dump(
|
||||
json_data,
|
||||
outfile,
|
||||
indent=4,
|
||||
separators=(",", ":"),
|
||||
ensure_ascii=False,
|
||||
cls=PythonObjectEncoder,
|
||||
)
|
||||
|
||||
msg = f"dump file: {json_file_abs_path}"
|
||||
logger.info(msg)
|
||||
|
||||
except TypeError as ex:
|
||||
msg = f"Failed to dump json file: {json_file_abs_path}\nReason: {ex}"
|
||||
logger.error(msg)
|
||||
|
||||
|
||||
def prepare_log_file_abs_path(test_path: str, file_name: str) -> str:
|
||||
""" prepare dump json file absolute path.
|
||||
"""
|
||||
current_working_dir = os.getcwd()
|
||||
|
||||
if not test_path:
|
||||
# running passed in testcase/testsuite data structure
|
||||
dump_file_name = f"tests_mapping.{file_name}"
|
||||
dumped_json_file_abs_path = os.path.join(
|
||||
current_working_dir, "logs", dump_file_name
|
||||
)
|
||||
return dumped_json_file_abs_path
|
||||
|
||||
# both test_path and pwd_dir_path are absolute path
|
||||
logs_dir_path = os.path.join(current_working_dir, "logs")
|
||||
|
||||
if os.path.isdir(test_path):
|
||||
file_foder_path = os.path.join(logs_dir_path, test_path)
|
||||
dump_file_name = f"all.{file_name}"
|
||||
else:
|
||||
file_relative_folder_path, test_file = os.path.split(test_path)
|
||||
file_foder_path = os.path.join(logs_dir_path, file_relative_folder_path)
|
||||
test_file_name, _file_suffix = os.path.splitext(test_file)
|
||||
dump_file_name = f"{test_file_name}.{file_name}"
|
||||
|
||||
dumped_json_file_abs_path = os.path.join(file_foder_path, dump_file_name)
|
||||
return dumped_json_file_abs_path
|
||||
def get_platform():
|
||||
return {
|
||||
"httprunner_version": __version__,
|
||||
"python_version": "{} {}".format(
|
||||
platform.python_implementation(), platform.python_version()
|
||||
),
|
||||
"platform": platform.platform(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user