fix: ensure project meta files exist in generated pytest folder files

This commit is contained in:
debugtalk
2020-06-06 09:38:00 +08:00
parent 91a8bcc78c
commit 1ecebd540b
3 changed files with 31 additions and 7 deletions

View File

@@ -416,7 +416,10 @@ def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta:
# environment variable maybe loaded in debugtalk.py # environment variable maybe loaded in debugtalk.py
# thus .env file should be loaded before loading debugtalk.py # thus .env file should be loaded before loading debugtalk.py
dot_env_path = os.path.join(project_working_directory, ".env") dot_env_path = os.path.join(project_working_directory, ".env")
project_meta.env = load_dot_env_file(dot_env_path) dot_env = load_dot_env_file(dot_env_path)
if dot_env:
project_meta.env = dot_env
project_meta.dot_env_path = dot_env_path
if debugtalk_path: if debugtalk_path:
# load debugtalk.py functions # load debugtalk.py functions

View File

@@ -1,6 +1,7 @@
import os import os
import string import string
import subprocess import subprocess
from shutil import copyfile
from typing import Text, List, Tuple, Dict, Set, NoReturn from typing import Text, List, Tuple, Dict, Set, NoReturn
import jinja2 import jinja2
@@ -97,6 +98,28 @@ def __ensure_testcase_module(path: Text) -> NoReturn:
f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!\n") f.write("# NOTICE: Generated By HttpRunner. DO NOT EDIT!\n")
def __ensure_project_meta_files(tests_path: Text) -> NoReturn:
""" ensure project meta files exist in generated pytest folder files
include debugtalk.py and .env
"""
project_meta = load_project_meta(tests_path)
# handle cases when generated pytest directory are different from original yaml/json testcases
debugtalk_path = project_meta.debugtalk_path
if debugtalk_path:
debugtalk_new_path = ensure_file_path_valid(debugtalk_path)
if debugtalk_new_path != debugtalk_path:
logger.info(f"copy debugtalk.py to {debugtalk_new_path}")
copyfile(debugtalk_path, debugtalk_new_path)
dot_csv_path = project_meta.dot_env_path
if dot_csv_path:
dot_csv_new_path = ensure_file_path_valid(dot_csv_path)
if dot_csv_new_path != dot_csv_path:
logger.info(f"copy .env to {dot_csv_new_path}")
copyfile(dot_csv_path, dot_csv_new_path)
def ensure_file_path_valid(file_path: Text) -> Text: def ensure_file_path_valid(file_path: Text) -> Text:
""" ensure file path valid for pytest """ ensure file path valid for pytest
@@ -108,12 +131,7 @@ def ensure_file_path_valid(file_path: Text) -> Text:
""" """
raw_file_name, file_suffix = os.path.splitext(file_path) raw_file_name, file_suffix = os.path.splitext(file_path)
file_suffix = file_suffix.lower() file_suffix = file_suffix.lower()
if file_suffix not in [".json", ".yml", ".yaml", ".har"]:
raise exceptions.ParamsError(
"testcase/testsuite file should have .yaml/.yml/.json/.har suffix"
)
if os.path.isabs(file_path): if os.path.isabs(file_path):
raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :] raw_file_relative_name = raw_file_name[len(os.getcwd()) + 1 :]
@@ -349,7 +367,8 @@ def make_testcase(testcase: Dict, dir_path: Text = None) -> Text:
# ensure new file's directory exists # ensure new file's directory exists
dir_path = os.path.dirname(testcase_python_path) dir_path = os.path.dirname(testcase_python_path)
os.makedirs(dir_path, exist_ok=True) if not os.path.exists(dir_path):
os.makedirs(dir_path)
with open(testcase_python_path, "w", encoding="utf-8") as f: with open(testcase_python_path, "w", encoding="utf-8") as f:
f.write(content) f.write(content)
@@ -479,6 +498,7 @@ def main_make(tests_paths: List[Text]) -> List[Text]:
tests_path = os.path.join(os.getcwd(), tests_path) tests_path = os.path.join(os.getcwd(), tests_path)
__make(tests_path) __make(tests_path)
__ensure_project_meta_files(tests_path)
# format pytest files # format pytest files
pytest_files_format_list = pytest_files_made_cache_mapping.keys() pytest_files_format_list = pytest_files_made_cache_mapping.keys()

View File

@@ -82,6 +82,7 @@ class TestCase(BaseModel):
class ProjectMeta(BaseModel): class ProjectMeta(BaseModel):
debugtalk_py: Text = "" # debugtalk.py file content debugtalk_py: Text = "" # debugtalk.py file content
debugtalk_path: Text = "" # debugtalk.py file path debugtalk_path: Text = "" # debugtalk.py file path
dot_env_path: Text = "" # .env file path
functions: FunctionsMapping = {} # functions defined in debugtalk.py functions: FunctionsMapping = {} # functions defined in debugtalk.py
env: Env = {} env: Env = {}
PWD: Text = os.getcwd() # project working directory, the path debugtalk.py located PWD: Text = os.getcwd() # project working directory, the path debugtalk.py located