summaryrefslogtreecommitdiffstats
path: root/tests/test_python/test_cmdline.py
blob: 470a7b4c4c8c8ce4ecb558c67250fa272bb637ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# vim:fileencoding=utf-8:noet

'''Tests for shell.py parser'''

from __future__ import (unicode_literals, division, absolute_import, print_function)

import sys

if sys.version_info < (3,):
	from io import BytesIO as StrIO
else:
	from io import StringIO as StrIO

from powerline.commands.main import get_argparser, finish_args

from tests.modules import TestCase
from tests.modules.lib import replace_attr


class TestParser(TestCase):
	def test_main_err(self):
		parser = get_argparser()
		out = StrIO()
		err = StrIO()

		def flush():
			out.truncate(0)
			err.truncate(0)

		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'),
				(['shell', '--renderer-module'],         'expected one argument'),
				(['shell', '--width'],                   'expected one argument'),
				(['shell', '--last-exit-code'],          'expected one argument'),
				(['shell', '--last-pipe-status'],        'expected one argument'),
				(['shell', '--config-override'],         'expected one argument'),
				(['shell', '--theme-override'],          'expected one argument'),
				(['shell', '--config-path'],             'expected one argument'),
				(['shell', '--renderer-arg'],            'expected one argument'),
				(['shell', '--jobnum'],                  'expected one argument'),
				(['-r', '.zsh'],                         'too few arguments|the following arguments are required: ext'),
				(['shell', '--last-exit-code', 'i'],     'invalid int_or_sig value'),
				(['shell', '--last-pipe-status', '1 i'], 'invalid <lambda> value'),
			]:
				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 [
				(['shell', 'left'],       {'ext': ['shell'], 'side': 'left'}),
				(['shell', 'left', '-r', '.zsh'], {'ext': ['shell'], 'renderer_module': '.zsh', 'side': 'left'}),
				([
					'shell',
					'left',
					'-r', '.zsh',
					'--last-exit-code', '10',
					'--last-pipe-status', '10 20 30',
					'--jobnum=10',
					'-w', '100',
					'-c', 'common.term_truecolor=true',
					'-c', 'common.spaces=4',
					'-t', 'default.segment_data.hostname.before=H:',
					'-p', '.',
					'-p', '..',
					'-R', 'smth={"abc":"def"}',
				], {
					'ext': ['shell'],
					'side': 'left',
					'renderer_module': '.zsh',
					'last_exit_code': 10,
					'last_pipe_status': [10, 20, 30],
					'jobnum': 10,
					'width': 100,
					'config_override': {'common': {'term_truecolor': True, 'spaces': 4}},
					'theme_override': {
						'default': {
							'segment_data': {
								'hostname': {
									'before': 'H:'
								}
							}
						}
					},
					'config_path': ['.', '..'],
					'renderer_arg': {'smth': {'abc': 'def'}},
				}),
				(['shell', 'left', '-R', 'arg=true'], {
					'ext': ['shell'],
					'side': 'left',
					'renderer_arg': {'arg': True},
				}),
				(['shell', 'left', '-R', 'arg=true', '-R', 'arg='], {
					'ext': ['shell'],
					'side': 'left',
					'renderer_arg': {},
				}),
				(['shell', 'left', '-R', 'arg='], {'ext': ['shell'], 'renderer_arg': {}, 'side': 'left'}),
				(['shell', 'left', '-t', 'default.segment_info={"hostname": {}}'], {
					'ext': ['shell'],
					'side': 'left',
					'theme_override': {
						'default': {
							'segment_info': {
								'hostname': {}
							}
						}
					},
				}),
				(['shell', 'left', '-c', 'common={ }'], {
					'ext': ['shell'],
					'side': 'left',
					'config_override': {'common': {}},
				}),
				(['shell', 'left', '--last-pipe-status='], {
					'ext': ['shell'],
					'side': 'left',
					'last_pipe_status': [],
				}),
			]:
				args = parser.parse_args(argv)
				finish_args(parser, {}, args)
				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.modules import main
	main()