Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lineage attribute across commands #1128

Merged
merged 3 commits into from
Feb 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ def name(self, value):
# Subclasses must implement setting/changing the cmd name.
raise NotImplementedError("name")

@property
def lineage(self):
# Represents how to get to a specific command using the CLI.
# It includes all commands that came before it and itself in
# a list.
return [self]

def __call__(self, args, parsed_globals):
"""Invoke CLI operation.
Expand Down Expand Up @@ -324,6 +331,7 @@ def __init__(self, cli_name, session, service_name=None):
self._service_name = cli_name
else:
self._service_name = service_name
self._lineage = [self]

@property
def name(self):
Expand All @@ -337,6 +345,14 @@ def name(self, value):
def service_object(self):
return self._service_object

@property
def lineage(self):
return self._lineage

@lineage.setter
def lineage(self, value):
self._lineage = value

def _get_command_table(self):
if self._command_table is None:
self._command_table = self._create_command_table()
Expand Down Expand Up @@ -371,8 +387,14 @@ def _create_command_table(self):
command_table=command_table,
session=self.session,
command_object=self)
self._add_lineage(command_table)
return command_table

def _add_lineage(self, command_table):
for command in command_table:
command_obj = command_table[command]
command_obj.lineage = self.lineage + [command_obj]

def create_help_command(self):
command_table = self._get_command_table()
service_object = self._get_service_object()
Expand Down Expand Up @@ -434,6 +456,23 @@ def __init__(self, name, parent_name, operation_object, operation_caller,
self._operation_object = operation_object
self._operation_caller = operation_caller
self._service_object = service_object
self._lineage = [self]

@property
def name(self):
return self._name

@name.setter
def name(self, value):
self._name = value

@property
def lineage(self):
return self._lineage

@lineage.setter
def lineage(self, value):
self._lineage = value

@property
def arg_table(self):
Expand Down
16 changes: 16 additions & 0 deletions awscli/customizations/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(self, session):
self._session = session
self._arg_table = None
self._subcommand_table = None
self._lineage = [self]

def __call__(self, args, parsed_globals):
# args is the remaining unparsed args.
Expand Down Expand Up @@ -213,6 +214,7 @@ def _build_subcommand_table(self):
command_table=subcommand_table,
session=self._session,
command_object=self)
self._add_lineage(subcommand_table)
return subcommand_table

def _display_help(self, parsed_args, parsed_globals):
Expand All @@ -234,6 +236,7 @@ def create_help_command_table(self):
commands = {}
for command in self.SUBCOMMANDS:
commands[command['name']] = command['command_class'](self._session)
self._add_lineage(commands)
return commands

def _build_arg_table(self):
Expand All @@ -253,6 +256,11 @@ def _build_arg_table(self):
arg_table[arg_data['name']] = custom_argument
return arg_table

def _add_lineage(self, command_table):
for command in command_table:
command_obj = command_table[command]
command_obj.lineage = self.lineage + [command_obj]

@property
def arg_table(self):
if self._arg_table is None:
Expand All @@ -273,6 +281,14 @@ def add_command(cls, command_table, session, **kwargs):
def name(self):
return self.NAME

@property
def lineage(self):
return self._lineage

@lineage.setter
def lineage(self, value):
self._lineage = value


class BasicHelp(HelpCommand):
event_class = 'command'
Expand Down
1 change: 1 addition & 0 deletions awscli/customizations/waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def _run_main(self, parsed_args, parsed_globals):
def _build_subcommand_table(self):
subcommand_table = super(WaitCommand, self)._build_subcommand_table()
self.waiter_cmd_builder.build_all_waiter_state_cmds(subcommand_table)
self._add_lineage(subcommand_table)
return subcommand_table

def create_help_command(self):
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,55 @@ def test_bad_lc_ctype_env_var_is_handled(self):
self.assertEqual(p.rc, 0)


class TestCommandLineage(unittest.TestCase):
def setUp(self):
self.driver = create_clidriver()
self.top_help = self.driver.create_help_command()

def assert_lineage_names(self, ref_lineage_names):
command_table = self.top_help.command_table
for i, cmd_name in enumerate(ref_lineage_names):
command = command_table[cmd_name]
help_command = command.create_help_command()
command_table = help_command.command_table

actual_lineage_names = []
for cmd in command.lineage:
actual_lineage_names.append(cmd.name)

self.assertEqual(actual_lineage_names, ref_lineage_names)

def test_service_level_commands(self):
# Check a normal unchanged service command
self.assert_lineage_names(['ec2'])

# Check a service that had its name changed.
self.assert_lineage_names(['s3api'])

# Check a couple custom service level commands.
self.assert_lineage_names(['s3'])
self.assert_lineage_names(['configure'])

def test_operation_level_commands(self):
# Check a normal unchanged service and operation command
self.assert_lineage_names(['dynamodb', 'create-table'])

# Check an operation commands with a service that had its name changed.
self.assert_lineage_names(['s3api', 'list-objects'])

# Check a custom operation level command with no
# custom service command.
self.assert_lineage_names(['emr', 'create-cluster'])

# Check a couple of operation level commands that
# are based off a custom service command
self.assert_lineage_names(['configure', 'set'])
self.assert_lineage_names(['s3', 'cp'])

def test_wait_commands(self):
self.assert_lineage_names(['ec2', 'wait'])
self.assert_lineage_names(['ec2', 'wait', 'instance-running'])


if __name__ == '__main__':
unittest.main()
15 changes: 15 additions & 0 deletions tests/unit/customizations/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@ def test_load_subcommand_table_property(self):
# Ensure the ``subcommand_table`` is not built again if
# ``subcommand_table`` property is called again.
self.assertIs(orig_subcommand_table, self.command.subcommand_table)

def test_load_lineage(self):
self.assertEqual(self.command.lineage, [self.command])

def test_pass_lineage_to_child_command(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we do this without patching by just creating a new command class that has subcommands instead of using the BasicCommand instance? I'd prefer to avoid mocking these types of calls especially if they don't need to be.

class MockCustomCommand(BasicCommand):
NAME = 'mock'

SUBCOMMANDS = [{'name': 'basic', 'command_class': BasicCommand}]

self.command = MockCustomCommand(self.session)
lineage = self.command.subcommand_table['basic'].lineage
self.assertEqual(len(lineage), 2)
self.assertEqual(lineage[0], self.command)
self.assertIsInstance(lineage[1], BasicCommand)
6 changes: 6 additions & 0 deletions tests/unit/customizations/test_waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ def setUp(self):
self.service_object = mock.Mock()
self.cmd = WaitCommand(self.model, self.service_object)

def test_passes_on_lineage(self):
child_cmd = self.cmd.subcommand_table['foo']
self.assertEqual(len(child_cmd.lineage), 2)
self.assertEqual(child_cmd.lineage[0], self.cmd)
self.assertIsInstance(child_cmd.lineage[1], WaiterStateCommand)

def test_run_main_error(self):
self.parsed_args = mock.Mock()
self.parsed_args.subcommand = None
Expand Down
62 changes: 62 additions & 0 deletions tests/unit/test_clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from awscli.clidriver import create_clidriver
from awscli.clidriver import CustomArgument
from awscli.clidriver import CLIOperationCaller
from awscli.clidriver import CLICommand
from awscli.clidriver import ServiceCommand
from awscli.clidriver import ServiceOperation
from awscli.customizations.commands import BasicCommand
from awscli import formatter
from botocore.hooks import HierarchicalEmitter
Expand Down Expand Up @@ -712,5 +715,64 @@ def test_bad_output(self):
formatter.get_formatter('bad-type', None)


class TestCLICommand(unittest.TestCase):
def setUp(self):
self.cmd = CLICommand()

def test_name(self):
with self.assertRaises(NotImplementedError):
self.cmd.name
with self.assertRaises(NotImplementedError):
self.cmd.name = 'foo'

def test_lineage(self):
self.assertEqual(self.cmd.lineage, [self.cmd])

def test_arg_table(self):
self.assertEqual(self.cmd.arg_table, {})


class TestServiceCommand(unittest.TestCase):
def setUp(self):
self.name = 'foo'
self.session = FakeSession()
self.cmd = ServiceCommand(self.name, self.session)

def test_name(self):
self.assertEqual(self.cmd.name, self.name)
self.cmd.name = 'bar'
self.assertEqual(self.cmd.name, 'bar')

def test_lineage(self):
self.assertEqual(self.cmd.lineage, [self.cmd])
self.cmd.lineage = ['foo']
self.assertEqual(self.cmd.lineage, ['foo'])

def test_pass_lineage_to_child(self):
# In order to introspect the service command's subcommands
# we introspect the subcommand via the help command since
# a service command's command table is not public.
help_command = self.cmd.create_help_command()
child_cmd = help_command.command_table['list-objects']
self.assertEqual(child_cmd.lineage,
[self.cmd, child_cmd])


class TestServiceOperation(unittest.TestCase):
def setUp(self):
self.name = 'foo'
self.cmd = ServiceOperation(self.name, None, None, None, None)

def test_name(self):
self.assertEqual(self.cmd.name, self.name)
self.cmd.name = 'bar'
self.assertEqual(self.cmd.name, 'bar')

def test_lineage(self):
self.assertEqual(self.cmd.lineage, [self.cmd])
self.cmd.lineage = ['foo']
self.assertEqual(self.cmd.lineage, ['foo'])


if __name__ == '__main__':
unittest.main()