From 9a4ee8f3a04399022b72403cadd6c60c40caf498 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 6 Jun 2020 09:38:00 +0800 Subject: [PATCH] fix: ensure project meta files exist in generated pytest folder files --- httprunner/loader.py | 5 ++++- httprunner/make.py | 32 ++++++++++++++++++++++++++------ httprunner/models.py | 1 + 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/httprunner/loader.py b/httprunner/loader.py index a7a9fabd..b0f1dc96 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -416,7 +416,10 @@ def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta: # environment variable maybe loaded in debugtalk.py # thus .env file should be loaded before loading debugtalk.py 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: # load debugtalk.py functions diff --git a/httprunner/make.py b/httprunner/make.py index 53f70d48..82faf410 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -1,6 +1,7 @@ import os import string import subprocess +from shutil import copyfile from typing import Text, List, Tuple, Dict, Set, NoReturn import jinja2 @@ -97,6 +98,28 @@ def __ensure_testcase_module(path: Text) -> NoReturn: 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: """ 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) - 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): 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 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: 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) __make(tests_path) + __ensure_project_meta_files(tests_path) # format pytest files pytest_files_format_list = pytest_files_made_cache_mapping.keys() diff --git a/httprunner/models.py b/httprunner/models.py index cac1a32c..59fbfcca 100644 --- a/httprunner/models.py +++ b/httprunner/models.py @@ -82,6 +82,7 @@ class TestCase(BaseModel): class ProjectMeta(BaseModel): debugtalk_py: Text = "" # debugtalk.py file content debugtalk_path: Text = "" # debugtalk.py file path + dot_env_path: Text = "" # .env file path functions: FunctionsMapping = {} # functions defined in debugtalk.py env: Env = {} PWD: Text = os.getcwd() # project working directory, the path debugtalk.py located