From 2536b19d2c54f09160c889c8ba8bbfadde9a20c2 Mon Sep 17 00:00:00 2001 From: Devesh Kumar Singh Date: Thu, 2 Apr 2020 02:01:51 +0530 Subject: [PATCH] Don't list cwd packages in pip list --- news/7731.bugfix | 1 + src/pip/__main__.py | 6 ++++++ tests/functional/test_list.py | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 news/7731.bugfix diff --git a/news/7731.bugfix b/news/7731.bugfix new file mode 100644 index 00000000000..0d2f551e2d4 --- /dev/null +++ b/news/7731.bugfix @@ -0,0 +1 @@ +Avoid listing packages from current directory, when invoked as 'python -m pip list' diff --git a/src/pip/__main__.py b/src/pip/__main__.py index e83b9e056b3..b368c2cfaee 100644 --- a/src/pip/__main__.py +++ b/src/pip/__main__.py @@ -13,6 +13,12 @@ path = os.path.dirname(os.path.dirname(__file__)) sys.path.insert(0, path) +# Remove '' and current working directory from the first entry +# of sys.path, if present to avoid using current directory +# in pip commands +if sys.path[0] in ('', os.getcwd()): + sys.path.pop(0) + from pip._internal.cli.main import main as _main # isort:skip # noqa if __name__ == '__main__': diff --git a/tests/functional/test_list.py b/tests/functional/test_list.py index 53f4152c2b7..0c488824726 100644 --- a/tests/functional/test_list.py +++ b/tests/functional/test_list.py @@ -3,6 +3,7 @@ import pytest +from tests.lib import create_test_package_with_setup from tests.lib.path import Path @@ -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