mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-22 11:13:34 +00:00
Use pytest-socket to ensure no tests are performing io (#1133)
This commit is contained in:
parent
936e45cad7
commit
b4aba36b73
@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
from unittest.mock import MagicMock, patch
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
@ -87,6 +88,11 @@ def pytest_addoption(parser):
|
|||||||
def pytest_collection_modifyitems(config, items):
|
def pytest_collection_modifyitems(config, items):
|
||||||
if not config.getoption("--ip"):
|
if not config.getoption("--ip"):
|
||||||
print("Testing against fixtures.")
|
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:
|
else:
|
||||||
print("Running against ip %s" % config.getoption("--ip"))
|
print("Running against ip %s" % config.getoption("--ip"))
|
||||||
requires_dummy = pytest.mark.skip(
|
requires_dummy = pytest.mark.skip(
|
||||||
@ -95,18 +101,45 @@ def pytest_collection_modifyitems(config, items):
|
|||||||
for item in items:
|
for item in items:
|
||||||
if "requires_dummy" in item.keywords:
|
if "requires_dummy" in item.keywords:
|
||||||
item.add_marker(requires_dummy)
|
item.add_marker(requires_dummy)
|
||||||
|
else:
|
||||||
|
item.add_marker(pytest.mark.enable_socket)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope="session")
|
@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."""
|
"""Patch sleep to prevent tests actually waiting."""
|
||||||
orig_asyncio_sleep = asyncio.sleep
|
orig_asyncio_sleep = asyncio.sleep
|
||||||
|
|
||||||
async def _asyncio_sleep(*_, **__):
|
async def _asyncio_sleep(*_, **__):
|
||||||
await orig_asyncio_sleep(0)
|
await orig_asyncio_sleep(0)
|
||||||
|
|
||||||
with patch("asyncio.sleep", side_effect=_asyncio_sleep):
|
if request.config.getoption("--ip"):
|
||||||
yield
|
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
|
# allow mocks to be awaited
|
||||||
|
@ -170,12 +170,13 @@ async def test_discover_single_hostname(discovery_mock, mocker):
|
|||||||
async def test_discover_credentials(mocker):
|
async def test_discover_credentials(mocker):
|
||||||
"""Make sure that discover gives credentials precedence over un and pw."""
|
"""Make sure that discover gives credentials precedence over un and pw."""
|
||||||
host = "127.0.0.1"
|
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.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__")
|
dp = mocker.spy(_DiscoverProtocol, "__init__")
|
||||||
|
|
||||||
# Only credentials passed
|
# Only credentials passed
|
||||||
@ -197,12 +198,13 @@ async def test_discover_credentials(mocker):
|
|||||||
async def test_discover_single_credentials(mocker):
|
async def test_discover_single_credentials(mocker):
|
||||||
"""Make sure that discover_single gives credentials precedence over un and pw."""
|
"""Make sure that discover_single gives credentials precedence over un and pw."""
|
||||||
host = "127.0.0.1"
|
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.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__")
|
dp = mocker.spy(_DiscoverProtocol, "__init__")
|
||||||
|
|
||||||
# Only credentials passed
|
# Only credentials passed
|
||||||
|
@ -57,6 +57,7 @@ dev-dependencies = [
|
|||||||
"pytest-freezer~=0.4",
|
"pytest-freezer~=0.4",
|
||||||
"mypy~=1.0",
|
"mypy~=1.0",
|
||||||
"pytest-xdist>=3.6.1",
|
"pytest-xdist>=3.6.1",
|
||||||
|
"pytest-socket>=0.7.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@ -108,6 +109,7 @@ markers = [
|
|||||||
asyncio_mode = "auto"
|
asyncio_mode = "auto"
|
||||||
asyncio_default_fixture_loop_scope = "function"
|
asyncio_default_fixture_loop_scope = "function"
|
||||||
timeout = 10
|
timeout = 10
|
||||||
|
addopts = "--disable-socket --allow-unix-socket"
|
||||||
|
|
||||||
[tool.doc8]
|
[tool.doc8]
|
||||||
paths = ["docs"]
|
paths = ["docs"]
|
||||||
|
14
uv.lock
14
uv.lock
@ -1277,6 +1277,18 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/f2/3b/b26f90f74e2986a82df6e7ac7e319b8ea7ccece1caec9f8ab6104dc70603/pytest_mock-3.14.0-py3-none-any.whl", hash = "sha256:0b72c38033392a5f4621342fe11e9219ac11ec9d375f8e2a0c164539e0d70f6f", size = 9863 },
|
{ url = "https://files.pythonhosted.org/packages/f2/3b/b26f90f74e2986a82df6e7ac7e319b8ea7ccece1caec9f8ab6104dc70603/pytest_mock-3.14.0-py3-none-any.whl", hash = "sha256:0b72c38033392a5f4621342fe11e9219ac11ec9d375f8e2a0c164539e0d70f6f", size = 9863 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pytest-socket"
|
||||||
|
version = "0.7.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "pytest" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/05/ff/90c7e1e746baf3d62ce864c479fd53410b534818b9437413903596f81580/pytest_socket-0.7.0.tar.gz", hash = "sha256:71ab048cbbcb085c15a4423b73b619a8b35d6a307f46f78ea46be51b1b7e11b3", size = 12389 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/19/58/5d14cb5cb59409e491ebe816c47bf81423cd03098ea92281336320ae5681/pytest_socket-0.7.0-py3-none-any.whl", hash = "sha256:7e0f4642177d55d317bbd58fc68c6bd9048d6eadb2d46a89307fa9221336ce45", size = 6754 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pytest-sugar"
|
name = "pytest-sugar"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@ -1369,6 +1381,7 @@ dev = [
|
|||||||
{ name = "pytest-cov" },
|
{ name = "pytest-cov" },
|
||||||
{ name = "pytest-freezer" },
|
{ name = "pytest-freezer" },
|
||||||
{ name = "pytest-mock" },
|
{ name = "pytest-mock" },
|
||||||
|
{ name = "pytest-socket" },
|
||||||
{ name = "pytest-sugar" },
|
{ name = "pytest-sugar" },
|
||||||
{ name = "pytest-timeout" },
|
{ name = "pytest-timeout" },
|
||||||
{ name = "pytest-xdist" },
|
{ name = "pytest-xdist" },
|
||||||
@ -1407,6 +1420,7 @@ dev = [
|
|||||||
{ name = "pytest-cov" },
|
{ name = "pytest-cov" },
|
||||||
{ name = "pytest-freezer", specifier = "~=0.4" },
|
{ name = "pytest-freezer", specifier = "~=0.4" },
|
||||||
{ name = "pytest-mock" },
|
{ name = "pytest-mock" },
|
||||||
|
{ name = "pytest-socket", specifier = ">=0.7.0" },
|
||||||
{ name = "pytest-sugar" },
|
{ name = "pytest-sugar" },
|
||||||
{ name = "pytest-timeout", specifier = "~=2.0" },
|
{ name = "pytest-timeout", specifier = "~=2.0" },
|
||||||
{ name = "pytest-xdist", specifier = ">=3.6.1" },
|
{ name = "pytest-xdist", specifier = ">=3.6.1" },
|
||||||
|
Loading…
Reference in New Issue
Block a user