Bump pytest-socket to 0.5.1 (#69624)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Erik Montnemery 2022-04-26 22:42:37 +02:00 committed by GitHub
parent c7c011d772
commit f96c1136b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 60 deletions

View file

@ -19,7 +19,7 @@ pylint-strict-informational==0.1
pytest-aiohttp==0.3.0 pytest-aiohttp==0.3.0
pytest-cov==3.0.0 pytest-cov==3.0.0
pytest-freezegun==0.4.2 pytest-freezegun==0.4.2
pytest-socket==0.4.1 pytest-socket==0.5.1
pytest-test-groups==1.0.3 pytest-test-groups==1.0.3
pytest-sugar==0.9.4 pytest-sugar==0.9.4
pytest-timeout==2.1.0 pytest-timeout==2.1.0

View file

@ -127,10 +127,15 @@ async def test_options_flow(hass: HomeAssistant, client: MagicMock) -> None:
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure( with patch(
result["flow_id"], "homeassistant.components.balboa.async_setup_entry",
user_input={CONF_SYNC_TIME: True}, return_value=True,
) ):
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_SYNC_TIME: True},
)
await hass.async_block_till_done()
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert dict(config_entry.options) == {CONF_SYNC_TIME: True} assert dict(config_entry.options) == {CONF_SYNC_TIME: True}

View file

@ -5,7 +5,6 @@ import asyncio
from collections.abc import AsyncGenerator from collections.abc import AsyncGenerator
import functools import functools
import logging import logging
import socket
import ssl import ssl
import threading import threading
from unittest.mock import AsyncMock, MagicMock, Mock, patch from unittest.mock import AsyncMock, MagicMock, Mock, patch
@ -87,65 +86,12 @@ def pytest_runtest_setup():
Modified to include https://github.com/spulec/freezegun/pull/424 Modified to include https://github.com/spulec/freezegun/pull/424
""" """
pytest_socket.socket_allow_hosts(["127.0.0.1"]) pytest_socket.socket_allow_hosts(["127.0.0.1"])
disable_socket(allow_unix_socket=True) pytest_socket.disable_socket(allow_unix_socket=True)
freezegun.api.datetime_to_fakedatetime = ha_datetime_to_fakedatetime freezegun.api.datetime_to_fakedatetime = ha_datetime_to_fakedatetime
freezegun.api.FakeDatetime = HAFakeDatetime freezegun.api.FakeDatetime = HAFakeDatetime
@pytest.fixture
def socket_disabled(pytestconfig):
"""Disable socket.socket for duration of this test function.
This incorporates changes from https://github.com/miketheman/pytest-socket/pull/76
and hardcodes allow_unix_socket to True because it's not passed on the command line.
"""
socket_was_enabled = socket.socket == pytest_socket._true_socket
disable_socket(allow_unix_socket=True)
yield
if socket_was_enabled:
pytest_socket.enable_socket()
@pytest.fixture
def socket_enabled(pytestconfig):
"""Enable socket.socket for duration of this test function.
This incorporates changes from https://github.com/miketheman/pytest-socket/pull/76
and hardcodes allow_unix_socket to True because it's not passed on the command line.
"""
socket_was_disabled = socket.socket != pytest_socket._true_socket
pytest_socket.enable_socket()
yield
if socket_was_disabled:
disable_socket(allow_unix_socket=True)
def disable_socket(allow_unix_socket=False):
"""Disable socket.socket to disable the Internet. useful in testing.
This incorporates changes from https://github.com/miketheman/pytest-socket/pull/75
"""
class GuardedSocket(socket.socket):
"""socket guard to disable socket creation (from pytest-socket)."""
def __new__(cls, *args, **kwargs):
try:
if len(args) > 0:
is_unix_socket = args[0] == socket.AF_UNIX
else:
is_unix_socket = kwargs.get("family") == socket.AF_UNIX
except AttributeError:
# AF_UNIX not supported on Windows https://bugs.python.org/issue33408
is_unix_socket = False
if is_unix_socket and allow_unix_socket:
return super().__new__(cls, *args, **kwargs)
raise pytest_socket.SocketBlockedError()
socket.socket = GuardedSocket
def ha_datetime_to_fakedatetime(datetime): def ha_datetime_to_fakedatetime(datetime):
"""Convert datetime to FakeDatetime. """Convert datetime to FakeDatetime.