From 5f172960427f9009fb826bc421b7d2cf20e21515 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 14 Jun 2020 22:01:09 +0800 Subject: [PATCH] fix: compatibility with different path separators of Linux and Windows --- httprunner/compat.py | 12 ++++++++++++ httprunner/ext/har2case/__init__.py | 2 ++ httprunner/make.py | 2 ++ tests/compat_test.py | 18 +++++++++++++++++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/httprunner/compat.py b/httprunner/compat.py index 20c442f4..356946c7 100644 --- a/httprunner/compat.py +++ b/httprunner/compat.py @@ -348,3 +348,15 @@ def session_fixture(request): f.write(conftest_content) logger.info("generated conftest.py to generate summary.json") + + +def ensure_path_sep(path: Text) -> Text: + """ ensure compatibility with different path separators of Linux and Windows + """ + if "/" in path: + path = os.sep.join(path.split("/")) + + if "\\" in path: + path = os.sep.join(path.split("\\")) + + return path diff --git a/httprunner/ext/har2case/__init__.py b/httprunner/ext/har2case/__init__.py index 3e5c21c4..c1a074b4 100644 --- a/httprunner/ext/har2case/__init__.py +++ b/httprunner/ext/har2case/__init__.py @@ -14,6 +14,7 @@ import sys from loguru import logger from sentry_sdk import capture_message +from httprunner.compat import ensure_path_sep from httprunner.ext.har2case.core import HarParser @@ -59,6 +60,7 @@ def main_har2case(args): logger.error("HAR file not specified.") sys.exit(1) + har_source_file = ensure_path_sep(har_source_file) if not os.path.isfile(har_source_file): logger.error(f"HAR file not exists: {har_source_file}") sys.exit(1) diff --git a/httprunner/make.py b/httprunner/make.py index c63f5345..bcbc4ecb 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -13,6 +13,7 @@ from httprunner.compat import ( ensure_testcase_v3_api, ensure_testcase_v3, convert_variables, + ensure_path_sep, ) from httprunner.loader import ( load_folder_files, @@ -498,6 +499,7 @@ def main_make(tests_paths: List[Text]) -> List[Text]: return [] for tests_path in tests_paths: + tests_path = ensure_path_sep(tests_path) if not os.path.isabs(tests_path): tests_path = os.path.join(os.getcwd(), tests_path) diff --git a/tests/compat_test.py b/tests/compat_test.py index 49e78500..b5d45250 100644 --- a/tests/compat_test.py +++ b/tests/compat_test.py @@ -2,7 +2,7 @@ import os import unittest from httprunner import compat, exceptions, loader -from httprunner.compat import convert_variables +from httprunner.compat import convert_variables, ensure_path_sep class TestCompat(unittest.TestCase): @@ -213,3 +213,19 @@ class TestCompat(unittest.TestCase): "--self-contained-html", ], ) + + def test_ensure_file_path(self): + self.assertEqual( + ensure_path_sep("demo\\test.yml"), os.sep.join(["demo", "test.yml"]) + ) + self.assertEqual( + ensure_path_sep(os.path.join(os.getcwd(), "demo\\test.yml")), + os.path.join(os.getcwd(), os.sep.join(["demo", "test.yml"])), + ) + self.assertEqual( + ensure_path_sep("demo/test.yml"), os.sep.join(["demo", "test.yml"]) + ) + self.assertEqual( + ensure_path_sep(os.path.join(os.getcwd(), "demo/test.yml")), + os.path.join(os.getcwd(), os.sep.join(["demo", "test.yml"])), + )