summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Maat <tristan.maat@codethink.co.uk>2017-09-06 16:52:26 +0100
committerTristan Maat <tristan.maat@codethink.co.uk>2017-09-13 17:10:10 +0100
commit06b4d3aeae88a30d922cd044feaf9f255946fe55 (patch)
tree0c0ab31cd4339fbe5d6bea24831c45056afa616d
parent5b995dd1543b9476abfe6f1d42dc43cd5d0ea7dc (diff)
downloadbuildstream-06b4d3aeae88a30d922cd044feaf9f255946fe55.tar.gz
Add _yaml.validate_node
-rw-r--r--buildstream/_yaml.py13
-rw-r--r--tests/yaml/data/invalid.yaml8
-rw-r--r--tests/yaml/yaml.py22
3 files changed, 43 insertions, 0 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py
index 5ba540fc9..aead2ed84 100644
--- a/buildstream/_yaml.py
+++ b/buildstream/_yaml.py
@@ -575,6 +575,19 @@ def node_sanitize(node):
return node
+def validate_node(node, valid_keys):
+
+ # Probably the fastest way to do this: https://stackoverflow.com/a/23062482
+ valid_keys = set(valid_keys)
+ valid_keys.add(PROVENANCE_KEY)
+ invalid = next((key for key in node if key not in valid_keys), None)
+
+ if invalid:
+ provenance = node_get_provenance(node, key=invalid)
+ raise LoadError(LoadErrorReason.INVALID_DATA,
+ "[{}]: Unexpected key: {}".format(provenance, invalid))
+
+
def node_chain_copy(source):
copy = collections.ChainMap({}, source)
for key, value in source.items():
diff --git a/tests/yaml/data/invalid.yaml b/tests/yaml/data/invalid.yaml
new file mode 100644
index 000000000..bc7f1ae22
--- /dev/null
+++ b/tests/yaml/data/invalid.yaml
@@ -0,0 +1,8 @@
+kind: pony
+description: The vehicle of choice for rainbow travel
+mods:
+ - happy
+ - sad
+children:
+ - naam: dopey
+ mood: silly
diff --git a/tests/yaml/yaml.py b/tests/yaml/yaml.py
index ec632674f..e972f89f4 100644
--- a/tests/yaml/yaml.py
+++ b/tests/yaml/yaml.py
@@ -163,6 +163,28 @@ def test_composited_array_append_provenance(datafiles):
@pytest.mark.datafiles(os.path.join(DATA_DIR))
+def test_validate_node(datafiles):
+
+ valid = os.path.join(datafiles.dirname,
+ datafiles.basename,
+ 'basics.yaml')
+ invalid = os.path.join(datafiles.dirname,
+ datafiles.basename,
+ 'invalid.yaml')
+
+ base = _yaml.load(valid)
+
+ _yaml.validate_node(base, ['kind', 'description', 'moods', 'children', 'extra'])
+
+ base = _yaml.load(invalid)
+
+ with pytest.raises(LoadError) as exc:
+ _yaml.validate_node(base, ['kind', 'description', 'moods', 'children', 'extra'])
+
+ assert (exc.value.reason == LoadErrorReason.INVALID_DATA)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR))
def test_node_get(datafiles):
filename = os.path.join(datafiles.dirname,