From f96c1136b0e25514db62cfcb93a7258385aad901 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 26 Apr 2022 22:42:37 +0200 Subject: [PATCH] Bump pytest-socket to 0.5.1 (#69624) Co-authored-by: J. Nick Koston --- requirements_test.txt | 2 +- tests/components/balboa/test_config_flow.py | 13 +++-- tests/conftest.py | 56 +-------------------- 3 files changed, 11 insertions(+), 60 deletions(-) diff --git a/requirements_test.txt b/requirements_test.txt index 7f827795676..31a87356fcc 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -19,7 +19,7 @@ pylint-strict-informational==0.1 pytest-aiohttp==0.3.0 pytest-cov==3.0.0 pytest-freezegun==0.4.2 -pytest-socket==0.4.1 +pytest-socket==0.5.1 pytest-test-groups==1.0.3 pytest-sugar==0.9.4 pytest-timeout==2.1.0 diff --git a/tests/components/balboa/test_config_flow.py b/tests/components/balboa/test_config_flow.py index 98c2a90abe2..ffb8c80a29e 100644 --- a/tests/components/balboa/test_config_flow.py +++ b/tests/components/balboa/test_config_flow.py @@ -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["step_id"] == "init" - result = await hass.config_entries.options.async_configure( - result["flow_id"], - user_input={CONF_SYNC_TIME: True}, - ) + with patch( + "homeassistant.components.balboa.async_setup_entry", + 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 dict(config_entry.options) == {CONF_SYNC_TIME: True} diff --git a/tests/conftest.py b/tests/conftest.py index 945e02e69f7..0155a965fe6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,6 @@ import asyncio from collections.abc import AsyncGenerator import functools import logging -import socket import ssl import threading 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 """ 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.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): """Convert datetime to FakeDatetime.