Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_register_cli_argument_with_overrides(self):
cl = CLICommandsLoader(self.mock_ctx)
base_type = CLIArgumentType(options_list=['--foo', '-f'], metavar='FOO', help='help1')
derived_type = CLIArgumentType(base_type=base_type, help='help2')
with CommandGroup(cl, 'test', '{}#{{}}'.format(__name__)) as g:
g.command('sample-get', '{}.{}'.format(TestCommandRegistration.__name__,
TestCommandRegistration.sample_command_handler.__name__))
g.command('command sample-get-1', '{}.{}'.format(TestCommandRegistration.__name__,
TestCommandRegistration.sample_command_handler.__name__))
g.command('command sample-get-2', '{}.{}'.format(TestCommandRegistration.__name__,
TestCommandRegistration.sample_command_handler.__name__))
self.assertEqual(len(cl.command_table), 3, 'We expect exactly three commands in the command table')
def test_with_command(command, target_value):
self._set_command_name(command)
with ArgumentsContext(cl, 'test') as c:
c.argument('resource_name', base_type)
with ArgumentsContext(cl, 'test command') as c:
def test_cli_exapp1(self):
def a_test_command_handler(_):
return [{'a': 1, 'b': 1234}, {'a': 3, 'b': 4}]
class MyCommandsLoader(CLICommandsLoader):
def load_command_table(self, args):
self.command_table['abc xyz'] = CLICommand(self.cli_ctx, 'abc xyz', a_test_command_handler)
self.command_table['abc list'] = CLICommand(self.cli_ctx, 'abc list', a_test_command_handler)
return OrderedDict(self.command_table)
mycli = CLI(cli_name='exapp1', config_dir=os.path.expanduser(os.path.join('~', '.exapp1')), commands_loader_cls=MyCommandsLoader)
expected_output = """[
{
"a": 1,
"b": 1234
},
{
"a": 3,
"b": 4
}
def test_cli_ctx_type_error(self):
with self.assertRaises(TypeError):
CLICommandsLoader(cli_ctx=object())
def test_register_cli_argument(self):
cl = CLICommandsLoader(self.mock_ctx)
command_name = self._set_command_name('test register sample-command')
with CommandGroup(cl, 'test register', '{}#{{}}'.format(__name__)) as g:
g.command('sample-command', '{}.{}'.format(TestCommandRegistration.__name__,
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
))
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]
self.assertEqual(len(command_metadata.arguments), 4, 'We expected exactly 4 arguments')
some_expected_arguments = {
'group_name': CLIArgumentType(dest='group_name', required=True),
'resource_name': CLIArgumentType(dest='resource_name', required=False),
def test_register_command_custom_excluded_params(self):
command_name = self._set_command_name('test sample-command')
ep = ['self', 'raw', 'custom_headers', 'operation_config', 'content_version', 'kwargs', 'client']
cl = CLICommandsLoader(self.mock_ctx, excluded_command_handler_args=ep)
with CommandGroup(cl, 'test', '{}#{{}}'.format(__name__)) as g:
g.command('sample-command', '{}.{}'.format(TestCommandRegistration.__name__,
TestCommandRegistration.sample_command_handler2.__name__))
self.assertEqual(len(cl.command_table), 1, 'We expect exactly one command in the command table')
cl.load_arguments(command_name)
command_metadata = cl.command_table[command_name]
self.assertEqual(len(command_metadata.arguments), 4, 'We expected exactly 4 arguments')
self.assertIn(command_name, cl.command_table)
def setUp(self):
from knack.help_files import helps
class DeprecationTestCommandLoader(CLICommandsLoader):
def load_command_table(self, args):
super(DeprecationTestCommandLoader, self).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:
def test_register_command_confirmation_bool(self):
cl = CLICommandsLoader(self.mock_ctx)
command_name = self._set_command_name('test sample-command')
with CommandGroup(cl, 'test', '{}#{{}}'.format(__name__)) as g:
g.command('sample-command', '{}.{}'.format(TestCommandRegistration.__name__,
TestCommandRegistration.sample_command_handler.__name__),
confirmation=True)
self.assertEqual(len(cl.command_table), 1, 'We expect exactly one command in the command table')
cl.load_arguments(command_name)
command_metadata = cl.command_table[command_name]
self.assertIn('yes', command_metadata.arguments)
self.assertEqual(command_metadata.arguments['yes'].type.settings['action'], 'store_true')
self.assertIs(command_metadata.confirmation, True)
def _command_handler(command_args):
op = CLICommandsLoader._get_op_handler(operation)
client = client_factory(command_args) if client_factory else None
result = op(client, **command_args) if client else op(**command_args)
return result
from vsts.cli.admin.arguments import load_admin_arguments
from vsts.cli.build.commands import load_build_commands
from vsts.cli.build.arguments import load_build_arguments
from vsts.cli.release.commands import load_release_commands
from vsts.cli.release.arguments import load_release_arguments
from vsts.cli.code.commands import load_code_commands
from vsts.cli.code.arguments import load_code_arguments
from vsts.cli.team.commands import load_team_commands
from vsts.cli.team.arguments import load_team_arguments
from vsts.cli.work.commands import load_work_commands
from vsts.cli.work.arguments import load_work_arguments
from vsts.cli.package.commands import load_package_commands
from vsts.cli.package.arguments import load_package_arguments
class VstsCommandsLoader(CLICommandsLoader):
def load_command_table(self, args):
load_admin_commands(self)
load_build_commands(self)
load_release_commands(self)
load_code_commands(self)
load_team_commands(self)
load_work_commands(self)
load_package_commands(self)
return super(VstsCommandsLoader, self).load_command_table(args)
def load_arguments(self, command):
load_admin_arguments(self)
load_build_arguments(self)
load_release_arguments(self)
load_code_arguments(self)
load_team_arguments(self)
should_suppress = ext.name == self.suppress_extension_name and ext.version and \
parse_version(ext.version) <= parse_version(self.suppress_up_to_version)
if should_suppress:
reason = self.reason or "Use --debug for more information."
logger.warning("Extension %s (%s) has been suppressed. %s",
ext.name, ext.version, reason)
logger.debug("Extension %s (%s) suppressed from being loaded due "
"to %s", ext.name, ext.version, self.mod_name)
if self.recommend_remove:
logger.warning("Remove this extension with 'az extension remove --name %s'", ext.name)
if self.recommend_update:
logger.warning("Update this extension with 'az extension update --name %s'", ext.name)
return should_suppress
class AzCommandsLoader(CLICommandsLoader): # pylint: disable=too-many-instance-attributes
def __init__(self, cli_ctx=None, command_group_cls=None, argument_context_cls=None,
suppress_extension=None, **kwargs):
from azure.cli.core.commands import AzCliCommand, AzCommandGroup, AzArgumentContext
super(AzCommandsLoader, self).__init__(cli_ctx=cli_ctx,
command_cls=AzCliCommand,
excluded_command_handler_args=EXCLUDED_PARAMS)
self.suppress_extension = suppress_extension
self.module_kwargs = kwargs
self.command_name = None
self.skip_applicability = False
self._command_group_cls = command_group_cls or AzCommandGroup
self._argument_context_cls = argument_context_cls or AzArgumentContext
def _update_command_definitions(self):