1
mirror of https://github.com/home-assistant/core synced 2024-07-21 14:24:50 +02:00

Fix including yaml files with scalar values (#103914)

This commit is contained in:
Erik Montnemery 2023-11-13 15:23:50 +01:00 committed by GitHub
parent 07af073735
commit f89194784d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 22 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Iterator
from contextlib import suppress
import fnmatch
from io import StringIO, TextIOWrapper
import logging
@ -230,8 +231,9 @@ def _add_reference( # type: ignore[no-untyped-def]
obj = NodeListClass(obj)
if isinstance(obj, str):
obj = NodeStrClass(obj)
setattr(obj, "__config_file__", loader.get_name())
setattr(obj, "__line__", node.start_mark.line + 1)
with suppress(AttributeError):
setattr(obj, "__config_file__", loader.get_name())
setattr(obj, "__line__", node.start_mark.line + 1)
return obj

View File

@ -110,7 +110,11 @@ def test_invalid_environment_variable(try_both_loaders) -> None:
@pytest.mark.parametrize(
("hass_config_yaml_files", "value"),
[({"test.yaml": "value"}, "value"), ({"test.yaml": None}, {})],
[
({"test.yaml": "value"}, "value"),
({"test.yaml": None}, {}),
({"test.yaml": "123"}, 123),
],
)
def test_include_yaml(
try_both_loaders, mock_hass_config_yaml: None, value: Any
@ -124,10 +128,14 @@ def test_include_yaml(
@patch("homeassistant.util.yaml.loader.os.walk")
@pytest.mark.parametrize(
"hass_config_yaml_files", [{"/test/one.yaml": "one", "/test/two.yaml": "two"}]
("hass_config_yaml_files", "value"),
[
({"/test/one.yaml": "one", "/test/two.yaml": "two"}, ["one", "two"]),
({"/test/one.yaml": "1", "/test/two.yaml": "2"}, [1, 2]),
],
)
def test_include_dir_list(
mock_walk, try_both_loaders, mock_hass_config_yaml: None
mock_walk, try_both_loaders, mock_hass_config_yaml: None, value: Any
) -> None:
"""Test include dir list yaml."""
mock_walk.return_value = [["/test", [], ["two.yaml", "one.yaml"]]]
@ -135,7 +143,7 @@ def test_include_dir_list(
conf = "key: !include_dir_list /test"
with io.StringIO(conf) as file:
doc = yaml_loader.parse_yaml(file)
assert doc["key"] == sorted(["one", "two"])
assert sorted(doc["key"]) == sorted(value)
@patch("homeassistant.util.yaml.loader.os.walk")
@ -170,11 +178,20 @@ def test_include_dir_list_recursive(
@patch("homeassistant.util.yaml.loader.os.walk")
@pytest.mark.parametrize(
"hass_config_yaml_files",
[{"/test/first.yaml": "one", "/test/second.yaml": "two"}],
("hass_config_yaml_files", "value"),
[
(
{"/test/first.yaml": "one", "/test/second.yaml": "two"},
{"first": "one", "second": "two"},
),
(
{"/test/first.yaml": "1", "/test/second.yaml": "2"},
{"first": 1, "second": 2},
),
],
)
def test_include_dir_named(
mock_walk, try_both_loaders, mock_hass_config_yaml: None
mock_walk, try_both_loaders, mock_hass_config_yaml: None, value: Any
) -> None:
"""Test include dir named yaml."""
mock_walk.return_value = [
@ -182,10 +199,9 @@ def test_include_dir_named(
]
conf = "key: !include_dir_named /test"
correct = {"first": "one", "second": "two"}
with io.StringIO(conf) as file:
doc = yaml_loader.parse_yaml(file)
assert doc["key"] == correct
assert doc["key"] == value
@patch("homeassistant.util.yaml.loader.os.walk")
@ -221,11 +237,20 @@ def test_include_dir_named_recursive(
@patch("homeassistant.util.yaml.loader.os.walk")
@pytest.mark.parametrize(
"hass_config_yaml_files",
[{"/test/first.yaml": "- one", "/test/second.yaml": "- two\n- three"}],
("hass_config_yaml_files", "value"),
[
(
{"/test/first.yaml": "- one", "/test/second.yaml": "- two\n- three"},
["one", "two", "three"],
),
(
{"/test/first.yaml": "- 1", "/test/second.yaml": "- 2\n- 3"},
[1, 2, 3],
),
],
)
def test_include_dir_merge_list(
mock_walk, try_both_loaders, mock_hass_config_yaml: None
mock_walk, try_both_loaders, mock_hass_config_yaml: None, value: Any
) -> None:
"""Test include dir merge list yaml."""
mock_walk.return_value = [["/test", [], ["first.yaml", "second.yaml"]]]
@ -233,7 +258,7 @@ def test_include_dir_merge_list(
conf = "key: !include_dir_merge_list /test"
with io.StringIO(conf) as file:
doc = yaml_loader.parse_yaml(file)
assert sorted(doc["key"]) == sorted(["one", "two", "three"])
assert sorted(doc["key"]) == sorted(value)
@patch("homeassistant.util.yaml.loader.os.walk")
@ -268,16 +293,26 @@ def test_include_dir_merge_list_recursive(
@patch("homeassistant.util.yaml.loader.os.walk")
@pytest.mark.parametrize(
"hass_config_yaml_files",
("hass_config_yaml_files", "value"),
[
{
"/test/first.yaml": "key1: one",
"/test/second.yaml": "key2: two\nkey3: three",
}
(
{
"/test/first.yaml": "key1: one",
"/test/second.yaml": "key2: two\nkey3: three",
},
{"key1": "one", "key2": "two", "key3": "three"},
),
(
{
"/test/first.yaml": "key1: 1",
"/test/second.yaml": "key2: 2\nkey3: 3",
},
{"key1": 1, "key2": 2, "key3": 3},
),
],
)
def test_include_dir_merge_named(
mock_walk, try_both_loaders, mock_hass_config_yaml: None
mock_walk, try_both_loaders, mock_hass_config_yaml: None, value: Any
) -> None:
"""Test include dir merge named yaml."""
mock_walk.return_value = [["/test", [], ["first.yaml", "second.yaml"]]]
@ -285,7 +320,7 @@ def test_include_dir_merge_named(
conf = "key: !include_dir_merge_named /test"
with io.StringIO(conf) as file:
doc = yaml_loader.parse_yaml(file)
assert doc["key"] == {"key1": "one", "key2": "two", "key3": "three"}
assert doc["key"] == value
@patch("homeassistant.util.yaml.loader.os.walk")