Skip to content

Commit

Permalink
Move assert_[not_]installed to Script and use it
Browse files Browse the repository at this point in the history
This help function is much better than the previous ad-hoc logic used in
test_uninstall.py, which has trouble identifying normalized names.
  • Loading branch information
uranusjr committed Jul 10, 2021
1 parent b292f1b commit 86e8f5d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 72 deletions.
22 changes: 4 additions & 18 deletions tests/functional/test_new_resolver.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,26 +15,14 @@
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):
Expand Down
30 changes: 2 additions & 28 deletions tests/functional/test_new_resolver_hashes.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,30 +11,6 @@
)


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")
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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")
37 changes: 11 additions & 26 deletions tests/functional/test_uninstall.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import os
import sys
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand All @@ -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):
Expand All @@ -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')
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import re
import shutil
Expand All @@ -16,6 +17,8 @@
import pytest
from scripttest import FoundDir, TestFileEnvironment

from pip._vendor.packaging.utils import canonicalize_name

from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
from pip._internal.locations import get_major_minor_version
Expand Down Expand Up @@ -668,6 +671,28 @@ def easy_install(self, *args, **kwargs):
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
Expand Down

0 comments on commit 86e8f5d

Please sign in to comment.