mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-03 06:49:38 +08:00
fix: handle cases when parent directory name includes dot/hyphen/space
This commit is contained in:
@@ -108,6 +108,17 @@ def __ensure_testcase_module(path: Text) -> NoReturn:
|
||||
f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!\n")
|
||||
|
||||
|
||||
def __ensure_dir_path_valid(dir_path: Text) -> Text:
|
||||
# handle cases when parent directory name includes dot/hyphen/space
|
||||
relative_dir_path = dir_path[len(os.getcwd()) + 1:]
|
||||
relative_dir_path = (
|
||||
relative_dir_path.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
)
|
||||
dir_path = os.path.join(os.getcwd(), relative_dir_path)
|
||||
os.makedirs(dir_path, exist_ok=True)
|
||||
return dir_path
|
||||
|
||||
|
||||
def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
|
||||
"""convert single YAML/JSON testcase path to python file"""
|
||||
testcase_path = __ensure_file_name(testcase_path)
|
||||
@@ -121,6 +132,7 @@ def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
|
||||
|
||||
file_name = raw_file_name.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
testcase_dir = os.path.dirname(testcase_path)
|
||||
testcase_dir = __ensure_dir_path_valid(testcase_dir)
|
||||
testcase_python_path = os.path.join(testcase_dir, f"{file_name}_test.py")
|
||||
|
||||
# convert title case, e.g. request_with_variables => RequestWithVariables
|
||||
@@ -325,9 +337,6 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
|
||||
}
|
||||
content = __TEMPLATE__.render(data)
|
||||
|
||||
testcase_python_dir = os.path.dirname(testcase_python_path)
|
||||
if not os.path.exists(testcase_python_dir):
|
||||
os.makedirs(testcase_python_dir)
|
||||
with open(testcase_python_path, "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
|
||||
@@ -345,7 +354,7 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
|
||||
load_testsuite(testsuite)
|
||||
|
||||
testsuite_config = testsuite["config"]
|
||||
testsuite_path = testsuite_config["path"]
|
||||
testsuite_path = __ensure_absolute(testsuite_config["path"])
|
||||
|
||||
testsuite_variables = testsuite_config.get("variables", {})
|
||||
if isinstance(testsuite_variables, Text):
|
||||
@@ -358,11 +367,15 @@ def make_testsuite(testsuite: Dict) -> NoReturn:
|
||||
logger.info(f"start to make testsuite: {testsuite_path}")
|
||||
|
||||
# create directory with testsuite file name, put its testcases under this directory
|
||||
testsuite_dir = os.path.join(
|
||||
os.path.dirname(testsuite_path),
|
||||
os.path.basename(testsuite_path).replace(".", "_"),
|
||||
)
|
||||
os.makedirs(testsuite_dir, exist_ok=True)
|
||||
testsuite_file_name, file_suffix = os.path.splitext(testsuite_path)
|
||||
|
||||
file_suffix = file_suffix.lower()
|
||||
if file_suffix not in [".json", ".yml", ".yaml", ".har"]:
|
||||
raise exceptions.ParamsError(
|
||||
"testsuite file should have .yaml/.yml/.json suffix"
|
||||
)
|
||||
|
||||
testsuite_dir = __ensure_dir_path_valid(testsuite_file_name)
|
||||
|
||||
for testcase in testsuite["testcases"]:
|
||||
# get referenced testcase content
|
||||
|
||||
Reference in New Issue
Block a user