Add tests for yaml syntax errors (#103908)

This commit is contained in:
Erik Montnemery 2023-11-13 14:04:58 +01:00 committed by GitHub
parent 2bdd969cf6
commit e64582ae9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 132 additions and 0 deletions

View File

@ -5,3 +5,4 @@ homeassistant/components/*/translations/*.json
homeassistant/generated/*
tests/components/lidarr/fixtures/initialize.js
tests/components/lidarr/fixtures/initialize-wrong.js
tests/fixtures/core/config/yaml_errors/

View File

@ -1,5 +1,6 @@
ignore: |
azure-*.yml
tests/fixtures/core/config/yaml_errors/
rules:
braces:
level: error

View File

@ -0,0 +1,4 @@
iot_domain:
# Indentation error
- platform: non_adr_0007
option1: abc

View File

@ -0,0 +1 @@
iot_domain: !include integrations/iot_domain.yaml

View File

@ -0,0 +1,3 @@
# Indentation error
- platform: non_adr_0007
option1: abc

View File

@ -0,0 +1 @@
iot_domain: !include_dir_list iot_domain

View File

@ -0,0 +1,3 @@
# Indentation error
platform: non_adr_0007
option1: abc

View File

@ -0,0 +1 @@
iot_domain: !include_dir_merge_list iot_domain

View File

@ -0,0 +1,3 @@
# Indentation error
- platform: non_adr_0007
option1: abc

View File

@ -0,0 +1,3 @@
homeassistant:
# Load packages
packages: !include_dir_named integrations

View File

@ -0,0 +1,4 @@
# Indentation error
adr_0007_1:
host: blah.com
port: 123

View File

@ -57,3 +57,73 @@
"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). ",
])
# ---
# name: test_yaml_error[basic]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic/configuration.yaml", line 4, column 14
'''
# ---
# name: test_yaml_error[basic].1
list([
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic/configuration.yaml", line 4, column 14
''',
])
# ---
# name: test_yaml_error[basic_include]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic_include/integrations/iot_domain.yaml", line 3, column 12
'''
# ---
# name: test_yaml_error[basic_include].1
list([
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/basic_include/integrations/iot_domain.yaml", line 3, column 12
''',
])
# ---
# name: test_yaml_error[include_dir_list]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_list/iot_domain/iot_domain_1.yaml", line 3, column 10
'''
# ---
# name: test_yaml_error[include_dir_list].1
list([
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_list/iot_domain/iot_domain_1.yaml", line 3, column 10
''',
])
# ---
# name: test_yaml_error[include_dir_merge_list]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_merge_list/iot_domain/iot_domain_1.yaml", line 3, column 12
'''
# ---
# name: test_yaml_error[include_dir_merge_list].1
list([
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/include_dir_merge_list/iot_domain/iot_domain_1.yaml", line 3, column 12
''',
])
# ---
# name: test_yaml_error[packages_include_dir_named]
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/packages_include_dir_named/integrations/adr_0007_1.yaml", line 4, column 9
'''
# ---
# name: test_yaml_error[packages_include_dir_named].1
list([
'''
mapping values are not allowed here
in "<BASE_PATH>/fixtures/core/config/yaml_errors/packages_include_dir_named/integrations/adr_0007_1.yaml", line 4, column 9
''',
])
# ---

View File

@ -1540,3 +1540,40 @@ async def test_package_merge_error(
if record.levelno == logging.ERROR
]
assert error_records == snapshot
@pytest.mark.parametrize(
"config_dir",
[
"basic",
"basic_include",
"include_dir_list",
"include_dir_merge_list",
"packages_include_dir_named",
],
)
async def test_yaml_error(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
config_dir: str,
mock_iot_domain_integration: Integration,
mock_non_adr_0007_integration: None,
mock_adr_0007_integrations: list[Integration],
snapshot: SnapshotAssertion,
) -> None:
"""Test schema error in component."""
base_path = os.path.dirname(__file__)
hass.config.config_dir = os.path.join(
base_path, "fixtures", "core", "config", "yaml_errors", config_dir
)
with pytest.raises(HomeAssistantError) as exc_info:
await config_util.async_hass_config_yaml(hass)
assert str(exc_info.value).replace(base_path, "<BASE_PATH>") == snapshot
error_records = [
record.message.replace(base_path, "<BASE_PATH>")
for record in caplog.get_records("call")
if record.levelno == logging.ERROR
]
assert error_records == snapshot