Skip to content

Commit e3ba37f

Browse files
hugovktjprescott
authored andcommitted
Update tests, upgrade Python syntax, fix some flake8 (#102)
* Upgrade unit tests to use more useful asserts * Fix some flake8 warnings * Upgrade Python syntax with pyupgrade * Fix some typos * Revert "Upgrade Python syntax with pyupgrade" This reverts commit 84fecd4. * Upgrade positional formatters * Fix flake8
1 parent 927bbf1 commit e3ba37f

9 files changed

+55
-54
lines changed

CONTRIBUTING.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ This project has adopted the `Microsoft Open Source Code of Conduct <https://ope
66
For more information see the `Code of Conduct FAQ <https://opensource.microsoft.com/codeofconduct/faq/>`__ or contact `opencode@microsoft.com <mailto:opencode@microsoft.com>`__ with any additional questions or comments.
77

88
If you would like to become an active contributor to this project please
9-
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__
9+
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__.

README.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ You can install knack as a non-privilaged user to your home directory by adding
3939
4040
------------
4141

42-
.. note:: The project is in `initial development phase <https://semver.org/#how-should-i-deal-with-revisions-in-the-0yz-initial-development-phase>`__ . We recommend pinning to at least a specific minor version when marking **knack** as a dependency in your project.
42+
.. note:: The project is in `initial development phase <https://semver.org/#how-should-i-deal-with-revisions-in-the-0yz-initial-development-phase>`__. We recommend pinning to at least a specific minor version when marking **knack** as a dependency in your project.
4343

4444
------------
4545

@@ -141,7 +141,7 @@ This project has adopted the `Microsoft Open Source Code of Conduct <https://ope
141141
For more information see the `Code of Conduct FAQ <https://opensource.microsoft.com/codeofconduct/faq/>`__ or contact `opencode@microsoft.com <mailto:opencode@microsoft.com>`__ with any additional questions or comments.
142142

143143
If you would like to become an active contributor to this project please
144-
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__
144+
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__.
145145

146146

147147
License

knack/help.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def __init__(self, name_source, description, required, choices=None,
307307

308308
def update_from_data(self, data):
309309
if self.name != data.get('name'):
310-
raise HelpAuthoringException(u"mismatched name {0} vs. {1}"
310+
raise HelpAuthoringException(u"mismatched name {} vs. {}"
311311
.format(self.name,
312312
data.get('name')))
313313

@@ -426,25 +426,25 @@ def _print_items(layouts):
426426

427427
@staticmethod
428428
def _get_choices_defaults_sources_str(p):
429-
choice_str = u' Allowed values: {0}.'.format(', '.join(sorted([str(x) for x in p.choices]))) \
429+
choice_str = u' Allowed values: {}.'.format(', '.join(sorted([str(x) for x in p.choices]))) \
430430
if p.choices else ''
431-
default_str = u' Default: {0}.'.format(p.default) \
431+
default_str = u' Default: {}.'.format(p.default) \
432432
if p.default and p.default != argparse.SUPPRESS else ''
433-
value_sources_str = u' Values from: {0}.'.format(', '.join(p.value_sources)) \
433+
value_sources_str = u' Values from: {}.'.format(', '.join(p.value_sources)) \
434434
if p.value_sources else ''
435-
return u'{0}{1}{2}'.format(choice_str, default_str, value_sources_str)
435+
return u'{}{}{}'.format(choice_str, default_str, value_sources_str)
436436

437437
@staticmethod
438438
def print_description_list(help_files):
439439
indent = 1
440440
max_length = max(len(f.name) for f in help_files) if help_files else 0
441441
for help_file in sorted(help_files, key=lambda h: h.name):
442442
column_indent = max_length - len(help_file.name)
443-
_print_indent(u'{0}{1}{2}'.format(help_file.name,
444-
' ' * column_indent,
445-
FIRST_LINE_PREFIX + help_file.short_summary
446-
if help_file.short_summary
447-
else ''),
443+
_print_indent(u'{}{}{}'.format(help_file.name,
444+
' ' * column_indent,
445+
FIRST_LINE_PREFIX + help_file.short_summary
446+
if help_file.short_summary
447+
else ''),
448448
indent,
449449
_get_hanging_indent(max_length, indent))
450450

tests/test_command_registration.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ def test_register_cli_argument(self):
6262
TestCommandRegistration.sample_command_handler.__name__))
6363
with ArgumentsContext(cl, command_name) as ac:
6464
ac.argument('resource_name', CLIArgumentType(
65-
options_list=('--wonky-name', '-n'), metavar='RNAME', help='Completely WONKY name...',
66-
required=False
67-
))
65+
options_list=('--wonky-name', '-n'), metavar='RNAME', help='Completely WONKY name...',
66+
required=False
67+
))
6868
cl.load_arguments(command_name)
6969
self.assertEqual(len(cl.command_table), 1, 'We expect exactly one command in the command table')
7070
command_metadata = cl.command_table[command_name]
@@ -192,9 +192,9 @@ def test_register_cli_argument_with_overrides(self):
192192
command1 = cl.command_table['test sample-get'].arguments['resource_name']
193193
command2 = cl.command_table['test command sample-get-1'].arguments['resource_name']
194194
command3 = cl.command_table['test command sample-get-2'].arguments['resource_name']
195-
self.assertTrue(command1.options['help'] == 'foo help')
196-
self.assertTrue(command2.options['help'] == 'first modification')
197-
self.assertTrue(command3.options['help'] == 'second modification')
195+
self.assertEqual(command1.options['help'], 'foo help')
196+
self.assertEqual(command2.options['help'], 'first modification')
197+
self.assertEqual(command3.options['help'], 'second modification')
198198

199199
def test_register_extra_cli_argument(self):
200200
cl = CLICommandsLoader(self.mock_ctx)
@@ -308,10 +308,10 @@ def test_validator_completer():
308308
g.command('foo', sample_sdk_method.__name__)
309309
with ArgumentsContext(cl, 'override_using_register_cli_argument') as ac:
310310
ac.argument('param_a',
311-
options_list=('--overridden', '-r'),
312-
validator=test_validator_completer,
313-
completer=test_validator_completer,
314-
required=False)
311+
options_list=('--overridden', '-r'),
312+
validator=test_validator_completer,
313+
completer=test_validator_completer,
314+
required=False)
315315
cl.load_arguments(command_name)
316316

317317
command_metadata = cl.command_table[command_name]
@@ -331,15 +331,15 @@ def test_override_argtype_with_argtype(self):
331331
completer=None, overrides=arg, help='overridden',
332332
required=CLIArgumentType.REMOVE)
333333
self.assertEqual(overriding_argtype.settings['validator'], 'overridden')
334-
self.assertEqual(overriding_argtype.settings['completer'], None)
334+
self.assertIsNone(overriding_argtype.settings['completer'])
335335
self.assertEqual(overriding_argtype.settings['options_list'], ('--overridden',))
336336
self.assertEqual(overriding_argtype.settings['help'], 'overridden')
337337
self.assertEqual(overriding_argtype.settings['required'], CLIArgumentType.REMOVE)
338338

339339
cmd_arg = CLICommandArgument(dest='whatever', argtype=overriding_argtype,
340340
help=CLIArgumentType.REMOVE)
341-
self.assertFalse('required' in cmd_arg.options)
342-
self.assertFalse('help' in cmd_arg.options)
341+
self.assertNotIn('required', cmd_arg.options)
342+
self.assertNotIn('help', cmd_arg.options)
343343

344344
def test_cli_ctx_type_error(self):
345345
with self.assertRaises(TypeError):

tests/test_config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_getboolean(self):
110110
value = 'true'
111111
self.cli_config.config_parser.add_section(section)
112112
self.cli_config.config_parser.set(section, option, value)
113-
self.assertEqual(self.cli_config.getboolean(section, option), True)
113+
self.assertTrue(self.cli_config.getboolean(section, option))
114114

115115
def test_getboolean_error(self):
116116
section = 'MySection'

tests/test_deprecation.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,31 @@ def test_deprecate_command_help_hidden(self):
113113
--arg -a : Allowed values: 1, 2, 3.
114114
--arg3
115115
""".format(self.cli_ctx.name)
116-
self.assertTrue(expected in actual)
116+
self.assertIn(expected, actual)
117117

118118
@redirect_io
119119
def test_deprecate_command_plain_execute(self):
120120
""" Ensure general warning displayed when running deprecated command. """
121121
self.cli_ctx.invoke('cmd1 -b b'.split())
122122
actual = self.io.getvalue()
123123
expected = "This command has been deprecated and will be removed in a future release. Use 'alt-cmd1' instead."
124-
self.assertTrue(expected in actual)
124+
self.assertIn(expected, actual)
125125

126126
@redirect_io
127127
def test_deprecate_command_hidden_execute(self):
128128
""" Ensure general warning displayed when running hidden deprecated command. """
129129
self.cli_ctx.invoke('cmd3 -b b'.split())
130130
actual = self.io.getvalue()
131131
expected = "This command has been deprecated and will be removed in a future release. Use 'alt-cmd3' instead."
132-
self.assertTrue(expected in actual)
132+
self.assertIn(expected, actual)
133133

134134
@redirect_io
135135
def test_deprecate_command_expiring_execute(self):
136136
""" Ensure specific warning displayed when running expiring deprecated command. """
137137
self.cli_ctx.invoke('cmd4 -b b'.split())
138138
actual = self.io.getvalue()
139139
expected = "This command has been deprecated and will be removed in version '1.0.0'. Use 'alt-cmd4' instead."
140-
self.assertTrue(expected in actual)
140+
self.assertIn(expected, actual)
141141

142142
@redirect_io
143143
def test_deprecate_command_expired_execute(self):
@@ -222,7 +222,7 @@ def test_deprecate_command_group_help_hidden(self):
222222
cmd1 : Short summary here.
223223
224224
""".format(self.cli_ctx.name)
225-
self.assertTrue(expected in actual)
225+
self.assertIn(expected, actual)
226226

227227
@redirect_io
228228
def test_deprecate_command_group_help_expiring(self):
@@ -236,7 +236,7 @@ def test_deprecate_command_group_help_expiring(self):
236236
This command group has been deprecated and will be removed in version '1.0.0'. Use
237237
'alt-group4' instead.
238238
""".format(self.cli_ctx.name)
239-
self.assertTrue(expected in actual)
239+
self.assertIn(expected, actual)
240240

241241
@redirect_io
242242
def test_deprecate_command_group_expired(self):
@@ -259,7 +259,7 @@ def test_deprecate_command_implicitly(self):
259259
command group 'group1' is deprecated and will be removed in a future release. Use 'alt-
260260
group1' instead.
261261
""".format(self.cli_ctx.name)
262-
self.assertTrue(expected in actual)
262+
self.assertIn(expected, actual)
263263

264264

265265
class TestArgumentDeprecation(unittest.TestCase):
@@ -337,23 +337,23 @@ def test_deprecate_arguments_execute(self):
337337
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
338338
actual = self.io.getvalue()
339339
expected = "Argument 'arg1' has been deprecated and will be removed in a future release."
340-
self.assertTrue(expected in actual)
340+
self.assertIn(expected, actual)
341341

342342
@redirect_io
343343
def test_deprecate_arguments_execute_hidden(self):
344344
""" Ensure hidden deprecated arguments can be used. """
345345
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg3 bar'.split())
346346
actual = self.io.getvalue()
347347
expected = "Argument 'arg3' has been deprecated and will be removed in a future release."
348-
self.assertTrue(expected in actual)
348+
self.assertIn(expected, actual)
349349

350350
@redirect_io
351351
def test_deprecate_arguments_execute_expiring(self):
352352
""" Ensure hidden deprecated arguments can be used. """
353353
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg4 bar'.split())
354354
actual = self.io.getvalue()
355355
expected = "Argument 'arg4' has been deprecated and will be removed in version '1.0.0'."
356-
self.assertTrue(expected in actual)
356+
self.assertIn(expected, actual)
357357

358358
@redirect_io
359359
def test_deprecate_arguments_execute_expired(self):
@@ -362,39 +362,39 @@ def test_deprecate_arguments_execute_expired(self):
362362
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg5 foo'.split())
363363
actual = self.io.getvalue()
364364
expected = 'unrecognized arguments: --arg5 foo'
365-
self.assertTrue(expected in actual)
365+
self.assertIn(expected, actual)
366366

367367
@redirect_io
368368
def test_deprecate_options_execute(self):
369369
""" Ensure deprecated options can be used with a warning. """
370370
self.cli_ctx.invoke('arg-test --arg1 foo --alt1 bar'.split())
371371
actual = self.io.getvalue()
372372
expected = "Option '--alt1' has been deprecated and will be removed in a future release. Use '--opt1' instead."
373-
self.assertTrue(expected in actual)
373+
self.assertIn(expected, actual)
374374

375375
@redirect_io
376376
def test_deprecate_options_execute_non_deprecated(self):
377377
""" Ensure non-deprecated options don't show warning. """
378378
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar'.split())
379379
actual = self.io.getvalue()
380380
expected = "Option '--alt1' has been deprecated and will be removed in a future release. Use '--opt1' instead."
381-
self.assertTrue(expected not in actual)
381+
self.assertNotIn(expected, actual)
382382

383383
@redirect_io
384384
def test_deprecate_options_execute_hidden(self):
385385
""" Ensure hidden deprecated options can be used with warning. """
386386
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt3 bar'.split())
387387
actual = self.io.getvalue()
388388
expected = "Option '--alt3' has been deprecated and will be removed in a future release. Use '--opt3' instead."
389-
self.assertTrue(expected in actual)
389+
self.assertIn(expected, actual)
390390

391391
@redirect_io
392392
def test_deprecate_options_execute_hidden_non_deprecated(self):
393393
""" Ensure hidden non-deprecated optionss can be used without warning. """
394394
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --opt3 bar'.split())
395395
actual = self.io.getvalue()
396396
expected = "Option '--alt3' has been deprecated and will be removed in a future release. Use '--opt3' instead."
397-
self.assertTrue(expected not in actual)
397+
self.assertNotIn(expected, actual)
398398

399399
@redirect_io
400400
def test_deprecate_options_execute_expired(self):
@@ -403,7 +403,7 @@ def test_deprecate_options_execute_expired(self):
403403
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt5 foo'.split())
404404
actual = self.io.getvalue()
405405
expected = 'unrecognized arguments: --alt5 foo'
406-
self.assertTrue(expected in actual)
406+
self.assertIn(expected, actual)
407407

408408
@redirect_io
409409
def test_deprecate_options_execute_expired_non_deprecated(self):
@@ -418,15 +418,15 @@ def test_deprecate_options_execute_expiring(self):
418418
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt4 bar'.split())
419419
actual = self.io.getvalue()
420420
expected = "Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--opt4' instead."
421-
self.assertTrue(expected in actual)
421+
self.assertIn(expected, actual)
422422

423423
@redirect_io
424424
def test_deprecate_options_execute_expiring_non_deprecated(self):
425425
""" Ensure non-expiring options can be used without warning. """
426426
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --opt4 bar'.split())
427427
actual = self.io.getvalue()
428428
expected = "Option '--alt4' has been deprecated and will be removed in version '1.0.0'. Use '--opt4' instead."
429-
self.assertTrue(expected not in actual)
429+
self.assertNotIn(expected, actual)
430430

431431

432432
if __name__ == '__main__':

tests/test_help.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_choice_list_with_ints(self):
208208
self.cli_ctx.invoke('n1 -h'.split())
209209
actual = io.getvalue()
210210
expected = 'Allowed values: 1, 2, 3'
211-
self.assertTrue(expected in actual)
211+
self.assertIn(expected, actual)
212212

213213
@redirect_io
214214
def test_help_param(self):
@@ -393,7 +393,7 @@ def test_help_extra_params(self):
393393

394394
actual = io.getvalue()
395395
expected = 'unrecognized arguments: -c extra'
396-
self.assertTrue(expected in actual)
396+
self.assertIn(expected, actual)
397397

398398
@redirect_io
399399
def test_help_group_help(self):

tests/test_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def __init__(self, test, substr=None):
143143

144144
def __call__(self, message):
145145
if self.substr:
146-
self.test.assertTrue(message.find(self.substr) >= 0)
146+
self.test.assertGreaterEqual(message.find(self.substr), 0)
147147
self.called = True
148148

149149

tests/test_prompting.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_prompt_msg_question_with_help_string(self, _):
5252
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
5353
actual_result = prompt('Please enter some text: ', help_string='Anything you want!')
5454
self.assertEqual(expected_result, actual_result)
55-
self.assertTrue('Anything you want!' in mock_stdout.getvalue())
55+
self.assertIn('Anything you want!', mock_stdout.getvalue())
5656

5757
@mock.patch('sys.stdin.isatty', return_value=True)
5858
def test_prompt_int(self, _):
@@ -95,7 +95,7 @@ def test_prompt_int_question_with_help_string(self, _):
9595
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
9696
actual_result = prompt_int('Please enter a number: ', help_string='Anything you want!')
9797
self.assertEqual(int(my_response), actual_result)
98-
self.assertTrue('Anything you want!' in mock_stdout.getvalue())
98+
self.assertIn('Anything you want!', mock_stdout.getvalue())
9999

100100
@mock.patch('sys.stdin.isatty', return_value=True)
101101
def test_prompt_pass(self, _):
@@ -132,7 +132,7 @@ def test_prompt_pass_question_with_help_string(self, _):
132132
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
133133
actual_result = prompt_pass(help_string='Anything you want!')
134134
self.assertEqual(my_password, actual_result)
135-
self.assertTrue('Anything you want!' in mock_stdout.getvalue())
135+
self.assertIn('Anything you want!', mock_stdout.getvalue())
136136

137137
@mock.patch('sys.stdin.isatty', return_value=True)
138138
def test_prompt_pass_confirm_valid(self, _):
@@ -207,7 +207,7 @@ def test_prompt_y_n_question_with_help_string(self, _):
207207
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
208208
actual_result = prompt_y_n('Do you accept?', help_string='y to accept conditions; no otherwise')
209209
self.assertTrue(actual_result)
210-
self.assertTrue('y to accept conditions; no otherwise' in mock_stdout.getvalue())
210+
self.assertIn('y to accept conditions; no otherwise', mock_stdout.getvalue())
211211

212212
@mock.patch('sys.stdin.isatty', return_value=True)
213213
def test_prompt_y_n_default(self, _):
@@ -262,7 +262,7 @@ def test_prompt_t_f_question_with_help_string(self, _):
262262
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
263263
actual_result = prompt_t_f('Do you accept?', help_string='t to accept conditions; no otherwise')
264264
self.assertTrue(actual_result)
265-
self.assertTrue('t to accept conditions; no otherwise' in mock_stdout.getvalue())
265+
self.assertIn('t to accept conditions; no otherwise', mock_stdout.getvalue())
266266

267267
@mock.patch('sys.stdin.isatty', return_value=True)
268268
def test_prompt_t_f_default(self, _):
@@ -314,7 +314,8 @@ def test_prompt_choice_list_question_with_help_string(self, _):
314314
a_list,
315315
help_string='Your real favourite.')
316316
self.assertEqual(0, actual_result)
317-
self.assertTrue('Your real favourite.' in mock_stdout.getvalue())
317+
self.assertIn('Your real favourite.', mock_stdout.getvalue())
318+
318319

319320

320321
if __name__ == '__main__':

0 commit comments

Comments
 (0)