From 3e8bd4b6f0692849606235ef78224f122bf4507e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 19 Jun 2020 15:46:38 +0800 Subject: [PATCH] change: rename ensure_file_abs_path_valid --- httprunner/compat.py | 4 ++-- httprunner/loader.py | 1 + httprunner/make.py | 11 ++++++----- httprunner/models.py | 2 +- httprunner/utils.py | 13 +++++-------- tests/utils_test.py | 22 +++++++++++----------- 6 files changed, 26 insertions(+), 27 deletions(-) diff --git a/httprunner/compat.py b/httprunner/compat.py index e899e3cc..594f6470 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -10,7 +10,7 @@ from loguru import logger from httprunner import exceptions from httprunner.loader import load_project_meta from httprunner.parser import parse_data -from httprunner.utils import sort_dict_by_custom_order, ensure_file_path_valid +from httprunner.utils import sort_dict_by_custom_order, ensure_file_abs_path_valid def convert_variables( @@ -260,7 +260,7 @@ def generate_conftest_for_summary(args: List): sys.exit(1) project_meta = load_project_meta(test_path) - project_root_dir = ensure_file_path_valid(project_meta.RootDir) + project_root_dir = ensure_file_abs_path_valid(project_meta.RootDir) conftest_path = os.path.join(project_root_dir, "conftest.py") conftest_content = '''# NOTICE: Generated By HttpRunner. import json diff --git a/httprunner/loader.py b/httprunner/loader.py index cc6799c0..c1cbe835 100644 --- a/httprunner/loader.py +++ b/httprunner/loader.py @@ -287,6 +287,7 @@ def locate_file(start_path: Text, file_name: Text) -> Text: file_path = os.path.join(start_dir_path, file_name) if os.path.isfile(file_path): + # ensure absolute return os.path.abspath(file_path) # system root dir diff --git a/httprunner/make.py b/httprunner/make.py index af9b1337..f3d39428 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -23,7 +23,7 @@ from httprunner.loader import ( load_project_meta, ) from httprunner.response import uniform_validator -from httprunner.utils import ensure_file_path_valid, override_config_variables +from httprunner.utils import ensure_file_abs_path_valid, override_config_variables """ cache converted pytest files, avoid duplicate making """ @@ -121,7 +121,7 @@ def __ensure_project_meta_files(tests_path: Text) -> NoReturn: # 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) + debugtalk_new_path = ensure_file_abs_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) @@ -131,7 +131,7 @@ def __ensure_project_meta_files(tests_path: Text) -> NoReturn: dot_csv_path = project_meta.dot_env_path if dot_csv_path: - dot_csv_new_path = ensure_file_path_valid(dot_csv_path) + dot_csv_new_path = ensure_file_abs_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) @@ -139,7 +139,7 @@ def __ensure_project_meta_files(tests_path: Text) -> NoReturn: 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) + testcase_new_path = ensure_file_abs_path_valid(testcase_path) dir_path = os.path.dirname(testcase_new_path) file_name, _ = os.path.splitext(os.path.basename(testcase_new_path)) @@ -410,7 +410,7 @@ 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_path = ensure_file_path_valid(testsuite_path) + testsuite_path = ensure_file_abs_path_valid(testsuite_path) testsuite_dir, file_suffix = os.path.splitext(testsuite_path) # demo_testsuite.yml => demo_testsuite_yml testsuite_dir = f"{testsuite_dir}_{file_suffix.lstrip('.')}" @@ -497,6 +497,7 @@ def __make(tests_path: Text) -> NoReturn: ) continue + # ensure path absolute test_content.setdefault("config", {})["path"] = test_file # testcase diff --git a/httprunner/models.py b/httprunner/models.py index edb2a559..fa9d492d 100644 --- a/httprunner/models.py +++ b/httprunner/models.py @@ -86,7 +86,7 @@ class ProjectMeta(BaseModel): dot_env_path: Text = "" # .env file path functions: FunctionsMapping = {} # functions defined in debugtalk.py env: Env = {} - RootDir: Text = os.getcwd() # project root directory, the path debugtalk.py located + RootDir: Text = os.getcwd() # project root directory (ensure absolute), the path debugtalk.py located class TestsMapping(BaseModel): diff --git a/httprunner/utils.py b/httprunner/utils.py index b2d0e3d7..0b22e049 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -181,26 +181,23 @@ def sort_dict_by_custom_order(raw_dict: Dict, custom_order: List): ) -def ensure_file_path_valid(file_path: Text) -> Text: +def ensure_file_abs_path_valid(file_abs_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 + file_abs_path: absolute file path Returns: ensured valid absolute file path """ - raw_file_name, file_suffix = os.path.splitext(file_path) + raw_abs_file_name, file_suffix = os.path.splitext(file_abs_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 + raw_file_relative_name = raw_abs_file_name[len(os.getcwd()) + 1 :] if raw_file_relative_name == "": - return file_path + return file_abs_path path_names = [] for name in raw_file_relative_name.rstrip(os.sep).split(os.sep): diff --git a/tests/utils_test.py b/tests/utils_test.py index bf81cfcc..bd4e3f53 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -5,7 +5,7 @@ import unittest from httprunner import loader, utils from httprunner.utils import ( - ensure_file_path_valid, + ensure_file_abs_path_valid, ExtendJSONEncoder, override_config_variables, ) @@ -107,36 +107,36 @@ class TestUtils(unittest.TestCase): def test_ensure_file_path_valid(self): self.assertEqual( - ensure_file_path_valid( - os.path.join("examples", "a-b.c", "d f", "hardcode.yml") + ensure_file_abs_path_valid( + os.path.join(os.getcwd(), "examples", "a-b.c", "d f", "hardcode.yml") ), os.path.join(os.getcwd(), "examples", "a_b_c", "d_f", "hardcode.yml"), ) self.assertEqual( - ensure_file_path_valid(os.path.join("1", "2B", "3.yml")), + ensure_file_abs_path_valid(os.path.join(os.getcwd(), "1", "2B", "3.yml")), os.path.join(os.getcwd(), "T1", "T2B", "T3.yml"), ) self.assertEqual( - ensure_file_path_valid( - os.path.join("examples", "a-b.c", "2B", "hardcode.yml") + ensure_file_abs_path_valid( + os.path.join(os.getcwd(), "examples", "a-b.c", "2B", "hardcode.yml") ), os.path.join(os.getcwd(), "examples", "a_b_c", "T2B", "hardcode.yml"), ) self.assertEqual( - ensure_file_path_valid( - os.path.join("examples", "postman_echo", "request_methods") + ensure_file_abs_path_valid( + os.path.join(os.getcwd(), "examples", "postman_echo", "request_methods") ), os.path.join(os.getcwd(), "examples", "postman_echo", "request_methods"), ) self.assertEqual( - ensure_file_path_valid(os.path.join(os.getcwd(), "test.yml")), + ensure_file_abs_path_valid(os.path.join(os.getcwd(), "test.yml")), os.path.join(os.getcwd(), "test.yml"), ) self.assertEqual( - ensure_file_path_valid(os.getcwd()), os.getcwd(), + ensure_file_abs_path_valid(os.getcwd()), os.getcwd(), ) self.assertEqual( - ensure_file_path_valid(os.path.join(os.getcwd(), "demo", ".csv")), + ensure_file_abs_path_valid(os.path.join(os.getcwd(), "demo", ".csv")), os.path.join(os.getcwd(), "demo", ".csv"), )