Skip to content

Commit

Permalink
refactor: move logic into setup phase
Browse files Browse the repository at this point in the history
When the plugin started out, I didn't know how to use the
`runtest_setup` phase, so I used an automatically-used fixture.

Once the hook usage was introduced, I can now consolidate a lot of the
behavior logic ion one place.

Signed-off-by: Mike Fiedler <[email protected]>
  • Loading branch information
miketheman committed Dec 20, 2021
1 parent 53ee202 commit 8f5fb28
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ def pytest_addoption(parser):
)


@pytest.fixture(autouse=True)
def _socket_marker(request):
"""
Create an automatically-used fixture that every test function will load.
The last option to set the fixture wins priority.
The expected behavior is that higher granularity options should override
lower granularity options.
"""
if request.config.__socket_disabled:
request.getfixturevalue('socket_disabled')

if request.node.get_closest_marker('disable_socket'):
request.getfixturevalue('socket_disabled')
if request.node.get_closest_marker('enable_socket'):
request.getfixturevalue('socket_enabled')


@pytest.fixture
def socket_disabled(pytestconfig):
""" disable socket.socket for duration of this test function """
Expand Down Expand Up @@ -115,6 +97,16 @@ def pytest_configure(config):


def pytest_runtest_setup(item):
"""During each test item's setup phase,
choose the behavior based on the configurations supplied."""
if item.config.__socket_disabled:
disable_socket(item.config.__socket_allow_unix_socket)

if item.get_closest_marker('disable_socket'):
disable_socket(item.config.__socket_allow_unix_socket)
if item.get_closest_marker('enable_socket'):
enable_socket()

mark_restrictions = item.get_closest_marker('allow_hosts')
cli_restrictions = item.config.__socket_allow_hosts
hosts = None
Expand Down

0 comments on commit 8f5fb28

Please sign in to comment.