Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't list packages in current directory #7955

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/7731.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid using the current directory for check, freeze, install, list and show commands, when invoked as 'python -m pip <command>'
7 changes: 7 additions & 0 deletions src/pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
import os
import sys

# Remove '' and current working directory from the first entry
# of sys.path, if present to avoid using current directory
# in pip commands check, freeze, install, list and show,
# when invoked as python -m pip <command>
if sys.path[0] in ('', os.getcwd()):
sys.path.pop(0)

# If we are running from a wheel, add the wheel to sys.path
# This allows the usage python pip-*.whl/pip install pip-*.whl
if __package__ == '':
Expand Down
42 changes: 42 additions & 0 deletions tests/functional/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from tests.lib import create_test_package_with_setup
from tests.lib.path import Path


Expand Down Expand Up @@ -543,3 +544,44 @@ def test_list_path_multiple(tmpdir, script, data):
json_result = json.loads(result.stdout)
assert {'name': 'simple', 'version': '2.0'} in json_result
assert {'name': 'simple2', 'version': '3.0'} in json_result


def test_list_skip_work_dir_pkg(script):
pradyunsg marked this conversation as resolved.
Show resolved Hide resolved
"""
Test that list should not include package in working directory
"""

# Create a test package and create .egg-info dir
pkg_path = create_test_package_with_setup(script,
name='simple',
version='1.0')
script.run('python', 'setup.py', 'egg_info',
expect_stderr=True, cwd=pkg_path)

# List should not include package simple when run from package directory
result = script.pip('list', '--format=json', cwd=pkg_path)
json_result = json.loads(result.stdout)
assert {'name': 'simple', 'version': '1.0'} not in json_result
deveshks marked this conversation as resolved.
Show resolved Hide resolved


def test_list_include_work_dir_pkg(script):
"""
Test that list should include package in working directory
if working directory is added in sys.path
"""

# Create a test package and create .egg-info dir
pkg_path = create_test_package_with_setup(script,
name='simple',
version='1.0')

script.run('python', 'setup.py', 'egg_info',
expect_stderr=True, cwd=pkg_path)

# Add PYTHONPATH env variable
script.environ.update({'PYTHONPATH': pkg_path})

# List should include package simple when run from package directory
result = script.pip('list', '--format=json', cwd=pkg_path)
json_result = json.loads(result.stdout)
assert {'name': 'simple', 'version': '1.0'} in json_result