From 87c1fea3e7336adc6440f345bda04216abbdec1f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 4 Mar 2017 17:57:09 +0200 Subject: Add append_to_list --- yarnhelper.py | 7 +++++++ yarnhelper_tests.py | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/yarnhelper.py b/yarnhelper.py index 18467ed..6272102 100644 --- a/yarnhelper.py +++ b/yarnhelper.py @@ -70,6 +70,13 @@ class YarnHelper(object): with open(variables_filename, 'w') as f: yaml.safe_dump(variables, f) + def append_to_list(self, list_name, value): + if self._variables is None: + self._variables = self._load_variables() + items = self._variables.get(list_name, []) + items.append(value) + self.set_variable(list_name, items) + def construct_aliased_http_request( self, address, method, url, data=None, headers=None): diff --git a/yarnhelper_tests.py b/yarnhelper_tests.py index e5cab3f..46a0604 100644 --- a/yarnhelper_tests.py +++ b/yarnhelper_tests.py @@ -80,6 +80,11 @@ class PersistentVariableTests(unittest.TestCase): h2 = yarnhelper.YarnHelper() self.assertEqual(h2.get_variable('FOO'), 'bar') + def test_appends_to_empty_list(self): + h = yarnhelper.YarnHelper() + h.append_to_list('foo', 1) + self.assertEqual(h.get_variable('foo'), [1]) + class HttpTests(unittest.TestCase): -- cgit v1.2.1