Changed HelpCommand to raise CommandError instead of InstalationError, when no command is found. It returns status SUCCESS (0) if find command or no command is specified.

This commit is contained in:
Hugo Lopes Tavares 2011-08-02 13:41:31 -04:00
parent 8e8ef0e34a
commit 7989ea9c5b
2 changed files with 70 additions and 4 deletions

View File

@ -1,8 +1,12 @@
from pip.basecommand import Command, command_dict, load_all_commands
from pip.exceptions import InstallationError
from pip.exceptions import CommandError
from pip.baseparser import parser
ERROR = 1
SUCCESS = 0
class HelpCommand(Command):
name = 'help'
usage = '%prog'
@ -14,10 +18,10 @@ class HelpCommand(Command):
## FIXME: handle errors better here
command = args[0]
if command not in command_dict:
raise InstallationError('No command with the name: %s' % command)
raise CommandError('No command with the name: %s' % command)
command = command_dict[command]
command.parser.print_help()
return
return SUCCESS
parser.print_help()
print('\nCommands available:')
commands = list(set(command_dict.values()))
@ -26,6 +30,6 @@ class HelpCommand(Command):
if command.hidden:
continue
print(' %s: %s' % (command.name, command.summary))
return SUCCESS
HelpCommand()

62
tests/test_help.py Normal file
View File

@ -0,0 +1,62 @@
from pip.exceptions import CommandError
from pip.commands.help import (HelpCommand,
SUCCESS,
ERROR,)
from mock import Mock
from nose.tools import assert_raises
from tests.test_pip import run_pip, reset_env
def test_run_method_should_return_sucess_when_finds_command_name():
"""
Test HelpCommand.run for existing command
"""
options_mock = Mock()
args = ('freeze',)
help_cmd = HelpCommand()
status = help_cmd.run(options_mock, args)
assert status == SUCCESS
def test_run_method_should_return_sucess_when_command_name_not_specified():
"""
Test HelpCommand.run when there are no args
"""
options_mock = Mock()
args = ()
help_cmd = HelpCommand()
status = help_cmd.run(options_mock, args)
assert status == SUCCESS
def test_run_method_should_raise_command_error_when_command_does_not_exist():
"""
Test HelpCommand.run for non-existing command
"""
options_mock = Mock()
args = ('mycommand',)
help_cmd = HelpCommand()
assert_raises(CommandError, help_cmd.run, options_mock, args)
def test_help_command_should_exit_status_ok_when_command_exists():
"""
Test `help` command for existing command
"""
reset_env()
result = run_pip('help', 'freeze')
assert result.returncode == SUCCESS
def test_help_command_should_exit_status_ok_when_no_command_is_specified():
"""
Test `help` command for no command
"""
reset_env()
result = run_pip('help')
assert result.returncode == SUCCESS
def test_help_command_should_exit_status_error_when_command_does_not_exist():
"""
Test `help` command for non-existing command
"""
reset_env()
result = run_pip('help', 'mycommand', expect_error=True)
assert result.returncode == ERROR