configdata: Add check for shadowing keys

This commit is contained in:
Florian Bruhin 2017-06-12 11:40:10 +02:00
parent 8b9b750f8f
commit 938946c48b
3 changed files with 32 additions and 1 deletions

View File

@ -663,6 +663,12 @@ def _read_yaml(yaml_data):
backends=_parse_yaml_backends(name, option.get('backend', None)),
description=option['desc'])
# Make sure no key shadows another.
for key1 in parsed:
for key2 in parsed:
if key2.startswith(key1 + '.'):
raise ValueError("Shadowing keys {} and {}".format(key1, key2))
return parsed

View File

@ -904,7 +904,7 @@ content.images:
type: Bool
desc: Whether images are automatically loaded in web pages.
content.javascript:
content.javascript.enabled:
default: true
type: Bool
desc: Enables or disables the running of JavaScript programs.

View File

@ -69,6 +69,31 @@ class TestReadYaml:
with pytest.raises(ValueError, match='Invalid keys'):
configdata._read_yaml(data)
@pytest.mark.parametrize('first, second, shadowing', [
('foo', 'foo.bar', True),
('foo.bar', 'foo', True),
('foo.bar', 'foo.bar.baz', True),
('foo.bar', 'foo.baz', False),
])
def test_shadowing(self, first, second, shadowing):
"""Make sure a setting can't shadow another."""
data = textwrap.dedent("""
{first}:
type: Bool
default: true
desc: Hello World
{second}:
type: Bool
default: true
desc: Hello World
""".format(first=first, second=second))
if shadowing:
with pytest.raises(ValueError, match='Shadowing keys'):
configdata._read_yaml(data)
else:
configdata._read_yaml(data)
class TestParseYamlType: