mirror of https://github.com/pypa/pip
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.
This commit is contained in:
parent
ef8299d559
commit
e6c317769a
|
@ -1,10 +1,8 @@
|
|||
import json
|
||||
import os
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
import pytest
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
|
||||
from tests.lib import (
|
||||
create_basic_sdist_for_package,
|
||||
|
@ -17,26 +15,14 @@ from tests.lib.path import Path
|
|||
from tests.lib.wheel import make_wheel
|
||||
|
||||
|
||||
# TODO: Remove me.
|
||||
def assert_installed(script, **kwargs):
|
||||
ret = script.pip('list', '--format=json')
|
||||
installed = {
|
||||
(canonicalize_name(val['name']), val['version'])
|
||||
for val in json.loads(ret.stdout)
|
||||
}
|
||||
expected = {(canonicalize_name(k), v) for k, v in kwargs.items()}
|
||||
assert expected <= installed, f"{expected!r} not all in {installed!r}"
|
||||
script.assert_installed(**kwargs)
|
||||
|
||||
|
||||
# TODO: Remove me.
|
||||
def assert_not_installed(script, *args):
|
||||
ret = script.pip("list", "--format=json")
|
||||
installed = {
|
||||
canonicalize_name(val["name"])
|
||||
for val in json.loads(ret.stdout)
|
||||
}
|
||||
# None of the given names should be listed as installed, i.e. their
|
||||
# intersection should be empty.
|
||||
expected = {canonicalize_name(k) for k in args}
|
||||
assert not (expected & installed), f"{expected!r} contained in {installed!r}"
|
||||
script.assert_not_installed(*args)
|
||||
|
||||
|
||||
def assert_editable(script, *args):
|
||||
|
|
|
@ -1,9 +1,7 @@
|
|||
import collections
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
|
||||
from pip._internal.utils.urls import path_to_url
|
||||
from tests.lib import create_basic_sdist_for_package, create_basic_wheel_for_package
|
||||
|
@ -13,30 +11,6 @@ _FindLinks = collections.namedtuple(
|
|||
)
|
||||
|
||||
|
||||
def assert_installed(script, **kwargs):
|
||||
ret = script.pip('list', '--format=json')
|
||||
installed = set(
|
||||
(canonicalize_name(val['name']), val['version'])
|
||||
for val in json.loads(ret.stdout)
|
||||
)
|
||||
expected = set((canonicalize_name(k), v) for k, v in kwargs.items())
|
||||
assert expected <= installed, \
|
||||
"{!r} not all in {!r}".format(expected, installed)
|
||||
|
||||
|
||||
def assert_not_installed(script, *args):
|
||||
ret = script.pip("list", "--format=json")
|
||||
installed = set(
|
||||
canonicalize_name(val["name"])
|
||||
for val in json.loads(ret.stdout)
|
||||
)
|
||||
# None of the given names should be listed as installed, i.e. their
|
||||
# intersection should be empty.
|
||||
expected = set(canonicalize_name(k) for k in args)
|
||||
assert not (expected & installed), \
|
||||
"{!r} contained in {!r}".format(expected, installed)
|
||||
|
||||
|
||||
def _create_find_links(script):
|
||||
sdist_path = create_basic_sdist_for_package(script, "base", "0.1.0")
|
||||
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(
|
|||
"--requirement", requirements_txt,
|
||||
)
|
||||
|
||||
assert_installed(script, base="0.1.0")
|
||||
script.assert_installed(base="0.1.0")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("constrain_by_hash", [False, True])
|
||||
|
@ -307,4 +281,4 @@ def test_new_resolver_hash_requirement_and_url_constraint_can_fail(
|
|||
"THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE."
|
||||
) in result.stderr, str(result)
|
||||
|
||||
assert_not_installed(script, "base", "other")
|
||||
script.assert_not_installed("base", "other")
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
@ -46,8 +45,7 @@ def test_basic_uninstall_distutils(script):
|
|||
"""))
|
||||
result = script.run('python', pkg_path / 'setup.py', 'install')
|
||||
result = script.pip('list', '--format=json')
|
||||
assert {"name": "distutils-install", "version": "0.1"} \
|
||||
in json.loads(result.stdout)
|
||||
script.assert_installed(distutils_install="0.1")
|
||||
result = script.pip('uninstall', 'distutils_install', '-y',
|
||||
expect_stderr=True, expect_error=True)
|
||||
assert (
|
||||
|
@ -217,16 +215,13 @@ def test_uninstall_entry_point_colon_in_name(script, console_scripts):
|
|||
)
|
||||
if sys.platform == 'win32':
|
||||
script_name += '.exe'
|
||||
result = script.pip('install', pkg_path)
|
||||
script.pip('install', pkg_path)
|
||||
assert script_name.exists()
|
||||
result = script.pip('list', '--format=json')
|
||||
assert {"name": "ep-install", "version": "0.1"} \
|
||||
in json.loads(result.stdout)
|
||||
script.assert_installed(ep_install="0.1")
|
||||
|
||||
script.pip('uninstall', 'ep_install', '-y')
|
||||
assert not script_name.exists()
|
||||
result2 = script.pip('list', '--format=json')
|
||||
assert {"name": "ep-install", "version": "0.1"} \
|
||||
not in json.loads(result2.stdout)
|
||||
script.assert_not_installed("ep-install")
|
||||
|
||||
|
||||
def test_uninstall_gui_scripts(script):
|
||||
|
@ -550,9 +545,7 @@ def test_uninstall_setuptools_develop_install(script, data):
|
|||
expect_stderr=True, cwd=pkg_path)
|
||||
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)
|
||||
script.assert_installed(FSPkg="0.1.dev0")
|
||||
# Uninstall both develop and install
|
||||
uninstall = script.pip('uninstall', 'FSPkg', '-y')
|
||||
assert any(filename.endswith('.egg')
|
||||
|
@ -561,8 +554,7 @@ def test_uninstall_setuptools_develop_install(script, data):
|
|||
assert join(
|
||||
script.site_packages, 'FSPkg.egg-link'
|
||||
) in uninstall2.files_deleted, list(uninstall2.files_deleted.keys())
|
||||
list_result2 = script.pip('list', '--format=json')
|
||||
assert "FSPkg" not in {p["name"] for p in json.loads(list_result2.stdout)}
|
||||
script.assert_not_installed("FSPkg")
|
||||
|
||||
|
||||
def test_uninstall_editable_and_pip_install(script, data):
|
||||
|
@ -578,9 +570,7 @@ def test_uninstall_editable_and_pip_install(script, data):
|
|||
# ensure both are installed with --ignore-installed:
|
||||
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)
|
||||
script.assert_installed(FSPkg="0.1.dev0")
|
||||
# Uninstall both develop and install
|
||||
uninstall = script.pip('uninstall', 'FSPkg', '-y')
|
||||
assert not any(filename.endswith('.egg-link')
|
||||
|
@ -589,8 +579,7 @@ def test_uninstall_editable_and_pip_install(script, data):
|
|||
assert join(
|
||||
script.site_packages, 'FSPkg.egg-link'
|
||||
) in uninstall2.files_deleted, list(uninstall2.files_deleted.keys())
|
||||
list_result2 = script.pip('list', '--format=json')
|
||||
assert "FSPkg" not in {p["name"] for p in json.loads(list_result2.stdout)}
|
||||
script.assert_not_installed("FSPkg")
|
||||
|
||||
|
||||
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):
|
|||
os.rename(easy_install_pth, pip_test_fspkg_pth)
|
||||
|
||||
# Confirm that FSPkg is installed
|
||||
list_result = script.pip('list', '--format=json')
|
||||
assert {"name": "FSPkg", "version": "0.1.dev0"} \
|
||||
in json.loads(list_result.stdout)
|
||||
script.assert_installed(FSPkg="0.1.dev0")
|
||||
|
||||
# Remove pip-test-fspkg.pth
|
||||
os.remove(pip_test_fspkg_pth)
|
||||
|
@ -632,9 +619,7 @@ def test_uninstall_editable_and_pip_install_easy_install_remove(script, data):
|
|||
) in uninstall.files_deleted, list(uninstall.files_deleted.keys())
|
||||
|
||||
# Confirm that FSPkg is uninstalled
|
||||
list_result = script.pip('list', '--format=json')
|
||||
assert {"name": "FSPkg", "version": "0.1.dev0"} \
|
||||
not in json.loads(list_result.stdout)
|
||||
script.assert_not_installed("FSPkg")
|
||||
|
||||
# Rename pip-test.pth back to easy-install.pth
|
||||
os.rename(pip_test_pth, easy_install_pth)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
|
@ -14,6 +15,7 @@ from typing import List, Optional
|
|||
from zipfile import ZipFile
|
||||
|
||||
import pytest
|
||||
from pip._vendor.packaging.utils import canonicalize_name
|
||||
from scripttest import FoundDir, TestFileEnvironment
|
||||
|
||||
from pip._internal.index.collector import LinkCollector
|
||||
|
@ -668,6 +670,27 @@ class PipTestEnvironment(TestFileEnvironment):
|
|||
args = ("-m", "easy_install") + args
|
||||
return self.run("python", *args, **kwargs)
|
||||
|
||||
def assert_installed(self, **kwargs):
|
||||
ret = self.pip("list", "--format=json")
|
||||
installed = set(
|
||||
(canonicalize_name(val["name"]), val["version"])
|
||||
for val in json.loads(ret.stdout)
|
||||
)
|
||||
expected = set((canonicalize_name(k), v) for k, v in kwargs.items())
|
||||
assert expected <= installed, "{!r} not all in {!r}".format(expected, installed)
|
||||
|
||||
def assert_not_installed(self, *args):
|
||||
ret = self.pip("list", "--format=json")
|
||||
installed = set(
|
||||
canonicalize_name(val["name"]) for val in json.loads(ret.stdout)
|
||||
)
|
||||
# None of the given names should be listed as installed, i.e. their
|
||||
# intersection should be empty.
|
||||
expected = set(canonicalize_name(k) for k in args)
|
||||
assert not (expected & installed), "{!r} contained in {!r}".format(
|
||||
expected, installed
|
||||
)
|
||||
|
||||
|
||||
# FIXME ScriptTest does something similar, but only within a single
|
||||
# ProcResult; this generalizes it so states can be compared across
|
||||
|
|
Loading…
Reference in New Issue