Skip to content

Make pytest-socket optional #218

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ tests/
return snapshot.use_extension(HomeAssistantSnapshotExtension)
```

## Installation

### Basic Installation
```bash
pip install pytest-homeassistant-custom-component
```

### With Socket Testing Support
If you need network socket testing capabilities (using pytest-socket), install with the socket extra:
```bash
pip install pytest-homeassistant-custom-component[socket]
```

The pytest-socket plugin helps prevent tests from making real network calls by blocking socket access. This is optional and only needed if you want to ensure your tests don't accidentally make network connections.

## Examples:
* See [list of custom components](https://github.com/MatthewFlamm/pytest-homeassistant-custom-component/network/dependents) as examples that use this package.
* Also see tests for `simple_integration` in this repository.
Expand Down
12 changes: 10 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
requirements = [
"sqlalchemy",
]
optional_requirements = []
with open("requirements_test.txt","r") as f:
for line in f:
if "txt" not in line and "#" not in line:
requirements.append(line)
line = line.strip()
if "txt" not in line and "#" not in line and line:
if line.startswith("pytest-socket"):
optional_requirements.append(line)
else:
requirements.append(line)

with open("version", "r") as f:
__version__ = f.read()
Expand All @@ -21,6 +26,9 @@
package_dir={"": "src"},
python_requires=">=3.13",
install_requires=requirements,
extras_require={
"socket": optional_requirements,
},
license="MIT license",
url="https://github.com/MatthewFlamm/pytest-homeassistant-custom-component",
author_email="[email protected]",
Expand Down
19 changes: 16 additions & 3 deletions src/pytest_homeassistant_custom_component/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import multidict
import pytest
import pytest_asyncio
import pytest_socket
try:
import pytest_socket
except ImportError:
pytest_socket = None
import requests_mock
import respx
from syrupy.assertion import SnapshotAssertion
Expand Down Expand Up @@ -191,8 +194,9 @@ def pytest_runtest_setup() -> None:
freezegun:
Modified to include https://github.com/spulec/freezegun/pull/424
"""
pytest_socket.socket_allow_hosts(["127.0.0.1"])
pytest_socket.disable_socket(allow_unix_socket=True)
if pytest_socket is not None:
pytest_socket.socket_allow_hosts(["127.0.0.1"])
pytest_socket.disable_socket(allow_unix_socket=True)

freezegun.api.datetime_to_fakedatetime = patch_time.ha_datetime_to_fakedatetime # type: ignore[attr-defined]
freezegun.api.FakeDatetime = patch_time.HAFakeDatetime # type: ignore[attr-defined]
Expand Down Expand Up @@ -2093,6 +2097,15 @@ def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion:
return snapshot.use_extension(HomeAssistantSnapshotExtension)


# Only define socket_enabled fixture if pytest-socket is not available
# When pytest-socket is installed, it provides its own socket_enabled fixture
if pytest_socket is None:
@pytest.fixture
def socket_enabled() -> None:
"""Fixture to enable socket (no-op when pytest-socket is not installed)."""
return None


@pytest.fixture
def disable_block_async_io() -> Generator[None]:
"""Fixture to disable the loop protection from block_async_io."""
Expand Down