diff --git a/news/2926.bugfix b/news/2926.bugfix new file mode 100644 index 00000000000..b50a85150f1 --- /dev/null +++ b/news/2926.bugfix @@ -0,0 +1 @@ +Avoid listing packages from current directory, when invoked as 'python -m pip freeze' diff --git a/src/pip/_internal/commands/freeze.py b/src/pip/_internal/commands/freeze.py index 4758e30343f..8a89de4f3a5 100644 --- a/src/pip/_internal/commands/freeze.py +++ b/src/pip/_internal/commands/freeze.py @@ -3,6 +3,7 @@ from __future__ import absolute_import +import os import sys from pip._internal.cache import WheelCache @@ -83,12 +84,18 @@ def run(self, options, args): cmdoptions.check_list_path_option(options) + paths = options.path + # Filter sys.path, to avoid listing distributions from + # current directory + if paths is None: + paths = [item for item in sys.path if item and item != os.getcwd()] + freeze_kwargs = dict( requirement=options.requirements, find_links=options.find_links, local_only=options.local, user_only=options.user, - paths=options.path, + paths=paths, skip_regex=options.skip_requirements_regex, isolated=options.isolated_mode, wheel_cache=wheel_cache, diff --git a/tests/functional/test_freeze.py b/tests/functional/test_freeze.py index 7027879bd44..d3b37550113 100644 --- a/tests/functional/test_freeze.py +++ b/tests/functional/test_freeze.py @@ -79,34 +79,36 @@ def test_freeze_with_pip(script): assert 'pip==' in result.stdout +def _fake_install(pkgname, dest): + egg_info_path = os.path.join( + dest, '{}-1.0-py{}.{}.egg-info'.format( + pkgname.replace('-', '_'), + sys.version_info[0], + sys.version_info[1] + ) + ) + with open(egg_info_path, 'w') as egg_info_file: + egg_info_file.write(textwrap.dedent("""\ + Metadata-Version: 1.0 + Name: {} + Version: 1.0 + """.format(pkgname) + )) + return egg_info_path + + def test_freeze_with_invalid_names(script): """ Test that invalid names produce warnings and are passed over gracefully. """ - def fake_install(pkgname, dest): - egg_info_path = os.path.join( - dest, '{}-1.0-py{}.{}.egg-info'.format( - pkgname.replace('-', '_'), - sys.version_info[0], - sys.version_info[1] - ) - ) - with open(egg_info_path, 'w') as egg_info_file: - egg_info_file.write(textwrap.dedent("""\ - Metadata-Version: 1.0 - Name: {} - Version: 1.0 - """.format(pkgname) - )) - valid_pkgnames = ('middle-dash', 'middle_underscore', 'middle.dot') invalid_pkgnames = ( '-leadingdash', '_leadingunderscore', '.leadingdot', 'trailingdash-', 'trailingunderscore_', 'trailingdot.' ) for pkgname in valid_pkgnames + invalid_pkgnames: - fake_install(pkgname, script.site_packages_path) + _fake_install(pkgname, script.site_packages_path) result = script.pip('freeze', expect_stderr=True) for pkgname in valid_pkgnames: _check_output( @@ -129,6 +131,18 @@ def fake_install(pkgname, dest): _check_output(result.stderr, expected) +def test_freeze_skip_curr_dir(script): + """ + Test that an .egginfo is skipped when present in current directory + """ + + curr_dir = os.getcwd() + egg_info_path = _fake_install("foo-package", curr_dir) + result = script.pip("freeze") + os.remove(egg_info_path) + assert "foo-package==" not in result.stdout + + @pytest.mark.git def test_freeze_editable_not_vcs(script, tmpdir): """