Warn on undefined variables in templates (#48140)

* Warn on undefined variables in templates

* Add test

* fix tests

* fix tests
pull/48163/head
Erik Montnemery 2021-03-20 15:16:04 +01:00 committed by GitHub
parent 863f75e65e
commit f8755a52c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 6 deletions

View File

@ -1318,7 +1318,7 @@ class TemplateEnvironment(ImmutableSandboxedEnvironment):
def __init__(self, hass, limited=False):
"""Initialise template environment."""
super().__init__()
super().__init__(undefined=jinja2.make_logging_undefined(logger=_LOGGER))
self.hass = hass
self.template_cache = weakref.WeakValueDictionary()
self.filters["round"] = forgiving_round

View File

@ -442,7 +442,7 @@ async def test_error_querying_influx(
@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,
@ -459,6 +459,7 @@ async def test_error_querying_influx(
},
_set_query_mock_v1,
_make_v1_resultset,
"where",
),
(
API_VERSION_2,
@ -466,12 +467,13 @@ async def test_error_querying_influx(
{"queries_flux": [{"name": "test", "query": "{{ illegal.template }}"}]},
_set_query_mock_v2,
_make_v2_resultset,
"query",
),
],
indirect=["mock_client"],
)
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."""
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"])
assert sensors[0].state == STATE_UNKNOWN
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
)

View File

@ -1277,8 +1277,7 @@ async def test_repeat_condition_warning(hass, caplog, condition):
hass.async_create_task(script_obj.async_run(context=Context()))
await asyncio.wait_for(hass.async_block_till_done(), 1)
assert len(caplog.record_tuples) == 1
assert caplog.record_tuples[0][1] == logging.WARNING
assert f"Error in '{condition}[0]' evaluation" in caplog.text
assert len(events) == count

View File

@ -2497,3 +2497,10 @@ async def test_parse_result(hass):
("0011101.00100001010001", "0011101.00100001010001"),
):
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