8000 Use pytest-socket to ensure no tests are performing io (#1133) · msz-coder/python-kasa@b4aba36 · GitHub
[go: up one dir, main page]

Skip to content

Commit b4aba36

Browse files
authored
Use pytest-socket to ensure no tests are performing io (python-kasa#1133)
1 parent 936e45c commit b4aba36

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

kasa/tests/conftest.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import sys
45
import warnings
56
from unittest.mock import MagicMock, patch
67

@@ -87,6 +88,11 @@ def pytest_addoption(parser):
8788
def pytest_collection_modifyitems(config, items):
8889
if not config.getoption("--ip"):
8990
print("Testing against fixtures.")
91+
# pytest_socket doesn't work properly in windows with asyncio
92+
# fine to disable as other platforms will pickup any issues.
93+
if sys.platform == "win32":
94+
for item in items:
95+
item.add_marker(pytest.mark.enable_socket)
9096
else:
9197
print("Running against ip %s" % config.getoption("--ip"))
9298
requires_dummy = pytest.mark.skip(
@@ -95,18 +101,45 @@ def pytest_collection_modifyitems(config, items):
95101
for item in items:
96102
if "requires_dummy" in item.keywords:
97103
item.add_marker(requires_dummy)
104+
else:
105+
item.add_marker(pytest.mark.enable_socket)
98106

99107

100108
@pytest.fixture(autouse=True, scope="session")
101-
def asyncio_sleep_fixture(): # noqa: PT004
109+
def asyncio_sleep_fixture(request): # noqa: PT004
102110
"""Patch sleep to prevent tests actually waiting."""
103111
orig_asyncio_sleep = asyncio.sleep
104112

105113
async def _asyncio_sleep(*_, **__):
106114
await orig_asyncio_sleep(0)
107115

108-
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
116+
if request.config.getoption("--ip"):
109117
yield
118+
else:
119+
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
120+
yield
121+
122+
123+
@pytest.fixture(autouse=True, scope="session")
124+
def mock_datagram_endpoint(request): # noqa: PT004
125+
"""Mock create_datagram_endpoint so it doesn't perform io."""
126+
127+
async def _create_datagram_endpoint(protocol_factory, *_, **__):
128+
protocol = protocol_factory()
129+
transport = MagicMock()
130+
try:
131+
return transport, protocol
132+
finally:
133+
protocol.connection_made(transport)
134+
135+
if request.config.getoption("--ip"):
136+
yield
137+
else:
138+
with patch(
139+
"asyncio.BaseEventLoop.create_datagram_endpoint",
140+
side_effect=_create_datagram_endpoint,
141+
):
142+
yield
110143

111144

112145
# allow mocks to be awaited

kasa/tests/test_discovery.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ async def test_discover_single_hostname(discovery_mock, mocker):
170170
async def test_discover_credentials(mocker):
171171
"""Make sure that discover gives credentials precedence over un and pw."""
172172
host = "127.0.0.1"
173-
mocker.patch("kasa.discover._DiscoverProtocol.wait_for_discovery_to_complete")
174173

175-
def mock_discover(self, *_, **__):
174+
async def mock_discover(self, *_, **__):
176175
self.discovered_devices = {host: MagicMock()}
176+
self.seen_hosts.add(host)
177+
self._handle_discovered_event()
177178

178-
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
179+
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
179180
dp = mocker.spy(_DiscoverProtocol, "__init__")
180181

181182
# Only credentials passed
@@ -197,12 +198,13 @@ def mock_discover(self, *_, **__):
197198
async def test_discover_single_credentials(mocker):
198199
"""Make sure that discover_single gives credentials precedence over un and pw."""
199200
host = "127.0.0.1"
200-
mocker.patch("kasa.discover._DiscoverProtocol.wait_for_discovery_to_complete")
201201

202-
def mock_discover(self, *_, **__):
202+
async def mock_discover(self, *_, **__):
203203
self.discovered_devices = {host: MagicMock()}
204+
self.seen_hosts.add(host)
205+
self._handle_discovered_event()
204206

205-
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
207+
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
206208
dp = mocker.spy(_DiscoverProtocol, "__init__")
207209

208210
# Only credentials passed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ dev-dependencies = [
5757
"pytest-freezer~=0.4",
5858
"mypy~=1.0",
5959
"pytest-xdist>=3.6.1",
60+
"pytest-socket>=0.7.0",
6061
]
6162

6263

@@ -108,6 +109,7 @@ markers = [
108109
asyncio_mode = "auto"
109110
asyncio_default_fixture_loop_scope = "function"
110111
timeout = 10
112+
addopts = "--disable-socket --allow-unix-socket"
111113

112114
[tool.doc8]
113115
paths = ["docs"]

uv.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0