diff options
Diffstat (limited to 'tests/test_basic.py')
-rw-r--r-- | tests/test_basic.py | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/tests/test_basic.py b/tests/test_basic.py index a4c1b3997..aa1129745 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -6,8 +6,11 @@ import sys from os.path import abspath, join, curdir, pardir from nose import SkipTest +from nose.tools import assert_raises +from mock import Mock, patch -from pip.util import rmtree +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) @@ -521,6 +524,70 @@ def test_find_command_folder_in_path(): mkdir(path_one/'foo') mkdir('path_two'); path_two = env.scratch_path/'path_two' write_file(path_two/'foo', '# nothing') - from pip.util import find_command 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 + """ + environ_before = os.environ + os.environ = {} + try: + 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): + """ + If no pathext should check default list of extensions, if file does not + exist. + """ + mock_isfile.return_value = False + # Patching os.pathsep failed on type checking + old_sep = os.pathsep + os.pathsep = ':' + + getpath_mock.return_value = os.pathsep.join([".COM", ".EXE"]) + + paths = [ os.path.join('path_one', f) for f in ['foo.com', 'foo.exe', 'foo'] ] + expected = [ ((p,),) for p in paths ] + + try: + assert_raises(BadCommand, find_command, 'foo', 'path_one') + assert mock_isfile.call_args_list == expected, "Actual: %s\nExpected %s" % (mock_isfile.call_args_list, expected) + assert getpath_mock.called, "Should call get_pathext" + finally: + os.pathsep = old_sep + +@patch('pip.util.get_pathext') +@patch('os.path.isfile') +def test_find_command_trys_supplied_pathext(mock_isfile, getpath_mock): + """ + If pathext supplied find_command should use all of its list of extensions to find file. + """ + mock_isfile.return_value = False + # Patching os.pathsep failed on type checking + old_sep = os.pathsep + os.pathsep = ':' + getpath_mock.return_value = ".FOO" + + pathext = os.pathsep.join([".RUN", ".CMD"]) + + paths = [ os.path.join('path_one', f) for f in ['foo.run', 'foo.cmd', 'foo'] ] + expected = [ ((p,),) for p in paths ] + + try: + assert_raises(BadCommand, find_command, 'foo', 'path_one', pathext) + assert mock_isfile.call_args_list == expected, "Actual: %s\nExpected %s" % (mock_isfile.call_args_list, expected) + assert not getpath_mock.called, "Should not call get_pathext" + finally: + os.pathsep = old_sep |