From e2703fff32df68c14708adb1d1bc1cb18b28870f Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 6 Jun 2020 21:17:00 +0800 Subject: [PATCH] change: rename PWD to project RootDir --- httprunner/compat.py | 6 ++--- httprunner/ext/uploader/__init__.py | 2 +- httprunner/loader.py | 40 ++++++++++++++--------------- httprunner/make.py | 2 +- httprunner/models.py | 2 +- httprunner/runner.py | 6 +++-- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/httprunner/compat.py b/httprunner/compat.py index f907c113..dd001804 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -220,7 +220,7 @@ def generate_conftest_for_summary(args: List): sys.exit(1) project_meta = load_project_meta(test_path) - conftest_path = os.path.join(project_meta.PWD, "conftest.py") + conftest_path = os.path.join(project_meta.RootDir, "conftest.py") if os.path.isfile(conftest_path): return @@ -291,8 +291,8 @@ def session_fixture(request): ''' test_path = os.path.abspath(test_path) - logs_dir_path = os.path.join(project_meta.PWD, "logs") - test_path_relative_path = test_path[len(project_meta.PWD) + 1 :] + logs_dir_path = os.path.join(project_meta.RootDir, "logs") + test_path_relative_path = test_path[len(project_meta.RootDir) + 1 :] if os.path.isdir(test_path): file_foder_path = os.path.join(logs_dir_path, test_path_relative_path) diff --git a/httprunner/ext/uploader/__init__.py b/httprunner/ext/uploader/__init__.py index 8768f8b2..4b242502 100644 --- a/httprunner/ext/uploader/__init__.py +++ b/httprunner/ext/uploader/__init__.py @@ -139,7 +139,7 @@ def multipart_encoder(**kwargs): project_meta = load_project_meta(os.getcwd()) - _file_path = os.path.join(project_meta.PWD, value) + _file_path = os.path.join(project_meta.RootDir, value) is_exists_file = os.path.isfile(_file_path) if is_exists_file: diff --git a/httprunner/loader.py b/httprunner/loader.py index b0f1dc96..ff37c15c 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -176,7 +176,7 @@ def load_csv_file(csv_file: Text) -> List[Dict]: raise exceptions.MyBaseFailure("load_project_meta() has not been called!") # make compatible with Windows/Linux - csv_file = os.path.join(project_meta.PWD, *csv_file.split("/")) + csv_file = os.path.join(project_meta.RootDir, *csv_file.split("/")) if not os.path.isfile(csv_file): # file path not exist @@ -327,14 +327,14 @@ def locate_debugtalk_py(start_path: Text) -> Text: return debugtalk_path -def locate_project_working_directory(test_path: Text) -> Tuple[Text, Text]: - """ locate debugtalk.py path as project working directory +def locate_project_root_directory(test_path: Text) -> Tuple[Text, Text]: + """ locate debugtalk.py path as project root directory Args: test_path: specified testfile path Returns: - (str, str): debugtalk.py path, project_working_directory + (str, str): debugtalk.py path, project_root_directory """ @@ -355,18 +355,18 @@ def locate_project_working_directory(test_path: Text) -> Tuple[Text, Text]: debugtalk_path = locate_debugtalk_py(test_path) if debugtalk_path: - # The folder contains debugtalk.py will be treated as PWD. - project_working_directory = os.path.dirname(debugtalk_path) + # The folder contains debugtalk.py will be treated as project RootDir. + project_root_directory = os.path.dirname(debugtalk_path) else: - # debugtalk.py not found, use os.getcwd() as PWD. - project_working_directory = os.getcwd() + # debugtalk.py not found, use os.getcwd() as project RootDir. + project_root_directory = os.getcwd() - return debugtalk_path, project_working_directory + return debugtalk_path, project_root_directory def load_debugtalk_functions() -> Dict[Text, Callable]: """ load project debugtalk.py module functions - debugtalk.py should be located in project working directory. + debugtalk.py should be located in project root directory. Returns: dict: debugtalk module functions mapping @@ -382,12 +382,12 @@ def load_debugtalk_functions() -> Dict[Text, Callable]: def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta: - """ load api, testcases, .env, debugtalk.py functions. - api/testcases folder is relative to project_working_directory + """ load testcases, .env, debugtalk.py functions. + testcases folder is relative to project_root_directory by default, project_meta will be loaded only once, unless set reload to true. Args: - test_path (str): test file/folder path, locate pwd from this path. + test_path (str): test file/folder path, locate project RootDir from this path. reload: reload project meta if set true, default to false Returns: @@ -404,18 +404,16 @@ def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta: if not test_path: return project_meta - debugtalk_path, project_working_directory = locate_project_working_directory( - test_path - ) + debugtalk_path, project_root_directory = locate_project_root_directory(test_path) - # add PWD to sys.path - sys.path.insert(0, project_working_directory) + # add project RootDir to sys.path + sys.path.insert(0, project_root_directory) # load .env file # NOTICE: # 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") + dot_env_path = os.path.join(project_root_directory, ".env") dot_env = load_dot_env_file(dot_env_path) if dot_env: project_meta.env = dot_env @@ -427,8 +425,8 @@ def load_project_meta(test_path: Text, reload: bool = False) -> ProjectMeta: else: debugtalk_functions = {} - # locate PWD and load debugtalk.py functions - project_meta.PWD = project_working_directory + # locate project RootDir and load debugtalk.py functions + project_meta.RootDir = project_root_directory project_meta.functions = debugtalk_functions project_meta.debugtalk_path = debugtalk_path diff --git a/httprunner/make.py b/httprunner/make.py index 83d5eef8..d550faa4 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -64,7 +64,7 @@ def __ensure_absolute(path: Text) -> Text: if os.path.isabs(path): absolute_path = path else: - absolute_path = os.path.join(project_meta.PWD, path) + absolute_path = os.path.join(project_meta.RootDir, path) if not os.path.isfile(absolute_path): raise exceptions.ParamsError(f"Invalid testcase file path: {absolute_path}") diff --git a/httprunner/models.py b/httprunner/models.py index 59fbfcca..dcfd40f2 100644 --- a/httprunner/models.py +++ b/httprunner/models.py @@ -85,7 +85,7 @@ class ProjectMeta(BaseModel): 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 + RootDir: Text = os.getcwd() # project root directory, the path debugtalk.py located class TestsMapping(BaseModel): diff --git a/httprunner/runner.py b/httprunner/runner.py index d135a8f7..c050deea 100644 --- a/httprunner/runner.py +++ b/httprunner/runner.py @@ -187,7 +187,9 @@ class HttpRunner(object): if os.path.isabs(step.testcase): ref_testcase_path = step.testcase else: - ref_testcase_path = os.path.join(self.__project_meta.PWD, step.testcase) + ref_testcase_path = os.path.join( + self.__project_meta.RootDir, step.testcase + ) case_result = ( HttpRunner() @@ -349,7 +351,7 @@ class HttpRunner(object): ) self.__case_id = self.__case_id or str(uuid.uuid4()) self.__log_path = self.__log_path or os.path.join( - self.__project_meta.PWD, "logs", f"{self.__case_id}.run.log" + self.__project_meta.RootDir, "logs", f"{self.__case_id}.run.log" ) log_handler = logger.add(self.__log_path, level="DEBUG")