Skip to content

Commit

Permalink
Update tests, upgrade Python syntax, fix some flake8 (#102)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hugovk authored and tjprescott committed Oct 12, 2018
1 parent 927bbf1 commit e3ba37f
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 54 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ This project has adopted the `Microsoft Open Source Code of Conduct <https://ope
For more information see the `Code of Conduct FAQ <https://opensource.microsoft.com/codeofconduct/faq/>`__ or contact `[email protected] <mailto:[email protected]>`__ with any additional questions or comments.

If you would like to become an active contributor to this project please
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__
follow the instructions provided in `Contribution License Agreement <https://cla.microsoft.com/>`__.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can install knack as a non-privilaged user to your home directory by adding
------------

.. 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.
.. 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.

------------

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

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


License
Expand Down
20 changes: 10 additions & 10 deletions knack/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def __init__(self, name_source, description, required, choices=None,

def update_from_data(self, data):
if self.name != data.get('name'):
raise HelpAuthoringException(u"mismatched name {0} vs. {1}"
raise HelpAuthoringException(u"mismatched name {} vs. {}"
.format(self.name,
data.get('name')))

Expand Down Expand Up @@ -426,25 +426,25 @@ def _print_items(layouts):

@staticmethod
def _get_choices_defaults_sources_str(p):
choice_str = u' Allowed values: {0}.'.format(', '.join(sorted([str(x) for x in p.choices]))) \
choice_str = u' Allowed values: {}.'.format(', '.join(sorted([str(x) for x in p.choices]))) \
if p.choices else ''
default_str = u' Default: {0}.'.format(p.default) \
default_str = u' Default: {}.'.format(p.default) \
if p.default and p.default != argparse.SUPPRESS else ''
value_sources_str = u' Values from: {0}.'.format(', '.join(p.value_sources)) \
value_sources_str = u' Values from: {}.'.format(', '.join(p.value_sources)) \
if p.value_sources else ''
return u'{0}{1}{2}'.format(choice_str, default_str, value_sources_str)
return u'{}{}{}'.format(choice_str, default_str, value_sources_str)

@staticmethod
def print_description_list(help_files):
indent = 1
max_length = max(len(f.name) for f in help_files) if help_files else 0
for help_file in sorted(help_files, key=lambda h: h.name):
column_indent = max_length - len(help_file.name)
_print_indent(u'{0}{1}{2}'.format(help_file.name,
' ' * column_indent,
FIRST_LINE_PREFIX + help_file.short_summary
if help_file.short_summary
else ''),
_print_indent(u'{}{}{}'.format(help_file.name,
' ' * column_indent,
FIRST_LINE_PREFIX + help_file.short_summary
if help_file.short_summary
else ''),
indent,
_get_hanging_indent(max_length, indent))

Expand Down
26 changes: 13 additions & 13 deletions tests/test_command_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ def test_register_cli_argument(self):
TestCommandRegistration.sample_command_handler.__name__))
with ArgumentsContext(cl, command_name) as ac:
ac.argument('resource_name', CLIArgumentType(
options_list=('--wonky-name', '-n'), metavar='RNAME', help='Completely WONKY name...',
required=False
))
options_list=('--wonky-name', '-n'), metavar='RNAME', help='Completely WONKY name...',
required=False
))
cl.load_arguments(command_name)
self.assertEqual(len(cl.command_table), 1, 'We expect exactly one command in the command table')
command_metadata = cl.command_table[command_name]
Expand Down Expand Up @@ -192,9 +192,9 @@ def test_register_cli_argument_with_overrides(self):
command1 = cl.command_table['test sample-get'].arguments['resource_name']
command2 = cl.command_table['test command sample-get-1'].arguments['resource_name']
command3 = cl.command_table['test command sample-get-2'].arguments['resource_name']
self.assertTrue(command1.options['help'] == 'foo help')
self.assertTrue(command2.options['help'] == 'first modification')
self.assertTrue(command3.options['help'] == 'second modification')
self.assertEqual(command1.options['help'], 'foo help')
self.assertEqual(command2.options['help'], 'first modification')
self.assertEqual(command3.options['help'], 'second modification')

def test_register_extra_cli_argument(self):
cl = CLICommandsLoader(self.mock_ctx)
Expand Down Expand Up @@ -308,10 +308,10 @@ def test_validator_completer():
g.command('foo', sample_sdk_method.__name__)
with ArgumentsContext(cl, 'override_using_register_cli_argument') as ac:
ac.argument('param_a',
options_list=('--overridden', '-r'),
validator=test_validator_completer,
completer=test_validator_completer,
required=False)
options_list=('--overridden', '-r'),
validator=test_validator_completer,
completer=test_validator_completer,
required=False)
cl.load_arguments(command_name)

command_metadata = cl.command_table[command_name]
Expand All @@ -331,15 +331,15 @@ def test_override_argtype_with_argtype(self):
completer=None, overrides=arg, help='overridden',
required=CLIArgumentType.REMOVE)
self.assertEqual(overriding_argtype.settings['validator'], 'overridden')
self.assertEqual(overriding_argtype.settings['completer'], None)
self.assertIsNone(overriding_argtype.settings['completer'])
self.assertEqual(overriding_argtype.settings['options_list'], ('--overridden',))
self.assertEqual(overriding_argtype.settings['help'], 'overridden')
self.assertEqual(overriding_argtype.settings['required'], CLIArgumentType.REMOVE)

cmd_arg = CLICommandArgument(dest='whatever', argtype=overriding_argtype,
help=CLIArgumentType.REMOVE)
self.assertFalse('required' in cmd_arg.options)
self.assertFalse('help' in cmd_arg.options)
self.assertNotIn('required', cmd_arg.options)
self.assertNotIn('help', cmd_arg.options)

def test_cli_ctx_type_error(self):
with self.assertRaises(TypeError):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_getboolean(self):
value = 'true'
self.cli_config.config_parser.add_section(section)
self.cli_config.config_parser.set(section, option, value)
self.assertEqual(self.cli_config.getboolean(section, option), True)
self.assertTrue(self.cli_config.getboolean(section, option))

def test_getboolean_error(self):
section = 'MySection'
Expand Down
36 changes: 18 additions & 18 deletions tests/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,31 @@ def test_deprecate_command_help_hidden(self):
--arg -a : Allowed values: 1, 2, 3.
--arg3
""".format(self.cli_ctx.name)
self.assertTrue(expected in actual)
self.assertIn(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.assertTrue(expected in actual)
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.assertTrue(expected in actual)
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.assertTrue(expected in actual)
self.assertIn(expected, actual)

@redirect_io
def test_deprecate_command_expired_execute(self):
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_deprecate_command_group_help_hidden(self):
cmd1 : Short summary here.
""".format(self.cli_ctx.name)
self.assertTrue(expected in actual)
self.assertIn(expected, actual)

@redirect_io
def test_deprecate_command_group_help_expiring(self):
Expand All @@ -236,7 +236,7 @@ def test_deprecate_command_group_help_expiring(self):
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.assertTrue(expected in actual)
self.assertIn(expected, actual)

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


class TestArgumentDeprecation(unittest.TestCase):
Expand Down Expand Up @@ -337,23 +337,23 @@ def test_deprecate_arguments_execute(self):
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.assertTrue(expected in actual)
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.assertTrue(expected in actual)
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.assertTrue(expected in actual)
self.assertIn(expected, actual)

@redirect_io
def test_deprecate_arguments_execute_expired(self):
Expand All @@ -362,39 +362,39 @@ def test_deprecate_arguments_execute_expired(self):
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --arg5 foo'.split())
actual = self.io.getvalue()
expected = 'unrecognized arguments: --arg5 foo'
self.assertTrue(expected in actual)
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.assertTrue(expected in actual)
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.assertTrue(expected not in actual)
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.assertTrue(expected in actual)
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.assertTrue(expected not in actual)
self.assertNotIn(expected, actual)

@redirect_io
def test_deprecate_options_execute_expired(self):
Expand All @@ -403,7 +403,7 @@ def test_deprecate_options_execute_expired(self):
self.cli_ctx.invoke('arg-test --arg1 foo --opt1 bar --alt5 foo'.split())
actual = self.io.getvalue()
expected = 'unrecognized arguments: --alt5 foo'
self.assertTrue(expected in actual)
self.assertIn(expected, actual)

@redirect_io
def test_deprecate_options_execute_expired_non_deprecated(self):
Expand All @@ -418,15 +418,15 @@ def test_deprecate_options_execute_expiring(self):
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.assertTrue(expected in actual)
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.assertTrue(expected not in actual)
self.assertNotIn(expected, actual)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_choice_list_with_ints(self):
self.cli_ctx.invoke('n1 -h'.split())
actual = io.getvalue()
expected = 'Allowed values: 1, 2, 3'
self.assertTrue(expected in actual)
self.assertIn(expected, actual)

@redirect_io
def test_help_param(self):
Expand Down Expand Up @@ -393,7 +393,7 @@ def test_help_extra_params(self):

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

@redirect_io
def test_help_group_help(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__(self, test, substr=None):

def __call__(self, message):
if self.substr:
self.test.assertTrue(message.find(self.substr) >= 0)
self.test.assertGreaterEqual(message.find(self.substr), 0)
self.called = True


Expand Down
13 changes: 7 additions & 6 deletions tests/test_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_prompt_msg_question_with_help_string(self, _):
with mock.patch('sys.stdout', new_callable=StringIO) as mock_stdout:
actual_result = prompt('Please enter some text: ', help_string='Anything you want!')
self.assertEqual(expected_result, actual_result)
self.assertTrue('Anything you want!' in mock_stdout.getvalue())
self.assertIn('Anything you want!', mock_stdout.getvalue())

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

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

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

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

@mock.patch('sys.stdin.isatty', return_value=True)
def test_prompt_t_f_default(self, _):
Expand Down Expand Up @@ -314,7 +314,8 @@ def test_prompt_choice_list_question_with_help_string(self, _):
a_list,
help_string='Your real favourite.')
self.assertEqual(0, actual_result)
self.assertTrue('Your real favourite.' in mock_stdout.getvalue())
self.assertIn('Your real favourite.', mock_stdout.getvalue())



if __name__ == '__main__':
Expand Down

0 comments on commit e3ba37f

Please sign in to comment.