summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Van Berkom <tristan.van.berkom@gmail.com>2018-12-07 08:33:14 +0000
committerTristan Van Berkom <tristan.van.berkom@gmail.com>2018-12-07 08:33:14 +0000
commit2a0676c3bf607a95a41bd802839eaf677588bf79 (patch)
tree44eef17c0cf48811c5c4706b621c6eaebddd0cad
parent2a6879a540ef28f4298f10e19924992bd179aea1 (diff)
parent642ae4e8ba4715023c98f24981b412ac146edb88 (diff)
downloadbuildstream-2a0676c3bf607a95a41bd802839eaf677588bf79.tar.gz
Merge branch 'tristan/yaml-optionally-allow-none' into 'master'
Only optionally allow None values in user provided YAML Closes #803 See merge request BuildStream/buildstream!999
-rw-r--r--buildstream/_yaml.py7
-rw-r--r--buildstream/plugin.py5
-rw-r--r--tests/format/project.py7
-rw-r--r--tests/format/project/empty-depends/manual.bst3
-rw-r--r--tests/format/project/empty-depends/project.conf1
5 files changed, 18 insertions, 5 deletions
diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py
index 4fe844a80..8d7302b80 100644
--- a/buildstream/_yaml.py
+++ b/buildstream/_yaml.py
@@ -352,6 +352,7 @@ _sentinel = object()
# key (str): The key to get a value for in node
# indices (list of ints): Optionally decend into lists of lists
# default_value: Optionally return this value if the key is not found
+# allow_none: (bool): Allow None to be a valid value
#
# Returns:
# The value if found in node, otherwise default_value is returned
@@ -362,7 +363,7 @@ _sentinel = object()
# Note:
# Returned strings are stripped of leading and trailing whitespace
#
-def node_get(node, expected_type, key, indices=None, default_value=_sentinel):
+def node_get(node, expected_type, key, indices=None, *, default_value=_sentinel, allow_none=False):
value = node.get(key, default_value)
provenance = node_get_provenance(node)
if value is _sentinel:
@@ -377,8 +378,8 @@ def node_get(node, expected_type, key, indices=None, default_value=_sentinel):
value = value[index]
path += '[{:d}]'.format(index)
- # We want to allow None as a valid value for any type
- if value is None:
+ # Optionally allow None as a valid value for any type
+ if value is None and (allow_none or default_value is None):
return None
if not isinstance(value, expected_type):
diff --git a/buildstream/plugin.py b/buildstream/plugin.py
index 1e8dd4b9f..2f51c8807 100644
--- a/buildstream/plugin.py
+++ b/buildstream/plugin.py
@@ -323,7 +323,7 @@ class Plugin():
provenance = _yaml.node_get_provenance(node, key=member_name)
return str(provenance)
- def node_get_member(self, node, expected_type, member_name, default=_yaml._sentinel):
+ def node_get_member(self, node, expected_type, member_name, default=_yaml._sentinel, *, allow_none=False):
"""Fetch the value of a node member, raising an error if the value is
missing or incorrectly typed.
@@ -332,6 +332,7 @@ class Plugin():
expected_type (type): The expected type of the node member
member_name (str): The name of the member to fetch
default (expected_type): A value to return when *member_name* is not specified in *node*
+ allow_none (bool): Allow explicitly set None values in the YAML (*Since: 1.4*)
Returns:
The value of *member_name* in *node*, otherwise *default*
@@ -352,7 +353,7 @@ class Plugin():
# Fetch an optional integer
level = self.node_get_member(node, int, 'level', -1)
"""
- return _yaml.node_get(node, expected_type, member_name, default_value=default)
+ return _yaml.node_get(node, expected_type, member_name, default_value=default, allow_none=allow_none)
def node_get_project_path(self, node, key, *,
check_is_file=False, check_is_dir=False):
diff --git a/tests/format/project.py b/tests/format/project.py
index 46145e578..02e8afb37 100644
--- a/tests/format/project.py
+++ b/tests/format/project.py
@@ -200,3 +200,10 @@ def test_element_path_project_path_contains_symlinks(cli, datafiles, tmpdir):
f.write("kind: manual\n")
result = cli.run(project=linked_project, args=['show', 'element.bst'])
result.assert_success()
+
+
+@pytest.mark.datafiles(os.path.join(DATA_DIR))
+def test_empty_depends(cli, datafiles):
+ project = os.path.join(datafiles.dirname, datafiles.basename, "empty-depends")
+ result = cli.run(project=project, args=['show', 'manual.bst'])
+ result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_DATA)
diff --git a/tests/format/project/empty-depends/manual.bst b/tests/format/project/empty-depends/manual.bst
new file mode 100644
index 000000000..0e168c422
--- /dev/null
+++ b/tests/format/project/empty-depends/manual.bst
@@ -0,0 +1,3 @@
+kind: manual
+
+depends:
diff --git a/tests/format/project/empty-depends/project.conf b/tests/format/project/empty-depends/project.conf
new file mode 100644
index 000000000..b32753625
--- /dev/null
+++ b/tests/format/project/empty-depends/project.conf
@@ -0,0 +1 @@
+name: test