Warn instead of raise on duplicate YAML key (#8834)
* Warn instead of raise on duplicate key * Update test_yaml.py * Lint * Change to error
This commit is contained in:
parent
c6aaacbb08
commit
5696e38dd6
2 changed files with 21 additions and 13 deletions
|
@ -185,12 +185,9 @@ def _ordered_dict(loader: SafeLineLoader,
|
|||
|
||||
if key in seen:
|
||||
fname = getattr(loader.stream, 'name', '')
|
||||
first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None)
|
||||
second_mark = yaml.Mark(fname, 0, line, -1, None, None)
|
||||
raise yaml.MarkedYAMLError(
|
||||
context="duplicate key: \"{}\"".format(key),
|
||||
context_mark=first_mark, problem_mark=second_mark,
|
||||
)
|
||||
_LOGGER.error(
|
||||
'YAML file %s contains duplicate key "%s". '
|
||||
'Check lines %d and %d.', fname, key, seen[key], line)
|
||||
seen[key] = line
|
||||
|
||||
return _add_reference(OrderedDict(nodes), loader, node)
|
||||
|
|
|
@ -5,12 +5,22 @@ import unittest
|
|||
import logging
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.util import yaml
|
||||
from homeassistant.config import YAML_CONFIG_FILE, load_yaml_config_file
|
||||
from tests.common import get_test_config_dir, patch_yaml_files
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_credstash():
|
||||
"""Mock credstash so it doesn't connect to the internet."""
|
||||
with patch.object(yaml, 'credstash') as mock_credstash:
|
||||
mock_credstash.getSecret.return_value = None
|
||||
yield mock_credstash
|
||||
|
||||
|
||||
class TestYaml(unittest.TestCase):
|
||||
"""Test util.yaml loader."""
|
||||
|
||||
|
@ -30,13 +40,6 @@ class TestYaml(unittest.TestCase):
|
|||
doc = yaml.yaml.safe_load(file)
|
||||
assert doc['key'] == 'value'
|
||||
|
||||
def test_duplicate_key(self):
|
||||
"""Test duplicate dict keys."""
|
||||
files = {YAML_CONFIG_FILE: 'key: thing1\nkey: thing2'}
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with patch_yaml_files(files):
|
||||
load_yaml_config_file(YAML_CONFIG_FILE)
|
||||
|
||||
def test_unhashable_key(self):
|
||||
"""Test an unhasable key."""
|
||||
files = {YAML_CONFIG_FILE: 'message:\n {{ states.state }}'}
|
||||
|
@ -411,3 +414,11 @@ def test_representing_yaml_loaded_data():
|
|||
with patch_yaml_files(files):
|
||||
data = load_yaml_config_file(YAML_CONFIG_FILE)
|
||||
assert yaml.dump(data) == "key:\n- 1\n- '2'\n- 3\n"
|
||||
|
||||
|
||||
def test_duplicate_key(caplog):
|
||||
"""Test duplicate dict keys."""
|
||||
files = {YAML_CONFIG_FILE: 'key: thing1\nkey: thing2'}
|
||||
with patch_yaml_files(files):
|
||||
load_yaml_config_file(YAML_CONFIG_FILE)
|
||||
assert 'contains duplicate key' in caplog.text
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue