From 95d13a334a3057cbe3a896a00bc1a30b898def79 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 21 Nov 2013 00:18:34 +0400 Subject: [PATCH] Test that nothing is reloaded with run_once=True --- tests/lib/config_mock.py | 18 +++++++++++++++++- tests/test_config_reload.py | 20 ++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/lib/config_mock.py b/tests/lib/config_mock.py index 7243fcb3..82d1d18a 100644 --- a/tests/lib/config_mock.py +++ b/tests/lib/config_mock.py @@ -4,6 +4,7 @@ from powerline.renderer import Renderer from powerline.lib.config import ConfigLoader from powerline import Powerline from copy import deepcopy +from functools import wraps access_log = [] @@ -34,13 +35,26 @@ def pop_events(): return r +def log_call(func): + @wraps(func) + def ret(self, *args, **kwargs): + self._calls.append((func.__name__, args, kwargs)) + return func(self, *args, **kwargs) + return ret + + class Watcher(object): events = set() lock = Lock() + def __init__(self): + self._calls = [] + + @log_call def watch(self, file): pass + @log_call def __call__(self, file): with self.lock: if file in self.events: @@ -48,11 +62,13 @@ class Watcher(object): return True return False + @log_call def _reset(self, files): with self.lock: self.events.clear() self.events.update(files) + @log_call def unsubscribe(self): pass @@ -100,7 +116,7 @@ def get_powerline(**kwargs): ext='test', renderer_module='tests.lib.config_mock', logger=Logger(), - config_loader=ConfigLoader(load=load_json_config, watcher=Watcher()), + config_loader=ConfigLoader(load=load_json_config, watcher=Watcher(), run_once=kwargs.get('run_once')), **kwargs ) diff --git a/tests/test_config_reload.py b/tests/test_config_reload.py index b58eaff8..7b0bb55b 100644 --- a/tests/test_config_reload.py +++ b/tests/test_config_reload.py @@ -97,7 +97,10 @@ def sleep(interval): def add_watcher_events(p, *args, **kwargs): - p.config_loader.watcher._reset(args) + try: + p.config_loader.watcher._reset(args) + except AttributeError: + return while not p._will_create_renderer(): sleep(kwargs.get('interval', 0.000001)) if not kwargs.get('wait', True): @@ -118,7 +121,6 @@ class TestConfigReload(TestCase): # When running once thread should not start self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>>') self.assertAccessEvents() - self.assertEqual(p.logger._pop_msgs(), []) # Without the following assertion test_reload_colors may fail for # unknown reason (with AssertionError telling about “config” accessed # one more time then needed) @@ -253,6 +255,20 @@ class TestConfigReload(TestCase): self.assertEqual(p.render(), '<1 2 1> col3<2 4 False>>><3 4 4>g<4 False False>>>') self.assertAccessEvents('themes/test/default') self.assertEqual(p.logger._pop_msgs(), []) + self.assertTrue(p.config_loader.watcher._calls) + pop_events() + + def test_run_once_no_theme_reload(self): + with replace_item(globals(), 'config', deepcopy(config)): + config['config']['common']['interval'] = None + with get_powerline(run_once=True) as p: + self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>>') + self.assertAccessEvents('config', 'colors', 'colorschemes/test/default', 'themes/test/default') + + config['themes/test/default']['segments']['left'][0]['contents'] = 'col3' + add_watcher_events(p, 'themes/test/default', wait=False) + self.assertEqual(p.render(), '<1 2 1> s<2 4 False>>><3 4 4>g<4 False False>>>') + self.assertAccessEvents() pop_events()