2013-03-11 10:40:09 +01:00
|
|
|
|
# vim:fileencoding=utf-8:noet
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
2013-12-01 16:25:19 +01:00
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
|
from powerline.segments import shell, common
|
|
|
|
|
import tests.vim as vim_module
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
2013-03-23 14:51:02 +01:00
|
|
|
|
from tests.lib import Args, urllib_read, replace_attr, new_module, replace_module_module, replace_env, Pl
|
2013-03-03 17:40:35 +01:00
|
|
|
|
from tests import TestCase
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vim = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestShell(TestCase):
|
|
|
|
|
def test_last_status(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-30 18:55:00 +01:00
|
|
|
|
segment_info = {'args': Args(last_exit_code=10)}
|
|
|
|
|
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'contents': '10', 'highlight_group': 'exit_fail'}])
|
2013-03-30 21:03:35 +01:00
|
|
|
|
segment_info['args'].last_exit_code = 0
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
|
2013-03-30 21:03:35 +01:00
|
|
|
|
segment_info['args'].last_exit_code = None
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_last_pipe_status(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-30 18:55:00 +01:00
|
|
|
|
segment_info = {'args': Args(last_pipe_status=[])}
|
|
|
|
|
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None)
|
2013-03-30 21:03:35 +01:00
|
|
|
|
segment_info['args'].last_pipe_status = [0, 0, 0]
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None)
|
2013-03-30 21:03:35 +01:00
|
|
|
|
segment_info['args'].last_pipe_status = [0, 2, 0]
|
2013-04-02 17:06:05 +02:00
|
|
|
|
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}
|
|
|
|
|
])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
2013-11-18 22:01:49 +01:00
|
|
|
|
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')
|
|
|
|
|
|
2014-02-16 17:41:01 +01:00
|
|
|
|
def test_continuation(self):
|
|
|
|
|
pl = Pl()
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info={}), None)
|
|
|
|
|
segment_info = {'parser_state': 'if cmdsubst'}
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'if',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'l',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, right_align=True), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'if',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'r',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, omit_cmdsubst=False), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'if',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': 'cmdsubst',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'l',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, omit_cmdsubst=False, right_align=True), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'if',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'r',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': 'cmdsubst',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, omit_cmdsubst=True, right_align=True), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'if',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'r',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, omit_cmdsubst=True, right_align=True, renames={'if': 'IF'}), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'IF',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'r',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info, omit_cmdsubst=True, right_align=True, renames={'if': None}), [
|
|
|
|
|
{
|
|
|
|
|
'contents': '',
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'r',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
segment_info = {'parser_state': 'then then then cmdsubst'}
|
|
|
|
|
self.assertEqual(shell.continuation(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{
|
|
|
|
|
'contents': 'then',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': 'then',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation',
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': 'then',
|
|
|
|
|
'draw_inner_divider': True,
|
|
|
|
|
'highlight_group': 'continuation:current',
|
|
|
|
|
'width': 'auto',
|
|
|
|
|
'align': 'l',
|
|
|
|
|
},
|
|
|
|
|
])
|
|
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
class TestCommon(TestCase):
|
|
|
|
|
def test_hostname(self):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_env('SSH_CLIENT', '192.168.0.12 40921 22') as segment_info:
|
2013-03-02 16:05:08 +01:00
|
|
|
|
with replace_module_module(common, 'socket', gethostname=lambda: 'abc'):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True), 'abc')
|
2013-04-02 11:29:08 +02:00
|
|
|
|
with replace_module_module(common, 'socket', gethostname=lambda: 'abc.mydomain'):
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc.mydomain')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, exclude_domain=True), 'abc')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True), 'abc.mydomain')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True, exclude_domain=True), 'abc')
|
|
|
|
|
segment_info['environ'].pop('SSH_CLIENT')
|
2013-03-02 16:05:08 +01:00
|
|
|
|
with replace_module_module(common, 'socket', gethostname=lambda: 'abc'):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True), None)
|
2013-03-30 23:32:29 +01:00
|
|
|
|
with replace_module_module(common, 'socket', gethostname=lambda: 'abc.mydomain'):
|
2013-04-02 11:29:08 +02:00
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc.mydomain')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, exclude_domain=True), 'abc')
|
|
|
|
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True, exclude_domain=True), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_user(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
new_os = new_module('os', getpid=lambda: 1)
|
2013-03-17 10:00:01 +01:00
|
|
|
|
new_psutil = new_module('psutil', Process=lambda pid: Args(username='def'))
|
2013-03-30 18:55:00 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_env('USER', 'def') as segment_info:
|
2014-01-10 23:13:25 +01:00
|
|
|
|
common.username = False
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, 'os', new_os):
|
|
|
|
|
with replace_attr(common, 'psutil', new_psutil):
|
2013-03-23 15:55:43 +01:00
|
|
|
|
with replace_attr(common, '_geteuid', lambda: 5):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{'contents': 'def', 'highlight_group': 'user'}
|
|
|
|
|
])
|
2014-01-10 22:00:25 +01:00
|
|
|
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='abc'), [
|
|
|
|
|
{'contents': 'def', 'highlight_group': 'user'}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='def'), None)
|
2013-03-23 15:55:43 +01:00
|
|
|
|
with replace_attr(common, '_geteuid', lambda: 0):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{'contents': 'def', 'highlight_group': ['superuser', 'user']}
|
|
|
|
|
])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_branch(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-30 18:55:00 +01:00
|
|
|
|
segment_info = {'getcwd': os.getcwd}
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory='/tmp/tests')):
|
2013-05-03 07:04:50 +02:00
|
|
|
|
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']}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'D ', directory='/tmp/tests')):
|
2013-05-03 07:04:50 +02:00
|
|
|
|
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'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, 'guess', lambda path: None):
|
2013-05-02 07:43:54 +02:00
|
|
|
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_cwd(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
new_os = new_module('os', path=os.path, sep='/')
|
|
|
|
|
pl = Pl()
|
2013-03-30 18:55:00 +01:00
|
|
|
|
cwd = [None]
|
|
|
|
|
|
|
|
|
|
def getcwd():
|
|
|
|
|
wd = cwd[0]
|
|
|
|
|
if isinstance(wd, Exception):
|
|
|
|
|
raise wd
|
|
|
|
|
else:
|
|
|
|
|
return wd
|
|
|
|
|
|
|
|
|
|
segment_info = {'getcwd': getcwd, 'home': None}
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, 'os', new_os):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
cwd[0] = '/abc/def/ghi/foo/bar'
|
2013-04-02 17:01:03 +02:00
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'def', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'ghi', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
2013-03-30 18:55:00 +01:00
|
|
|
|
segment_info['home'] = '/abc/def/ghi'
|
2013-04-02 17:01:03 +02:00
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
|
|
|
|
|
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=3), [
|
|
|
|
|
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1), [
|
|
|
|
|
{'contents': '⋯', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1, use_path_separator=True), [
|
|
|
|
|
{'contents': '⋯/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2), [
|
|
|
|
|
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'fo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2, use_path_separator=True), [
|
|
|
|
|
{'contents': '~/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
|
|
|
|
|
{'contents': 'fo/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
|
|
|
|
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']}
|
|
|
|
|
])
|
2014-01-23 15:45:09 +01:00
|
|
|
|
cwd[0] = '/etc'
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, use_path_separator=False), [
|
|
|
|
|
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
|
|
|
|
{'contents': 'etc', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, use_path_separator=True), [
|
|
|
|
|
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
|
|
|
|
|
{'contents': 'etc', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
|
|
|
|
cwd[0] = '/'
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, use_path_separator=False), [
|
|
|
|
|
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, use_path_separator=True), [
|
|
|
|
|
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']},
|
|
|
|
|
])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
ose = OSError()
|
|
|
|
|
ose.errno = 2
|
2013-03-30 18:55:00 +01:00
|
|
|
|
cwd[0] = ose
|
|
|
|
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2),
|
2013-04-02 17:01:03 +02:00
|
|
|
|
[{'contents': '[not found]', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd'], 'draw_inner_divider': True}])
|
2013-03-30 18:55:00 +01:00
|
|
|
|
cwd[0] = OSError()
|
|
|
|
|
self.assertRaises(OSError, common.cwd, pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2)
|
|
|
|
|
cwd[0] = ValueError()
|
|
|
|
|
self.assertRaises(ValueError, common.cwd, pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_date(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
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'}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_fuzzy_time(self):
|
|
|
|
|
time = Args(hour=0, minute=45)
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_attr(common, 'datetime', Args(now=lambda: time)):
|
|
|
|
|
self.assertEqual(common.fuzzy_time(pl=pl), 'quarter to one')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
time.hour = 23
|
|
|
|
|
time.minute = 59
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.fuzzy_time(pl=pl), 'round about midnight')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
time.minute = 33
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.fuzzy_time(pl=pl), 'twenty-five to twelve')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
time.minute = 60
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.fuzzy_time(pl=pl), 'twelve o\'clock')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_external_ip(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_attr(common, 'urllib_read', urllib_read):
|
|
|
|
|
self.assertEqual(common.external_ip(pl=pl), [{'contents': '127.0.0.1', 'divider_highlight_group': 'background:divider'}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_uptime(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-04-03 14:21:32 +02:00
|
|
|
|
with replace_attr(common, '_get_uptime', lambda: 259200):
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl), [{'contents': '3d', 'divider_highlight_group': 'background:divider'}])
|
|
|
|
|
with replace_attr(common, '_get_uptime', lambda: 93784):
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl), [{'contents': '1d 2h 3m', 'divider_highlight_group': 'background:divider'}])
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl, shorten_len=4), [{'contents': '1d 2h 3m 4s', 'divider_highlight_group': 'background:divider'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, '_get_uptime', lambda: 65536):
|
2013-04-03 14:21:32 +02:00
|
|
|
|
self.assertEqual(common.uptime(pl=pl), [{'contents': '18h 12m 16s', 'divider_highlight_group': 'background:divider'}])
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl, shorten_len=2), [{'contents': '18h 12m', 'divider_highlight_group': 'background:divider'}])
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl, shorten_len=1), [{'contents': '18h', 'divider_highlight_group': 'background:divider'}])
|
2013-03-17 15:46:59 +01:00
|
|
|
|
|
|
|
|
|
def _get_uptime():
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(common, '_get_uptime', _get_uptime):
|
|
|
|
|
self.assertEqual(common.uptime(pl=pl), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_weather(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_attr(common, 'urllib_read', urllib_read):
|
|
|
|
|
self.assertEqual(common.weather(pl=pl), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9°C', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, temp_coldest=0, temp_hottest=100), [
|
2013-03-23 21:55:54 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9°C', 'gradient_level': 0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, temp_coldest=-100, temp_hottest=-50), [
|
2013-03-23 21:55:54 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9°C', 'gradient_level': 100}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, icons={'cloudy': 'o'}), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': 'o '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9°C', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, icons={'partly_cloudy_day': 'x'}), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': 'x '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9°C', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, unit='F'), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '16°F', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, unit='K'), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '264K', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.weather(pl=pl, temp_format='{temp:.1e}C'), [
|
2013-03-17 15:46:59 +01:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_condition_partly_cloudy_day', 'weather_condition_cloudy', 'weather_conditions', 'weather'], 'contents': '☁ '},
|
2013-03-31 21:47:55 +02:00
|
|
|
|
{'divider_highlight_group': 'background:divider', 'highlight_group': ['weather_temp_gradient', 'weather_temp', 'weather'], 'contents': '-9.0e+00C', 'gradient_level': 30.0}
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_system_load(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-02 15:40:13 +01:00
|
|
|
|
with replace_module_module(common, 'os', getloadavg=lambda: (7.5, 3.5, 1.5)):
|
2013-04-05 15:59:51 +02:00
|
|
|
|
with replace_attr(common, '_cpu_count', lambda: 2):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.system_load(pl=pl),
|
2013-03-31 21:47:55 +02:00
|
|
|
|
[{'contents': '7.5 ', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 100},
|
|
|
|
|
{'contents': '3.5 ', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 75.0},
|
|
|
|
|
{'contents': '1.5', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 0}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(common.system_load(pl=pl, format='{avg:.0f}', threshold_good=0, threshold_bad=1),
|
2013-03-31 21:47:55 +02:00
|
|
|
|
[{'contents': '8 ', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 100},
|
|
|
|
|
{'contents': '4 ', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 100},
|
|
|
|
|
{'contents': '2', 'highlight_group': ['system_load_gradient', 'system_load'], 'divider_highlight_group': 'background:divider', 'gradient_level': 75.0}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_cpu_load_percent(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-13 19:38:56 +01:00
|
|
|
|
with replace_module_module(common, 'psutil', cpu_percent=lambda **kwargs: 52.3):
|
2013-04-08 06:04:22 +02:00
|
|
|
|
self.assertEqual(common.cpu_load_percent(pl=pl), [{
|
|
|
|
|
'contents': '52%',
|
|
|
|
|
'gradient_level': 52.3,
|
|
|
|
|
'highlight_group': ['cpu_load_percent_gradient', 'cpu_load_percent'],
|
|
|
|
|
}])
|
|
|
|
|
self.assertEqual(common.cpu_load_percent(pl=pl, format='{0:.1f}%'), [{
|
|
|
|
|
'contents': '52.3%',
|
|
|
|
|
'gradient_level': 52.3,
|
|
|
|
|
'highlight_group': ['cpu_load_percent_gradient', 'cpu_load_percent'],
|
|
|
|
|
}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_network_load(self):
|
2013-03-24 17:36:28 +01:00
|
|
|
|
from time import sleep
|
2013-03-24 21:10:54 +01:00
|
|
|
|
|
2013-03-23 14:51:02 +01:00
|
|
|
|
def gb(interface):
|
2013-03-17 15:46:59 +01:00
|
|
|
|
return None
|
2013-03-23 14:51:02 +01:00
|
|
|
|
|
|
|
|
|
f = [gb]
|
2013-03-24 16:59:56 +01:00
|
|
|
|
|
2013-03-23 14:51:02 +01:00
|
|
|
|
def _get_bytes(interface):
|
|
|
|
|
return f[0](interface)
|
|
|
|
|
|
|
|
|
|
pl = Pl()
|
|
|
|
|
|
|
|
|
|
with replace_attr(common, '_get_bytes', _get_bytes):
|
|
|
|
|
common.network_load.startup(pl=pl)
|
2013-03-25 15:45:43 +01:00
|
|
|
|
try:
|
|
|
|
|
self.assertEqual(common.network_load(pl=pl, interface='eth0'), None)
|
|
|
|
|
sleep(common.network_load.interval)
|
|
|
|
|
self.assertEqual(common.network_load(pl=pl, interface='eth0'), None)
|
|
|
|
|
while 'prev' not in common.network_load.interfaces.get('eth0', {}):
|
|
|
|
|
sleep(0.1)
|
|
|
|
|
self.assertEqual(common.network_load(pl=pl, interface='eth0'), None)
|
|
|
|
|
|
|
|
|
|
l = [0, 0]
|
|
|
|
|
|
|
|
|
|
def gb2(interface):
|
|
|
|
|
l[0] += 1200
|
|
|
|
|
l[1] += 2400
|
|
|
|
|
return tuple(l)
|
|
|
|
|
f[0] = gb2
|
|
|
|
|
|
|
|
|
|
while not common.network_load.interfaces.get('eth0', {}).get('prev', (None, None))[1]:
|
|
|
|
|
sleep(0.1)
|
|
|
|
|
self.assertEqual(common.network_load(pl=pl, interface='eth0'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': '⬇ 1 KiB/s', 'highlight_group': ['network_load_recv', 'network_load']},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': '⬆ 2 KiB/s', 'highlight_group': ['network_load_sent', 'network_load']},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
self.assertEqual(common.network_load(pl=pl, interface='eth0', recv_format='r {value}', sent_format='s {value}'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 'r 1 KiB/s', 'highlight_group': ['network_load_recv', 'network_load']},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 's 2 KiB/s', 'highlight_group': ['network_load_sent', 'network_load']},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
self.assertEqual(common.network_load(pl=pl, recv_format='r {value}', sent_format='s {value}', suffix='bps', interface='eth0'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 'r 1 Kibps', 'highlight_group': ['network_load_recv', 'network_load']},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 's 2 Kibps', 'highlight_group': ['network_load_sent', 'network_load']},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
self.assertEqual(common.network_load(pl=pl, recv_format='r {value}', sent_format='s {value}', si_prefix=True, interface='eth0'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 'r 1 kB/s', 'highlight_group': ['network_load_recv', 'network_load']},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 's 2 kB/s', 'highlight_group': ['network_load_sent', 'network_load']},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
self.assertEqual(common.network_load(pl=pl, recv_format='r {value}', sent_format='s {value}', recv_max=0, interface='eth0'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 'r 1 KiB/s', 'highlight_group': ['network_load_recv_gradient', 'network_load_gradient', 'network_load_recv', 'network_load'], 'gradient_level': 100},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 's 2 KiB/s', 'highlight_group': ['network_load_sent', 'network_load']},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
|
|
|
|
|
class ApproxEqual(object):
|
|
|
|
|
def __eq__(self, i):
|
|
|
|
|
return abs(i - 50.0) < 1
|
|
|
|
|
|
|
|
|
|
self.assertEqual(common.network_load(pl=pl, recv_format='r {value}', sent_format='s {value}', sent_max=4800, interface='eth0'), [
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 'r 1 KiB/s', 'highlight_group': ['network_load_recv', 'network_load']},
|
|
|
|
|
{'divider_highlight_group': 'background:divider', 'contents': 's 2 KiB/s', 'highlight_group': ['network_load_sent_gradient', 'network_load_gradient', 'network_load_sent', 'network_load'], 'gradient_level': ApproxEqual()},
|
2013-03-25 16:04:18 +01:00
|
|
|
|
])
|
2013-03-25 15:45:43 +01:00
|
|
|
|
finally:
|
|
|
|
|
common.network_load.shutdown()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_virtualenv(self):
|
2013-03-30 18:55:00 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
with replace_env('VIRTUAL_ENV', '/abc/def/ghi') as segment_info:
|
|
|
|
|
self.assertEqual(common.virtualenv(pl=pl, segment_info=segment_info), 'ghi')
|
|
|
|
|
segment_info['environ'].pop('VIRTUAL_ENV')
|
|
|
|
|
self.assertEqual(common.virtualenv(pl=pl, segment_info=segment_info), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
2013-05-02 16:35:13 +02:00
|
|
|
|
def test_environment(self):
|
|
|
|
|
pl = Pl()
|
|
|
|
|
variable = 'FOO';
|
|
|
|
|
value = 'bar';
|
|
|
|
|
with replace_env(variable, value) as segment_info:
|
|
|
|
|
self.assertEqual(common.environment(pl=pl, segment_info=segment_info, variable=variable), value)
|
|
|
|
|
segment_info['environ'].pop(variable)
|
|
|
|
|
self.assertEqual(common.environment(pl=pl, segment_info=segment_info, variable=variable), None)
|
|
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
|
def test_email_imap_alert(self):
|
|
|
|
|
# TODO
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def test_now_playing(self):
|
|
|
|
|
# TODO
|
|
|
|
|
pass
|
|
|
|
|
|
2013-05-09 05:28:24 +02:00
|
|
|
|
def test_battery(self):
|
|
|
|
|
pl = Pl()
|
|
|
|
|
|
2014-02-08 22:48:20 +01:00
|
|
|
|
def _get_capacity(pl):
|
2013-05-09 05:28:24 +02:00
|
|
|
|
return 86
|
|
|
|
|
|
|
|
|
|
with replace_attr(common, '_get_capacity', _get_capacity):
|
|
|
|
|
self.assertEqual(common.battery(pl=pl), [{
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'contents': '86%',
|
2013-05-09 05:28:24 +02:00
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'gradient_level': 86
|
2013-05-09 05:28:24 +02:00
|
|
|
|
}])
|
2014-02-09 10:44:06 +01:00
|
|
|
|
self.assertEqual(common.battery(pl=pl, format='{capacity:.2f}'), [{
|
|
|
|
|
'contents': '0.86',
|
2013-05-09 05:28:24 +02:00
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'gradient_level': 86
|
2013-05-09 05:28:24 +02:00
|
|
|
|
}])
|
|
|
|
|
self.assertEqual(common.battery(pl=pl, steps=7), [{
|
|
|
|
|
'contents': '86%',
|
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'gradient_level': 86
|
2013-05-09 05:28:24 +02:00
|
|
|
|
}])
|
|
|
|
|
self.assertEqual(common.battery(pl=pl, gamify=True), [
|
|
|
|
|
{
|
|
|
|
|
'contents': '♥♥♥♥',
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'draw_inner_divider': False,
|
2013-05-09 05:28:24 +02:00
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
|
|
|
|
'gradient_level': 99
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': '♥',
|
2014-02-09 10:44:06 +01:00
|
|
|
|
'draw_inner_divider': False,
|
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
|
|
|
|
'gradient_level': 1
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
self.assertEqual(common.battery(pl=pl, gamify=True, full_heart='+', empty_heart='-', steps='10'), [
|
|
|
|
|
{
|
|
|
|
|
'contents': '++++++++',
|
|
|
|
|
'draw_inner_divider': False,
|
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
|
|
|
|
'gradient_level': 99
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'contents': '--',
|
|
|
|
|
'draw_inner_divider': False,
|
2013-05-09 05:28:24 +02:00
|
|
|
|
'highlight_group': ['battery_gradient', 'battery'],
|
|
|
|
|
'gradient_level': 1
|
|
|
|
|
}
|
|
|
|
|
])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
class TestVim(TestCase):
|
|
|
|
|
def test_mode(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info), 'NORMAL')
|
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info, override={'i': 'INS'}), 'NORMAL')
|
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info, override={'n': 'NORM'}), 'NORM')
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('mode', 'i') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info), 'INSERT')
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('mode', chr(ord('V') - 0x40)) as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info), 'V·BLCK')
|
|
|
|
|
self.assertEqual(vim.mode(pl=pl, segment_info=segment_info, override={'^V': 'VBLK'}), 'VBLK')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
2013-08-06 10:34:14 +02:00
|
|
|
|
def test_visual_range(self):
|
|
|
|
|
# TODO
|
|
|
|
|
pass
|
|
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
|
def test_modified_indicator(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.modified_indicator(pl=pl, segment_info=segment_info), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info['buffer'][0] = 'abc'
|
|
|
|
|
try:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.modified_indicator(pl=pl, segment_info=segment_info), '+')
|
|
|
|
|
self.assertEqual(vim.modified_indicator(pl=pl, segment_info=segment_info, text='-'), '-')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
finally:
|
2013-03-02 21:02:07 +01:00
|
|
|
|
vim_module._bw(segment_info['bufnr'])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_paste_indicator(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.paste_indicator(pl=pl, segment_info=segment_info), None)
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('options', paste=1):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.paste_indicator(pl=pl, segment_info=segment_info), 'PASTE')
|
|
|
|
|
self.assertEqual(vim.paste_indicator(pl=pl, segment_info=segment_info, text='P'), 'P')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_readonly_indicator(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info), None)
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('bufoptions', readonly=1):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info), '')
|
|
|
|
|
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info, text='L'), 'L')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_file_directory(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), None)
|
|
|
|
|
with replace_env('HOME', '/home/foo', os.environ):
|
2013-12-01 16:25:19 +01:00
|
|
|
|
with vim_module._with('buffer', '/tmp/’’/abc') as segment_info:
|
|
|
|
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/’’/')
|
|
|
|
|
with vim_module._with('buffer', b'/tmp/\xFF\xFF/abc') as segment_info:
|
|
|
|
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/<ff><ff>/')
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('buffer', '/tmp/abc') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/')
|
2013-03-02 15:40:13 +01:00
|
|
|
|
os.environ['HOME'] = '/tmp'
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '~/')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_file_name(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info), None)
|
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info, display_no_file=True),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'contents': '[No file]', 'highlight_group': ['file_name_no_file', 'file_name']}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info, display_no_file=True, no_file_text='X'),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'contents': 'X', 'highlight_group': ['file_name_no_file', 'file_name']}])
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('buffer', '/tmp/abc') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info), 'abc')
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('buffer', '/tmp/’’') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info), '’’')
|
2013-12-01 16:25:19 +01:00
|
|
|
|
with vim_module._with('buffer', b'/tmp/\xFF\xFF') as segment_info:
|
|
|
|
|
self.assertEqual(vim.file_name(pl=pl, segment_info=segment_info), '<ff><ff>')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_file_size(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_size(pl=pl, segment_info=segment_info), '0 B')
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('buffer', os.path.join(os.path.dirname(__file__), 'empty')) as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_size(pl=pl, segment_info=segment_info), '0 B')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_file_opts(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_format(pl=pl, segment_info=segment_info),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'divider_highlight_group': 'background:divider', 'contents': 'unix'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_encoding(pl=pl, segment_info=segment_info),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'divider_highlight_group': 'background:divider', 'contents': 'utf-8'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_type(pl=pl, segment_info=segment_info), None)
|
2013-03-02 21:02:07 +01:00
|
|
|
|
with vim_module._with('bufoptions', filetype='python'):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.file_type(pl=pl, segment_info=segment_info),
|
2013-02-24 11:03:14 +01:00
|
|
|
|
[{'divider_highlight_group': 'background:divider', 'contents': 'python'}])
|
|
|
|
|
|
|
|
|
|
def test_line_percent(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
|
|
|
|
segment_info['buffer'][0:-1] = [str(i) for i in range(100)]
|
|
|
|
|
try:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.line_percent(pl=pl, segment_info=segment_info), '1')
|
2013-02-24 11:03:14 +01:00
|
|
|
|
vim_module._set_cursor(50, 0)
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.line_percent(pl=pl, segment_info=segment_info), '50')
|
|
|
|
|
self.assertEqual(vim.line_percent(pl=pl, segment_info=segment_info, gradient=True),
|
2013-03-23 21:55:54 +01:00
|
|
|
|
[{'contents': '50', 'highlight_group': ['line_percent_gradient', 'line_percent'], 'gradient_level': 50 * 100.0 / 101}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
finally:
|
|
|
|
|
vim_module._bw(segment_info['bufnr'])
|
|
|
|
|
|
2014-01-15 10:32:04 +01:00
|
|
|
|
def test_position(self):
|
|
|
|
|
pl = Pl()
|
|
|
|
|
segment_info = vim_module._get_segment_info()
|
|
|
|
|
try:
|
2014-01-23 09:23:53 +01:00
|
|
|
|
segment_info['buffer'][0:-1] = [str(i) for i in range(99)]
|
|
|
|
|
vim_module._set_cursor(49, 0)
|
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info), '50%')
|
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, gradient=True),
|
|
|
|
|
[{'contents': '50%', 'highlight_group': ['position_gradient', 'position'], 'gradient_level': 50.0}])
|
|
|
|
|
vim_module._set_cursor(0, 0)
|
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info), 'Top')
|
|
|
|
|
vim_module._set_cursor(97, 0)
|
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, position_strings={'top':'Comienzo', 'bottom':'Final', 'all':'Todo'}), 'Final')
|
|
|
|
|
segment_info['buffer'][0:-1] = [str(i) for i in range(2)]
|
|
|
|
|
vim_module._set_cursor(0, 0)
|
2014-01-21 15:48:05 +01:00
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, position_strings={'top':'Comienzo', 'bottom':'Final', 'all':'Todo'}), 'Todo')
|
2014-01-15 10:32:04 +01:00
|
|
|
|
self.assertEqual(vim.position(pl=pl, segment_info=segment_info, gradient=True),
|
|
|
|
|
[{'contents': 'All', 'highlight_group': ['position_gradient', 'position'], 'gradient_level': 0.0}])
|
|
|
|
|
finally:
|
|
|
|
|
vim_module._bw(segment_info['bufnr'])
|
|
|
|
|
|
2013-02-24 11:03:14 +01:00
|
|
|
|
def test_cursor_current(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-02-24 11:03:14 +01:00
|
|
|
|
segment_info = vim_module._get_segment_info()
|
2013-03-23 14:51:02 +01:00
|
|
|
|
self.assertEqual(vim.line_current(pl=pl, segment_info=segment_info), '1')
|
|
|
|
|
self.assertEqual(vim.col_current(pl=pl, segment_info=segment_info), '1')
|
2013-04-13 17:28:34 +02:00
|
|
|
|
self.assertEqual(vim.virtcol_current(pl=pl, segment_info=segment_info), [{
|
|
|
|
|
'highlight_group': ['virtcol_current_gradient', 'virtcol_current', 'col_current'], 'contents': '1', 'gradient_level': 100.0 / 80,
|
|
|
|
|
}])
|
|
|
|
|
self.assertEqual(vim.virtcol_current(pl=pl, segment_info=segment_info, gradient=False), [{
|
|
|
|
|
'highlight_group': ['virtcol_current', 'col_current'], 'contents': '1',
|
|
|
|
|
}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_modified_buffers(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
|
|
|
|
self.assertEqual(vim.modified_buffers(pl=pl), None)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_branch(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-17 15:46:59 +01:00
|
|
|
|
with vim_module._with('buffer', '/foo') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory=path)):
|
2013-05-03 07:04:50 +02:00
|
|
|
|
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'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
with replace_attr(vim, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'DU', directory=path)):
|
2013-05-03 07:04:50 +02:00
|
|
|
|
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'}])
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
def test_file_vcs_status(self):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
pl = Pl()
|
2013-03-17 15:46:59 +01:00
|
|
|
|
with vim_module._with('buffer', '/foo') as segment_info:
|
2013-03-23 14:51:02 +01:00
|
|
|
|
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),
|
2013-03-17 15:46:59 +01:00
|
|
|
|
[{'highlight_group': ['file_vcs_status_M', 'file_vcs_status'], 'contents': 'M'}])
|
2013-03-23 14:51:02 +01:00
|
|
|
|
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)
|
2013-03-17 15:46:59 +01:00
|
|
|
|
with vim_module._with('buffer', '/bar') as segment_info:
|
|
|
|
|
with vim_module._with('bufoptions', buftype='nofile'):
|
2013-03-23 14:51:02 +01:00
|
|
|
|
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)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
|
|
|
|
|
old_cwd = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setUpModule():
|
|
|
|
|
global old_cwd
|
2013-03-03 17:40:35 +01:00
|
|
|
|
global __file__
|
2013-03-02 16:05:08 +01:00
|
|
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), 'path')))
|
2013-02-24 11:03:14 +01:00
|
|
|
|
old_cwd = os.getcwd()
|
2013-03-03 17:40:35 +01:00
|
|
|
|
__file__ = os.path.abspath(__file__)
|
2013-02-24 11:03:14 +01:00
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
from powerline.segments import vim
|
|
|
|
|
globals()['vim'] = vim
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def tearDownModule():
|
|
|
|
|
global old_cwd
|
|
|
|
|
os.chdir(old_cwd)
|
2013-03-02 16:05:08 +01:00
|
|
|
|
sys.path.pop(0)
|
2013-03-03 17:40:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
from tests import main
|
|
|
|
|
main()
|