Skip to content

Commit

Permalink
Fix bypassed pip upgrade warning on Windows
Browse files Browse the repository at this point in the history
Merge pull request #6864 from atugushev/fix-issue-6841
  • Loading branch information
pradyunsg authored Aug 25, 2019
2 parents ea947c1 + f9fc667 commit d2b7082
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/6841.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bypassed pip upgrade warning on Windows.
10 changes: 5 additions & 5 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1299,11 +1299,11 @@ def protect_pip_from_modification_on_windows(modifying_pip):
On Windows, any operation modifying pip should be run as:
python -m pip ...
"""
pip_names = [
"pip.exe",
"pip{}.exe".format(sys.version_info[0]),
"pip{}.{}.exe".format(*sys.version_info[:2])
]
pip_names = set()
for ext in ('', '.exe'):
pip_names.add('pip{ext}'.format(ext=ext))
pip_names.add('pip{}{ext}'.format(sys.version_info[0], ext=ext))
pip_names.add('pip{}.{}{ext}'.format(*sys.version_info[:2], ext=ext))

# See https://github.com/pypa/pip/issues/1299 for more discussion
should_show_use_python_msg = (
Expand Down
84 changes: 84 additions & 0 deletions tests/functional/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

from pip import __version__ as pip_current_version
from pip._internal import pep425tags
from pip._internal.cli.status_codes import ERROR, SUCCESS
from pip._internal.models.index import PyPI, TestPyPI
Expand Down Expand Up @@ -1552,3 +1553,86 @@ def test_target_install_ignores_distutils_config_install_prefix(script):
(target - script.base_path) in result.files_created and
(prefix - script.base_path) not in result.files_created
), str(result)


@pytest.mark.network
@pytest.mark.skipif("sys.platform != 'win32'")
@pytest.mark.parametrize('pip_name', [
'pip',
'pip{}'.format(sys.version_info[0]),
'pip{}.{}'.format(*sys.version_info[:2]),
'pip.exe',
'pip{}.exe'.format(sys.version_info[0]),
'pip{}.{}.exe'.format(*sys.version_info[:2])
])
def test_protect_pip_from_modification_on_windows(script, pip_name):
"""
Test that pip modification command using ``pip install ...``
raises an error on Windows.
"""
command = [pip_name, 'install', 'pip != {}'.format(pip_current_version)]
result = script.run(*command, expect_error=True)
new_command = [sys.executable, '-m', 'pip'] + command[1:]
expected_message = (
'To modify pip, please run the following command:\n{}'
.format(' '.join(new_command))
)
assert expected_message in result.stderr, str(result)


@pytest.mark.network
@pytest.mark.skipif("sys.platform != 'win32'")
def test_protect_pip_from_modification_via_deps_on_windows(script):
"""
Test ``pip install pkga`` raises an error on Windows
if `pkga` implicitly tries to upgrade pip.
"""
pkga_wheel_path = create_basic_wheel_for_package(
script,
'pkga', '0.1',
depends=['pip != {}'.format(pip_current_version)],
)

# Make sure pip install pkga raises an error
args = ['install', pkga_wheel_path]
result = script.pip(*args, expect_error=True, use_module=False)
new_command = [sys.executable, '-m', 'pip'] + args
expected_message = (
'To modify pip, please run the following command:\n{}'
.format(' '.join(new_command))
)
assert expected_message in result.stderr, str(result)


@pytest.mark.network
@pytest.mark.skipif("sys.platform != 'win32'")
def test_protect_pip_from_modification_via_sub_deps_on_windows(script):
"""
Test ``pip install pkga`` raises an error on Windows
if sub-dependencies of `pkga` implicitly tries to upgrade pip.
"""
# Make a wheel for pkga which requires pkgb
pkga_wheel_path = create_basic_wheel_for_package(
script,
'pkga', '0.1',
depends=['pkgb'],
)

# Make a wheel for pkgb which requires pip
pkgb_wheel_path = create_basic_wheel_for_package(
script,
'pkgb', '0.1',
depends=['pip != {}'.format(pip_current_version)],
)

# Make sure pip install pkga raises an error
args = [
'install', pkga_wheel_path, '--find-links', pkgb_wheel_path.parent
]
result = script.pip(*args, expect_error=True, use_module=False)
new_command = [sys.executable, '-m', 'pip'] + args
expected_message = (
'To modify pip, please run the following command:\n{}'
.format(' '.join(new_command))
)
assert expected_message in result.stderr, str(result)

0 comments on commit d2b7082

Please sign in to comment.