Skip to content

Commit

Permalink
Fail on unused ignore_translations fixture (home-assistant#128422)
Browse files Browse the repository at this point in the history
* Fail on unused ignore_translations fixture

* Cleanup melcloud

* Use pytest.fail

* Cleanup tplink

* Cleanup matter
  • Loading branch information
epenet authored Oct 15, 2024
1 parent a14cb13 commit 84b2c74
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 34 deletions.
32 changes: 20 additions & 12 deletions tests/components/application_credentials/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@
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."""
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand All @@ -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
Expand Down
22 changes: 17 additions & 5 deletions tests/components/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,15 @@ 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,
) -> None:
"""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])
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -535,7 +540,7 @@ async def _async_handle_step(
):
await _ensure_translation_exists(
flow.hass,
ignore_translations,
_ignore_translations,
category,
component,
f"abort.{result["reason"]}",
Expand All @@ -548,3 +553,10 @@ async def _async_handle_step(
_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."
)
4 changes: 0 additions & 4 deletions tests/components/matter/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 0 additions & 9 deletions tests/components/melcloud/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down
4 changes: 0 additions & 4 deletions tests/components/tplink/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down

0 comments on commit 84b2c74

Please sign in to comment.