Skip to content

Commit

Permalink
marks: turn fixtures into keyword argument for topology marker
Browse files Browse the repository at this point in the history
The current state is not possible to extend very easily without
breaking existing setups. This change allows full flexibility
when adding new functionality to the marker.
  • Loading branch information
pbrezina committed Jan 29, 2024
1 parent 29a5ed8 commit 9e5b5a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion docs/quick-start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fixtures.
@pytest.mark.topology(
"kdc", Topology(TopologyDomain("test", client=1, kdc=1)),
client="test.client[0]", kdc="test.kdc[0]"
fixtures=dict(client="test.client[0]", kdc="test.kdc[0]")
)
def test_example(client: Client, kdc: KDC):
pass
Expand Down
10 changes: 5 additions & 5 deletions docs/topology.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The marker is used as:

.. code-block:: python
@pytest.mark.topology(name, topology, *, fixtures ...)
@pytest.mark.topology(name, topology, *, fixtures=dict(...))
def test_example():
assert True
Expand Down Expand Up @@ -179,7 +179,7 @@ The example above can be rewritten as:
@pytest.mark.topology(
'ldap', Topology(TopologyDomain('test', client=1, ldap=1)),
client='test.client[0]', ldap='test.ldap[0]'
fixtures=dict(client='test.client[0]', ldap='test.ldap[0]')
)
def test_example(client: Client, ldap: LDAP):
assert client.role == 'client'
Expand All @@ -198,7 +198,7 @@ benefit from it.
@pytest.mark.topology(
'ldap', Topology(TopologyDomain('test', client=1, ldap=1)),
clients='test.client', ldap='test.ldap[0]'
fixtures=dict(clients='test.client', ldap='test.ldap[0]')
)
def test_example(clients: list[Client], ldap: LDAP):
for client in clients:
Expand All @@ -217,7 +217,7 @@ benefit from it.
@pytest.mark.topology(
'ldap', Topology(TopologyDomain('test', client=1, ldap=1)),
clients='test.client'
fixtures=dict(clients='test.client')
)
def test_example(mh: MultihostFixture, clients: list[Client]):
pass
Expand All @@ -231,7 +231,7 @@ benefit from it.
@pytest.mark.topology(
'ldap', Topology(TopologyDomain('test', client=1, ldap=1)),
client='test.client[0]', ldap='test.ldap[0]', provider='test.ldap[0]'
fixtures=dict(client='test.client[0]', ldap='test.ldap[0]', provider='test.ldap[0]')
)
def test_example(client: Client, provider: GenericProvider):
pass
Expand Down
9 changes: 5 additions & 4 deletions pytest_mh/_private/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TopologyMark(object):
.. code-block:: python
:caption: Example usage
@pytest.mark.topology(name, topology, fixture1='path1', fixture2='path2', ...)
@pytest.mark.topology(name, topology, fixture=dict(fixture1='path1', fixture2='path2', ...))
def test_fixture_name(fixture1: BaseRole, fixture2: BaseRole, ...):
assert True
Expand All @@ -40,6 +40,7 @@ def __init__(
self,
name: str,
topology: Topology,
*,
fixtures: dict[str, str] | None = None,
) -> None:
"""
Expand Down Expand Up @@ -176,17 +177,17 @@ def _CreateFromArgs(cls, item: pytest.Function, args: Tuple, kwargs: Mapping[str
:return: Instance of TopologyMark.
:rtype: TopologyMark
"""
# First three parameters are positional, the rest are keyword arguments.
# First two parameters are positional, the rest are keyword arguments.
if len(args) != 2:
nodeid = item.parent.nodeid if item.parent is not None else ""
error = f"{nodeid}::{item.originalname}: invalid arguments for @pytest.mark.topology"
raise ValueError(error)

name = args[0]
topology = args[1]
fixtures = {k: str(v) for k, v in kwargs.items()}
fixtures = {k: str(v) for k, v in kwargs.get("fixtures", {}).items()}

return cls(name, topology, fixtures)
return cls(name, topology, fixtures=fixtures)


class KnownTopologyBase(Enum):
Expand Down

0 comments on commit 9e5b5a4

Please sign in to comment.