diff --git a/changelogs/fragments/ansible-test-errno.yml b/changelogs/fragments/ansible-test-errno.yml new file mode 100644 index 00000000000..b2a6a190c20 --- /dev/null +++ b/changelogs/fragments/ansible-test-errno.yml @@ -0,0 +1,2 @@ +minor_changes: + - ansible-test - Update error handling code to use Python 3.x constructs, avoiding direct use of ``errno``. diff --git a/test/lib/ansible_test/_internal/commands/coverage/__init__.py b/test/lib/ansible_test/_internal/commands/coverage/__init__.py index cdf2d544e9e..6b063cf65d4 100644 --- a/test/lib/ansible_test/_internal/commands/coverage/__init__.py +++ b/test/lib/ansible_test/_internal/commands/coverage/__init__.py @@ -2,7 +2,6 @@ from __future__ import annotations import collections.abc as c -import errno import json import os import re @@ -135,11 +134,8 @@ def get_coverage_files(language: str, path: t.Optional[str] = None) -> list[str] try: coverage_files = [os.path.join(coverage_dir, f) for f in os.listdir(coverage_dir) if '=coverage.' in f and '=%s' % language in f] - except IOError as ex: - if ex.errno == errno.ENOENT: - return [] - - raise + except FileNotFoundError: + return [] return coverage_files diff --git a/test/lib/ansible_test/_internal/core_ci.py b/test/lib/ansible_test/_internal/core_ci.py index cc2a868ff7a..15898ef8bf4 100644 --- a/test/lib/ansible_test/_internal/core_ci.py +++ b/test/lib/ansible_test/_internal/core_ci.py @@ -8,7 +8,6 @@ import os import re import traceback import uuid -import errno import time import typing as t @@ -347,18 +346,14 @@ class AnsibleCoreCI: try: self.connection = None os.remove(self.path) - except OSError as ex: - if ex.errno != errno.ENOENT: - raise + except FileNotFoundError: + pass def _load(self): """Load instance information.""" try: data = read_text_file(self.path) - except IOError as ex: - if ex.errno != errno.ENOENT: - raise - + except FileNotFoundError: return False if not data.startswith('{'): diff --git a/test/lib/ansible_test/_internal/io.py b/test/lib/ansible_test/_internal/io.py index a7e9f1b205b..41f2ec03df5 100644 --- a/test/lib/ansible_test/_internal/io.py +++ b/test/lib/ansible_test/_internal/io.py @@ -1,7 +1,6 @@ """Functions for disk IO.""" from __future__ import annotations -import errno import io import json import os @@ -32,11 +31,7 @@ def read_binary_file(path: str) -> bytes: def make_dirs(path: str) -> None: """Create a directory at path, including any necessary parent directories.""" - try: - os.makedirs(to_bytes(path)) - except OSError as ex: - if ex.errno != errno.EEXIST: - raise + os.makedirs(to_bytes(path), exist_ok=True) def write_json_file(path: str, diff --git a/test/lib/ansible_test/_internal/util.py b/test/lib/ansible_test/_internal/util.py index 123162393fd..95a8280f5cb 100644 --- a/test/lib/ansible_test/_internal/util.py +++ b/test/lib/ansible_test/_internal/util.py @@ -3,7 +3,6 @@ from __future__ import annotations import abc import collections.abc as c -import errno import enum import fcntl import importlib.util @@ -467,10 +466,8 @@ def raw_command( cmd_bytes = [to_bytes(arg) for arg in cmd] env_bytes = dict((to_bytes(k), to_bytes(v)) for k, v in env.items()) process = subprocess.Popen(cmd_bytes, env=env_bytes, stdin=stdin, stdout=stdout, stderr=stderr, cwd=cwd) # pylint: disable=consider-using-with - except OSError as ex: - if ex.errno == errno.ENOENT: - raise ApplicationError('Required program "%s" not found.' % cmd[0]) - raise + except FileNotFoundError as ex: + raise ApplicationError('Required program "%s" not found.' % cmd[0]) from ex if communicate: data_bytes = to_optional_bytes(data) @@ -694,12 +691,11 @@ def verified_chmod(path: str, mode: int) -> None: def remove_tree(path: str) -> None: - """Remove the specified directory, siliently continuing if the directory does not exist.""" + """Remove the specified directory, silently continuing if the directory does not exist.""" try: shutil.rmtree(to_bytes(path)) - except OSError as ex: - if ex.errno != errno.ENOENT: - raise + except FileNotFoundError: + pass def is_binary_file(path: str) -> bool: diff --git a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py index ce440bca1f3..77c380c7aad 100644 --- a/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py +++ b/test/lib/ansible_test/_util/controller/sanity/validate-modules/validate_modules/main.py @@ -22,7 +22,6 @@ import argparse import ast import datetime import json -import errno import os import re import subprocess @@ -2467,12 +2466,9 @@ class GitCache: self.head_tree = self._get_module_files() else: raise - except OSError as ex: - if ex.errno == errno.ENOENT: - # fallback when git is not installed - self.head_tree = self._get_module_files() - else: - raise + except FileNotFoundError: + # fallback when git is not installed + self.head_tree = self._get_module_files() allowed_exts = ('.py', '.ps1') if plugin_type != 'module':