From 53494c634ce20b17d6c572197743069cfea8ebdd Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 29 Jan 2020 14:46:22 +0800 Subject: [PATCH 1/3] Include editable locations in JSON output of list This matches the JSON format with the default columns format. --- news/7664.feature.rst | 3 +++ src/pip/_internal/commands/list.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 news/7664.feature.rst diff --git a/news/7664.feature.rst b/news/7664.feature.rst new file mode 100644 index 00000000000..7ad13f3d720 --- /dev/null +++ b/news/7664.feature.rst @@ -0,0 +1,3 @@ +Include ``location`` field for editable distribution entries in +``pip list --json``, so the fields outputted match the default "columns" +format. diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index dcf9432638a..381a2fbbe8a 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -309,8 +309,9 @@ def format_for_json(packages, options): 'name': dist.project_name, 'version': str(dist.version), } - if options.verbose >= 1: + if options.verbose >= 1 or dist_is_editable(dist): info['location'] = dist.location + if options.verbose >= 1: info['installer'] = get_installer(dist) if options.outdated: info['latest_version'] = str(dist.latest_version) From 8ef46c81948659eadab3c9acc81684477c10dac2 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Tue, 25 Feb 2020 21:01:14 +0800 Subject: [PATCH 2/3] Fix tests using list --json results --- tests/functional/test_list.py | 44 ++++++++++++++++++++---------- tests/functional/test_uninstall.py | 17 +++++++++--- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/tests/functional/test_list.py b/tests/functional/test_list.py index 40dfbdea30d..099137fc27d 100644 --- a/tests/functional/test_list.py +++ b/tests/functional/test_list.py @@ -160,11 +160,17 @@ def test_uptodate_flag(script, data): 'list', '-f', data.find_links, '--no-index', '--uptodate', '--format=json', ) - assert {"name": "simple", "version": "1.0"} \ - not in json.loads(result.stdout) # 3.0 is latest - assert {"name": "pip-test-package", "version": "0.1.1"} \ - in json.loads(result.stdout) # editables included - assert {"name": "simple2", "version": "3.0"} in json.loads(result.stdout) + json_result = json.loads(result.stdout) + + # 3.0 is latest + assert "simple" not in {d["name"] for d in json_result} + # editables included + assert { + "name": "pip-test-package", + "version": "0.1.1", + "location": str(script.venv_path / "src" / "pip-test-package"), + } in json_result + assert {"name": "simple2", "version": "3.0"} in json_result @pytest.mark.network @@ -212,15 +218,25 @@ def test_outdated_flag(script, data): 'list', '-f', data.find_links, '--no-index', '--outdated', '--format=json', ) - assert {"name": "simple", "version": "1.0", - "latest_version": "3.0", "latest_filetype": "sdist"} \ - in json.loads(result.stdout) - assert dict(name="simplewheel", version="1.0", - latest_version="2.0", latest_filetype="wheel") \ - in json.loads(result.stdout) - assert dict(name="pip-test-package", version="0.1", - latest_version="0.1.1", latest_filetype="sdist") \ - in json.loads(result.stdout) + assert { + "name": "simple", + "version": "1.0", + "latest_version": "3.0", + "latest_filetype": "sdist", + } in json.loads(result.stdout) + assert { + "name": "simplewheel", + "version": "1.0", + "latest_version": "2.0", + "latest_filetype": "wheel", + } in json.loads(result.stdout) + assert { + "name": "pip-test-package", + "version": "0.1", + "location": str(script.venv_path / "src" / "pip-test-package"), + "latest_version": "0.1.1", + "latest_filetype": "sdist", + } in json.loads(result.stdout) assert "simple2" not in {p["name"] for p in json.loads(result.stdout)} diff --git a/tests/functional/test_uninstall.py b/tests/functional/test_uninstall.py index 878e713ed9e..7c51be105f9 100644 --- a/tests/functional/test_uninstall.py +++ b/tests/functional/test_uninstall.py @@ -506,8 +506,14 @@ def test_uninstall_setuptools_develop_install(script, data): script.run('python', 'setup.py', 'install', expect_stderr=True, cwd=pkg_path) list_result = script.pip('list', '--format=json') - assert {"name": os.path.normcase("FSPkg"), "version": "0.1.dev0"} \ - in json.loads(list_result.stdout), str(list_result) + assert { + "name": os.path.normcase("FSPkg"), + "version": "0.1.dev0", + "location": str( + script.site_packages_path / + "FSPkg-0.1.dev0-py{0}.{1}.egg".format(*sys.version_info) + ), + } in json.loads(list_result.stdout), str(list_result) # Uninstall both develop and install uninstall = script.pip('uninstall', 'FSPkg', '-y') assert any(filename.endswith('.egg') @@ -534,8 +540,11 @@ def test_uninstall_editable_and_pip_install(script, data): script.pip('install', '--ignore-installed', '.', expect_stderr=True, cwd=pkg_path) list_result = script.pip('list', '--format=json') - assert {"name": "FSPkg", "version": "0.1.dev0"} \ - in json.loads(list_result.stdout) + assert { + "name": "FSPkg", + "version": "0.1.dev0", + "location": str(script.site_packages_path), + } in json.loads(list_result.stdout) # Uninstall both develop and install uninstall = script.pip('uninstall', 'FSPkg', '-y') assert not any(filename.endswith('.egg-link') From b9dbeb99ab17ea3abb29fed0c99fab50029e9bea Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Wed, 26 Feb 2020 00:03:34 +0800 Subject: [PATCH 3/3] normcase for Windows --- tests/functional/test_list.py | 8 ++++++-- tests/functional/test_uninstall.py | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/functional/test_list.py b/tests/functional/test_list.py index 099137fc27d..08beb6cad59 100644 --- a/tests/functional/test_list.py +++ b/tests/functional/test_list.py @@ -168,7 +168,9 @@ def test_uptodate_flag(script, data): assert { "name": "pip-test-package", "version": "0.1.1", - "location": str(script.venv_path / "src" / "pip-test-package"), + "location": os.path.normcase( + str(script.venv_path / "src" / "pip-test-package") + ), } in json_result assert {"name": "simple2", "version": "3.0"} in json_result @@ -233,7 +235,9 @@ def test_outdated_flag(script, data): assert { "name": "pip-test-package", "version": "0.1", - "location": str(script.venv_path / "src" / "pip-test-package"), + "location": os.path.normcase( + str(script.venv_path / "src" / "pip-test-package") + ), "latest_version": "0.1.1", "latest_filetype": "sdist", } in json.loads(result.stdout) diff --git a/tests/functional/test_uninstall.py b/tests/functional/test_uninstall.py index 7c51be105f9..34f92c0e726 100644 --- a/tests/functional/test_uninstall.py +++ b/tests/functional/test_uninstall.py @@ -506,12 +506,12 @@ def test_uninstall_setuptools_develop_install(script, data): script.run('python', 'setup.py', 'install', expect_stderr=True, cwd=pkg_path) list_result = script.pip('list', '--format=json') + egg_name = "FSPkg-0.1.dev0-py{0}.{1}.egg".format(*sys.version_info) assert { "name": os.path.normcase("FSPkg"), "version": "0.1.dev0", - "location": str( - script.site_packages_path / - "FSPkg-0.1.dev0-py{0}.{1}.egg".format(*sys.version_info) + "location": os.path.normcase( + str(script.site_packages_path / egg_name) ), } in json.loads(list_result.stdout), str(list_result) # Uninstall both develop and install @@ -543,7 +543,7 @@ def test_uninstall_editable_and_pip_install(script, data): assert { "name": "FSPkg", "version": "0.1.dev0", - "location": str(script.site_packages_path), + "location": os.path.normcase(str(script.site_packages_path)), } in json.loads(list_result.stdout) # Uninstall both develop and install uninstall = script.pip('uninstall', 'FSPkg', '-y')