From 7579c650d09588418650bfbe7ea9113af18c81f7 Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Sat, 22 Sep 2018 11:36:17 -0400 Subject: [PATCH] YAML to Mux: unwind the normalize_path utility function The function where this utility method lives is really big and complex. Let's break it down for the sake of code readability, while adding better docstrings. At the same time, this allows us to write tests for those smaller units. I've timed the performance changes caused by this, and on all trials they presented slightly better (but overall insignifcant) improvements. This is just mentioned to assert that there are no performance downsides to this change. Signed-off-by: Cleber Rosa --- .../avocado_varianter_yaml_to_mux/__init__.py | 28 ++++++++++++------- .../varianter_yaml_to_mux/tests/test_mux.py | 7 +++++ 2 files changed, 25 insertions(+), 10 deletions(-) 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 bf09344d..e0bb0686 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 @@ -73,20 +73,28 @@ class ListOfNodeObjects(list): # Few methods pylint: disable=R0903 """ +def _normalize_path(path): + """ + End the path with single '/' + + :param path: original path + :type path: str + :returns: path with trailing '/', or None when empty path + :rtype: str or None + """ + if not path: + return + if path[-1] != '/': + path += '/' + return path + + 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(node, value): """Handling of YAML tags (except of !using)""" - def normalize_path(path): - """End the path with single '/', None when empty path""" - if not path: - return - if path[-1] != '/': - path += '/' - return path - if value[0].code == YAML_INCLUDE: # Include file ypath = value[1] @@ -105,11 +113,11 @@ def _create_from_yaml(path, cls_node=mux.MuxTreeNode): elif value[0].code == YAML_MUX: node.multiplex = True elif value[0].code == YAML_FILTER_ONLY: - new_value = normalize_path(value[1]) + new_value = _normalize_path(value[1]) if new_value: node.filters[0].append(new_value) elif value[0].code == YAML_FILTER_OUT: - new_value = normalize_path(value[1]) + new_value = _normalize_path(value[1]) if new_value: node.filters[1].append(new_value) 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 7f93b5f6..1ed9d4d3 100644 --- a/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py +++ b/optional_plugins/varianter_yaml_to_mux/tests/test_mux.py @@ -535,6 +535,13 @@ class TestPathParent(unittest.TestCase): self.assertNotEqual(mux.path_parent('/os/linux'), '/') +class TestCreateFromYaml(unittest.TestCase): + + def test_normalize_path(self): + self.assertEqual(yaml_to_mux._normalize_path(''), None) + self.assertEqual(yaml_to_mux._normalize_path('path'), 'path/') + + class TestFingerprint(unittest.TestCase): def test_fingerprint(self): -- GitLab