Use pytest-socket to ensure no tests are performing io (#1133)

This commit is contained in:
Steven B.
2024-09-27 17:20:25 +01:00
committed by GitHub
parent 936e45cad7
commit b4aba36b73
4 changed files with 59 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import sys
import warnings
from unittest.mock import MagicMock, patch
@@ -87,6 +88,11 @@ def pytest_addoption(parser):
def pytest_collection_modifyitems(config, items):
if not config.getoption("--ip"):
print("Testing against fixtures.")
# pytest_socket doesn't work properly in windows with asyncio
# fine to disable as other platforms will pickup any issues.
if sys.platform == "win32":
for item in items:
item.add_marker(pytest.mark.enable_socket)
else:
print("Running against ip %s" % config.getoption("--ip"))
requires_dummy = pytest.mark.skip(
@@ -95,18 +101,45 @@ def pytest_collection_modifyitems(config, items):
for item in items:
if "requires_dummy" in item.keywords:
item.add_marker(requires_dummy)
else:
item.add_marker(pytest.mark.enable_socket)
@pytest.fixture(autouse=True, scope="session")
def asyncio_sleep_fixture(): # noqa: PT004
def asyncio_sleep_fixture(request): # noqa: PT004
"""Patch sleep to prevent tests actually waiting."""
orig_asyncio_sleep = asyncio.sleep
async def _asyncio_sleep(*_, **__):
await orig_asyncio_sleep(0)
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
if request.config.getoption("--ip"):
yield
else:
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
yield
@pytest.fixture(autouse=True, scope="session")
def mock_datagram_endpoint(request): # noqa: PT004
"""Mock create_datagram_endpoint so it doesn't perform io."""
async def _create_datagram_endpoint(protocol_factory, *_, **__):
protocol = protocol_factory()
transport = MagicMock()
try:
return transport, protocol
finally:
protocol.connection_made(transport)
if request.config.getoption("--ip"):
yield
else:
with patch(
"asyncio.BaseEventLoop.create_datagram_endpoint",
side_effect=_create_datagram_endpoint,
):
yield
# allow mocks to be awaited

View File

@@ -170,12 +170,13 @@ async def test_discover_single_hostname(discovery_mock, mocker):
async def test_discover_credentials(mocker):
"""Make sure that discover gives credentials precedence over un and pw."""
host = "127.0.0.1"
mocker.patch("kasa.discover._DiscoverProtocol.wait_for_discovery_to_complete")
def mock_discover(self, *_, **__):
async def mock_discover(self, *_, **__):
self.discovered_devices = {host: MagicMock()}
self.seen_hosts.add(host)
self._handle_discovered_event()
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
dp = mocker.spy(_DiscoverProtocol, "__init__")
# Only credentials passed
@@ -197,12 +198,13 @@ async def test_discover_credentials(mocker):
async def test_discover_single_credentials(mocker):
"""Make sure that discover_single gives credentials precedence over un and pw."""
host = "127.0.0.1"
mocker.patch("kasa.discover._DiscoverProtocol.wait_for_discovery_to_complete")
def mock_discover(self, *_, **__):
async def mock_discover(self, *_, **__):
self.discovered_devices = {host: MagicMock()}
self.seen_hosts.add(host)
self._handle_discovered_event()
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
dp = mocker.spy(_DiscoverProtocol, "__init__")
# Only credentials passed