Skip to content

Commit

Permalink
Merge "Expose load_commands publicly"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Jun 16, 2014
2 parents f1a06ae + 69966df commit 556f564
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 60 deletions.
7 changes: 6 additions & 1 deletion cliff/commandmanager.py
Expand Up @@ -37,7 +37,12 @@ def __init__(self, namespace, convert_underscores=True):
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
Expand Down
29 changes: 6 additions & 23 deletions cliff/tests/test_commandmanager.py
Expand Up @@ -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():
Expand All @@ -28,7 +11,7 @@ def check(mgr, argv):
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'],
Expand All @@ -42,7 +25,7 @@ def check(mgr, argv):
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'],
Expand All @@ -52,7 +35,7 @@ def check(mgr, argv):


def test_find_invalid_command():
mgr = TestCommandManager('test')
mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)

def check_one(argv):
try:
Expand All @@ -68,7 +51,7 @@ def check_one(argv):


def test_find_unknown_command():
mgr = TestCommandManager('test')
mgr = utils.TestCommandManager(utils.TEST_NAMESPACE)
try:
mgr.find_command(['a', 'b'])
except ValueError as err:
Expand All @@ -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'])
Expand Down
49 changes: 13 additions & 36 deletions cliff/tests/test_help.py
Expand Up @@ -6,47 +6,18 @@
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():
# FIXME(dhellmann): Are commands tied too closely to the app? Or
# 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')
Expand All @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
30 changes: 30 additions & 0 deletions 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)

0 comments on commit 556f564

Please sign in to comment.