From 24d7540a7f227f83ae44de0e1204372a0eaadaa4 Mon Sep 17 00:00:00 2001 From: mataotao Date: Thu, 27 Jun 2024 10:28:13 +0800 Subject: [PATCH] Using the pathlib module to handle file paths enhances the readability and maintainability of the code Signed-off-by: mataotao --- avocado/core/result.py | 24 ++++++++++++------------ avocado/utils/asset.py | 12 +++++++++--- 2 files changed, 21 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..b3a01f831f 100644 --- a/avocado/utils/asset.py +++ b/avocado/utils/asset.py @@ -22,6 +22,7 @@ import logging import operator import os +import pathlib import re import shutil import stat @@ -684,9 +685,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):