Merge pull request #934 from ZyX-I/fix-932

Ignore IOErrors in ConfigLoader.update
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2014-07-26 01:58:39 +04:00
commit 9a02f82d27
4 changed files with 147 additions and 1 deletions

View File

@ -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:

80
tests/lib/fsconfig.py Normal file
View File

@ -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)

View File

@ -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):

51
tests/test_lib_config.py Normal file
View File

@ -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()