summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2014-06-16 17:16:00 +0000
committerGerrit Code Review <review@openstack.org>2014-06-16 17:16:00 +0000
commit556f564c877a60bc5df2eb72523754278fa34306 (patch)
tree44561fa7bf0d8aa5c9ab9cc01c81c50c9af5a5b1
parentf1a06ae26debd19d008b6c041919d5c2f9cf2b0b (diff)
parent69966df0727d61b8c46056ee9af4567f176bb73f (diff)
downloadcliff-556f564c877a60bc5df2eb72523754278fa34306.tar.gz
Merge "Expose load_commands publicly"
-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 a6aef53..1469e75 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)