mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-10 02:09:51 +08:00
fix: ensure generated conftest.py located in generated pytest folder
This commit is contained in:
@@ -9,7 +9,7 @@ from loguru import logger
|
||||
|
||||
from httprunner import exceptions
|
||||
from httprunner.loader import load_project_meta
|
||||
from httprunner.utils import sort_dict_by_custom_order
|
||||
from httprunner.utils import sort_dict_by_custom_order, ensure_file_path_valid
|
||||
|
||||
|
||||
def convert_jmespath(raw: Text) -> Text:
|
||||
@@ -221,6 +221,7 @@ def generate_conftest_for_summary(args: List):
|
||||
|
||||
project_meta = load_project_meta(test_path)
|
||||
conftest_path = os.path.join(project_meta.RootDir, "conftest.py")
|
||||
conftest_path = ensure_file_path_valid(conftest_path)
|
||||
if os.path.isfile(conftest_path):
|
||||
return
|
||||
|
||||
@@ -308,6 +309,10 @@ def session_fixture(request):
|
||||
"{{SUMMARY_PATH_PLACEHOLDER}}", summary_path
|
||||
)
|
||||
|
||||
dir_path = os.path.dirname(conftest_path)
|
||||
if not os.path.exists(dir_path):
|
||||
os.makedirs(dir_path)
|
||||
|
||||
with open(conftest_path, "w", encoding="utf-8") as f:
|
||||
f.write(conftest_content)
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
import string
|
||||
import subprocess
|
||||
from shutil import copyfile
|
||||
from typing import Text, List, Tuple, Dict, Set, NoReturn
|
||||
@@ -19,6 +18,7 @@ from httprunner.loader import (
|
||||
)
|
||||
from httprunner.parser import parse_data
|
||||
from httprunner.response import uniform_validator
|
||||
from httprunner.utils import ensure_file_path_valid
|
||||
|
||||
""" cache converted pytest files, avoid duplicate making
|
||||
"""
|
||||
@@ -123,41 +123,6 @@ def __ensure_project_meta_files(tests_path: Text) -> NoReturn:
|
||||
copyfile(dot_csv_path, dot_csv_new_path)
|
||||
|
||||
|
||||
def ensure_file_path_valid(file_path: Text) -> Text:
|
||||
""" ensure file path valid for pytest
|
||||
|
||||
Args:
|
||||
file_path: absolute or relative file path
|
||||
|
||||
Returns:
|
||||
ensured valid absolute file path
|
||||
|
||||
"""
|
||||
raw_file_name, file_suffix = os.path.splitext(file_path)
|
||||
file_suffix = file_suffix.lower()
|
||||
|
||||
if os.path.isabs(file_path):
|
||||
raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :]
|
||||
else:
|
||||
raw_file_relative_name = raw_file_name
|
||||
|
||||
path_names = []
|
||||
for name in raw_file_relative_name.split(os.sep):
|
||||
|
||||
if name[0] in string.digits:
|
||||
# ensure file name not startswith digit
|
||||
# 19 => T19, 2C => T2C
|
||||
name = f"T{name}"
|
||||
|
||||
# handle cases when directory name includes dot/hyphen/space
|
||||
name = name.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
|
||||
path_names.append(name)
|
||||
|
||||
new_file_path = os.path.join(os.getcwd(), f"{os.sep.join(path_names)}{file_suffix}")
|
||||
return new_file_path
|
||||
|
||||
|
||||
def convert_testcase_path(testcase_path: Text) -> Tuple[Text, Text]:
|
||||
"""convert single YAML/JSON testcase path to python file"""
|
||||
testcase_new_path = ensure_file_path_valid(testcase_path)
|
||||
|
||||
@@ -2,8 +2,9 @@ import collections
|
||||
import json
|
||||
import os.path
|
||||
import platform
|
||||
import string
|
||||
import uuid
|
||||
from typing import Dict, List, Any
|
||||
from typing import Dict, List, Any, Text
|
||||
|
||||
import sentry_sdk
|
||||
from loguru import logger
|
||||
@@ -176,3 +177,38 @@ def sort_dict_by_custom_order(raw_dict: Dict, custom_order: List):
|
||||
return dict(
|
||||
sorted(raw_dict.items(), key=lambda i: get_index_from_list(custom_order, i[0]))
|
||||
)
|
||||
|
||||
|
||||
def ensure_file_path_valid(file_path: Text) -> Text:
|
||||
""" ensure file path valid for pytest, handle cases when directory name includes dot/hyphen/space
|
||||
|
||||
Args:
|
||||
file_path: absolute or relative file path
|
||||
|
||||
Returns:
|
||||
ensured valid absolute file path
|
||||
|
||||
"""
|
||||
raw_file_name, file_suffix = os.path.splitext(file_path)
|
||||
file_suffix = file_suffix.lower()
|
||||
|
||||
if os.path.isabs(file_path):
|
||||
raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :]
|
||||
else:
|
||||
raw_file_relative_name = raw_file_name
|
||||
|
||||
path_names = []
|
||||
for name in raw_file_relative_name.split(os.sep):
|
||||
|
||||
if name[0] in string.digits:
|
||||
# ensure file name not startswith digit
|
||||
# 19 => T19, 2C => T2C
|
||||
name = f"T{name}"
|
||||
|
||||
# handle cases when directory name includes dot/hyphen/space
|
||||
name = name.replace(" ", "_").replace(".", "_").replace("-", "_")
|
||||
|
||||
path_names.append(name)
|
||||
|
||||
new_file_path = os.path.join(os.getcwd(), f"{os.sep.join(path_names)}{file_suffix}")
|
||||
return new_file_path
|
||||
|
||||
Reference in New Issue
Block a user