def test_tmux(self): from powerline.segments import common from imp import reload reload(common) from powerline.shell import ShellPowerline with replace_attr(common, 'urllib_read', urllib_read): with ShellPowerline(Args(ext=['tmux']), run_once=False) as powerline: powerline.render() with ShellPowerline(Args(ext=['tmux']), run_once=False) as powerline: powerline.render()
def test_jobnum(self): pl = Pl() segment_info = {'args': Args(jobnum=0)} self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info), None) self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info, show_zero=False), None) self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info, show_zero=True), '0') segment_info = {'args': Args(jobnum=1)} self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info), '1') self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info, show_zero=False), '1') self.assertEqual(shell.jobnum(pl=pl, segment_info=segment_info, show_zero=True), '1')
def test_fuzzy_time(self): time = Args(hour=0, minute=45) with replace_module_attr(common, 'datetime', Args(now=lambda: time)): self.assertEqual(common.fuzzy_time(), 'quarter to one') time.hour = 23 time.minute = 59 self.assertEqual(common.fuzzy_time(), 'round about midnight') time.minute = 33 self.assertEqual(common.fuzzy_time(), 'twenty-five to twelve') time.minute = 60 self.assertEqual(common.fuzzy_time(), 'twelve o\'clock')
def swap_attributes(config, powerline_module): return replace_attr(powerline_module, 'os', Args( path=Args( isfile=lambda path: path.lstrip('/').replace('.json', '') in config, join=os.path.join, expanduser=lambda path: path, realpath=lambda path: path, dirname=os.path.dirname, ), environ={}, ))
def test_file_vcs_status(self): pl = Pl() with vim_module._with('buffer', '/foo') as segment_info: with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda file: 'M', directory=path)): self.assertEqual(vim.file_vcs_status(pl=pl, segment_info=segment_info), [{'highlight_group': ['file_vcs_status_M', 'file_vcs_status'], 'contents': 'M'}]) with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda file: None, directory=path)): self.assertEqual(vim.file_vcs_status(pl=pl, segment_info=segment_info), None) with vim_module._with('buffer', '/bar') as segment_info: with vim_module._with('bufoptions', buftype='nofile'): with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda file: 'M', directory=path)): self.assertEqual(vim.file_vcs_status(pl=pl, segment_info=segment_info), None)
def test_branch(self): pl = Pl() with vim_module._with('buffer', '/foo') as segment_info: with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory=path)): with replace_attr(vim, 'tree_status', lambda repo, pl: None): self.assertEqual(vim.branch(pl=pl, segment_info=segment_info, status_colors=False), [{'divider_highlight_group': 'branch:divider', 'highlight_group': ['branch'], 'contents': 'foo'}]) self.assertEqual(vim.branch(pl=pl, segment_info=segment_info, status_colors=True), [{'divider_highlight_group': 'branch:divider', 'highlight_group': ['branch_clean', 'branch'], 'contents': 'foo'}]) with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'DU', directory=path)): with replace_attr(vim, 'tree_status', lambda repo, pl: 'DU'): self.assertEqual(vim.branch(pl=pl, segment_info=segment_info, status_colors=False), [{'divider_highlight_group': 'branch:divider', 'highlight_group': ['branch'], 'contents': 'foo'}]) self.assertEqual(vim.branch(pl=pl, segment_info=segment_info, status_colors=True), [{'divider_highlight_group': 'branch:divider', 'highlight_group': ['branch_dirty', 'branch'], 'contents': 'foo'}])
def test_fbterm_escapes(self, config): from powerline.shell import ShellPowerline import powerline as powerline_module config['config']['common']['term_escape_style'] = 'fbterm' with swap_attributes(config, powerline_module): with get_powerline_raw(config, ShellPowerline, args=Args(config_path=[''])) as powerline: self.assertEqual(powerline.render(segment_info={}, side='left'), '\x1b[0m\x1b[1;5}\x1b[2;6}\xa0s\x1b[0m\x1b[1;6}\x1b[49m\x1b[22m>>\x1b[0m')
def test_term_truecolor_escapes(self, config): from powerline.shell import ShellPowerline import powerline as powerline_module config['config']['common']['term_truecolor'] = True with swap_attributes(config, powerline_module): with get_powerline_raw(config, ShellPowerline, args=Args(config_path=[''])) as powerline: self.assertEqual(powerline.render(segment_info={}, side='left'), '\x1b[0;38;2;192;0;192;48;2;0;128;128m\xa0s\x1b[0;38;2;0;128;128;49;22m>>\x1b[0m')
def test_bash(self): from powerline.shell import ShellPowerline args = Args(last_exit_code=1, jobnum=0, ext=['shell'], renderer_module='.bash', config={'ext': {'shell': {'theme': 'default_leftonly'}}}) with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info={'args': args}) with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info={'args': args})
def test_screen_escapes(self, config): from powerline.shell import ShellPowerline import powerline as powerline_module config['config']['common']['additional_escapes'] = 'screen' with swap_attributes(config, powerline_module): with get_powerline_raw(config, ShellPowerline, args=Args(config_path=[''])) as powerline: self.assertEqual(powerline.render(segment_info={}, side='left'), '\x1bP\x1b\x1b[0;38;5;5;48;5;6m\x1b\\\xa0s\x1bP\x1b\x1b[0;38;5;6;49;22m\x1b\\>>\x1bP\x1b\x1b[0m\x1b\\')
def test_ipython(self): from powerline.ipython import IPythonPowerline class IpyPowerline(IPythonPowerline): config_paths = None config_overrides = None theme_overrides = {} segment_info = Args(prompt_count=1) with IpyPowerline(logger=get_logger()) as powerline: for prompt_type in ['in', 'in2']: powerline.render(is_prompt=True, matcher_info=prompt_type, segment_info=segment_info) powerline.render(is_prompt=True, matcher_info=prompt_type, segment_info=segment_info) with IpyPowerline(logger=get_logger()) as powerline: for prompt_type in ['out', 'rewrite']: powerline.render(is_prompt=False, matcher_info=prompt_type, segment_info=segment_info) powerline.render(is_prompt=False, matcher_info=prompt_type, segment_info=segment_info)
def test_zsh(self): from powerline.shell import ShellPowerline args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt') with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info={'args': args}) with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info={'args': args})
def test_last_status(self): pl = Pl() segment_info = {'args': Args(last_exit_code=10)} self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [{'contents': '10', 'highlight_group': 'exit_fail'}]) segment_info['args'].last_exit_code = 0 self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None) segment_info['args'].last_exit_code = None self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
def test_escapes(self, config): from powerline.shell import ShellPowerline import powerline as powerline_module with swap_attributes(config, powerline_module): with get_powerline_raw(config, ShellPowerline, args=Args(config_path=[''])) as powerline: self.assertEqual( powerline.render(segment_info={}, side='left'), '\x1b[0;38;5;5;48;5;6m\xa0s\x1b[0;38;5;6;49;22m>>\x1b[0m')
def test_branch(self): pl = Pl() segment_info = {'getcwd': os.getcwd} with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory='/tmp/tests')): with replace_attr(common, 'tree_status', lambda repo, pl: None): self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), [{'highlight_group': ['branch'], 'contents': 'tests'}]) self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=True), [{'contents': 'tests', 'highlight_group': ['branch_clean', 'branch']}]) with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'D ', directory='/tmp/tests')): with replace_attr(common, 'tree_status', lambda repo, pl: 'D '): self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), [{'highlight_group': ['branch'], 'contents': 'tests'}]) self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=True), [{'contents': 'tests', 'highlight_group': ['branch_dirty', 'branch']}]) self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), [{'highlight_group': ['branch'], 'contents': 'tests'}]) with replace_attr(common, 'guess', lambda path: None): self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), None)
def test_no_attributes(self, p, config): def m1(divider=',', **kwargs): return divider.join(kwargs.keys()) + divider config['themes/test/default']['segments'] = { 'left': [{ 'function': 'bar.m1' }] } with replace_item(sys.modules, 'bar', Args(m1=m1)): self.assertRenderEqual(p, '{56} pl,{6-}>>{--}')
def test_last_pipe_status(self): pl = Pl() segment_info = {'args': Args(last_pipe_status=[])} self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None) segment_info['args'].last_pipe_status = [0, 0, 0] self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None) segment_info['args'].last_pipe_status = [0, 2, 0] self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [ {'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True}, {'contents': '2', 'highlight_group': 'exit_fail', 'draw_inner_divider': True}, {'contents': '0', 'highlight_group': 'exit_success', 'draw_inner_divider': True} ])
def test_fuzzy_time(self): time = Args(hour=0, minute=45) pl = Pl() with replace_attr(common, 'datetime', Args(now=lambda: time)): self.assertEqual(common.fuzzy_time(pl=pl), 'quarter to one') time.hour = 23 time.minute = 59 self.assertEqual(common.fuzzy_time(pl=pl), 'round about midnight') time.minute = 33 self.assertEqual(common.fuzzy_time(pl=pl), 'twenty-five to twelve') time.minute = 60 self.assertEqual(common.fuzzy_time(pl=pl), 'twelve o\'clock')
def test_exinclude_function(self, p, config): config['themes/test/default']['segments'] = { 'left': [ highlighted_string('s1', 'g1', exclude_function='mod.foo'), highlighted_string('s2', 'g1', exclude_function='mod.foo', include_function='mod.bar'), highlighted_string('s3', 'g1', include_function='mod.bar'), ] } launched = set() fool = [None] barl = [None] def foo(*args, **kwargs): launched.add('foo') self.assertEqual(set(kwargs.keys()), set(('pl', 'segment_info', 'mode'))) self.assertEqual(args, ()) return fool[0] def bar(*args, **kwargs): launched.add('bar') self.assertEqual(set(kwargs.keys()), set(('pl', 'segment_info', 'mode'))) self.assertEqual(args, ()) return barl[0] with replace_item(sys.modules, 'mod', Args(foo=foo, bar=bar)): fool[0] = True barl[0] = True self.assertRenderEqual(p, '{56} s3{6-}>>{--}') self.assertEqual(launched, set(('foo', 'bar'))) fool[0] = False barl[0] = True self.assertRenderEqual(p, '{56} s1{56}>{56}s2{56}>{56}s3{6-}>>{--}') self.assertEqual(launched, set(('foo', 'bar'))) fool[0] = False barl[0] = False self.assertRenderEqual(p, '{56} s1{6-}>>{--}') self.assertEqual(launched, set(('foo', 'bar'))) fool[0] = True barl[0] = False self.assertRenderEqual(p, '{--}') self.assertEqual(launched, set(('foo', 'bar')))
def test_user(self): new_os = new_module('os', getpid=lambda: 1) new_psutil = new_module('psutil', Process=lambda pid: Args(username='******')) pl = Pl() with replace_env('USER', 'def') as segment_info: with replace_attr(common, 'os', new_os): with replace_attr(common, 'psutil', new_psutil): with replace_attr(common, '_geteuid', lambda: 5): self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ {'contents': 'def', 'highlight_group': 'user'} ]) with replace_attr(common, '_geteuid', lambda: 0): self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ {'contents': 'def', 'highlight_group': ['superuser', 'user']} ])
def test_ipython(self): from powerline.ipython import IpythonPowerline class IpyPowerline(IpythonPowerline): path = None config_overrides = None theme_overrides = {} with IpyPowerline() as powerline: segment_info = Args(prompt_count=1) for prompt_type in ['in', 'in2', 'out', 'rewrite']: powerline.render(matcher_info=prompt_type, segment_info=segment_info) powerline.render(matcher_info=prompt_type, segment_info=segment_info)
def test_zsh(self): from powerline.shell import ShellPowerline args = Args(last_pipe_status=[1, 0], jobnum=0, ext=['shell'], renderer_module='.zsh') segment_info = {'args': args} with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info=segment_info) with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info=segment_info) segment_info['local_theme'] = 'select' with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info=segment_info) segment_info['local_theme'] = 'continuation' segment_info['parser_state'] = 'if cmdsubst' with ShellPowerline(args, run_once=False) as powerline: powerline.render(segment_info=segment_info)
def test_truncate(self, p, config): def m1(divider=',', **kwargs): return divider.join(kwargs.keys()) + divider def truncate(pl, amount, segment, **kwargs): return segment['contents'][:-amount] m1.truncate = truncate config['themes/test/default']['segments'] = { 'left': [{ 'function': 'bar.m1' }] } with replace_item(sys.modules, 'bar', Args(m1=m1)): self.assertRenderEqual(p, '{56} p{6-}>>{--}', width=4)
def test_expand(self, p, config): def m1(divider=',', **kwargs): return divider.join(kwargs.keys()) + divider def expand(pl, amount, segment, **kwargs): return ('-' * amount) + segment['contents'] m1.expand = expand config['themes/test/default']['segments'] = { 'left': [{ 'function': 'bar.m1', 'width': 'auto' }] } with replace_item(sys.modules, 'bar', Args(m1=m1)): self.assertRenderEqual(p, '{56} ----pl,{6-}>>{--}', width=10)
def test_fuzzy_time(self): time = Args(hour=0, minute=45) pl = Pl() with replace_attr(common, 'datetime', Args(now=lambda: time)): self.assertEqual(common.fuzzy_time(pl=pl), 'quarter to one') time.hour = 23 time.minute = 59 self.assertEqual(common.fuzzy_time(pl=pl), 'round about midnight') time.minute = 33 self.assertEqual(common.fuzzy_time(pl=pl), 'twenty‐five to twelve') time.minute = 60 self.assertEqual(common.fuzzy_time(pl=pl), 'twelve o’clock') time.minute = 33 self.assertEqual(common.fuzzy_time(pl=pl, unicode_text=False), 'twenty-five to twelve') time.minute = 60 self.assertEqual(common.fuzzy_time(pl=pl, unicode_text=False), 'twelve o\'clock')
def test_segment_data(self, p, config): def m1(**kwargs): return 'S' def m2(**kwargs): return 'S' sys.modules['bar'] = Args(m1=m1, m2=m2) config['themes/powerline']['segment_data'] = { 'm1': { 'before': '1' }, 'bar.m2': { 'before': '2' }, 'n': { 'before': '3' }, 'm2': { 'before': '4' }, } config['themes/test/default']['segments'] = { 'left': [ { 'function': 'bar.m1' }, { 'function': 'bar.m1', 'name': 'n' }, { 'function': 'bar.m2', 'name': 'n' }, { 'function': 'bar.m2' } ] } self.assertRenderEqual(p, '{56} 1S{56}>{56}3S{610}>>{910}3S{910}>{910}2S{10-}>>{--}')
def test_exinclude_modes_override_functions(self, p, config): config['themes/test/default']['segments'] = { 'left': [ highlighted_string('s1', 'g1', exclude_function='mod.foo', exclude_modes=['m2']), highlighted_string('s2', 'g1', exclude_function='mod.foo', include_modes=['m2']), highlighted_string('s3', 'g1', include_function='mod.foo', exclude_modes=['m2']), highlighted_string('s4', 'g1', include_function='mod.foo', include_modes=['m2']), ] } fool = [None] def foo(*args, **kwargs): return fool[0] with replace_item(sys.modules, 'mod', Args(foo=foo)): fool[0] = True self.assertRenderEqual(p, '{56} s4{6-}>>{--}', mode='m2') self.assertRenderEqual(p, '{56} s3{56}>{56}s4{6-}>>{--}', mode='m1') fool[0] = False self.assertRenderEqual(p, '{56} s2{56}>{56}s4{6-}>>{--}', mode='m2') self.assertRenderEqual(p, '{56} s1{6-}>>{--}', mode='m1')
def test_segment_datas(self, p, config): def m1(divider=',', **kwargs): return divider.join(kwargs.keys()) + divider m1.powerline_segment_datas = { 'powerline': { 'args': { 'divider': ';' } }, 'ascii': { 'args': { 'divider': '--' } } } config['themes/test/default']['segments'] = { 'left': [{ 'function': 'bar.m1' }] } with replace_item(sys.modules, 'bar', Args(m1=m1)): self.assertRenderEqual(p, '{56} pl;{6-}>>{--}')
def test_workspace_lister(self): pl = Pl() with replace_attr(i3wm, 'get_i3_connection', lambda: Args(get_workspaces=self.get_workspaces)): self.assertEqual( list(i3wm.workspace_lister(pl=pl, segment_info={'a': 1})), [ ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '1: w1', 'focused': False, 'urgent': False, 'visible': False } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '2: w2', 'focused': False, 'urgent': False, 'visible': True } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'HDMI1', 'workspace': { 'name': '3: w3', 'focused': False, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'DVI01', 'workspace': { 'name': '4: w4', 'focused': True, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ]) self.assertEqual( list( i3wm.workspace_lister(pl=pl, segment_info={'a': 1}, output='LVDS1')), [ ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '1: w1', 'focused': False, 'urgent': False, 'visible': False } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '2: w2', 'focused': False, 'urgent': False, 'visible': True } }, { 'draw_inner_divider': None }), ]) self.assertEqual( list( i3wm.workspace_lister(pl=pl, segment_info={ 'a': 1, 'output': 'LVDS1' })), [ ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '1: w1', 'focused': False, 'urgent': False, 'visible': False } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '2: w2', 'focused': False, 'urgent': False, 'visible': True } }, { 'draw_inner_divider': None }), ]) self.assertEqual( list( i3wm.workspace_lister(pl=pl, segment_info={ 'a': 1, 'output': 'LVDS1' }, output=False)), [ ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '1: w1', 'focused': False, 'urgent': False, 'visible': False } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'LVDS1', 'workspace': { 'name': '2: w2', 'focused': False, 'urgent': False, 'visible': True } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'HDMI1', 'workspace': { 'name': '3: w3', 'focused': False, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'DVI01', 'workspace': { 'name': '4: w4', 'focused': True, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ]) self.assertEqual( list( i3wm.workspace_lister(pl=pl, segment_info={'a': 1}, only_show=['focused', 'urgent'])), [ ({ 'a': 1, 'output': 'HDMI1', 'workspace': { 'name': '3: w3', 'focused': False, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ({ 'a': 1, 'output': 'DVI01', 'workspace': { 'name': '4: w4', 'focused': True, 'urgent': True, 'visible': True } }, { 'draw_inner_divider': None }), ])
def test_date(self): pl = Pl() with replace_attr(common, 'datetime', Args(now=lambda: Args(strftime=lambda fmt: fmt))): self.assertEqual(common.date(pl=pl), [{'contents': '%Y-%m-%d', 'highlight_group': ['date'], 'divider_highlight_group': None}]) self.assertEqual(common.date(pl=pl, format='%H:%M', istime=True), [{'contents': '%H:%M', 'highlight_group': ['time', 'date'], 'divider_highlight_group': 'time:divider'}])