From cfaf18f942191745c7ef731c5e57aad07f322df8 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 5 Mar 2025 18:42:34 +0100 Subject: [PATCH] Improve the mock_integration_frame test fixture (#139850) * Improve the mock_integration_frame test fixture * Update test --- tests/conftest.py | 4 ++++ tests/helpers/snapshots/test_frame.ambr | 16 ++++++++-------- tests/helpers/test_frame.py | 2 +- tests/test_config_entries.py | 3 ++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 2f7330ebf22..dc834633774 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,6 +16,7 @@ import reprlib from shutil import rmtree import sqlite3 import ssl +import sys import threading from typing import TYPE_CHECKING, Any, cast from unittest.mock import AsyncMock, MagicMock, Mock, _patch, patch @@ -1889,12 +1890,15 @@ def mock_integration_frame(integration_frame_path: str) -> Generator[Mock]: Defaults to calling from `hue` core integration, and can be parametrized with `integration_frame_path`. """ + correct_filename = f"/home/paulus/{integration_frame_path}/light.py" + correct_module_name = f"{integration_frame_path.replace('/', '.')}.light" correct_frame = Mock( filename=f"/home/paulus/{integration_frame_path}/light.py", lineno="23", line="self.light.is_on", ) with ( + patch.dict(sys.modules, {correct_module_name: Mock(__file__=correct_filename)}), patch( "homeassistant.helpers.frame.linecache.getline", return_value=correct_frame.line, diff --git a/tests/helpers/snapshots/test_frame.ambr b/tests/helpers/snapshots/test_frame.ambr index f3fbd54cf45..996fd33ada4 100644 --- a/tests/helpers/snapshots/test_frame.ambr +++ b/tests/helpers/snapshots/test_frame.ambr @@ -10,7 +10,7 @@ # --- # name: test_report[custom integration default] list([ - "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_custom_integration%22", + "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_custom_integration' custom integration", ]) # --- # name: test_report[disable error_if_core] @@ -25,7 +25,7 @@ # --- # name: test_report[error_if_integration with custom integration] list([ - "Detected that custom integration 'test_integration_frame' test_report_string at custom_components/test_integration_frame/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_integration_frame%22", + "Detected that custom integration 'test_integration_frame' test_report_string at custom_components/test_integration_frame/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_integration_frame' custom integration", ]) # --- # name: test_report[log_custom_component_only with core integration] @@ -34,7 +34,7 @@ # --- # name: test_report[log_custom_component_only with custom integration] list([ - "Detected that custom integration 'test_integration_frame' test_report_string at custom_components/test_integration_frame/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_integration_frame%22", + "Detected that custom integration 'test_integration_frame' test_report_string at custom_components/test_integration_frame/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_integration_frame' custom integration", ]) # --- # name: test_report_usage[core default] @@ -66,12 +66,12 @@ # --- # name: test_report_usage[custom integration default] list([ - "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_custom_integration%22", + "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_custom_integration' custom integration", ]) # --- # name: test_report_usage[custom integration error] list([ - "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_custom_integration%22", + "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_custom_integration' custom integration", ]) # --- # name: test_report_usage[custom integration ignore] @@ -95,7 +95,7 @@ # --- # name: test_report_usage_find_issue_tracker[unknown custom integration] list([ - "Detected that custom integration 'unknown_custom_integration' test_report_string at custom_components/unknown_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+unknown_custom_integration%22", + "Detected that custom integration 'unknown_custom_integration' test_report_string at custom_components/unknown_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'unknown_custom_integration' custom integration", ]) # --- # name: test_report_usage_find_issue_tracker_other_thread[core integration] @@ -110,11 +110,11 @@ # --- # name: test_report_usage_find_issue_tracker_other_thread[custom integration] list([ - "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+test_custom_integration%22", + "Detected that custom integration 'test_custom_integration' test_report_string at custom_components/test_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'test_custom_integration' custom integration", ]) # --- # name: test_report_usage_find_issue_tracker_other_thread[unknown custom integration] list([ - "Detected that custom integration 'unknown_custom_integration' test_report_string at custom_components/unknown_custom_integration/light.py, line 23: self.light.is_on. Please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+unknown_custom_integration%22", + "Detected that custom integration 'unknown_custom_integration' test_report_string at custom_components/unknown_custom_integration/light.py, line 23: self.light.is_on. Please report it to the author of the 'unknown_custom_integration' custom integration", ]) # --- diff --git a/tests/helpers/test_frame.py b/tests/helpers/test_frame.py index 22209380dfe..6d53088d9df 100644 --- a/tests/helpers/test_frame.py +++ b/tests/helpers/test_frame.py @@ -23,7 +23,7 @@ async def test_extract_frame_integration( custom_integration=False, frame=mock_integration_frame, integration="hue", - module=None, + module="homeassistant.components.hue.light", relative_filename="homeassistant/components/hue/light.py", ) diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 66aa29d95d1..857c5952df9 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -8849,7 +8849,8 @@ async def test_options_flow_deprecated_config_entry_setter( "config_entry explicitly, which is deprecated at " "custom_components/my_integration/light.py, line 23: " "self.light.is_on. This will stop working in Home Assistant 2025.12, please " - "create a bug report at " in caplog.text + "report it to the author of the 'my_integration' custom integration" + in caplog.text )