mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Warn on undefined variables in templates (#48140)
* Warn on undefined variables in templates * Add test * fix tests * fix tests
This commit is contained in:
parent
863f75e65e
commit
f8755a52c2
@ -1318,7 +1318,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
|
|||||||
|
|
||||||
def __init__(self, hass, limited=False):
|
def __init__(self, hass, limited=False):
|
||||||
"""Initialise template environment."""
|
"""Initialise template environment."""
|
||||||
super().__init__()
|
super().__init__(undefined=jinja2.make_logging_undefined(logger=_LOGGER))
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.template_cache = weakref.WeakValueDictionary()
|
self.template_cache = weakref.WeakValueDictionary()
|
||||||
self.filters["round"] = forgiving_round
|
self.filters["round"] = forgiving_round
|
||||||
|
@ -442,7 +442,7 @@ async def test_error_querying_influx(
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"mock_client, config_ext, queries, set_query_mock, make_resultset",
|
"mock_client, config_ext, queries, set_query_mock, make_resultset, key",
|
||||||
[
|
[
|
||||||
(
|
(
|
||||||
DEFAULT_API_VERSION,
|
DEFAULT_API_VERSION,
|
||||||
@ -459,6 +459,7 @@ async def test_error_querying_influx(
|
|||||||
},
|
},
|
||||||
_set_query_mock_v1,
|
_set_query_mock_v1,
|
||||||
_make_v1_resultset,
|
_make_v1_resultset,
|
||||||
|
"where",
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
API_VERSION_2,
|
API_VERSION_2,
|
||||||
@ -466,12 +467,13 @@ async def test_error_querying_influx(
|
|||||||
{"queries_flux": [{"name": "test", "query": "{{ illegal.template }}"}]},
|
{"queries_flux": [{"name": "test", "query": "{{ illegal.template }}"}]},
|
||||||
_set_query_mock_v2,
|
_set_query_mock_v2,
|
||||||
_make_v2_resultset,
|
_make_v2_resultset,
|
||||||
|
"query",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
indirect=["mock_client"],
|
indirect=["mock_client"],
|
||||||
)
|
)
|
||||||
async def test_error_rendering_template(
|
async def test_error_rendering_template(
|
||||||
hass, caplog, mock_client, config_ext, queries, set_query_mock, make_resultset
|
hass, caplog, mock_client, config_ext, queries, set_query_mock, make_resultset, key
|
||||||
):
|
):
|
||||||
"""Test behavior of sensor with error rendering template."""
|
"""Test behavior of sensor with error rendering template."""
|
||||||
set_query_mock(mock_client, return_value=make_resultset(42))
|
set_query_mock(mock_client, return_value=make_resultset(42))
|
||||||
@ -479,7 +481,15 @@ async def test_error_rendering_template(
|
|||||||
sensors = await _setup(hass, config_ext, queries, ["sensor.test"])
|
sensors = await _setup(hass, config_ext, queries, ["sensor.test"])
|
||||||
assert sensors[0].state == STATE_UNKNOWN
|
assert sensors[0].state == STATE_UNKNOWN
|
||||||
assert (
|
assert (
|
||||||
len([record for record in caplog.records if record.levelname == "ERROR"]) == 1
|
len(
|
||||||
|
[
|
||||||
|
record
|
||||||
|
for record in caplog.records
|
||||||
|
if record.levelname == "ERROR"
|
||||||
|
and f"Could not render {key} template" in record.msg
|
||||||
|
]
|
||||||
|
)
|
||||||
|
== 1
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1277,8 +1277,7 @@ async def test_repeat_condition_warning(hass, caplog, condition):
|
|||||||
hass.async_create_task(script_obj.async_run(context=Context()))
|
hass.async_create_task(script_obj.async_run(context=Context()))
|
||||||
await asyncio.wait_for(hass.async_block_till_done(), 1)
|
await asyncio.wait_for(hass.async_block_till_done(), 1)
|
||||||
|
|
||||||
assert len(caplog.record_tuples) == 1
|
assert f"Error in '{condition}[0]' evaluation" in caplog.text
|
||||||
assert caplog.record_tuples[0][1] == logging.WARNING
|
|
||||||
|
|
||||||
assert len(events) == count
|
assert len(events) == count
|
||||||
|
|
||||||
|
@ -2497,3 +2497,10 @@ async def test_parse_result(hass):
|
|||||||
("0011101.00100001010001", "0011101.00100001010001"),
|
("0011101.00100001010001", "0011101.00100001010001"),
|
||||||
):
|
):
|
||||||
assert template.Template(tpl, hass).async_render() == result
|
assert template.Template(tpl, hass).async_render() == result
|
||||||
|
|
||||||
|
|
||||||
|
async def test_undefined_variable(hass, caplog):
|
||||||
|
"""Test a warning is logged on undefined variables."""
|
||||||
|
tpl = template.Template("{{ no_such_variable }}", hass)
|
||||||
|
assert tpl.async_render() == ""
|
||||||
|
assert "Template variable warning: no_such_variable is undefined" in caplog.text
|
||||||
|
Loading…
x
Reference in New Issue
Block a user