Skip to content

Commit 56d50b2

Browse files
committed
Don't use cwd in pip check, list and freeze
1 parent 657cf25 commit 56d50b2

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

news/7731.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid using the current directory for check, freeze and list commands, when invoked as 'python -m pip <command>'

src/pip/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
import os
44
import sys
55

6+
# Remove '' and current working directory from the first entry
7+
# of sys.path, if present to avoid using current directory
8+
# in pip commands check, freeze and list, when invoked as
9+
# python -m pip <command>
10+
if sys.path[0] in ('', os.getcwd()):
11+
sys.path.pop(0)
12+
13+
614
# If we are running from a wheel, add the wheel to sys.path
715
# This allows the usage python pip-*.whl/pip install pip-*.whl
816
if __package__ == '':
@@ -13,6 +21,7 @@
1321
path = os.path.dirname(os.path.dirname(__file__))
1422
sys.path.insert(0, path)
1523

24+
1625
from pip._internal.cli.main import main as _main # isort:skip # noqa
1726

1827
if __name__ == '__main__':

tests/functional/test_list.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
from tests.lib import create_test_package_with_setup
67
from tests.lib.path import Path
78

89

@@ -543,3 +544,21 @@ def test_list_path_multiple(tmpdir, script, data):
543544
json_result = json.loads(result.stdout)
544545
assert {'name': 'simple', 'version': '2.0'} in json_result
545546
assert {'name': 'simple2', 'version': '3.0'} in json_result
547+
548+
549+
def test_list_skip_work_dir_pkg(script):
550+
"""
551+
Test that list should not include package in working directory
552+
"""
553+
554+
# Create a test package and create .egg-info dir
555+
pkg_path = create_test_package_with_setup(script,
556+
name='simple',
557+
version='1.0')
558+
script.run('python', 'setup.py', 'egg_info',
559+
expect_stderr=True, cwd=pkg_path)
560+
561+
# List should not include package simple when run from package directory
562+
result = script.pip('list', '--format=json', cwd=pkg_path)
563+
json_result = json.loads(result.stdout)
564+
assert {'name': 'simple', 'version': '1.0'} not in json_result

0 commit comments

Comments
 (0)