Skip to content

Commit 2536b19

Browse files
committed
Don't list cwd packages in pip list
1 parent 657cf25 commit 2536b19

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-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 listing packages from current directory, when invoked as 'python -m pip list'

src/pip/__main__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
path = os.path.dirname(os.path.dirname(__file__))
1414
sys.path.insert(0, path)
1515

16+
# Remove '' and current working directory from the first entry
17+
# of sys.path, if present to avoid using current directory
18+
# in pip commands
19+
if sys.path[0] in ('', os.getcwd()):
20+
sys.path.pop(0)
21+
1622
from pip._internal.cli.main import main as _main # isort:skip # noqa
1723

1824
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)