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:
Paulus Schoutsen 2017-08-06 10:47:19 -07:00 committed by GitHub
parent c6aaacbb08
commit 5696e38dd6
2 changed files with 21 additions and 13 deletions

View File

@ -185,12 +185,9 @@ def _ordered_dict(loader: SafeLineLoader,
if key in seen: if key in seen:
fname = getattr(loader.stream, 'name', '') fname = getattr(loader.stream, 'name', '')
first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None) _LOGGER.error(
second_mark = yaml.Mark(fname, 0, line, -1, None, None) 'YAML file %s contains duplicate key "%s". '
raise yaml.MarkedYAMLError( 'Check lines %d and %d.', fname, key, seen[key], line)
context="duplicate key: \"{}\"".format(key),
context_mark=first_mark, problem_mark=second_mark,
)
seen[key] = line seen[key] = line
return _add_reference(OrderedDict(nodes), loader, node) return _add_reference(OrderedDict(nodes), loader, node)

View File

@ -5,12 +5,22 @@ import unittest
import logging import logging
from unittest.mock import patch from unittest.mock import patch
import pytest
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.util import yaml from homeassistant.util import yaml
from homeassistant.config import YAML_CONFIG_FILE, load_yaml_config_file from homeassistant.config import YAML_CONFIG_FILE, load_yaml_config_file
from tests.common import get_test_config_dir, patch_yaml_files 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): class TestYaml(unittest.TestCase):
"""Test util.yaml loader.""" """Test util.yaml loader."""
@ -30,13 +40,6 @@ class TestYaml(unittest.TestCase):
doc = yaml.yaml.safe_load(file) doc = yaml.yaml.safe_load(file)
assert doc['key'] == 'value' 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): def test_unhashable_key(self):
"""Test an unhasable key.""" """Test an unhasable key."""
files = {YAML_CONFIG_FILE: 'message:\n {{ states.state }}'} files = {YAML_CONFIG_FILE: 'message:\n {{ states.state }}'}
@ -411,3 +414,11 @@ def test_representing_yaml_loaded_data():
with patch_yaml_files(files): with patch_yaml_files(files):
data = load_yaml_config_file(YAML_CONFIG_FILE) data = load_yaml_config_file(YAML_CONFIG_FILE)
assert yaml.dump(data) == "key:\n- 1\n- '2'\n- 3\n" 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