Skip to content

Commit

Permalink
Don't use cwd in pip check, list and freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
deveshks committed Apr 5, 2020
1 parent 657cf25 commit 56d50b2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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 and list commands, when invoked as 'python -m pip <command>'
9 changes: 9 additions & 0 deletions src/pip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
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 and list, 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 All @@ -13,6 +21,7 @@
path = os.path.dirname(os.path.dirname(__file__))
sys.path.insert(0, path)


from pip._internal.cli.main import main as _main # isort:skip # noqa

if __name__ == '__main__':
Expand Down
19 changes: 19 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,21 @@ 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):
"""
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

0 comments on commit 56d50b2

Please sign in to comment.