2014-07-13 21:02:05 +02:00
|
|
|
|
# vim:fileencoding=utf-8:noet
|
2014-08-31 20:55:26 +02:00
|
|
|
|
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
2014-07-13 21:02:05 +02:00
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import json
|
2014-08-31 20:55:26 +02:00
|
|
|
|
|
2014-07-13 21:15:10 +02:00
|
|
|
|
from subprocess import check_call
|
|
|
|
|
from operator import add
|
2014-08-31 20:55:26 +02:00
|
|
|
|
from shutil import rmtree
|
|
|
|
|
|
2014-12-13 11:42:46 +01:00
|
|
|
|
from powerline.lib.dict import mergedicts_copy as mdc
|
2014-08-31 20:55:26 +02:00
|
|
|
|
from powerline import Powerline
|
|
|
|
|
|
|
|
|
|
from tests import TestCase
|
|
|
|
|
from tests.lib.config_mock import select_renderer
|
2014-07-13 21:02:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CONFIG_DIR = 'tests/config'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root_config = lambda: {
|
|
|
|
|
'common': {
|
2014-07-13 21:15:10 +02:00
|
|
|
|
'interval': None,
|
|
|
|
|
'watcher': 'auto',
|
2014-07-13 21:02:05 +02:00
|
|
|
|
},
|
|
|
|
|
'ext': {
|
|
|
|
|
'test': {
|
|
|
|
|
'theme': 'default',
|
|
|
|
|
'colorscheme': 'default',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
colors_config = lambda: {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c1': 1,
|
|
|
|
|
'c2': 2,
|
|
|
|
|
},
|
|
|
|
|
'gradients': {
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
colorscheme_config = lambda: {
|
|
|
|
|
'groups': {
|
2015-01-06 13:25:18 +01:00
|
|
|
|
'g': {'fg': 'c1', 'bg': 'c2', 'attrs': []},
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
theme_config = lambda: {
|
|
|
|
|
'segment_data': {
|
|
|
|
|
's': {
|
|
|
|
|
'before': 'b',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
'segments': {
|
|
|
|
|
'left': [
|
|
|
|
|
{
|
|
|
|
|
'type': 'string',
|
|
|
|
|
'name': 's',
|
|
|
|
|
'contents': 't',
|
2015-01-06 13:13:43 +01:00
|
|
|
|
'highlight_groups': ['g'],
|
2014-07-13 21:02:05 +02:00
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
'right': [],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 18:29:03 +02:00
|
|
|
|
top_theme_config = lambda: {
|
|
|
|
|
'dividers': {
|
|
|
|
|
'left': {
|
|
|
|
|
'hard': '#>',
|
|
|
|
|
'soft': '|>',
|
|
|
|
|
},
|
|
|
|
|
'right': {
|
|
|
|
|
'hard': '<#',
|
|
|
|
|
'soft': '<|',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
'spaces': 0,
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-13 21:02:05 +02:00
|
|
|
|
|
|
|
|
|
main_tree = lambda: {
|
|
|
|
|
'1/config': root_config(),
|
|
|
|
|
'1/colors': colors_config(),
|
|
|
|
|
'1/colorschemes/default': colorscheme_config(),
|
|
|
|
|
'1/themes/test/default': theme_config(),
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'1/themes/powerline': top_theme_config(),
|
|
|
|
|
'1/themes/other1': mdc(top_theme_config(), {
|
|
|
|
|
'dividers': {
|
|
|
|
|
'left': {
|
|
|
|
|
'hard': '!>',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
'1/themes/other2': mdc(top_theme_config(), {
|
|
|
|
|
'dividers': {
|
|
|
|
|
'left': {
|
|
|
|
|
'hard': '>>',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mkdir_recursive(directory):
|
|
|
|
|
if os.path.isdir(directory):
|
|
|
|
|
return
|
|
|
|
|
mkdir_recursive(os.path.dirname(directory))
|
|
|
|
|
os.mkdir(directory)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPowerline(Powerline):
|
|
|
|
|
def get_config_paths(self):
|
|
|
|
|
return tuple(sorted([
|
|
|
|
|
os.path.join(CONFIG_DIR, d)
|
|
|
|
|
for d in os.listdir(CONFIG_DIR)
|
|
|
|
|
]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WithConfigTree(object):
|
|
|
|
|
__slots__ = ('tree', 'p', 'p_kwargs')
|
|
|
|
|
|
|
|
|
|
def __init__(self, tree, p_kwargs={'run_once': True}):
|
|
|
|
|
self.tree = tree
|
|
|
|
|
self.p = None
|
|
|
|
|
self.p_kwargs = p_kwargs
|
|
|
|
|
|
|
|
|
|
def __enter__(self, *args):
|
|
|
|
|
os.mkdir(CONFIG_DIR)
|
|
|
|
|
for k, v in self.tree.items():
|
|
|
|
|
fname = os.path.join(CONFIG_DIR, k) + '.json'
|
|
|
|
|
mkdir_recursive(os.path.dirname(fname))
|
|
|
|
|
with open(fname, 'w') as F:
|
|
|
|
|
json.dump(v, F)
|
2014-08-05 11:39:01 +02:00
|
|
|
|
select_renderer(simpler_renderer=True)
|
2014-07-13 21:02:05 +02:00
|
|
|
|
self.p = TestPowerline(
|
|
|
|
|
ext='test',
|
|
|
|
|
renderer_module='tests.lib.config_mock',
|
|
|
|
|
**self.p_kwargs
|
|
|
|
|
)
|
2014-07-13 21:15:10 +02:00
|
|
|
|
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
|
2014-07-13 21:02:05 +02:00
|
|
|
|
return self.p.__enter__(*args)
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
|
try:
|
|
|
|
|
rmtree(CONFIG_DIR)
|
|
|
|
|
finally:
|
|
|
|
|
if self.p:
|
|
|
|
|
self.p.__exit__(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestMerging(TestCase):
|
|
|
|
|
def assertRenderEqual(self, p, output, **kwargs):
|
|
|
|
|
self.assertEqual(p.render(**kwargs).replace(' ', ' '), output)
|
|
|
|
|
|
|
|
|
|
def test_not_merged_config(self):
|
|
|
|
|
with WithConfigTree(main_tree()) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{12} bt{2-}#>{--}')
|
|
|
|
|
|
|
|
|
|
def test_root_config_merging(self):
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/config': {
|
|
|
|
|
'common': {
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'default_top_theme': 'other1',
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{12} bt{2-}!>{--}')
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/config': {
|
|
|
|
|
'common': {
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'default_top_theme': 'other1',
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'3/config': {
|
|
|
|
|
'common': {
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'default_top_theme': 'other2',
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{12} bt{2-}>>{--}')
|
2014-08-05 18:29:03 +02:00
|
|
|
|
|
|
|
|
|
def test_top_theme_merging(self):
|
2014-07-13 21:02:05 +02:00
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'2/themes/powerline': {
|
|
|
|
|
'spaces': 1,
|
2014-07-13 21:02:05 +02:00
|
|
|
|
},
|
2014-08-05 18:29:03 +02:00
|
|
|
|
'3/themes/powerline': {
|
|
|
|
|
'dividers': {
|
|
|
|
|
'left': {
|
|
|
|
|
'hard': '>>',
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
2014-07-13 21:15:10 +02:00
|
|
|
|
self.assertRenderEqual(p, '{12} bt {2-}>>{--}')
|
2014-07-13 21:02:05 +02:00
|
|
|
|
|
|
|
|
|
def test_colors_config_merging(self):
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/colors': {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c1': 3,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{32} bt{2-}#>{--}')
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/colors': {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c1': 3,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'3/colors': {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c1': 4,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{42} bt{2-}#>{--}')
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/colors': {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c1': 3,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'3/colors': {
|
|
|
|
|
'colors': {
|
|
|
|
|
'c2': 4,
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{34} bt{4-}#>{--}')
|
|
|
|
|
|
|
|
|
|
def test_colorschemes_merging(self):
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/colorschemes/default': {
|
|
|
|
|
'groups': {
|
2015-01-06 13:25:18 +01:00
|
|
|
|
'g': {'fg': 'c2', 'bg': 'c1', 'attrs': []},
|
2014-07-13 21:02:05 +02:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{21} bt{1-}#>{--}')
|
|
|
|
|
|
|
|
|
|
def test_theme_merging(self):
|
|
|
|
|
with WithConfigTree(mdc(main_tree(), {
|
|
|
|
|
'2/themes/test/default': {
|
|
|
|
|
'segment_data': {
|
|
|
|
|
's': {
|
|
|
|
|
'after': 'a',
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})) as p:
|
|
|
|
|
self.assertRenderEqual(p, '{12} bta{2-}#>{--}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
from tests import main
|
|
|
|
|
main()
|