Skip to content

Commit

Permalink
Address issue #1139: make --force-reinstall not require --upgrade (#4432
Browse files Browse the repository at this point in the history
)

* Add failing tests for issue #1139 re: --force-reinstall.

* Address issue #1139: make --force-reinstall not require --upgrade.

* Address review comments.

* Address @xavfernandez's review comments.

This makes the new tests not require network access.
  • Loading branch information
cjerdonek authored and xavfernandez committed Oct 23, 2017
1 parent feab014 commit b112292
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions news/1139.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make ``pip install --force-reinstall`` not require passing ``--upgrade``.
4 changes: 2 additions & 2 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ def __init__(self, *args, **kw):
'--force-reinstall',
dest='force_reinstall',
action='store_true',
help='When upgrading, reinstall all packages even if they are '
'already up-to-date.')
help='Reinstall all packages even if they are already '
'up-to-date.')

cmd_opts.add_option(
'-I', '--ignore-installed',
Expand Down
8 changes: 6 additions & 2 deletions src/pip/_internal/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ def _check_skip_installed(self, req_to_install):
if not req_to_install.satisfied_by:
return None

if self.force_reinstall:
self._set_req_to_reinstall(req_to_install)
return None

if not self._is_upgrade_allowed(req_to_install):
if self.upgrade_strategy == "only-if-needed":
return 'not upgraded as not directly required'
Expand All @@ -158,7 +162,7 @@ def _check_skip_installed(self, req_to_install):
# Check for the possibility of an upgrade. For link-based
# requirements we have to pull the tree down and inspect to assess
# the version #, so it's handled way down.
if not (self.force_reinstall or req_to_install.link):
if not req_to_install.link:
try:
self.finder.find_requirement(req_to_install, upgrade=True)
except BestVersionAlreadyInstalled:
Expand Down Expand Up @@ -216,7 +220,7 @@ def _get_abstract_dist_for(self, req):
if req.satisfied_by:
should_modify = (
self.upgrade_strategy != "to-satisfy-only" or
self.ignore_installed
self.force_reinstall or self.ignore_installed
)
if should_modify:
self._set_req_to_reinstall(req)
Expand Down
47 changes: 47 additions & 0 deletions tests/functional/test_install_force_reinstall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pytest

from tests.lib import assert_all_changes


def check_installed_version(script, package, expected):
result = script.pip('show', package)
lines = result.stdout.splitlines()
version = None
for line in lines:
if line.startswith('Version: '):
version = line.split()[-1]
break
assert version == expected, 'version {} != {}'.format(version, expected)


def check_force_reinstall(script, specifier, expected):
"""
Args:
specifier: the requirement specifier to force-reinstall.
expected: the expected version after force-reinstalling.
"""
result = script.pip_install_local('simplewheel==1.0')
check_installed_version(script, 'simplewheel', '1.0')

result2 = script.pip_install_local('--force-reinstall', specifier)
assert result2.files_updated, 'force-reinstall failed'
check_installed_version(script, 'simplewheel', expected)

result3 = script.pip('uninstall', 'simplewheel', '-y', expect_error=True)
assert_all_changes(result, result3, [script.venv / 'build', 'cache'])


def test_force_reinstall_with_no_version_specifier(script):
"""
Check --force-reinstall when there is no version specifier and the
installed version is not the newest version.
"""
check_force_reinstall(script, 'simplewheel', '2.0')


def test_force_reinstall_with_same_version_specifier(script):
"""
Check --force-reinstall when the version specifier equals the installed
version and the installed version is not the newest version.
"""
check_force_reinstall(script, 'simplewheel==1.0', '1.0')

0 comments on commit b112292

Please sign in to comment.