mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Fix including yaml files with scalar values (#103914)
This commit is contained in:
parent
07af073735
commit
f89194784d
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Iterator
|
from collections.abc import Iterator
|
||||||
|
from contextlib import suppress
|
||||||
import fnmatch
|
import fnmatch
|
||||||
from io import StringIO, TextIOWrapper
|
from io import StringIO, TextIOWrapper
|
||||||
import logging
|
import logging
|
||||||
@ -230,8 +231,9 @@ def _add_reference( # type: ignore[no-untyped-def]
|
|||||||
obj = NodeListClass(obj)
|
obj = NodeListClass(obj)
|
||||||
if isinstance(obj, str):
|
if isinstance(obj, str):
|
||||||
obj = NodeStrClass(obj)
|
obj = NodeStrClass(obj)
|
||||||
setattr(obj, "__config_file__", loader.get_name())
|
with suppress(AttributeError):
|
||||||
setattr(obj, "__line__", node.start_mark.line + 1)
|
setattr(obj, "__config_file__", loader.get_name())
|
||||||
|
setattr(obj, "__line__", node.start_mark.line + 1)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
@ -110,7 +110,11 @@ def test_invalid_environment_variable(try_both_loaders) -> None:
|
|||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("hass_config_yaml_files", "value"),
|
("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(
|
def test_include_yaml(
|
||||||
try_both_loaders, mock_hass_config_yaml: None, value: Any
|
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")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@pytest.mark.parametrize(
|
@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(
|
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:
|
) -> None:
|
||||||
"""Test include dir list yaml."""
|
"""Test include dir list yaml."""
|
||||||
mock_walk.return_value = [["/test", [], ["two.yaml", "one.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"
|
conf = "key: !include_dir_list /test"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
doc = yaml_loader.parse_yaml(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")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@ -170,11 +178,20 @@ def test_include_dir_list_recursive(
|
|||||||
|
|
||||||
@patch("homeassistant.util.yaml.loader.os.walk")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"hass_config_yaml_files",
|
("hass_config_yaml_files", "value"),
|
||||||
[{"/test/first.yaml": "one", "/test/second.yaml": "two"}],
|
[
|
||||||
|
(
|
||||||
|
{"/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(
|
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:
|
) -> None:
|
||||||
"""Test include dir named yaml."""
|
"""Test include dir named yaml."""
|
||||||
mock_walk.return_value = [
|
mock_walk.return_value = [
|
||||||
@ -182,10 +199,9 @@ def test_include_dir_named(
|
|||||||
]
|
]
|
||||||
|
|
||||||
conf = "key: !include_dir_named /test"
|
conf = "key: !include_dir_named /test"
|
||||||
correct = {"first": "one", "second": "two"}
|
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
doc = yaml_loader.parse_yaml(file)
|
doc = yaml_loader.parse_yaml(file)
|
||||||
assert doc["key"] == correct
|
assert doc["key"] == value
|
||||||
|
|
||||||
|
|
||||||
@patch("homeassistant.util.yaml.loader.os.walk")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@ -221,11 +237,20 @@ def test_include_dir_named_recursive(
|
|||||||
|
|
||||||
@patch("homeassistant.util.yaml.loader.os.walk")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"hass_config_yaml_files",
|
("hass_config_yaml_files", "value"),
|
||||||
[{"/test/first.yaml": "- one", "/test/second.yaml": "- two\n- three"}],
|
[
|
||||||
|
(
|
||||||
|
{"/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(
|
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:
|
) -> None:
|
||||||
"""Test include dir merge list yaml."""
|
"""Test include dir merge list yaml."""
|
||||||
mock_walk.return_value = [["/test", [], ["first.yaml", "second.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"
|
conf = "key: !include_dir_merge_list /test"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
doc = yaml_loader.parse_yaml(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")
|
@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")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
@pytest.mark.parametrize(
|
@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(
|
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:
|
) -> None:
|
||||||
"""Test include dir merge named yaml."""
|
"""Test include dir merge named yaml."""
|
||||||
mock_walk.return_value = [["/test", [], ["first.yaml", "second.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"
|
conf = "key: !include_dir_merge_named /test"
|
||||||
with io.StringIO(conf) as file:
|
with io.StringIO(conf) as file:
|
||||||
doc = yaml_loader.parse_yaml(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")
|
@patch("homeassistant.util.yaml.loader.os.walk")
|
||||||
|
Loading…
Reference in New Issue
Block a user