From b400060aca5ca456bf0fae7decd30df3bf99d48d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 6 Jun 2020 22:13:59 +0800 Subject: [PATCH] fix: ensure generated conftest.py located in generated pytest folder --- httprunner/compat.py | 7 ++++++- httprunner/make.py | 37 +------------------------------------ httprunner/utils.py | 38 +++++++++++++++++++++++++++++++++++++- tests/compat_test.py | 4 +--- tests/make_test.py | 29 ----------------------------- tests/utils_test.py | 29 +++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 70 deletions(-) diff --git a/httprunner/compat.py b/httprunner/compat.py index dd001804..d4dd78d8 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -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) diff --git a/httprunner/make.py b/httprunner/make.py index d550faa4..ede80460 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -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) diff --git a/httprunner/utils.py b/httprunner/utils.py index 3b206eea..07aa3ffc 100644 --- a/httprunner/utils.py +++ b/httprunner/utils.py @@ -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 diff --git a/tests/compat_test.py b/tests/compat_test.py index 04b55988..53a71baa 100644 --- a/tests/compat_test.py +++ b/tests/compat_test.py @@ -153,9 +153,7 @@ class TestCompat(unittest.TestCase): compat.ensure_cli_args(args2), ["examples/postman-echo/request.methods/hardcode.yml"], ) - self.assertTrue( - os.path.isfile("examples/postman_echo/request_methods/conftest.py") - ) + self.assertTrue(os.path.isfile("examples/postman_echo/conftest.py")) args3 = [ "examples/postman-echo/request.methods/hardcode.yml", diff --git a/tests/make_test.py b/tests/make_test.py index 0cb75282..122fbde6 100644 --- a/tests/make_test.py +++ b/tests/make_test.py @@ -8,7 +8,6 @@ from httprunner.make import ( make_config_chain_style, make_teststep_chain_style, pytest_files_run_set, - ensure_file_path_valid, ) @@ -67,34 +66,6 @@ from examples.postman_echo.request_methods.request_with_functions_test import ( testcase_python_list, ) - def test_ensure_file_path_valid(self): - self.assertEqual( - ensure_file_path_valid( - "examples/postman-echo/request.methods/hardcode.yml" - ), - os.path.join( - os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml" - ), - ) - self.assertEqual( - ensure_file_path_valid( - os.path.join(os.getcwd(), "postman-echo/request.methods/hardcode.yml") - ), - os.path.join(os.getcwd(), "postman_echo/request_methods/hardcode.yml"), - ) - self.assertEqual( - ensure_file_path_valid( - "examples/postman echo/request methods/hardcode.yml" - ), - os.path.join( - os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml" - ), - ) - self.assertEqual( - ensure_file_path_valid("1/2B/3.yml"), - os.path.join(os.getcwd(), "T1/T2B/T3.yml"), - ) - def test_convert_testcase_path(self): self.assertEqual( convert_testcase_path("mubu.login.yml"), diff --git a/tests/utils_test.py b/tests/utils_test.py index cfb86e85..8a584054 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -2,6 +2,7 @@ import os import unittest from httprunner import loader, utils +from httprunner.utils import ensure_file_path_valid class TestUtils(unittest.TestCase): @@ -97,3 +98,31 @@ class TestUtils(unittest.TestCase): ), ["A", "D", "C", "B"], ) + + def test_ensure_file_path_valid(self): + self.assertEqual( + ensure_file_path_valid( + "examples/postman-echo/request.methods/hardcode.yml" + ), + os.path.join( + os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml" + ), + ) + self.assertEqual( + ensure_file_path_valid( + os.path.join(os.getcwd(), "postman-echo/request.methods/hardcode.yml") + ), + os.path.join(os.getcwd(), "postman_echo/request_methods/hardcode.yml"), + ) + self.assertEqual( + ensure_file_path_valid( + "examples/postman echo/request methods/hardcode.yml" + ), + os.path.join( + os.getcwd(), "examples/postman_echo/request_methods/hardcode.yml" + ), + ) + self.assertEqual( + ensure_file_path_valid("1/2B/3.yml"), + os.path.join(os.getcwd(), "T1/T2B/T3.yml"), + )