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

feat: force enable socket CLI flag #186

Merged
merged 3 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def pytest_runtest_setup():
disable_socket()
```

If you exceptionally want to enable socket for one particular execution
pass `--force-enable-socket`. It takes precedence over `--disable-socket`.

To enable Unix sockets during the test run (e.g. for async), add this option:

```ini
Expand Down Expand Up @@ -101,6 +104,7 @@ or for whole test run
addopts = --allow-hosts=127.0.0.1,127.0.1.1
```


### Frequently Asked Questions

Q: Why is network access disabled in some of my tests but not others?
Expand Down
16 changes: 13 additions & 3 deletions pytest_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ def pytest_addoption(parser):
dest="disable_socket",
help="Disable socket.socket by default to block network calls.",
)
group.addoption(
"--force-enable-socket",
action="store_true",
dest="force_enable_socket",
help="Force enable socket.socket network calls (override --disable-socket).",
)
group.addoption(
"--allow-hosts",
dest="allow_hosts",
Expand Down Expand Up @@ -100,6 +106,7 @@ def pytest_configure(config):
)

# Store the global configs in the `pytest.Config` object.
config.__socket_force_enabled = config.getoption("--force-enable-socket")
config.__socket_disabled = config.getoption("--disable-socket")
config.__socket_allow_unix_socket = config.getoption("--allow-unix-socket")
config.__socket_allow_hosts = config.getoption("--allow-hosts")
Expand All @@ -119,9 +126,12 @@ def pytest_runtest_setup(item) -> None:
if not hasattr(item, "fixturenames"):
return

# 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"
# If test has the `enable_socket` marker, fixture or
# it's forced from the CLI, we accept this as most explicit.
if (
"socket_enabled" in item.fixturenames
or item.get_closest_marker("enable_socket")
or item.config.__socket_force_enabled
):
enable_socket()
return
Expand Down
28 changes: 28 additions & 0 deletions tests/test_precedence.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ def test_socket():
assert_socket_blocked(result)


def test_force_enable_socket_via_cli_flag(testdir):
testdir.makepyfile(
"""
import socket
import pytest

@pytest.mark.disable_socket
def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
"""
)
result = testdir.runpytest("--force-enable-socket")
result.assert_outcomes(passed=1)


def test_force_enable_cli_flag_precedence(testdir):
testdir.makepyfile(
"""
import socket

def test_socket():
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
"""
)
result = testdir.runpytest("--disable-socket", "--force-enable-socket")
result.assert_outcomes(passed=1)


def test_global_disable_via_config(testdir):
testdir.makepyfile(
"""
Expand Down