Fail on unused ignore_translations fixture (#128422)

* Fail on unused ignore_translations fixture

* Cleanup melcloud

* Use pytest.fail

* Cleanup tplink

* Cleanup matter
pull/128434/head
epenet 2024-10-15 12:40:47 +02:00 committed by GitHub
parent a14cb13194
commit 84b2c74057
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 37 additions and 34 deletions

View File

@ -48,18 +48,6 @@ NAME = "Name"
TEST_DOMAIN = "fake_integration"
@pytest.fixture
def ignore_translations() -> list[str]:
"""Ignore specific translations.
We can ignore translations for the fake_integration we are testing with.
"""
return [
f"component.{TEST_DOMAIN}.config.abort.missing_configuration",
f"component.{TEST_DOMAIN}.config.abort.missing_credentials",
]
@pytest.fixture
async def authorization_server() -> AuthorizationServer:
"""Fixture AuthorizationServer for mock application_credentials integration."""
@ -435,6 +423,10 @@ async def test_import_named_credential(
]
@pytest.mark.parametrize(
"ignore_translations",
["component.fake_integration.config.abort.missing_credentials"],
)
async def test_config_flow_no_credentials(hass: HomeAssistant) -> None:
"""Test config flow base case with no credentials registered."""
result = await hass.config_entries.flow.async_init(
@ -444,6 +436,10 @@ async def test_config_flow_no_credentials(hass: HomeAssistant) -> None:
assert result.get("reason") == "missing_credentials"
@pytest.mark.parametrize(
"ignore_translations",
["component.fake_integration.config.abort.missing_credentials"],
)
async def test_config_flow_other_domain(
hass: HomeAssistant,
ws_client: ClientFixture,
@ -571,6 +567,10 @@ async def test_config_flow_multiple_entries(
)
@pytest.mark.parametrize(
"ignore_translations",
["component.fake_integration.config.abort.missing_credentials"],
)
async def test_config_flow_create_delete_credential(
hass: HomeAssistant,
ws_client: ClientFixture,
@ -616,6 +616,10 @@ async def test_config_flow_with_config_credential(
assert result["data"].get("auth_implementation") == TEST_DOMAIN
@pytest.mark.parametrize(
"ignore_translations",
["component.fake_integration.config.abort.missing_configuration"],
)
@pytest.mark.parametrize("mock_application_credentials_integration", [None])
async def test_import_without_setup(hass: HomeAssistant, config_credential) -> None:
"""Test import of credentials without setting up the integration."""
@ -631,6 +635,10 @@ async def test_import_without_setup(hass: HomeAssistant, config_credential) -> N
assert result.get("reason") == "missing_configuration"
@pytest.mark.parametrize(
"ignore_translations",
["component.fake_integration.config.abort.missing_configuration"],
)
@pytest.mark.parametrize("mock_application_credentials_integration", [None])
async def test_websocket_without_platform(
hass: HomeAssistant, ws_client: ClientFixture

View File

@ -468,7 +468,7 @@ def supervisor_client() -> Generator[AsyncMock]:
async def _ensure_translation_exists(
hass: HomeAssistant,
ignore_translations: list[str],
ignore_translations: dict[str, StoreInfo],
category: str,
component: str,
key: str,
@ -476,6 +476,7 @@ async def _ensure_translation_exists(
"""Raise if translation doesn't exist."""
full_key = f"component.{component}.{category}.{key}"
if full_key in ignore_translations:
ignore_translations[full_key] = "used"
return
translations = await async_get_translations(hass, "en", category, [component])
@ -497,14 +498,14 @@ async def _ensure_translation_exists(
):
return
raise ValueError(
pytest.fail(
f"Translation not found for {component}: `{category}.{key}`. "
f"Please add to homeassistant/components/{component}/strings.json"
)
@pytest.fixture
def ignore_translations() -> list[str]:
def ignore_translations() -> str | list[str]:
"""Ignore specific translations.
Override or parametrize this fixture with a fixture that returns,
@ -514,8 +515,12 @@ def ignore_translations() -> list[str]:
@pytest.fixture(autouse=True)
def check_config_translations(ignore_translations: list[str]) -> Generator[None]:
def check_config_translations(ignore_translations: str | list[str]) -> Generator[None]:
"""Ensure config_flow translations are available."""
if not isinstance(ignore_translations, list):
ignore_translations = [ignore_translations]
_ignore_translations = {k: "unused" for k in ignore_translations}
_original = FlowManager._async_handle_step
async def _async_handle_step(
@ -535,7 +540,7 @@ def check_config_translations(ignore_translations: list[str]) -> Generator[None]
):
await _ensure_translation_exists(
flow.hass,
ignore_translations,
_ignore_translations,
category,
component,
f"abort.{result["reason"]}",
@ -548,3 +553,10 @@ def check_config_translations(ignore_translations: list[str]) -> Generator[None]
_async_handle_step,
):
yield
unused_ignore = [k for k, v in _ignore_translations.items() if v == "unused"]
if unused_ignore:
pytest.fail(
f"Unused ignore translations: {', '.join(unused_ignore)}. "
"Please remove them from the ignore_translations fixture."
)

View File

@ -827,10 +827,6 @@ async def test_addon_running(
assert setup_entry.call_count == 1
@pytest.mark.parametrize( # Remove when translations fixed
"ignore_translations",
["component.matter.config.abort.cannot_connect"],
)
@pytest.mark.parametrize(
(
"discovery_info",

View File

@ -98,15 +98,6 @@ async def test_form_errors(
assert result["reason"] == reason
@pytest.mark.parametrize( # Remove when translations fixed
"ignore_translations",
[
[
"component.melcloud.config.abort.cannot_connect",
"component.melcloud.config.abort.invalid_auth",
],
],
)
@pytest.mark.parametrize(
("error", "message"),
[

View File

@ -1348,10 +1348,6 @@ async def test_reauth_errors(
assert result3["reason"] == "reauth_successful"
@pytest.mark.parametrize( # Remove when translations fixed
"ignore_translations",
["component.tplink.config.abort.cannot_connect"],
)
@pytest.mark.parametrize(
("error_type", "expected_flow"),
[