summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@redhat.com>2014-05-22 14:29:26 +1000
committerJamie Lennox <jamielennox@redhat.com>2014-05-23 08:26:50 +1000
commit69966df0727d61b8c46056ee9af4567f176bb73f (patch)
tree51d40d96b0fcfec327ec2fc5f6168457ce4ad6e5
parent4bdf5fc90ee3e84ac675f5f26f7a0d94aacf91c5 (diff)
downloadcliff-69966df0727d61b8c46056ee9af4567f176bb73f.tar.gz
Expose load_commands publicly
This will allow us to load commands from other entrypoints into the list. It brings together the two CommandManager overrides in tests into one place in utils to simply usage. Change-Id: Ib463eff98c2ac04488ce4a8875cf3e1b7779b27c
-rw-r--r--cliff/commandmanager.py7
-rw-r--r--cliff/tests/test_commandmanager.py29
-rw-r--r--cliff/tests/test_help.py49
-rw-r--r--cliff/tests/utils.py30
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)