diff options
-rw-r--r-- | cliff/commandmanager.py | 7 | ||||
-rw-r--r-- | cliff/tests/test_commandmanager.py | 29 | ||||
-rw-r--r-- | cliff/tests/test_help.py | 49 | ||||
-rw-r--r-- | cliff/tests/utils.py | 30 |
4 files changed, 55 insertions, 60 deletions
diff --git a/cliff/commandmanager.py b/cliff/commandmanager.py index 2f83eb2..2b91f17 100644 --- a/cliff/commandmanager.py +++ b/cliff/commandmanager.py @@ -37,7 +37,12 @@ class CommandManager(object): self._load_commands() def _load_commands(self): - for ep in pkg_resources.iter_entry_points(self.namespace): + # NOTE(jamielennox): kept for compatability. + self.load_commands(self.namespace) + + def load_commands(self, namespace): + """Load all the commands from an entrypoint""" + for ep in pkg_resources.iter_entry_points(namespace): LOG.debug('found command %r', ep.name) cmd_name = (ep.name.replace('_', ' ') if self.convert_underscores diff --git a/cliff/tests/test_commandmanager.py b/cliff/tests/test_commandmanager.py index 9a50a5e..91dadc1 100644 --- a/cliff/tests/test_commandmanager.py +++ b/cliff/tests/test_commandmanager.py @@ -2,24 +2,7 @@ import mock from cliff.commandmanager import CommandManager - - -class TestCommand(object): - @classmethod - def load(cls): - return cls - - def __init__(self): - return - - -class TestCommandManager(CommandManager): - def _load_commands(self): - self.commands = { - 'one': TestCommand, - 'two words': TestCommand, - 'three word command': TestCommand, - } +from cliff.tests import utils def test_lookup_and_find(): @@ -28,7 +11,7 @@ def test_lookup_and_find(): assert cmd assert name == ' '.join(argv) assert not remaining - mgr = TestCommandManager('test') + mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) for expected in [['one'], ['two', 'words'], ['three', 'word', 'command'], @@ -42,7 +25,7 @@ def test_lookup_with_remainder(): cmd, name, remaining = mgr.find_command(argv) assert cmd assert remaining == ['--opt'] - mgr = TestCommandManager('test') + mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) for expected in [['one', '--opt'], ['two', 'words', '--opt'], ['three', 'word', 'command', '--opt'], @@ -52,7 +35,7 @@ def test_lookup_with_remainder(): def test_find_invalid_command(): - mgr = TestCommandManager('test') + mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) def check_one(argv): try: @@ -68,7 +51,7 @@ def test_find_invalid_command(): def test_find_unknown_command(): - mgr = TestCommandManager('test') + mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) try: mgr.find_command(['a', 'b']) except ValueError as err: @@ -78,7 +61,7 @@ def test_find_unknown_command(): def test_add_command(): - mgr = TestCommandManager('test') + mgr = utils.TestCommandManager(utils.TEST_NAMESPACE) mock_cmd = mock.Mock() mgr.add_command('mock', mock_cmd) found_cmd, name, args = mgr.find_command(['mock']) diff --git a/cliff/tests/test_help.py b/cliff/tests/test_help.py index bdf9d71..db106a5 100644 --- a/cliff/tests/test_help.py +++ b/cliff/tests/test_help.py @@ -6,39 +6,8 @@ except: import mock from cliff.app import App -from cliff.command import Command -from cliff.commandmanager import CommandManager from cliff.help import HelpCommand - - -class TestParser(object): - - def print_help(self, stdout): - stdout.write('TestParser') - - -class TestCommand(Command): - - @classmethod - def load(cls): - return cls - - def get_parser(self, ignore): - # Make it look like this class is the parser - # so parse_args() is called. - return TestParser() - - def take_action(self, args): - return - - -class TestCommandManager(CommandManager): - def _load_commands(self): - self.commands = { - 'one': TestCommand, - 'two words': TestCommand, - 'three word command': TestCommand, - } +from cliff.tests import utils def test_show_help_for_command(): @@ -46,7 +15,9 @@ def test_show_help_for_command(): # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() - app = App('testing', '1', TestCommandManager('cliff.test'), stdout=stdout) + app = App('testing', '1', + utils.TestCommandManager(utils.TEST_NAMESPACE), + stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') @@ -63,7 +34,9 @@ def test_list_matching_commands(): # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() - app = App('testing', '1', TestCommandManager('cliff.test'), stdout=stdout) + app = App('testing', '1', + utils.TestCommandManager(utils.TEST_NAMESPACE), + stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') @@ -83,7 +56,9 @@ def test_list_matching_commands_no_match(): # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() - app = App('testing', '1', TestCommandManager('cliff.test'), stdout=stdout) + app = App('testing', '1', + utils.TestCommandManager(utils.TEST_NAMESPACE), + stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') @@ -103,7 +78,9 @@ def test_show_help_for_help(): # do commands know too much about apps by using them to get to the # command manager? stdout = StringIO() - app = App('testing', '1', TestCommandManager('cliff.test'), stdout=stdout) + app = App('testing', '1', + utils.TestCommandManager(utils.TEST_NAMESPACE), + stdout=stdout) app.NAME = 'test' help_cmd = HelpCommand(app, mock.Mock()) parser = help_cmd.get_parser('test') diff --git a/cliff/tests/utils.py b/cliff/tests/utils.py new file mode 100644 index 0000000..bcab2a2 --- /dev/null +++ b/cliff/tests/utils.py @@ -0,0 +1,30 @@ + +from cliff.command import Command +from cliff.commandmanager import CommandManager + +TEST_NAMESPACE = 'cliff.test' + + +class TestParser(object): + + def print_help(self, stdout): + stdout.write('TestParser') + + +class TestCommand(Command): + + def get_parser(self, ignore): + # Make it look like this class is the parser + # so parse_args() is called. + return TestParser() + + def take_action(self, args): + return + + +class TestCommandManager(CommandManager): + + def load_commands(self, namespace): + if namespace == TEST_NAMESPACE: + for key in ('one', 'two words', 'three word command'): + self.add_command(key, TestCommand) |