Skip to content

Commit

Permalink
Fix failure to raise on bad YAML syntax from include files (#75510)
Browse files Browse the repository at this point in the history
Co-authored-by: Franck Nijhof <[email protected]>
  • Loading branch information
bdraco and frenck committed Jul 20, 2022
1 parent e53a072 commit 4ac7d68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions homeassistant/util/yaml/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import OrderedDict
from collections.abc import Iterator
import fnmatch
from io import StringIO
from io import StringIO, TextIOWrapper
import logging
import os
from pathlib import Path
Expand Down Expand Up @@ -169,7 +169,7 @@ def parse_yaml(
except yaml.YAMLError:
# Loading failed, so we now load with the slow line loader
# since the C one will not give us line numbers
if isinstance(content, (StringIO, TextIO)):
if isinstance(content, (StringIO, TextIO, TextIOWrapper)):
# Rewind the stream so we can try again
content.seek(0, 0)
return _parse_yaml_pure_python(content, secrets)
Expand Down
26 changes: 26 additions & 0 deletions tests/util/yaml/fixtures/bad.yaml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- id: '1658085239190'
alias: Config validation test
description: ''
trigger:
- platform: time
at: 00:02:03
condition: []
action:
- service: script.notify_admin
data:
title: 'Here's something that does not work...!'
message: failing
mode: single
- id: '165808523911590'
alias: Config validation test FIXED
description: ''
trigger:
- platform: time
at: 00:02:03
condition: []
action:
- service: script.notify_admin
data:
title: 'Here is something that should work...!'
message: fixed?
mode: single
10 changes: 10 additions & 0 deletions tests/util/yaml/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import importlib
import io
import os
import pathlib
import unittest
from unittest.mock import patch

Expand Down Expand Up @@ -490,3 +491,12 @@ def test_input(try_both_loaders, try_both_dumpers):
def test_c_loader_is_available_in_ci():
"""Verify we are testing the C loader in the CI."""
assert yaml.loader.HAS_C_LOADER is True


async def test_loading_actual_file_with_syntax(hass, try_both_loaders):
"""Test loading a real file with syntax errors."""
with pytest.raises(HomeAssistantError):
fixture_path = pathlib.Path(__file__).parent.joinpath(
"fixtures", "bad.yaml.txt"
)
await hass.async_add_executor_job(load_yaml_config_file, fixture_path)

0 comments on commit 4ac7d68

Please sign in to comment.