Fix/improve unit tests (#1281)

* Fix conditional import.

* Don't count lines for code coverage which should never execute.

* Fix tests to use pytest.raises.
This commit is contained in:
Felix Fontein 2026-06-06 21:27:09 +02:00 committed by GitHub
parent 604747372d
commit ce69e51d73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 22 additions and 25 deletions

View File

@ -20,7 +20,7 @@ from ansible_collections.community.docker.plugins.inventory.docker_containers im
)
if t.TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Callable # pragma: no cover
@pytest.fixture(scope="module", name="templar")

View File

@ -43,7 +43,7 @@ from ansible_collections.community.docker.tests.unit.plugins.module_utils._api.c
from .. import fake_api
if t.TYPE_CHECKING:
from ansible_collections.community.docker.plugins.module_utils._api.auth import (
from ansible_collections.community.docker.plugins.module_utils._api.auth import ( # pragma: no cover
AuthConfig,
)

View File

@ -18,7 +18,7 @@ from ansible_collections.community.docker.tests.unit.plugins.module_utils._api.c
from . import fake_stat
if t.TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Callable # pragma: no cover
CURRENT_VERSION = f"v{DEFAULT_DOCKER_API_VERSION}"

View File

@ -13,9 +13,9 @@ from ansible_collections.community.docker.plugins.module_utils._copy import (
)
if t.TYPE_CHECKING:
from collections.abc import Sequence
from collections.abc import Sequence # pragma: no cover
T = t.TypeVar("T")
T = t.TypeVar("T") # pragma: no cover
def _simple_generator(sequence: Sequence[T]) -> t.Generator[T]:

View File

@ -61,12 +61,10 @@ def test_archived_image_manifest_extracts_nothing_when_file_not_present(
def test_archived_image_manifest_raises_when_file_not_a_tar() -> None:
try:
with pytest.raises(ImageArchiveInvalidException, match=__file__) as exc_info:
archived_image_manifest(__file__)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, tarfile.ReadError)
assert str(__file__) in str(e)
assert isinstance(exc_info.value.__cause__, tarfile.ReadError)
assert str(__file__) in str(exc_info.value)
def test_archived_image_manifest_raises_when_tar_missing_manifest(
@ -74,12 +72,10 @@ def test_archived_image_manifest_raises_when_tar_missing_manifest(
) -> None:
write_irrelevant_tar(tar_file_name)
try:
with pytest.raises(ImageArchiveInvalidException) as exc_info:
archived_image_manifest(tar_file_name)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, KeyError)
assert "manifest.json" in str(e.__cause__)
assert isinstance(exc_info.value.__cause__, KeyError)
assert "manifest.json" in str(exc_info.value.__cause__)
def test_archived_image_manifest_raises_when_manifest_missing_id(
@ -89,9 +85,7 @@ def test_archived_image_manifest_raises_when_manifest_missing_id(
write_imitation_archive_with_manifest(tar_file_name, manifest)
try:
with pytest.raises(ImageArchiveInvalidException) as exc_info:
archived_image_manifest(tar_file_name)
raise AssertionError()
except ImageArchiveInvalidException as e:
assert isinstance(e.__cause__, KeyError)
assert "Config" in str(e.__cause__)
assert isinstance(exc_info.value.__cause__, KeyError)
assert "Config" in str(exc_info.value.__cause__)

View File

@ -17,12 +17,12 @@ from ansible_collections.community.docker.plugins.module_utils._util import (
if t.TYPE_CHECKING:
class DAMSpec(t.TypedDict):
class DAMSpec(t.TypedDict): # pragma: no cover
av: dict[str, t.Any]
bv: dict[str, t.Any]
result: bool
class Spec(t.TypedDict):
class Spec(t.TypedDict): # pragma: no cover
a: t.Any
b: t.Any
method: t.Literal["strict", "ignore", "allow_more_present"]

View File

@ -21,11 +21,13 @@ from ..test_support.docker_image_archive_stubbing import (
)
if t.TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Callable # pragma: no cover
def assert_no_logging(msg: str) -> t.NoReturn:
raise AssertionError(f"Should not have logged anything but logged {msg}")
raise AssertionError(
f"Should not have logged anything but logged {msg}"
) # pragma: no cover
def capture_logging(messages: list[str]) -> Callable[[str], None]:

View File

@ -12,7 +12,8 @@ from ansible_collections.community.docker.plugins.modules import (
docker_swarm_service,
)
APIError = pytest.importorskip("docker.errors.APIError")
docker_errors = pytest.importorskip("docker.errors")
APIError = docker_errors.APIError
def test_retry_on_out_of_sequence_error(mocker: t.Any) -> None: