From d1f9b102744bed9baba30cdc3e6f9ca074d555b6 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 7 Jun 2020 13:30:55 +0800 Subject: [PATCH] fix: handle exception for loaded invalid format test content --- httprunner/ext/har2case/utils.py | 4 +--- httprunner/make.py | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/httprunner/ext/har2case/utils.py b/httprunner/ext/har2case/utils.py index 0d9f22b2..ed200995 100644 --- a/httprunner/ext/har2case/utils.py +++ b/httprunner/ext/har2case/utils.py @@ -31,12 +31,10 @@ def load_har_log_entries(file_path): with io.open(file_path, "r+", encoding="utf-8-sig") as f: try: content_json = json.loads(f.read()) + return content_json["log"]["entries"] except (TypeError, JSONDecodeError) as ex: logger.error(f"failed to load HAR file {file_path}: {ex}") sys.exit(1) - - try: - return content_json["log"]["entries"] except KeyError: logger.error(f"log entries not found in HAR file: {content_json}") sys.exit(1) diff --git a/httprunner/make.py b/httprunner/make.py index 5ddc22b6..670901aa 100644 --- a/httprunner/make.py +++ b/httprunner/make.py @@ -1,5 +1,6 @@ import os import subprocess +import sys from shutil import copyfile from typing import Text, List, Tuple, Dict, Set, NoReturn @@ -428,12 +429,19 @@ def __make(tests_path: Text) -> NoReturn: logger.warning(ex) continue + if not isinstance(test_content, Dict): + raise exceptions.FileFormatError( + f"test content not in dict format: {test_content}" + ) + # api in v2 format, convert to v3 testcase - if "request" in test_content: + if "request" in test_content and "name" in test_content: test_content = ensure_testcase_v3_api(test_content) - if not (isinstance(test_content, Dict) and "config" in test_content): - raise exceptions.FileFormatError("Invalid testcase/testsuite v2/v3 format!") + if "config" not in test_content: + raise exceptions.FileFormatError( + f"miss config part in testcase/testsuite: {test_content}" + ) test_content.setdefault("config", {})["path"] = test_file @@ -465,7 +473,12 @@ def main_make(tests_paths: List[Text]) -> List[Text]: if not os.path.isabs(tests_path): tests_path = os.path.join(os.getcwd(), tests_path) - __make(tests_path) + try: + __make(tests_path) + except exceptions.MyBaseError as ex: + logger.error(ex) + sys.exit(1) + __ensure_project_meta_files(tests_path) # format pytest files