Skip to content

Commit

Permalink
Merge pull request #203 from dcermak/fix-python-6-compat
Browse files Browse the repository at this point in the history
Fix compatibility with pytest 6
  • Loading branch information
dcermak authored Apr 3, 2024
2 parents 35d8d66 + 9f14b09 commit bf9ab21
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 27 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ Documentation:
Internal changes:


0.4.1 (2 April 2024)
--------------------

Breaking changes:


Improvements and new features:


Documentation:


Internal changes:

- fix imports for older pytest releases


0.4.0 (27 March 2024)
---------------------

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pytest_container"
version = "0.4.0"
version = "0.4.1"
description = "Pytest fixtures for writing container based tests"
authors = ["Dan Čermák <[email protected]>"]
homepage = "https://dcermak.github.io/pytest_container/"
Expand Down
34 changes: 17 additions & 17 deletions pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
from uuid import uuid4

import deprecation
import pytest
import testinfra
from _pytest.mark import Mark
from _pytest.mark import MarkDecorator
from _pytest.mark import ParameterSet
from filelock import BaseFileLock
from filelock import FileLock
from pytest import param
from pytest_container.helpers import get_always_pull_option
from pytest_container.inspect import ContainerHealth
from pytest_container.inspect import ContainerInspect
Expand Down Expand Up @@ -896,9 +898,7 @@ def inspect(self) -> ContainerInspect:

def container_to_pytest_param(
container: ContainerBase,
marks: Optional[
Union[Collection[pytest.MarkDecorator], pytest.MarkDecorator]
] = None,
marks: Optional[Union[Collection[MarkDecorator], MarkDecorator]] = None,
) -> ParameterSet:
"""Converts a subclass of :py:class:`~pytest_container.container.ContainerBase`
(:py:class:`~pytest_container.container.Container` or
Expand All @@ -911,38 +911,38 @@ def container_to_pytest_param(
:py:attr:`~pytest_container.container.ContainerBase.container_id`)
"""
return pytest.param(container, marks=marks or [], id=str(container))
return param(container, marks=marks or [], id=str(container))


@overload
def container_and_marks_from_pytest_param(
param: Container,
ctr_or_param: Container,
) -> Tuple[Container, Literal[None]]:
...


@overload
def container_and_marks_from_pytest_param(
param: DerivedContainer,
ctr_or_param: DerivedContainer,
) -> Tuple[DerivedContainer, Literal[None]]:
...


@overload
def container_and_marks_from_pytest_param(
param: ParameterSet,
ctr_or_param: ParameterSet,
) -> Tuple[
Union[Container, DerivedContainer],
Optional[Collection[Union[pytest.MarkDecorator, pytest.Mark]]],
Optional[Collection[Union[MarkDecorator, Mark]]],
]:
...


def container_and_marks_from_pytest_param(
param: Union[ParameterSet, Container, DerivedContainer],
ctr_or_param: Union[ParameterSet, Container, DerivedContainer],
) -> Tuple[
Union[Container, DerivedContainer],
Optional[Collection[Union[pytest.MarkDecorator, pytest.Mark]]],
Optional[Collection[Union[MarkDecorator, Mark]]],
]:
"""Extracts the :py:class:`~pytest_container.container.Container` or
:py:class:`~pytest_container.container.DerivedContainer` and the
Expand All @@ -955,15 +955,15 @@ def container_and_marks_from_pytest_param(
returned directly and the second return value is ``None``.
"""
if isinstance(param, (Container, DerivedContainer)):
return param, None
if isinstance(ctr_or_param, (Container, DerivedContainer)):
return ctr_or_param, None

if len(param.values) > 0 and isinstance(
param.values[0], (Container, DerivedContainer)
if len(ctr_or_param.values) > 0 and isinstance(
ctr_or_param.values[0], (Container, DerivedContainer)
):
return param.values[0], param.marks
return ctr_or_param.values[0], ctr_or_param.marks

raise ValueError(f"Invalid pytest.param values: {param.values}")
raise ValueError(f"Invalid pytest.param values: {ctr_or_param.values}")


@deprecation.deprecated(
Expand Down
15 changes: 8 additions & 7 deletions pytest_container/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
else:
from typing_extensions import Literal

import pytest
from pytest import fixture
from pytest import skip
from _pytest.config import Config
from _pytest.fixtures import SubRequest


@pytest.fixture(scope="session")
@fixture(scope="session")
def container_runtime() -> OciRuntimeBase:
"""pytest fixture that returns the currently selected container runtime
according to the rules outlined :ref:`here <runtime selection rules>`.
Expand Down Expand Up @@ -63,7 +64,7 @@ def _create_auto_container_fixture(
) -> Callable[
[SubRequest, OciRuntimeBase, Config], Generator[ContainerData, None, None]
]:
def fixture(
def fixture_funct(
request: SubRequest,
# we must call this parameter container runtime, so that pytest will
# treat it as a fixture, but that causes pylint to complain…
Expand Down Expand Up @@ -125,15 +126,15 @@ def fixture(
launcher._container_id, container_runtime
)

return pytest.fixture(scope=scope)(fixture)
return fixture(scope=scope)(fixture_funct)


def _create_auto_pod_fixture(
scope: Literal["session", "function"]
) -> Callable[
[SubRequest, OciRuntimeBase, Config], Generator[PodData, None, None]
]:
def fixture(
def fixture_funct(
request: SubRequest,
# we must call this parameter container runtime, so that pytest will
# treat it as a fixture, but that causes pylint to complain…
Expand All @@ -142,7 +143,7 @@ def fixture(
pytestconfig: Config,
) -> Generator[PodData, None, None]:
if "podman" not in container_runtime.runner_binary:
pytest.skip("Pods are only supported in podman")
skip("Pods are only supported in podman")

pod = pod_from_pytest_param(request.param)
with PodLauncher(
Expand All @@ -163,7 +164,7 @@ def fixture(
ctr_launcher._container_id, container_runtime
)

return pytest.fixture(scope=scope)(fixture)
return fixture(scope=scope)(fixture_funct)


#: This fixture parametrizes the test function once for each container image
Expand Down
4 changes: 2 additions & 2 deletions pytest_container/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from typing import TYPE_CHECKING
from typing import Union

import pytest
import testinfra
from _pytest.mark.structures import ParameterSet
from pytest import param
from pytest_container.inspect import BindMount
from pytest_container.inspect import Config
from pytest_container.inspect import ContainerHealth
Expand Down Expand Up @@ -65,7 +65,7 @@ class ToParamMixin:

def to_pytest_param(self) -> ParameterSet:
"""Convert this class into a ``pytest.param``"""
return pytest.param(self, id=str(self), marks=self.marks or ())
return param(self, id=str(self), marks=self.marks or ())


@dataclass(frozen=True)
Expand Down

0 comments on commit bf9ab21

Please sign in to comment.