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
def cmd(self): if self._cmd is not None: return self._cmd command = find_command(self.name) logger.info('Found command %r at %r' % (self.name, command)) self._cmd = command return command
def cmd(self): if self._cmd is not None: return self._cmd command = find_command(self.name) logger.info('Found command {0!r} at {1!r}'.format(self.name, command)) self._cmd = command return command
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 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] with pytest.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"
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 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] with pytest.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"
def cmd(self): 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
def test_find_command_folder_in_path(): """ If a folder named e.g. 'git' is in PATH, and find_command is looking for the 'git' executable, it should not match the folder, but rather keep looking. """ env = reset_env() mkdir('path_one'); path_one = env.scratch_path/'path_one' mkdir(path_one/'foo') mkdir('path_two'); path_two = env.scratch_path/'path_two' write_file(path_two/'foo', '# nothing') found_path = find_command('foo', map(str, [path_one, path_two])) assert found_path == path_two/'foo'
def test_find_command_folder_in_path(script): """ If a folder named e.g. 'git' is in PATH, and find_command is looking for the 'git' executable, it should not match the folder, but rather keep looking. """ script.scratch_path.join("path_one").mkdir() path_one = script.scratch_path/'path_one' path_one.join("foo").mkdir() script.scratch_path.join("path_two").mkdir() path_two = script.scratch_path/'path_two' path_two.join("foo").write("# nothing") found_path = find_command('foo', map(str, [path_one, path_two])) assert found_path == path_two/'foo'
def test_find_command_folder_in_path(): """ If a folder named e.g. 'git' is in PATH, and find_command is looking for the 'git' executable, it should not match the folder, but rather keep looking. """ env = reset_env() mkdir('path_one') path_one = env.scratch_path / 'path_one' mkdir(path_one / 'foo') mkdir('path_two') path_two = env.scratch_path / 'path_two' write_file(path_two / 'foo', '# nothing') found_path = find_command('foo', map(str, [path_one, path_two])) assert found_path == path_two / 'foo'
def test_find_command_folder_in_path(): """ If a folder named e.g. 'git' is in PATH, and find_command is looking for the 'git' executable, it should not match the folder, but rather keep looking. """ env = reset_env() mkdir("path_one") path_one = env.scratch_path / "path_one" mkdir(path_one / "foo") mkdir("path_two") path_two = env.scratch_path / "path_two" write_file(path_two / "foo", "# nothing") found_path = find_command("foo", map(str, [path_one, path_two])) assert found_path == path_two / "foo"
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"]) found_path = find_command('foo', 'path_one', pathext) paths = [ os.path.join('path_one', f) for f in ['foo.run', 'foo.cmd', 'foo'] ] expected = [ ((p,),) for p in paths ] assert found_path is None, "Should not find path" 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" os.pathsep = old_sep
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"]) found_path = find_command('foo', 'path_one') paths = [ os.path.join('path_one', f) for f in ['foo.com', 'foo.exe', 'foo'] ] expected = [ ((p,),) for p in paths ] assert found_path is None, "Should not find path" 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" os.pathsep = old_sep
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"]) found_path = find_command('foo', 'path_one', pathext) paths = [ os.path.join('path_one', f) for f in ['foo.run', 'foo.cmd', 'foo'] ] expected = [((p, ), ) for p in paths] assert found_path is None, "Should not find path" 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" os.pathsep = old_sep
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"]) found_path = find_command('foo', 'path_one') paths = [ os.path.join('path_one', f) for f in ['foo.com', 'foo.exe', 'foo'] ] expected = [((p, ), ) for p in paths] assert found_path is None, "Should not find path" 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" os.pathsep = old_sep