cli3 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
cli/tests/parser.py ADDED
@@ -0,0 +1,294 @@
1
+ """
2
+ CLI Parser UnitTests
3
+ """
4
+ from pathlib import Path
5
+ from typing import Iterable, List, Tuple, Type, Union, cast
6
+ from unittest import TestCase
7
+
8
+ from ._apps import APP_V1, APP_V2
9
+ from .. import Arg, Command, Flag, Parser, ParsedCmd
10
+ from ..parser import Invalid, MissingValue, ParseCtx, CommandRequired, Missing, Unexpected
11
+
12
+ #** Variables **#
13
+ __all__ = ['ParserTestsV1', 'ParserTestsV2']
14
+
15
+ DEFAULT_OPTS = {'user': 'root', 'log': 10, 'debug': False, 'repeat': None, 'help': False}
16
+
17
+ #** Classes **#
18
+
19
+ class ParserTests(TestCase):
20
+ __test__ = False
21
+
22
+ app: Command
23
+
24
+ def parse(self, args: List[str]) -> ParsedCmd:
25
+ """
26
+ """
27
+ return Parser(self.app).parse(args)
28
+
29
+ def assertPath(self, ctx: ParseCtx, path: List[str]):
30
+ """
31
+ """
32
+ expected = [c.name for c in ctx.path]
33
+ self.assertListEqual(expected, path)
34
+
35
+ def assertMissing(self,
36
+ args: List[str],
37
+ path: List[str],
38
+ missing: List[Union[Arg, Flag]],
39
+ exception: Union[Type[Missing], Type[MissingValue]] = Missing,
40
+ ):
41
+ """
42
+ """
43
+ error = None
44
+ try:
45
+ self.parse(args)
46
+ self.assertFalse(True)
47
+ except exception as e:
48
+ error = e
49
+ self.assertIsNotNone(error)
50
+ error = cast(Missing, error)
51
+ self.assertPath(error.ctx, [self.app.name, *path])
52
+ self.assertEqual(len(error.missing), len(missing))
53
+ for actual, expect in zip(error.missing, missing):
54
+ self.assertEqual(actual.__class__, expect.__class__)
55
+ self.assertEqual(actual.name, expect.name)
56
+
57
+ def assertMissingMulti(self,
58
+ tests: Iterable[Tuple[List[str], List[str], List[Union[Arg, Flag]]]],
59
+ exception: Union[Type[Missing], Type[MissingValue]] = Missing,
60
+ ):
61
+ """
62
+ """
63
+ for args, path, unexpected in tests:
64
+ with self.subTest(args):
65
+ self.assertMissing(args, path, unexpected, exception)
66
+
67
+ def assertInvalid(self,
68
+ args: List[str], path: List[str], missing: List[Union[Arg, Flag]]):
69
+ """
70
+ """
71
+ error = None
72
+ try:
73
+ self.parse(args)
74
+ self.assertFalse(True)
75
+ except Invalid as e:
76
+ error = e
77
+ self.assertIsNotNone(error)
78
+ error = cast(Invalid, error)
79
+ self.assertPath(error.ctx, [self.app.name, *path])
80
+ self.assertEqual(len(error.invalid), len(missing))
81
+ for actual, expect in zip(error.invalid, missing):
82
+ self.assertEqual(actual.__class__, expect.__class__)
83
+ self.assertEqual(actual.name, expect.name)
84
+
85
+ def assertInvalidMulti(self,
86
+ tests: Iterable[Tuple[List[str], List[str], List[Union[Arg, Flag]]]]):
87
+ """
88
+ """
89
+ for args, path, unexpected in tests:
90
+ with self.subTest(args):
91
+ self.assertInvalid(args, path, unexpected)
92
+
93
+ def assertUnexpected(self,
94
+ args: List[str], path: List[str], unexpected: List[str]):
95
+ """
96
+ """
97
+ error = None
98
+ try:
99
+ self.parse(args)
100
+ self.assertFalse(True)
101
+ except Unexpected as e:
102
+ error = e
103
+ self.assertIsNotNone(error)
104
+ error = cast(Unexpected, error)
105
+ self.assertPath(error.ctx, [self.app.name, *path])
106
+ self.assertListEqual(error.unexpected, unexpected)
107
+
108
+ def assertUnexpectedMulti(self,
109
+ tests: Iterable[Tuple[List[str], List[str], List[str]]]):
110
+ """
111
+ """
112
+ for args, path, unexpected in tests:
113
+ with self.subTest(args):
114
+ self.assertUnexpected(args, path, unexpected)
115
+
116
+ def test_flag_not_defined(self):
117
+ """
118
+ ensure error is raised on undefined-flags
119
+ """
120
+ self.assertUnexpectedMulti([
121
+ (['-a'], [], ['-a']),
122
+ (['echo', '-a'], ['echo'], ['-a']),
123
+ (['do', 'run', '1', '-a'], ['do', 'run'], ['-a']),
124
+ ])
125
+
126
+ def test_flag_invalid(self):
127
+ """
128
+ ensure error invalid flag location
129
+ """
130
+ self.assertUnexpectedMulti([
131
+ (['-f', 'echo', 'test'], [], ['-f']),
132
+ (['echo', 'test', '-l'], ['echo'], ['-l']),
133
+ (['echo', 'test', '-u'], ['echo'], ['-u']),
134
+ ])
135
+
136
+ def test_flag_invalid_value(self):
137
+ """
138
+ ensure error on flag invalid value
139
+ """
140
+ self.assertInvalidMulti([
141
+ (['-l', 'a'], [], [Flag('log')]),
142
+ (['-l', 'a', '-r', '1', '-r', '2'], [], [Flag('log')]),
143
+ (['-l', 'a', '-r', '1', '-r', 'a'], [], [Flag('log'), Flag('repeat')]),
144
+ ])
145
+
146
+ def test_flag_missing_value(self):
147
+ """
148
+ ensure error on flag missing value
149
+ """
150
+ self.assertMissingMulti([
151
+ (['-u'], [], [Flag('user')]),
152
+ (['-l'], [], [Flag('log')]),
153
+ (['-r', '1', '-r'], [], [Flag('repeat')]),
154
+ (['-u', 'echo', 'test'], [], [Flag('user')]),
155
+ (['echo', 'test', '-f'], ['echo'], [Flag('file')]),
156
+ ], MissingValue)
157
+
158
+ def test_flag_repeat(self):
159
+ """
160
+ ensure repeated flags work as intended or error if not repeat
161
+ """
162
+ result = self.parse(['-r', '1', '-r', '2', '-r', '3'])
163
+ self.assertDictEqual(result.args, {})
164
+ self.assertDictEqual(result.flags, {**DEFAULT_OPTS, 'repeat': [1,2,3]})
165
+ self.assertDictEqual(result.commands, {})
166
+ self.assertUnexpectedMulti([
167
+ (['-u', 'a', '-u', 'b'], [], ['-u', 'b']),
168
+ (['-d', '-d'], [], ['-d']),
169
+ (['-d', '--debug'], [], ['--debug']),
170
+ (['--debug', '-d'], [], ['-d']),
171
+ (['echo', 'test', '-f', 'a', '-f', 'b'], ['echo'], ['-f']),
172
+ ])
173
+
174
+ def test_argument_extra(self):
175
+ """
176
+ ensure error is raised when too many arguments are present
177
+ """
178
+ self.assertUnexpectedMulti([
179
+ (['do', 'run', '1', '2', '3'], ['do', 'run'], ['3']),
180
+ (['do', 'fly', '4', '5'], ['do', 'fly'], ['5']),
181
+ ])
182
+
183
+ def test_argument_missing(self):
184
+ """
185
+ ensure error is raised when arguments arent enough
186
+ """
187
+ self.assertMissingMulti([
188
+ (['echo'], ['echo'], [Arg('test')]),
189
+ (['do', 'run'], ['do', 'run'], [Arg('dist1')]),
190
+ (['do', 'fly'], ['do', 'fly'], [Arg('dist2')]),
191
+ ])
192
+
193
+ def test_argument_invalid(self):
194
+ """
195
+ ensure error is raised when arugment is invalid
196
+ """
197
+ self.assertInvalidMulti([
198
+ (['do', 'run', 'a'], ['do', 'run'], [Arg('dist1')]),
199
+ (['do', 'run', '1', 'b'], ['do', 'run'], [Arg('dist2')]),
200
+ (['do', 'run', 'a', 'b'], ['do', 'run'], [Arg('dist1'), Arg('dist2')]),
201
+ (['do', 'fly', 'a'], ['do', 'fly'], [Arg('dist2')])
202
+ ])
203
+
204
+ def test_command_invalid(self):
205
+ """
206
+ ensure error is raised with invalid command/argument
207
+ """
208
+ self.assertUnexpectedMulti([
209
+ (['badcmd'], [], ['badcmd']),
210
+ (['do', 'badcmd'], ['do'], ['badcmd']),
211
+ ])
212
+
213
+ def test_command_missing(self):
214
+ """
215
+ ensure error is raised when subcommand is required
216
+ """
217
+ try:
218
+ self.parse(['do'])
219
+ self.assertFalse(True)
220
+ except CommandRequired as e:
221
+ self.assertPath(e.ctx, [self.app.name, 'do'])
222
+ self.assertListEqual([c.name for c in e.commands], ['run', 'fly'])
223
+
224
+ def test_simple(self):
225
+ """
226
+ ensure simple single-command parse works as intended
227
+ """
228
+ result = self.parse([])
229
+ self.assertDictEqual(result.args, {})
230
+ self.assertDictEqual(result.flags, DEFAULT_OPTS)
231
+ self.assertDictEqual(result.commands, {})
232
+
233
+ result = self.parse(['-d'])
234
+ self.assertDictEqual(result.args, {})
235
+ self.assertDictEqual(result.flags, {**DEFAULT_OPTS, 'debug': True})
236
+ self.assertDictEqual(result.commands, {})
237
+
238
+ for value in ('info', '20'):
239
+ with self.subTest(value):
240
+ result = self.parse(['-l', value])
241
+ self.assertDictEqual(result.args, {})
242
+ self.assertDictEqual(result.flags, {**DEFAULT_OPTS, 'log': 20})
243
+ self.assertDictEqual(result.commands, {})
244
+
245
+ def test_sub_command(self):
246
+ """
247
+ ensure single sub-command parse works as intended
248
+ """
249
+ result = self.parse(['echo', '-d', '-f', 'file', 'test'])
250
+ echo = result.commands['echo']
251
+ self.assertEqual(len(echo), 1)
252
+ self.assertDictEqual(result.args, {})
253
+ self.assertDictEqual(result.flags, DEFAULT_OPTS)
254
+ self.assertEqual(len(result.commands), 1)
255
+ self.assertDictEqual(echo[0].flags, {'dry': True, 'file': Path('file')})
256
+
257
+ result = self.parse(['echo', '--', '-d', '-f', 'file', 'test'])
258
+ echo = result.commands['echo']
259
+ self.assertEqual(len(echo), 1)
260
+ self.assertDictEqual(result.args, {})
261
+ self.assertDictEqual(result.flags, DEFAULT_OPTS)
262
+ self.assertEqual(len(result.commands), 1)
263
+ self.assertDictEqual(echo[0].args, {'test': ['-d', '-f', 'file', 'test']})
264
+ self.assertDictEqual(echo[0].flags, {'dry': False, 'file': None})
265
+
266
+ def test_subsub_command(self):
267
+ """
268
+ ensure double sub-command parse works as intended
269
+ """
270
+ args = (['do', 'run', '--', '5'], ['do', 'run', '5'])
271
+ for args in args:
272
+ with self.subTest(args):
273
+ result = self.parse(args)
274
+ dos = result.commands['do']
275
+ do = dos[0]
276
+ runs = do.commands['run']
277
+ run = runs[0]
278
+ self.assertEqual(len(dos), 1)
279
+ self.assertEqual(len(runs), 1)
280
+ self.assertDictEqual(result.args, {})
281
+ self.assertDictEqual(result.flags, DEFAULT_OPTS)
282
+ self.assertDictEqual(do.args, {})
283
+ self.assertDictEqual(do.flags, {'kill': False})
284
+ self.assertDictEqual(run.args, {'dist1': 5, 'dist2': 42})
285
+ self.assertDictEqual(run.flags, {'km': False})
286
+ self.assertDictEqual(run.commands, {})
287
+
288
+ class ParserTestsV1(ParserTests):
289
+ __test__ = True
290
+ app: Command = APP_V1
291
+
292
+ class ParserTestsV2(ParserTests):
293
+ __test__ = True
294
+ app: Command = APP_V2
cli/tests/suggest.py ADDED
@@ -0,0 +1,77 @@
1
+ """
2
+ CLI Suggestion UnitTests
3
+ """
4
+ from typing import List
5
+ from unittest import TestCase
6
+
7
+ from ._apps import USERS, APP_V1, APP_V2
8
+ from .. import Command, Suggestor
9
+
10
+ #** Variables **#
11
+ __all__ = ['SuggestTestsV1', 'SuggestTestsV2']
12
+
13
+ #** Classes **#
14
+
15
+ class SuggestTests(TestCase):
16
+ __test__ = False
17
+
18
+ app: Command
19
+
20
+ def suggest(self, args: List[str], partial: bool = False) -> List[str]:
21
+ """
22
+ """
23
+ return Suggestor(self.app).suggest_list(args, partial)
24
+
25
+ def assertSuggest(self,
26
+ args: List[str], expect: List[str], partial: bool = False):
27
+ """
28
+ """
29
+ results = self.suggest(args, partial)
30
+ self.assertListEqual(results, expect)
31
+
32
+ def test_command(self):
33
+ """
34
+ """
35
+ self.assertSuggest([], ['echo', 'do', '-u', '--user', '-l', '--log', '-d', '--debug', '-r', '--repeat'])
36
+ self.assertSuggest(['do'], ['run', 'fly'])
37
+
38
+ def test_flag(self):
39
+ """
40
+ """
41
+ self.assertSuggest(['-'], ['u', '-user', 'l', '-log', 'd', '-debug', 'r', '-repeat'], True)
42
+ self.assertSuggest(['--'], ['user', 'log', 'debug', 'repeat'], True)
43
+ self.assertSuggest(['-u'], [], True)
44
+ self.assertSuggest(['-u', 'jeff', '--'], ['log', 'debug', 'repeat'], True)
45
+ self.assertSuggest(['-u', 'jeff', '-r', '1', '--'], ['log', 'debug', 'repeat'], True)
46
+ self.assertSuggest(['do', 'run', '1', '1', '-'], ['-km'], True)
47
+
48
+ def test_flag_value(self):
49
+ """
50
+ """
51
+ self.assertSuggest(['-u'], USERS)
52
+ self.assertSuggest(['-u', 'b'], ['ob'], True)
53
+ self.assertSuggest(['-u', 'a'], ['iden', 'dmin'], True)
54
+ self.assertSuggest(['-u', 'ai'], ['den'], True)
55
+ self.assertSuggest(['-u', 'j'], ['eff', 'erry'], True)
56
+ self.assertSuggest(['-u', 'je'], ['ff', 'rry'], True)
57
+ self.assertSuggest(['-u', 'jeff'], [], True)
58
+ self.assertSuggest(['-u', 'jeff'], ['echo', 'do', '-l', '--log', '-d', '--debug', '-r', '--repeat'])
59
+
60
+ def test_argument(self):
61
+ """
62
+ """
63
+ self.assertSuggest(['do', 'run'], [], True)
64
+ self.assertSuggest(['do', 'run'], ['1', '2', '3', '11'])
65
+ self.assertSuggest(['do', 'run', '1'], ['1'], True)
66
+ self.assertSuggest(['do', 'run', '1'], ['42'])
67
+ self.assertSuggest(['do', 'run', '1', '1'], ['--km'])
68
+ self.assertSuggest(['do', 'fly'], [])
69
+ self.assertSuggest(['do', 'fly', '1'], ['--km'])
70
+
71
+ class SuggestTestsV1(SuggestTests):
72
+ __test__ = True
73
+ app: Command = APP_V1
74
+
75
+ class SuggestTestsV2(SuggestTests):
76
+ __test__ = True
77
+ app: Command = APP_V2