Use loader.async_suggest_report_issue in frame helper (#101461)

pull/101490/head
Erik Montnemery 2023-10-05 19:52:26 +02:00 committed by GitHub
parent cb0a05142d
commit dce5099d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 30 deletions

View File

@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import Callable from collections.abc import Callable
from contextlib import suppress
from dataclasses import dataclass from dataclasses import dataclass
import functools import functools
import logging import logging
@ -10,7 +11,9 @@ import sys
from traceback import FrameSummary, extract_stack from traceback import FrameSummary, extract_stack
from typing import Any, TypeVar, cast from typing import Any, TypeVar, cast
from homeassistant.core import HomeAssistant, async_get_hass
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.loader import async_suggest_report_issue
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -118,23 +121,25 @@ def _report_integration(
return return
_REPORTED_INTEGRATIONS.add(key) _REPORTED_INTEGRATIONS.add(key)
if integration_frame.custom_integration: hass: HomeAssistant | None = None
extra = " to the custom integration author" with suppress(HomeAssistantError):
else: hass = async_get_hass()
extra = "" report_issue = async_suggest_report_issue(
hass,
integration_domain=integration_frame.integration,
module=integration_frame.module,
)
_LOGGER.log( _LOGGER.log(
level, level,
( "Detected that %sintegration '%s' %s at %s, line %s: %s, please %s",
"Detected integration that %s. " "custom " if integration_frame.custom_integration else "",
"Please report issue%s for %s using this method at %s, line %s: %s"
),
what,
extra,
integration_frame.integration, integration_frame.integration,
what,
integration_frame.relative_filename, integration_frame.relative_filename,
found_frame.lineno, found_frame.lineno,
(found_frame.line or "?").strip(), (found_frame.line or "?").strip(),
report_issue,
) )

View File

@ -21,7 +21,7 @@ from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE, HomeAssistant
import homeassistant.helpers.aiohttp_client as client import homeassistant.helpers.aiohttp_client as client
from homeassistant.util.color import RGBColor from homeassistant.util.color import RGBColor
from tests.common import MockConfigEntry from tests.common import MockConfigEntry, MockModule, mock_integration
from tests.test_util.aiohttp import AiohttpClientMocker from tests.test_util.aiohttp import AiohttpClientMocker
@ -155,9 +155,10 @@ async def test_warning_close_session_integration(
session = client.async_get_clientsession(hass) session = client.async_get_clientsession(hass)
await session.close() await session.close()
assert ( assert (
"Detected integration that closes the Home Assistant aiohttp session. " "Detected that integration 'hue' closes the Home Assistant aiohttp session at "
"Please report issue for hue using this method at " "homeassistant/components/hue/light.py, line 23: await session.close(), "
"homeassistant/components/hue/light.py, line 23: await session.close()" "please create a bug report at https://github.com/home-assistant/core/issues?"
"q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+hue%22"
) in caplog.text ) in caplog.text
@ -166,6 +167,7 @@ async def test_warning_close_session_custom(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test log warning message when closing the session from custom context.""" """Test log warning message when closing the session from custom context."""
mock_integration(hass, MockModule("hue"), built_in=False)
with patch( with patch(
"homeassistant.helpers.frame.extract_stack", "homeassistant.helpers.frame.extract_stack",
return_value=[ return_value=[
@ -189,10 +191,10 @@ async def test_warning_close_session_custom(
session = client.async_get_clientsession(hass) session = client.async_get_clientsession(hass)
await session.close() await session.close()
assert ( assert (
"Detected integration that closes the Home Assistant aiohttp session. Please" "Detected that custom integration 'hue' closes the Home Assistant aiohttp "
" report issue to the custom integration author for hue using this method at" "session at custom_components/hue/light.py, line 23: await session.close(), "
" custom_components/hue/light.py, line 23: await session.close()" in caplog.text "please report it to the author of the 'hue' custom integration"
) ) in caplog.text
async def test_async_aiohttp_proxy_stream( async def test_async_aiohttp_proxy_stream(

View File

@ -132,7 +132,7 @@ async def test_extract_frame_no_integration(caplog: pytest.LogCaptureFixture) ->
@patch.object(frame, "_REPORTED_INTEGRATIONS", set()) @patch.object(frame, "_REPORTED_INTEGRATIONS", set())
async def test_prevent_flooding( async def test_prevent_flooding(
caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock hass: HomeAssistant, caplog: pytest.LogCaptureFixture, mock_integration_frame: Mock
) -> None: ) -> None:
"""Test to ensure a report is only written once to the log.""" """Test to ensure a report is only written once to the log."""
@ -142,9 +142,10 @@ async def test_prevent_flooding(
filename = "homeassistant/components/hue/light.py" filename = "homeassistant/components/hue/light.py"
expected_message = ( expected_message = (
f"Detected integration that {what}. Please report issue for {integration} using" f"Detected that integration '{integration}' {what} at {filename}, line "
f" this method at {filename}, line " f"{mock_integration_frame.lineno}: {mock_integration_frame.line}, "
f"{mock_integration_frame.lineno}: {mock_integration_frame.line}" f"please create a bug report at https://github.com/home-assistant/core/issues?"
f"q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+{integration}%22"
) )
frame.report(what, error_if_core=False) frame.report(what, error_if_core=False)

View File

@ -7,6 +7,8 @@ import pytest
from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE, HomeAssistant from homeassistant.core import EVENT_HOMEASSISTANT_CLOSE, HomeAssistant
import homeassistant.helpers.httpx_client as client import homeassistant.helpers.httpx_client as client
from tests.common import MockModule, mock_integration
async def test_get_async_client_with_ssl(hass: HomeAssistant) -> None: async def test_get_async_client_with_ssl(hass: HomeAssistant) -> None:
"""Test init async client with ssl.""" """Test init async client with ssl."""
@ -125,9 +127,10 @@ async def test_warning_close_session_integration(
await httpx_session.aclose() await httpx_session.aclose()
assert ( assert (
"Detected integration that closes the Home Assistant httpx client. " "Detected that integration 'hue' closes the Home Assistant httpx client at "
"Please report issue for hue using this method at " "homeassistant/components/hue/light.py, line 23: await session.aclose(), "
"homeassistant/components/hue/light.py, line 23: await session.aclose()" "please create a bug report at https://github.com/home-assistant/core/issues?"
"q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+hue%22"
) in caplog.text ) in caplog.text
@ -136,6 +139,7 @@ async def test_warning_close_session_custom(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None:
"""Test log warning message when closing the session from custom context.""" """Test log warning message when closing the session from custom context."""
mock_integration(hass, MockModule("hue"), built_in=False)
with patch( with patch(
"homeassistant.helpers.frame.extract_stack", "homeassistant.helpers.frame.extract_stack",
return_value=[ return_value=[
@ -159,8 +163,7 @@ async def test_warning_close_session_custom(
httpx_session = client.get_async_client(hass) httpx_session = client.get_async_client(hass)
await httpx_session.aclose() await httpx_session.aclose()
assert ( assert (
"Detected integration that closes the Home Assistant httpx client. Please" "Detected that custom integration 'hue' closes the Home Assistant httpx client "
" report issue to the custom integration author for hue using this method at" "at custom_components/hue/light.py, line 23: await session.aclose(), "
" custom_components/hue/light.py, line 23: await session.aclose()" "please report it to the author of the 'hue' custom integration"
in caplog.text ) in caplog.text
)