You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by no...@apache.org on 2020/12/29 12:30:44 UTC

[buildstream] 01/07: Add _yaml.validate_node

This is an automated email from the ASF dual-hosted git repository.

not-in-ldap pushed a commit to branch validation
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 2f545d57b982d97638951bca7facb45f43f63169
Author: Tristan Maat <tr...@codethink.co.uk>
AuthorDate: Wed Sep 6 16:52:26 2017 +0100

    Add _yaml.validate_node
---
 buildstream/_yaml.py         | 18 ++++++++++++++++++
 tests/yaml/data/invalid.yaml |  8 ++++++++
 tests/yaml/yaml.py           | 22 ++++++++++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py
index 527f537..c07996b 100644
--- a/buildstream/_yaml.py
+++ b/buildstream/_yaml.py
@@ -587,6 +587,24 @@ 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_keys = [key for key in node if key not in valid_keys]
+
+    if invalid_keys:
+        provenance = node_get_provenance(node)
+        error_prefix = ""
+        if provenance:
+            error_prefix = "[%s]: " % str(provenance)
+
+        key_list = ', '.join(invalid_keys)
+        raise LoadError(LoadErrorReason.INVALID_YAML,
+                        "{}Unexpected keys: {}".format(error_prefix, key_list))
+
+
 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 0000000..bc7f1ae
--- /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 ec63267..a6e6f49 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_YAML)
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR))
 def test_node_get(datafiles):
 
     filename = os.path.join(datafiles.dirname,