Merge branch 'kruton-develop' into develop

Fixes #748
This commit is contained in:
ZyX 2014-01-14 23:42:22 +04:00
commit 31f85800b6
2 changed files with 11 additions and 1 deletions

View File

@ -591,9 +591,12 @@ username = False
_geteuid = getattr(os, 'geteuid', lambda: 1) _geteuid = getattr(os, 'geteuid', lambda: 1)
def user(pl, segment_info=None): def user(pl, segment_info=None, hide_user=None):
'''Return the current user. '''Return the current user.
:param str hide_user:
Omit showing segment for users with names equal to this string.
Highlights the user with the ``superuser`` if the effective user ID is 0. Highlights the user with the ``superuser`` if the effective user ID is 0.
Highlight groups used: ``superuser`` or ``user``. It is recommended to define all highlight groups. Highlight groups used: ``superuser`` or ``user``. It is recommended to define all highlight groups.
@ -604,6 +607,8 @@ def user(pl, segment_info=None):
if username is None: if username is None:
pl.warn('Failed to get username') pl.warn('Failed to get username')
return None return None
if username == hide_user:
return None
euid = _geteuid() euid = _geteuid()
return [{ return [{
'contents': username, 'contents': username,

View File

@ -73,12 +73,17 @@ class TestCommon(TestCase):
new_psutil = new_module('psutil', Process=lambda pid: Args(username='def')) new_psutil = new_module('psutil', Process=lambda pid: Args(username='def'))
pl = Pl() pl = Pl()
with replace_env('USER', 'def') as segment_info: with replace_env('USER', 'def') as segment_info:
common.username = False
with replace_attr(common, 'os', new_os): with replace_attr(common, 'os', new_os):
with replace_attr(common, 'psutil', new_psutil): with replace_attr(common, 'psutil', new_psutil):
with replace_attr(common, '_geteuid', lambda: 5): with replace_attr(common, '_geteuid', lambda: 5):
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
{'contents': 'def', 'highlight_group': 'user'} {'contents': 'def', 'highlight_group': 'user'}
]) ])
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)
with replace_attr(common, '_geteuid', lambda: 0): with replace_attr(common, '_geteuid', lambda: 0):
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
{'contents': 'def', 'highlight_group': ['superuser', 'user']} {'contents': 'def', 'highlight_group': ['superuser', 'user']}