Compare commits

...

8 Commits

Author SHA1 Message Date
Erik Montnemery
d937ebaede Merge branch 'dev' into always_fail_tests_when_socket_blocked_is_raised 2025-11-11 15:00:49 +01:00
Erik Montnemery
ad62ee1bba Merge branch 'dev' into always_fail_tests_when_socket_blocked_is_raised 2025-10-29 08:41:23 +01:00
Erik
276129272d Adjust 2025-10-28 16:35:12 +01:00
Erik Montnemery
db22332007 Merge branch 'dev' into always_fail_tests_when_socket_blocked_is_raised 2025-10-27 17:23:08 +01:00
Erik Montnemery
dec9620d00 Update conftest.py 2025-10-24 18:11:27 +02:00
Erik
f50f8e29db Adjust 2025-09-10 12:14:40 +02:00
Erik
2a91efdd74 Adjust 2025-09-10 11:05:00 +02:00
Erik
a2f8b85cf9 Always fail tests when pytest_socket.SocketBlockedError is raised 2025-09-10 10:56:57 +02:00

View File

@@ -21,6 +21,7 @@ import threading
from typing import TYPE_CHECKING, Any, cast
from unittest.mock import AsyncMock, MagicMock, Mock, _patch, patch
import _pytest
from aiohttp import client
from aiohttp.resolver import AsyncResolver
from aiohttp.test_utils import (
@@ -180,18 +181,27 @@ def pytest_runtest_setup() -> None:
"""Prepare pytest_socket and freezegun.
pytest_socket:
Throw if tests attempt to open sockets.
- Throw if tests attempt to open sockets.
allow_unix_socket is set to True because it's needed by asyncio.
Important: socket_allow_hosts must be called before disable_socket, otherwise all
destinations will be allowed.
- allow_unix_socket is set to True because it's needed by asyncio.
Important: socket_allow_hosts must be called before disable_socket, otherwise all
destinations will be allowed.
- Replace pytest_socket.SocketBlockedError with a variant which inherits from
"pytest.Failed" instead of "RuntimeError" so that it is not caught when catching
and logging "Exception".
freezegun:
Modified to include https://github.com/spulec/freezegun/pull/424 and improve class str.
- Modified to include https://github.com/spulec/freezegun/pull/424 and improve class str.
"""
pytest_socket.socket_allow_hosts(["127.0.0.1"])
pytest_socket.disable_socket(allow_unix_socket=True)
class SocketBlockedError(_pytest.outcomes.Failed):
def __init__(self, *_args, **_kwargs) -> None:
super().__init__("A test tried to use socket.socket.")
pytest_socket.SocketBlockedError = SocketBlockedError
freezegun.api.FakeDate = patch_time.HAFakeDate # type: ignore[attr-defined]
freezegun.api.datetime_to_fakedatetime = patch_time.ha_datetime_to_fakedatetime # type: ignore[attr-defined]