Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor/fix: move logic into setup phase and correct order #88

Merged
merged 5 commits into from
Dec 22, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: address conflict with configurations
The previous configuration allowed for `allow-hosts` to be passed
without `--disable-socket` to implicitly activate blocking.

This was confusing and inconsistent so we now require it to enable the
global behavior.

This introduces a little more complexity to the setup method, but is
more clear as to what is actually happening.

Signed-off-by: Mike Fiedler <[email protected]>
  • Loading branch information
miketheman committed Dec 21, 2021
commit e4fff1a04b0f73ac313bc0e40d149960fb20258c
27 changes: 20 additions & 7 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,26 @@ def pytest_configure(config):
config.__socket_allow_hosts = config.getoption('--allow-hosts')


def pytest_runtest_setup(item):
def pytest_runtest_setup(item) -> None:
miketheman marked this conversation as resolved.
Show resolved Hide resolved
"""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)
choose the behavior based on the configurations supplied.

if item.get_closest_marker('disable_socket'):
disable_socket(item.config.__socket_allow_unix_socket)
if item.get_closest_marker('enable_socket'):
This is the bulk of the logic for the plugin.
As the logic can be extensive, this method is allowed complexity.
It may be refactored in the future to be more readable.
"""

# If test has the `enable_socket` marker, we accept this as most explicit.
if 'socket_enabled' in item.fixturenames or item.get_closest_marker('enable_socket'):
enable_socket()
return

# If the test has the `disable_socket` marker, it's explicitly disabled.
if 'socket_disabled' in item.fixturenames or item.get_closest_marker('disable_socket'):
disable_socket(item.config.__socket_allow_unix_socket)
return

# Resolve `allow_hosts` behaviors.
mark_restrictions = item.get_closest_marker('allow_hosts')
cli_restrictions = item.config.__socket_allow_hosts
hosts = None
Expand All @@ -114,6 +123,10 @@ def pytest_runtest_setup(item):
hosts = cli_restrictions
socket_allow_hosts(hosts)

# Finally, check the global config and disable socket if needed.
if item.config.__socket_disabled and not hosts:
disable_socket(item.config.__socket_allow_unix_socket)


def pytest_runtest_teardown():
_remove_restrictions()
Expand Down