Skip to content

Commit e64582a

Browse files
authored
Add tests for yaml syntax errors (home-assistant#103908)
1 parent 2bdd969 commit e64582a

File tree

13 files changed

+132
-0
lines changed

13 files changed

+132
-0
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ homeassistant/components/*/translations/*.json
55
homeassistant/generated/*
66
tests/components/lidarr/fixtures/initialize.js
77
tests/components/lidarr/fixtures/initialize-wrong.js
8+
tests/fixtures/core/config/yaml_errors/

.yamllint

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ignore: |
22
azure-*.yml
3+
tests/fixtures/core/config/yaml_errors/
34
rules:
45
braces:
56
level: error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
iot_domain:
2+
# Indentation error
3+
- platform: non_adr_0007
4+
option1: abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iot_domain: !include integrations/iot_domain.yaml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Indentation error
2+
- platform: non_adr_0007
3+
option1: abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iot_domain: !include_dir_list iot_domain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Indentation error
2+
platform: non_adr_0007
3+
option1: abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
iot_domain: !include_dir_merge_list iot_domain
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Indentation error
2+
- platform: non_adr_0007
3+
option1: abc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
homeassistant:
2+
# Load packages
3+
packages: !include_dir_named integrations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Indentation error
2+
adr_0007_1:
3+
host: blah.com
4+
port: 123

tests/snapshots/test_config.ambr

+70
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,73 @@
5757
"Package adr_0007_3_2 setup failed. Integration adr_0007_3 has duplicate key 'host' (See <BASE_PATH>/fixtures/core/config/package_errors/packages_include_dir_named/integrations/adr_0007_3_2.yaml:1). ",
5858
])
5959
# ---
60+
# name: test_yaml_error[basic]
61+
'''
62+
mapping values are not allowed here
63+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic/configuration.yaml", line 4, column 14
64+
'''
65+
# ---
66+
# name: test_yaml_error[basic].1
67+
list([
68+
'''
69+
mapping values are not allowed here
70+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic/configuration.yaml", line 4, column 14
71+
''',
72+
])
73+
# ---
74+
# name: test_yaml_error[basic_include]
75+
'''
76+
mapping values are not allowed here
77+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic_include/integrations/iot_domain.yaml", line 3, column 12
78+
'''
79+
# ---
80+
# name: test_yaml_error[basic_include].1
81+
list([
82+
'''
83+
mapping values are not allowed here
84+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic_include/integrations/iot_domain.yaml", line 3, column 12
85+
''',
86+
])
87+
# ---
88+
# name: test_yaml_error[include_dir_list]
89+
'''
90+
mapping values are not allowed here
91+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_list/iot_domain/iot_domain_1.yaml", line 3, column 10
92+
'''
93+
# ---
94+
# name: test_yaml_error[include_dir_list].1
95+
list([
96+
'''
97+
mapping values are not allowed here
98+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_list/iot_domain/iot_domain_1.yaml", line 3, column 10
99+
''',
100+
])
101+
# ---
102+
# name: test_yaml_error[include_dir_merge_list]
103+
'''
104+
mapping values are not allowed here
105+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_merge_list/iot_domain/iot_domain_1.yaml", line 3, column 12
106+
'''
107+
# ---
108+
# name: test_yaml_error[include_dir_merge_list].1
109+
list([
110+
'''
111+
mapping values are not allowed here
112+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_merge_list/iot_domain/iot_domain_1.yaml", line 3, column 12
113+
''',
114+
])
115+
# ---
116+
# name: test_yaml_error[packages_include_dir_named]
117+
'''
118+
mapping values are not allowed here
119+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/packages_include_dir_named/integrations/adr_0007_1.yaml", line 4, column 9
120+
'''
121+
# ---
122+
# name: test_yaml_error[packages_include_dir_named].1
123+
list([
124+
'''
125+
mapping values are not allowed here
126+
in "<BASE_PATH>/fixtures/core/config/yaml_errors/packages_include_dir_named/integrations/adr_0007_1.yaml", line 4, column 9
127+
''',
128+
])
129+
# ---

tests/test_config.py

+37
Original file line numberDiff line numberDiff line change
@@ -1540,3 +1540,40 @@ async def test_package_merge_error(
15401540
if record.levelno == logging.ERROR
15411541
]
15421542
assert error_records == snapshot
1543+
1544+
1545+
@pytest.mark.parametrize(
1546+
"config_dir",
1547+
[
1548+
"basic",
1549+
"basic_include",
1550+
"include_dir_list",
1551+
"include_dir_merge_list",
1552+
"packages_include_dir_named",
1553+
],
1554+
)
1555+
async def test_yaml_error(
1556+
hass: HomeAssistant,
1557+
caplog: pytest.LogCaptureFixture,
1558+
config_dir: str,
1559+
mock_iot_domain_integration: Integration,
1560+
mock_non_adr_0007_integration: None,
1561+
mock_adr_0007_integrations: list[Integration],
1562+
snapshot: SnapshotAssertion,
1563+
) -> None:
1564+
"""Test schema error in component."""
1565+
1566+
base_path = os.path.dirname(__file__)
1567+
hass.config.config_dir = os.path.join(
1568+
base_path, "fixtures", "core", "config", "yaml_errors", config_dir
1569+
)
1570+
with pytest.raises(HomeAssistantError) as exc_info:
1571+
await config_util.async_hass_config_yaml(hass)
1572+
assert str(exc_info.value).replace(base_path, "<BASE_PATH>") == snapshot
1573+
1574+
error_records = [
1575+
record.message.replace(base_path, "<BASE_PATH>")
1576+
for record in caplog.get_records("call")
1577+
if record.levelno == logging.ERROR
1578+
]
1579+
assert error_records == snapshot

0 commit comments

Comments
 (0)