From 1eff29f446a47fc869242c593f894195fa8d61ad Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Thu, 13 Mar 2025 20:17:49 +0800 Subject: [PATCH] test: add exception trapping and printing info --- tests/army/frame/caseBase.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/tests/army/frame/caseBase.py b/tests/army/frame/caseBase.py index 7717930ef4..a59c9a4441 100644 --- a/tests/army/frame/caseBase.py +++ b/tests/army/frame/caseBase.py @@ -482,19 +482,39 @@ class TBase: # generate new json file def genNewJson(self, jsonFile, modifyFunc=None): - with open(jsonFile, 'r', encoding='utf-8') as f: - data = json.load(f) + try: + with open(jsonFile, 'r', encoding='utf-8') as f: + data = json.load(f) + except FileNotFoundError: + tdLog.info(f"the specified json file '{jsonFile}' was not found.") + return None + except Exception as e: + tdLog.info(f"error reading the json file: {e}") + return None if callable(modifyFunc): modifyFunc(data) tempDir = os.path.join(tempfile.gettempdir(), 'json_templates') - os.makedirs(tempDir, exist_ok=True) + try: + os.makedirs(tempDir, exist_ok=True) + except PermissionError: + tdLog.info(f"no sufficient permissions to create directory at '{tempDir}'.") + return None + except Exception as e: + tdLog.info(f"error creating temporary directory: {e}") + return None + tempPath = os.path.join(tempDir, f"temp_{uuid.uuid4().hex}.json") - with open(tempPath, 'w', encoding='utf-8') as f: - json.dump(data, f, indent=2, ensure_ascii=False) + try: + with open(tempPath, 'w', encoding='utf-8') as f: + json.dump(data, f, indent=2, ensure_ascii=False) + except Exception as e: + tdLog.info(f"error writing to temporary json file: {e}") + return None + tdLog.info(f"create temporary json file successfully, file: {tempPath}") return tempPath # delete file