2013-11-16 22:27:20 +01:00
|
|
|
# vim:fileencoding=utf-8:noet
|
|
|
|
|
|
|
|
'''Tests for shell.py parser'''
|
|
|
|
|
2014-08-31 20:55:26 +02:00
|
|
|
from __future__ import (unicode_literals, division, absolute_import, print_function)
|
2013-11-16 22:27:20 +01:00
|
|
|
|
|
|
|
import sys
|
2014-08-31 20:55:26 +02:00
|
|
|
|
2013-11-16 22:27:20 +01:00
|
|
|
if sys.version_info < (3,):
|
|
|
|
from io import BytesIO as StrIO
|
|
|
|
else:
|
2014-08-31 20:55:26 +02:00
|
|
|
from io import StringIO as StrIO
|
|
|
|
|
2014-10-25 14:00:32 +02:00
|
|
|
from powerline.commands.main import get_argparser, finish_args
|
2014-08-31 20:55:26 +02:00
|
|
|
|
|
|
|
from tests import TestCase
|
|
|
|
from tests.lib import replace_attr
|
2013-11-16 22:27:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestParser(TestCase):
|
|
|
|
def test_main_err(self):
|
|
|
|
parser = get_argparser()
|
|
|
|
out = StrIO()
|
|
|
|
err = StrIO()
|
2014-02-26 05:49:25 +01:00
|
|
|
|
2013-11-16 22:27:20 +01:00
|
|
|
def flush():
|
|
|
|
out.truncate(0)
|
|
|
|
err.truncate(0)
|
2014-02-26 05:49:25 +01:00
|
|
|
|
2013-11-16 22:27:20 +01:00
|
|
|
with replace_attr(sys, 'stdout', out, 'stderr', err):
|
|
|
|
for raising_args, raising_reg in [
|
|
|
|
([], 'too few arguments|the following arguments are required: ext'),
|
|
|
|
(['-r'], 'expected one argument'),
|
|
|
|
(['shell', '-r'], 'expected one argument'),
|
|
|
|
(['shell', '-w'], 'expected one argument'),
|
|
|
|
(['shell', '-c'], 'expected one argument'),
|
|
|
|
(['shell', '-t'], 'expected one argument'),
|
|
|
|
(['shell', '-p'], 'expected one argument'),
|
|
|
|
(['shell', '-R'], 'expected one argument'),
|
2015-01-06 14:29:54 +01:00
|
|
|
(['shell', '--renderer-module'], 'expected one argument'),
|
2013-11-16 22:27:20 +01:00
|
|
|
(['shell', '--width'], 'expected one argument'),
|
2015-01-06 14:29:54 +01:00
|
|
|
(['shell', '--last-exit-code'], 'expected one argument'),
|
|
|
|
(['shell', '--last-pipe-status'], 'expected one argument'),
|
2015-01-06 18:58:46 +01:00
|
|
|
(['shell', '--config-override'], 'expected one argument'),
|
|
|
|
(['shell', '--theme-override'], 'expected one argument'),
|
2015-01-06 14:29:54 +01:00
|
|
|
(['shell', '--config-path'], 'expected one argument'),
|
|
|
|
(['shell', '--renderer-arg'], 'expected one argument'),
|
2014-01-17 17:54:47 +01:00
|
|
|
(['shell', '--jobnum'], 'expected one argument'),
|
2014-08-15 17:15:49 +02:00
|
|
|
(['-r', '.zsh'], 'too few arguments|the following arguments are required: ext'),
|
2015-01-07 23:46:16 +01:00
|
|
|
(['shell', '--last-exit-code', 'i'], 'invalid int_or_sig value'),
|
2015-01-06 14:29:54 +01:00
|
|
|
(['shell', '--last-pipe-status', '1 i'], 'invalid <lambda> value'),
|
2013-11-16 22:27:20 +01:00
|
|
|
]:
|
|
|
|
self.assertRaises(SystemExit, parser.parse_args, raising_args)
|
|
|
|
self.assertFalse(out.getvalue())
|
|
|
|
self.assertRegexpMatches(err.getvalue(), raising_reg)
|
|
|
|
flush()
|
|
|
|
|
|
|
|
def test_main_normal(self):
|
|
|
|
parser = get_argparser()
|
|
|
|
out = StrIO()
|
|
|
|
err = StrIO()
|
|
|
|
with replace_attr(sys, 'stdout', out, 'stderr', err):
|
|
|
|
for argv, expargs in [
|
2014-08-15 17:15:49 +02:00
|
|
|
(['shell'], {'ext': ['shell']}),
|
|
|
|
(['shell', '-r', '.zsh'], {'ext': ['shell'], 'renderer_module': '.zsh'}),
|
2013-11-16 22:27:20 +01:00
|
|
|
([
|
|
|
|
'shell',
|
|
|
|
'left',
|
2014-08-15 17:15:49 +02:00
|
|
|
'-r', '.zsh',
|
2015-01-06 14:29:54 +01:00
|
|
|
'--last-exit-code', '10',
|
|
|
|
'--last-pipe-status', '10 20 30',
|
2014-01-17 17:54:47 +01:00
|
|
|
'--jobnum=10',
|
2013-11-16 22:27:20 +01:00
|
|
|
'-w', '100',
|
|
|
|
'-c', 'common.term_truecolor=true',
|
|
|
|
'-c', 'common.spaces=4',
|
|
|
|
'-t', 'default.segment_data.hostname.before=H:',
|
|
|
|
'-p', '.',
|
2014-08-05 11:39:01 +02:00
|
|
|
'-p', '..',
|
2014-02-26 05:49:25 +01:00
|
|
|
'-R', 'smth={"abc":"def"}',
|
2013-11-16 22:27:20 +01:00
|
|
|
], {
|
|
|
|
'ext': ['shell'],
|
|
|
|
'side': 'left',
|
2014-08-15 17:15:49 +02:00
|
|
|
'renderer_module': '.zsh',
|
2013-11-16 22:27:20 +01:00
|
|
|
'last_exit_code': 10,
|
|
|
|
'last_pipe_status': [10, 20, 30],
|
2014-01-17 17:54:47 +01:00
|
|
|
'jobnum': 10,
|
2013-11-16 22:27:20 +01:00
|
|
|
'width': 100,
|
2015-01-06 18:58:46 +01:00
|
|
|
'config_override': {'common': {'term_truecolor': True, 'spaces': 4}},
|
|
|
|
'theme_override': {
|
2013-11-16 22:27:20 +01:00
|
|
|
'default': {
|
|
|
|
'segment_data': {
|
|
|
|
'hostname': {
|
|
|
|
'before': 'H:'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2014-08-05 11:39:01 +02:00
|
|
|
'config_path': ['.', '..'],
|
2013-11-16 22:27:20 +01:00
|
|
|
'renderer_arg': {'smth': {'abc': 'def'}},
|
|
|
|
}),
|
|
|
|
(['shell', '-R', 'arg=true'], {'ext': ['shell'], 'renderer_arg': {'arg': True}}),
|
2014-05-04 10:58:09 +02:00
|
|
|
(['shell', '-R', 'arg=true', '-R', 'arg='], {'ext': ['shell'], 'renderer_arg': {}}),
|
|
|
|
(['shell', '-R', 'arg='], {'ext': ['shell'], 'renderer_arg': {}}),
|
2013-11-16 22:27:20 +01:00
|
|
|
(['shell', '-t', 'default.segment_info={"hostname": {}}'], {
|
|
|
|
'ext': ['shell'],
|
2015-01-06 18:58:46 +01:00
|
|
|
'theme_override': {
|
2013-11-16 22:27:20 +01:00
|
|
|
'default': {
|
|
|
|
'segment_info': {
|
|
|
|
'hostname': {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}),
|
2015-01-06 18:58:46 +01:00
|
|
|
(['shell', '-c', 'common={ }'], {'ext': ['shell'], 'config_override': {'common': {}}}),
|
2013-11-16 22:27:20 +01:00
|
|
|
]:
|
|
|
|
args = parser.parse_args(argv)
|
2015-01-06 20:05:51 +01:00
|
|
|
finish_args({}, args)
|
2013-11-16 22:27:20 +01:00
|
|
|
for key, val in expargs.items():
|
|
|
|
self.assertEqual(getattr(args, key), val)
|
|
|
|
for key, val in args.__dict__.items():
|
|
|
|
if key not in expargs:
|
|
|
|
self.assertFalse(val, msg='key {0} is {1} while it should be something false'.format(key, val))
|
|
|
|
self.assertFalse(err.getvalue() + out.getvalue(), msg='unexpected output: {0!r} {1!r}'.format(
|
|
|
|
err.getvalue(),
|
|
|
|
out.getvalue(),
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from tests import main
|
|
|
|
main()
|