forked from microsoft/knack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_deprecation.py
461 lines (383 loc) · 19.7 KB
/
test_deprecation.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import unittest
from unittest import mock
from knack.arguments import ArgumentsContext
from knack.commands import CLICommandsLoader, CommandGroup
from tests.util import DummyCLI, redirect_io, assert_in_multi_line, disable_color
def example_handler(arg1, arg2=None, arg3=None):
""" Short summary here. Long summary here. Still long summary. """
pass
def example_arg_handler(arg1, opt1, arg2=None, opt2=None, arg3=None,
opt3=None, arg4=None, opt4=None, arg5=None, opt5=None):
pass
# pylint: disable=line-too-long
class TestCommandDeprecation(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class DeprecationTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().load_command_table(args)
with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g:
g.command('cmd1', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd1'))
g.command('cmd2', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd2', hide='1.0.0'))
g.command('cmd3', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd3', hide='0.1.0'))
g.command('cmd4', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd4', expiration='1.0.0'))
g.command('cmd5', 'example_handler', deprecate_info=g.deprecate(redirect='alt-cmd5', expiration='0.1.0'))
with CommandGroup(self, 'grp1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-grp1')) as g:
g.command('cmd1', 'example_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, '') as c:
c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3])
c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c'])
super().load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader)
@redirect_io
def test_deprecate_command_group_help(self):
""" Ensure deprecated commands appear (or don't appear) correctly in group help view. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('-h'.split())
actual = self.io.getvalue()
expected = """
Group
{}
Subgroups:
grp1 [Deprecated] : A group.
Commands:
cmd1 [Deprecated] : Short summary here.
cmd2 [Deprecated] : Short summary here.
cmd4 [Deprecated] : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_deprecate_command_help_hidden(self):
""" Ensure hidden deprecated command can be used. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('cmd3 -h'.split())
actual = self.io.getvalue()
expected = """
Command
{} cmd3 : Short summary here.
Long summary here. Still long summary.
This command has been deprecated and will be removed in a future release. Use 'alt-
cmd3' instead.
Arguments
-b [Required] : Allowed values: a, b, c.
--arg -a : Allowed values: 1, 2, 3.
--arg3
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_deprecate_command_plain_execute(self):
""" Ensure general warning displayed when running deprecated command. """
self.cli_ctx.invoke('cmd1 -b b'.split())
actual = self.io.getvalue()
expected = "This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_hidden_execute(self):
""" Ensure general warning displayed when running hidden deprecated command. """
self.cli_ctx.invoke('cmd3 -b b'.split())
actual = self.io.getvalue()
expected = "This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_expiring_execute(self):
""" Ensure specific warning displayed when running expiring deprecated command. """
self.cli_ctx.invoke('cmd4 -b b'.split())
actual = self.io.getvalue()
expected = "This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_expiring_execute_no_color(self):
""" Ensure warning is displayed without color. """
self.cli_ctx.enable_color = False
self.cli_ctx.invoke('cmd4 -b b'.split())
actual = self.io.getvalue()
expected = "WARNING: This command has been deprecated and will be removed in version '1.0.0'"
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_expired_execute(self):
""" Ensure expired command cannot be reached. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('cmd5 -h'.split())
actual = self.io.getvalue()
expected = """cli: 'cmd5' is not in the 'cli' command group."""
self.assertIn(expected, actual)
@redirect_io
@disable_color
def test_deprecate_command_expired_execute_no_color(self):
""" Ensure error is displayed without color. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('cmd5 -h'.split())
actual = self.io.getvalue()
expected = """ERROR: cli: 'cmd5' is not in the 'cli' command group."""
self.assertIn(expected, actual)
class TestCommandGroupDeprecation(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class DeprecationTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().load_command_table(args)
with CommandGroup(self, 'group1', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group1')) as g:
g.command('cmd1', 'example_handler')
with CommandGroup(self, 'group2', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group2', hide='1.0.0')) as g:
g.command('cmd1', 'example_handler')
with CommandGroup(self, 'group3', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group3', hide='0.1.0')) as g:
g.command('cmd1', 'example_handler')
with CommandGroup(self, 'group4', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group4', expiration='1.0.0')) as g:
g.command('cmd1', 'example_handler')
with CommandGroup(self, 'group5', '{}#{{}}'.format(__name__), deprecate_info=self.deprecate(redirect='alt-group5', expiration='0.1.0')) as g:
g.command('cmd1', 'example_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, '') as c:
c.argument('arg1', options_list=['--arg', '-a'], required=False, type=int, choices=[1, 2, 3])
c.argument('arg2', options_list=['-b'], required=True, choices=['a', 'b', 'c'])
super().load_arguments(command)
helps['group1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader)
@redirect_io
def test_deprecate_command_group_help_plain(self):
""" Ensure help warnings appear for deprecated command group help. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group1 -h'.split())
actual = self.io.getvalue()
expected = """
Group
cli group1 : A group.
This command group has been deprecated and will be removed in a future release. Use
'alt-group1' instead.
Commands:
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_deprecate_command_group_help_hidden(self):
""" Ensure hidden deprecated command can be used. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group3 -h'.split())
actual = self.io.getvalue()
expected = """
Group
{} group3
This command group has been deprecated and will be removed in a future release. Use
'alt-group3' instead.
Commands:
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_deprecate_command_group_help_expiring(self):
""" Ensure specific warning displayed when running expiring deprecated command. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group4 -h'.split())
actual = self.io.getvalue()
expected = """
Group
{} group4
This command group has been deprecated and will be removed in version '1.0.0'. Use
'alt-group4' instead.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
@disable_color
def test_deprecate_command_group_help_expiring_no_color(self):
""" Ensure warning is displayed without color. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group4 -h'.split())
actual = self.io.getvalue()
expected = """
Group
cli group4
WARNING: This command group has been deprecated and will be removed in version \'1.0.0\'. Use
'alt-group4' instead.
""".format(self.cli_ctx.name)
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_group_expired(self):
""" Ensure expired command cannot be reached. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group5 -h'.split())
actual = self.io.getvalue()
expected = """The most similar choices to 'group5'"""
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_command_implicitly(self):
""" Ensure help warning displayed for command deprecated because of a deprecated parent group. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('group1 cmd1 -h'.split())
actual = self.io.getvalue()
expected = """
Command
{} group1 cmd1 : Short summary here.
Long summary here. Still long summary.
This command is implicitly deprecated because command group 'group1' is deprecated and
will be removed in a future release. Use 'alt-group1' instead.
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
class TestArgumentDeprecation(unittest.TestCase):
def setUp(self):
from knack.help_files import helps
class DeprecationTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super().load_command_table(args)
with CommandGroup(self, '', '{}#{{}}'.format(__name__)) as g:
g.command('arg-test', 'example_arg_handler')
return self.command_table
def load_arguments(self, command):
with ArgumentsContext(self, 'arg-test') as c:
c.argument('arg1', help='Arg1', deprecate_info=c.deprecate())
c.argument('opt1', help='Opt1', options_list=['--opt1', c.deprecate(redirect='--opt1', target='--alt1')])
c.argument('arg2', help='Arg2', deprecate_info=c.deprecate(hide='1.0.0'))
c.argument('opt2', help='Opt2', options_list=['--opt2', c.deprecate(redirect='--opt2', target='--alt2', hide='1.0.0')])
c.argument('arg3', help='Arg3', deprecate_info=c.deprecate(hide='0.1.0'))
c.argument('opt3', help='Opt3', options_list=['--opt3', c.deprecate(redirect='--opt3', target='--alt3', hide='0.1.0')])
c.argument('arg4', deprecate_info=c.deprecate(expiration='1.0.0'))
c.argument('opt4', options_list=['--opt4', c.deprecate(redirect='--opt4', target='--alt4', expiration='1.0.0')])
c.argument('arg5', deprecate_info=c.deprecate(expiration='0.1.0'))
c.argument('opt5', options_list=['--opt5', c.deprecate(redirect='--opt5', target='--alt5', expiration='0.1.0')])
super().load_arguments(command)
helps['grp1'] = """
type: group
short-summary: A group.
"""
self.cli_ctx = DummyCLI(commands_loader_cls=DeprecationTestCommandLoader)
@redirect_io
def test_deprecate_arguments_command_help(self):
""" Ensure deprecated arguments and options appear (or don't appear)
correctly in command help view. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('arg-test -h'.split())
actual = self.io.getvalue()
expected = """
Command
{} arg-test
Arguments
--alt1 [Deprecated] [Required] : Opt1.
Option '--alt1' has been deprecated and will be removed in a future release. Use '--
opt1' instead.
--arg1 [Deprecated] [Required] : Arg1.
Argument 'arg1' has been deprecated and will be removed in a future release.
--opt1 [Required] : Opt1.
--alt2 [Deprecated] : Opt2.
Option '--alt2' has been deprecated and will be removed in a future release. Use '--
opt2' instead.
--alt4 [Deprecated]
Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--
opt4' instead.
--arg2 [Deprecated] : Arg2.
Argument 'arg2' has been deprecated and will be removed in a future release.
--arg4 [Deprecated]
Argument 'arg4' has been deprecated and will be removed in version '1.0.0'.
--opt2 : Opt2.
--opt3 : Opt3.
--opt4
--opt5
""".format(self.cli_ctx.name)
assert_in_multi_line(expected, actual)
@redirect_io
def test_deprecate_arguments_execute(self):
""" Ensure deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
expected = "Argument 'arg1' has been deprecated and will be removed in a future release."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_arguments_execute_hidden(self):
""" Ensure hidden deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg3 bar'.split())
actual = self.io.getvalue()
expected = "Argument 'arg3' has been deprecated and will be removed in a future release."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_arguments_execute_expiring(self):
""" Ensure hidden deprecated arguments can be used. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg4 bar'.split())
actual = self.io.getvalue()
expected = "Argument 'arg4' has been deprecated and will be removed in version '1.0.0'."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_arguments_execute_expired(self):
""" Ensure expired deprecated arguments can't be used. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg5 foo'.split())
actual = self.io.getvalue()
expected = 'unrecognized arguments: --arg5 foo'
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_options_execute(self):
""" Ensure deprecated options can be used with a warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --alt1 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt1' has been deprecated and will be removed in a future release. Use '--opt1' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_non_deprecated(self):
""" Ensure non-deprecated options don't show warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt1' has been deprecated and will be removed in a future release. Use '--opt1' instead."
self.assertNotIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_hidden(self):
""" Ensure hidden deprecated options can be used with warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt3 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt3' has been deprecated and will be removed in a future release. Use '--opt3' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_hidden_non_deprecated(self):
""" Ensure hidden non-deprecated optionss can be used without warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --opt3 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt3' has been deprecated and will be removed in a future release. Use '--opt3' instead."
self.assertNotIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_expired(self):
""" Ensure expired deprecated options can't be used. """
with self.assertRaises(SystemExit):
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt5 foo'.split())
actual = self.io.getvalue()
expected = 'unrecognized arguments: --alt5 foo'
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_expired_non_deprecated(self):
""" Ensure non-expired options can be used without warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --opt5 foo'.split())
actual = self.io.getvalue()
self.assertTrue('--alt5' not in actual and '--opt5' not in actual)
@redirect_io
def test_deprecate_options_execute_expiring(self):
""" Ensure expiring options can be used with warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt4 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--opt4' instead."
self.assertIn(expected, actual)
@redirect_io
@disable_color
def test_deprecate_options_execute_expiring_no_color(self):
""" Ensure error is displayed without color. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt4 bar'.split())
actual = self.io.getvalue()
expected = "WARNING: Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--opt4' instead."
self.assertIn(expected, actual)
@redirect_io
def test_deprecate_options_execute_expiring_non_deprecated(self):
""" Ensure non-expiring options can be used without warning. """
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --opt4 bar'.split())
actual = self.io.getvalue()
expected = "Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--opt4' instead."
self.assertNotIn(expected, actual)
if __name__ == '__main__':
unittest.main()