Skip to content

Commit 86e8f5d

Browse files
committed
Move assert_[not_]installed to Script and use it
This help function is much better than the previous ad-hoc logic used in test_uninstall.py, which has trouble identifying normalized names.
1 parent b292f1b commit 86e8f5d

File tree

4 files changed

+42
-72
lines changed

4 files changed

+42
-72
lines changed

tests/functional/test_new_resolver.py

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import json
21
import os
32
import sys
43
import textwrap
54

65
import pytest
7-
from pip._vendor.packaging.utils import canonicalize_name
86

97
from tests.lib import (
108
create_basic_sdist_for_package,
@@ -17,26 +15,14 @@
1715
from tests.lib.wheel import make_wheel
1816

1917

18+
# TODO: Remove me.
2019
def assert_installed(script, **kwargs):
21-
ret = script.pip('list', '--format=json')
22-
installed = {
23-
(canonicalize_name(val['name']), val['version'])
24-
for val in json.loads(ret.stdout)
25-
}
26-
expected = {(canonicalize_name(k), v) for k, v in kwargs.items()}
27-
assert expected <= installed, f"{expected!r} not all in {installed!r}"
20+
script.assert_installed(**kwargs)
2821

2922

23+
# TODO: Remove me.
3024
def assert_not_installed(script, *args):
31-
ret = script.pip("list", "--format=json")
32-
installed = {
33-
canonicalize_name(val["name"])
34-
for val in json.loads(ret.stdout)
35-
}
36-
# None of the given names should be listed as installed, i.e. their
37-
# intersection should be empty.
38-
expected = {canonicalize_name(k) for k in args}
39-
assert not (expected & installed), f"{expected!r} contained in {installed!r}"
25+
script.assert_not_installed(*args)
4026

4127

4228
def assert_editable(script, *args):

tests/functional/test_new_resolver_hashes.py

+2-28
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import collections
22
import hashlib
3-
import json
43

54
import pytest
6-
from pip._vendor.packaging.utils import canonicalize_name
75

86
from pip._internal.utils.urls import path_to_url
97
from tests.lib import create_basic_sdist_for_package, create_basic_wheel_for_package
@@ -13,30 +11,6 @@
1311
)
1412

1513

16-
def assert_installed(script, **kwargs):
17-
ret = script.pip('list', '--format=json')
18-
installed = set(
19-
(canonicalize_name(val['name']), val['version'])
20-
for val in json.loads(ret.stdout)
21-
)
22-
expected = set((canonicalize_name(k), v) for k, v in kwargs.items())
23-
assert expected <= installed, \
24-
"{!r} not all in {!r}".format(expected, installed)
25-
26-
27-
def assert_not_installed(script, *args):
28-
ret = script.pip("list", "--format=json")
29-
installed = set(
30-
canonicalize_name(val["name"])
31-
for val in json.loads(ret.stdout)
32-
)
33-
# None of the given names should be listed as installed, i.e. their
34-
# intersection should be empty.
35-
expected = set(canonicalize_name(k) for k in args)
36-
assert not (expected & installed), \
37-
"{!r} contained in {!r}".format(expected, installed)
38-
39-
4014
def _create_find_links(script):
4115
sdist_path = create_basic_sdist_for_package(script, "base", "0.1.0")
4216
wheel_path = create_basic_wheel_for_package(script, "base", "0.1.0")
@@ -265,7 +239,7 @@ def test_new_resolver_hash_requirement_and_url_constraint_can_succeed(
265239
"--requirement", requirements_txt,
266240
)
267241

268-
assert_installed(script, base="0.1.0")
242+
script.assert_installed(base="0.1.0")
269243

270244

271245
@pytest.mark.parametrize("constrain_by_hash", [False, True])
@@ -307,4 +281,4 @@ def test_new_resolver_hash_requirement_and_url_constraint_can_fail(
307281
"THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE."
308282
) in result.stderr, str(result)
309283

310-
assert_not_installed(script, "base", "other")
284+
script.assert_not_installed("base", "other")

tests/functional/test_uninstall.py

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import logging
32
import os
43
import sys
@@ -46,8 +45,7 @@ def test_basic_uninstall_distutils(script):
4645
"""))
4746
result = script.run('python', pkg_path / 'setup.py', 'install')
4847
result = script.pip('list', '--format=json')
49-
assert {"name": "distutils-install", "version": "0.1"} \
50-
in json.loads(result.stdout)
48+
script.assert_installed(distutils_install="0.1")
5149
result = script.pip('uninstall', 'distutils_install', '-y',
5250
expect_stderr=True, expect_error=True)
5351
assert (
@@ -217,16 +215,13 @@ def test_uninstall_entry_point_colon_in_name(script, console_scripts):
217215
)
218216
if sys.platform == 'win32':
219217
script_name += '.exe'
220-
result = script.pip('install', pkg_path)
218+
script.pip('install', pkg_path)
221219
assert script_name.exists()
222-
result = script.pip('list', '--format=json')
223-
assert {"name": "ep-install", "version": "0.1"} \
224-
in json.loads(result.stdout)
220+
script.assert_installed(ep_install="0.1")
221+
225222
script.pip('uninstall', 'ep_install', '-y')
226223
assert not script_name.exists()
227-
result2 = script.pip('list', '--format=json')
228-
assert {"name": "ep-install", "version": "0.1"} \
229-
not in json.loads(result2.stdout)
224+
script.assert_not_installed("ep-install")
230225

231226

232227
def test_uninstall_gui_scripts(script):
@@ -550,9 +545,7 @@ def test_uninstall_setuptools_develop_install(script, data):
550545
expect_stderr=True, cwd=pkg_path)
551546
script.run('python', 'setup.py', 'install',
552547
expect_stderr=True, cwd=pkg_path)
553-
list_result = script.pip('list', '--format=json')
554-
assert {"name": os.path.normcase("FSPkg"), "version": "0.1.dev0"} \
555-
in json.loads(list_result.stdout), str(list_result)
548+
script.assert_installed(FSPkg="0.1.dev0")
556549
# Uninstall both develop and install
557550
uninstall = script.pip('uninstall', 'FSPkg', '-y')
558551
assert any(filename.endswith('.egg')
@@ -561,8 +554,7 @@ def test_uninstall_setuptools_develop_install(script, data):
561554
assert join(
562555
script.site_packages, 'FSPkg.egg-link'
563556
) in uninstall2.files_deleted, list(uninstall2.files_deleted.keys())
564-
list_result2 = script.pip('list', '--format=json')
565-
assert "FSPkg" not in {p["name"] for p in json.loads(list_result2.stdout)}
557+
script.assert_not_installed("FSPkg")
566558

567559

568560
def test_uninstall_editable_and_pip_install(script, data):
@@ -578,9 +570,7 @@ def test_uninstall_editable_and_pip_install(script, data):
578570
# ensure both are installed with --ignore-installed:
579571
script.pip('install', '--ignore-installed', '.',
580572
expect_stderr=True, cwd=pkg_path)
581-
list_result = script.pip('list', '--format=json')
582-
assert {"name": "FSPkg", "version": "0.1.dev0"} \
583-
in json.loads(list_result.stdout)
573+
script.assert_installed(FSPkg="0.1.dev0")
584574
# Uninstall both develop and install
585575
uninstall = script.pip('uninstall', 'FSPkg', '-y')
586576
assert not any(filename.endswith('.egg-link')
@@ -589,8 +579,7 @@ def test_uninstall_editable_and_pip_install(script, data):
589579
assert join(
590580
script.site_packages, 'FSPkg.egg-link'
591581
) in uninstall2.files_deleted, list(uninstall2.files_deleted.keys())
592-
list_result2 = script.pip('list', '--format=json')
593-
assert "FSPkg" not in {p["name"] for p in json.loads(list_result2.stdout)}
582+
script.assert_not_installed("FSPkg")
594583

595584

596585
def test_uninstall_editable_and_pip_install_easy_install_remove(script, data):
@@ -616,9 +605,7 @@ def test_uninstall_editable_and_pip_install_easy_install_remove(script, data):
616605
os.rename(easy_install_pth, pip_test_fspkg_pth)
617606

618607
# Confirm that FSPkg is installed
619-
list_result = script.pip('list', '--format=json')
620-
assert {"name": "FSPkg", "version": "0.1.dev0"} \
621-
in json.loads(list_result.stdout)
608+
script.assert_installed(FSPkg="0.1.dev0")
622609

623610
# Remove pip-test-fspkg.pth
624611
os.remove(pip_test_fspkg_pth)
@@ -632,9 +619,7 @@ def test_uninstall_editable_and_pip_install_easy_install_remove(script, data):
632619
) in uninstall.files_deleted, list(uninstall.files_deleted.keys())
633620

634621
# Confirm that FSPkg is uninstalled
635-
list_result = script.pip('list', '--format=json')
636-
assert {"name": "FSPkg", "version": "0.1.dev0"} \
637-
not in json.loads(list_result.stdout)
622+
script.assert_not_installed("FSPkg")
638623

639624
# Rename pip-test.pth back to easy-install.pth
640625
os.rename(pip_test_pth, easy_install_pth)

tests/lib/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23
import re
34
import shutil
@@ -16,6 +17,8 @@
1617
import pytest
1718
from scripttest import FoundDir, TestFileEnvironment
1819

20+
from pip._vendor.packaging.utils import canonicalize_name
21+
1922
from pip._internal.index.collector import LinkCollector
2023
from pip._internal.index.package_finder import PackageFinder
2124
from pip._internal.locations import get_major_minor_version
@@ -668,6 +671,28 @@ def easy_install(self, *args, **kwargs):
668671
args = ("-m", "easy_install") + args
669672
return self.run("python", *args, **kwargs)
670673

674+
def assert_installed(self, **kwargs):
675+
ret = self.pip('list', '--format=json')
676+
installed = set(
677+
(canonicalize_name(val['name']), val['version'])
678+
for val in json.loads(ret.stdout)
679+
)
680+
expected = set((canonicalize_name(k), v) for k, v in kwargs.items())
681+
assert expected <= installed, \
682+
"{!r} not all in {!r}".format(expected, installed)
683+
684+
def assert_not_installed(self, *args):
685+
ret = self.pip("list", "--format=json")
686+
installed = set(
687+
canonicalize_name(val["name"])
688+
for val in json.loads(ret.stdout)
689+
)
690+
# None of the given names should be listed as installed, i.e. their
691+
# intersection should be empty.
692+
expected = set(canonicalize_name(k) for k in args)
693+
assert not (expected & installed), \
694+
"{!r} contained in {!r}".format(expected, installed)
695+
671696

672697
# FIXME ScriptTest does something similar, but only within a single
673698
# ProcResult; this generalizes it so states can be compared across

0 commit comments

Comments
 (0)