From ee32a501a4cd106bf13019bec5d5b721da379c91 Mon Sep 17 00:00:00 2001 From: mataotao Date: Fri, 26 Jul 2024 16:52:33 +0800 Subject: [PATCH] Add exception handling to os.remove Signed-off-by: mataotao --- avocado/core/result.py | 24 ++++++++++++------------ avocado/utils/asset.py | 11 ++++++++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/avocado/core/result.py b/avocado/core/result.py index 5cde243818..a1bb8f0520 100644 --- a/avocado/core/result.py +++ b/avocado/core/result.py @@ -103,18 +103,18 @@ def check_test(self, state): :param test: A dict with test internal state """ status = state.get("status") - status_mapping = { - "PASS": "passed", - "SKIP": "skipped", - "FAIL": "failed", - "WARN": "warned", - "INTERRUPTED": "interrupted", - "CANCEL": "cancelled", - } - if status in status_mapping: - setattr( - self, status_mapping[status], getattr(self, status_mapping[status]) + 1 - ) + if status == "PASS": + self.passed += 1 + elif status == "SKIP": + self.skipped += 1 + elif status == "FAIL": + self.failed += 1 + elif status == "WARN": + self.warned += 1 + elif status == "INTERRUPTED": + self.interrupted += 1 + elif status == "CANCEL": + self.cancelled += 1 else: self.errors += 1 self.end_test(state) diff --git a/avocado/utils/asset.py b/avocado/utils/asset.py index 33758ccda0..27326d9510 100644 --- a/avocado/utils/asset.py +++ b/avocado/utils/asset.py @@ -684,9 +684,14 @@ def remove_asset_by_path(cls, asset_path): :param asset_path: full path of the asset file. """ - os.remove(asset_path) - filename = f"{asset_path}-CHECKSUM" - os.remove(filename) + try: + os.remove(asset_path) + filename = f"{asset_path}-CHECKSUM" + os.remove(filename) + except FileNotFoundError: + LOG.error(f"File not found: {asset_path} or its checksum file.") + except Exception as e: + LOG.error(f"An error occurred while removing files: {e}") @property def urls(self):