added fix for edge case in pip.utils.find_command and moved `BadCommand` raise from `VersionControl.cmd` to `find_command`

This commit is contained in:
Hugo Lopes Tavares 2011-04-28 01:01:08 -03:00
parent 07fd30bbef
commit d14d3b1986
3 changed files with 22 additions and 6 deletions

View File

@ -7,7 +7,7 @@ import posixpath
import pkg_resources
import zipfile
import tarfile
from pip.exceptions import InstallationError
from pip.exceptions import InstallationError, BadCommand
from pip.backwardcompat import WindowsError, string_types, raw_input
from pip.locations import site_packages, running_under_virtualenv
from pip.log import logger
@ -71,7 +71,7 @@ def backup_dir(dir, ext='.bak'):
def find_command(cmd, paths=None, pathext=None):
"""Searches the PATH for the given command and returns its path"""
if paths is None:
paths = os.environ.get('PATH', []).split(os.pathsep)
paths = os.environ.get('PATH', '').split(os.pathsep)
if isinstance(paths, string_types):
paths = [paths]
# check if there are funny path extensions for executables, e.g. Windows
@ -92,7 +92,8 @@ def find_command(cmd, paths=None, pathext=None):
return cmd_path_ext
if os.path.isfile(cmd_path):
return cmd_path
return None
raise BadCommand('Cannot find command %r' % cmd)
def get_pathext(default_pathext=None):
"""Returns the path extensions from environment or a default"""

View File

@ -4,7 +4,6 @@ import os
import shutil
from pip.backwardcompat import urlparse, urllib
from pip.exceptions import BadCommand
from pip.log import logger
from pip.util import display_path, backup_dir, find_command, ask, rmtree
@ -106,8 +105,6 @@ class VersionControl(object):
if self._cmd is not None:
return self._cmd
command = find_command(self.name)
if command is None:
raise BadCommand('Cannot find command %r' % self.name)
logger.info('Found command %r at %r' % (self.name, command))
self._cmd = command
return command

View File

@ -9,6 +9,7 @@ from nose import SkipTest
from mock import Mock, patch
from pip.util import rmtree, find_command
from pip.exceptions import BadCommand
from tests.test_pip import (here, reset_env, run_pip, pyversion, mkdir,
src_folder, write_file)
@ -525,6 +526,23 @@ def test_find_command_folder_in_path():
found_path = find_command('foo', map(str, [path_one, path_two]))
assert found_path == path_two/'foo'
def test_does_not_find_command_because_there_is_no_path():
"""
Test calling `pip.utils.find_command` when there is no PATH env variable
"""
from pip.util import find_command
environ_before = os.environ
os.environ = {}
try:
find_command('anycommand')
except BadCommand:
e = sys.exc_info()[1]
assert e.args == ("Cannot find command 'anycommand'",)
else:
raise AssertionError("`find_command` should raise `BadCommand`")
finally:
os.environ = environ_before
@patch('pip.util.get_pathext')
@patch('os.path.isfile')
def test_find_command_trys_all_pathext(mock_isfile, getpath_mock):