diff --git a/optional_plugins/varianter_yaml_to_mux/avocado_varianter_yaml_to_mux/__init__.py b/optional_plugins/varianter_yaml_to_mux/avocado_varianter_yaml_to_mux/__init__.py index da8cd4f19a27b201023c4fe77d853eff9f9ac810..9a07d2ceec3688e898c3d4ace5a32a0ea9b4bb5c 100644 --- a/optional_plugins/varianter_yaml_to_mux/avocado_varianter_yaml_to_mux/__init__.py +++ b/optional_plugins/varianter_yaml_to_mux/avocado_varianter_yaml_to_mux/__init__.py @@ -125,22 +125,33 @@ def _handle_control_tag(path, cls_node, node, value): node.filters[1].append(new_value) +def _handle_control_tag_using(path, name, using, value): + """ + Handling of the "!using" YAML control tag + + :param path: path on the YAML + :type path: str + :param name: name to be applied in the "!using" tag + :type name: str + :param using: wether using is already being used + :type using: bool + :param value: the value of the node + """ + if using: + raise ValueError("!using can be used only once per " + "node! (%s:%s)" % (path, name)) + using = value + if using[0] == '/': + using = using[1:] + if using[-1] == '/': + using = using[:-1] + return using + + def _create_from_yaml(path, cls_node=mux.MuxTreeNode): """Create tree structure from yaml stream""" def tree_node_from_values(name, values): """Create `name` node and add values""" - def handle_control_tag_using(name, using, value): - """Handling of the !using tag""" - if using: - raise ValueError("!using can be used only once per " - "node! (%s:%s)" % (path, name)) - using = value - if using[0] == '/': - using = using[1:] - if using[-1] == '/': - using = using[:-1] - return using - def node_content_from_node(node, values, using): """Processes node values into the current node content""" for value in values: @@ -148,7 +159,7 @@ def _create_from_yaml(path, cls_node=mux.MuxTreeNode): node.add_child(value) elif isinstance(value[0], mux.Control): if value[0].code == YAML_USING: - using = handle_control_tag_using(name, using, value[1]) + using = _handle_control_tag_using(path, name, using, value[1]) else: _handle_control_tag(path, cls_node, node, value) elif isinstance(value[1], collections.OrderedDict): @@ -163,7 +174,7 @@ def _create_from_yaml(path, cls_node=mux.MuxTreeNode): for key, value in iteritems(values): if isinstance(key, mux.Control): if key.code == YAML_USING: - using = handle_control_tag_using(name, using, value) + using = _handle_control_tag_using(path, name, using, value) else: _handle_control_tag(path, cls_node, node, [key, value]) elif (isinstance(value, collections.OrderedDict) or diff --git a/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py b/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py index 2761e9c0718c993dfe45cb99890eb443b20a0f2f..0431dbd6f5508e2b0e4cfe2a523e38f5d1fde7c2 100644 --- a/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py +++ b/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py @@ -560,6 +560,18 @@ class TestCreateFromYaml(unittest.TestCase): self.assertEqual(control.value, to_be_removed) self.assertIn(control, node.ctrl) + def test_handle_control_tag_using_multiple(self): + with self.assertRaises(ValueError): + yaml_to_mux._handle_control_tag_using('original_fake_file.yaml', + 'name', True, 'using') + + def test_handle_control_tag_using(self): + using = yaml_to_mux._handle_control_tag_using('fake_path', + 'name', + False, + '/using/path/') + self.assertEqual(using, 'using/path') + class TestFingerprint(unittest.TestCase):