From a279ea236c286804d4bc8e794a98417dc542347a Mon Sep 17 00:00:00 2001 From: ZyX Date: Fri, 25 Jul 2014 16:25:50 +0400 Subject: [PATCH 1/2] Also check error messages --- tests/test_configuration.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index d62ce173..b532db81 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -190,6 +190,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{78} a{910}b{1112}c{56}A{910}B{1112}C{56}1{78}2{910}3{--}') + self.assertEqual(p.logger._pop_msgs(), []) @add_p_arg def test_group_redirects_no_main(self, p): @@ -205,6 +206,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{78} a{56}1{78}2{910}3{--}') + self.assertEqual(p.logger._pop_msgs(), []) @add_p_arg def test_group_redirects_no_top_default(self, p): @@ -218,6 +220,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{1112} c{1112}C{--}') + self.assertEqual(p.logger._pop_msgs(), []) @add_p_arg def test_group_redirects_no_test_default(self, p): @@ -235,6 +238,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{56} A{910}B{1112}C{56}1{78}2{910}3{--}') + self.assertEqual(p.logger._pop_msgs(), []) @add_p_arg def test_group_redirects_only_main(self, p): @@ -250,6 +254,12 @@ class TestColorschemesHierarchy(TestRender): # Powerline is not able to work without default colorscheme # somewhere, thus it will output error string self.assertRenderEqual(p, 'colorschemes/test/default') + self.assertEqual(p.logger._pop_msgs(), [ + 'exception:test:powerline:Failed to load colorscheme: colorschemes/default', + 'exception:test:powerline:Failed to load colorscheme: colorschemes/test/default', + 'exception:test:powerline:Failed to create renderer: colorschemes/test/default', + 'exception:test:powerline:Failed to render: colorschemes/test/default', + ]) @add_p_arg def test_group_redirects_only_top_default(self, p): @@ -265,6 +275,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{56} 1{78}2{910}3{--}') + self.assertEqual(p.logger._pop_msgs(), []) @add_p_arg def test_group_redirects_only_test_default(self, p): @@ -278,6 +289,7 @@ class TestColorschemesHierarchy(TestRender): 'right': [], } self.assertRenderEqual(p, '{121} s{--}') + self.assertEqual(p.logger._pop_msgs(), []) class TestVim(TestCase): From d0c4d4e266fe7f3c05d3cf031702f5fd7dbf3e90 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 26 Jul 2014 01:25:32 +0400 Subject: [PATCH 2/2] =?UTF-8?q?Ignore=20IOError=E2=80=99s=20in=20ConfigLoa?= =?UTF-8?q?der.update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #932 --- powerline/lib/config.py | 5 ++- tests/lib/fsconfig.py | 80 ++++++++++++++++++++++++++++++++++++++++ tests/test_lib_config.py | 51 +++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 tests/lib/fsconfig.py create mode 100644 tests/test_lib_config.py diff --git a/powerline/lib/config.py b/powerline/lib/config.py index 8dd50afe..eaf4c502 100644 --- a/powerline/lib/config.py +++ b/powerline/lib/config.py @@ -109,7 +109,8 @@ class ConfigLoader(MultiRunnedThread): :param function condition_function: Function which will be called each ``interval`` seconds. All - exceptions from it will be ignored. + exceptions from it will be logged and ignored. IOError exception + will be ignored without logging. :param function function: Function which will be called if condition_function returns something that is true. Accepts result of condition_function as an @@ -179,6 +180,8 @@ class ConfigLoader(MultiRunnedThread): for condition_function, function in list(functions): try: path = condition_function(key) + except IOError: + pass except Exception as e: self.exception('Error while running condition function for key {0}: {1}', key, str(e)) else: diff --git a/tests/lib/fsconfig.py b/tests/lib/fsconfig.py new file mode 100644 index 00000000..db80488b --- /dev/null +++ b/tests/lib/fsconfig.py @@ -0,0 +1,80 @@ +# vim:fileencoding=utf-8:noet +from __future__ import unicode_literals, absolute_import, division + +import os +import json + +from subprocess import check_call +from operator import add +from shutil import rmtree + +from powerline import Powerline + + +CONFIG_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'config') + + +class TestPowerline(Powerline): + def __init__(self, _paths, *args, **kwargs): + super(TestPowerline, self).__init__(*args, **kwargs) + self._paths = _paths + + def get_config_paths(self): + return self._paths + + +def mkdir_recursive(directory): + if os.path.isdir(directory): + return + mkdir_recursive(os.path.dirname(directory)) + os.mkdir(directory) + + +class FSTree(object): + __slots__ = ('tree', 'p', 'p_kwargs', 'create_p', 'get_config_paths', 'root') + + def __init__( + self, + tree, + p_kwargs={'run_once': True}, + root=CONFIG_DIR, + get_config_paths=lambda p: (p,), + create_p=False + ): + self.tree = tree + self.root = root + self.get_config_paths = get_config_paths + self.create_p = create_p + self.p = None + self.p_kwargs = p_kwargs + + def __enter__(self, *args): + os.mkdir(self.root) + for k, v in self.tree.items(): + fname = os.path.join(self.root, k) + '.json' + mkdir_recursive(os.path.dirname(fname)) + with open(fname, 'w') as F: + json.dump(v, F) + if self.create_p: + self.p = TestPowerline( + _paths=self.get_config_paths(self.root), + ext='test', + renderer_module='tests.lib.config_mock', + **self.p_kwargs + ) + if os.environ.get('POWERLINE_RUN_LINT_DURING_TESTS'): + try: + check_call(['scripts/powerline-lint'] + reduce(add, ( + ['-p', d] for d in self.p.get_config_paths() + ))) + except: + self.__exit__() + raise + return self.p and self.p.__enter__(*args) + + def __exit__(self, *args): + try: + rmtree(self.root) + finally: + if self.p: + self.p.__exit__(*args) diff --git a/tests/test_lib_config.py b/tests/test_lib_config.py new file mode 100644 index 00000000..5c941d72 --- /dev/null +++ b/tests/test_lib_config.py @@ -0,0 +1,51 @@ +# vim:fileencoding=utf-8:noet +from __future__ import unicode_literals, absolute_import, division + +import os + +from powerline.lib.config import ConfigLoader +from tests import TestCase +from tests.lib.fsconfig import FSTree + + +FILE_ROOT = os.path.join(os.path.dirname(__file__), 'cfglib') + + +class LoadedList(list): + def pop_all(self): + try: + return self[:] + finally: + self[:] = () + + +loaded = LoadedList() + + +def on_load(key): + loaded.append(key) + + +def check_file(path): + if os.path.exists(path): + return path + else: + raise IOError + + +class TestLoaderCondition(TestCase): + def test_update_missing(self): + loader = ConfigLoader(run_once=True) + fpath = os.path.join(FILE_ROOT, 'file.json') + self.assertRaises(IOError, loader.load, fpath) + loader.register_missing(check_file, on_load, fpath) + loader.update() # This line must not raise IOError + with FSTree({'file': {'test': 1}}, root=FILE_ROOT): + loader.update() + self.assertEqual(loader.load(fpath), {'test': 1}) + self.assertEqual(loaded.pop_all(), [fpath]) + + +if __name__ == '__main__': + from tests import main + main()