Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assert_raises_regexp function with pytest.raises #10413

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
13 changes: 0 additions & 13 deletions tests/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,19 +991,6 @@ def _change_test_package_version(script, version_pkg_path):
_git_commit(script, version_pkg_path, message="messed version", stage_modified=True)


def assert_raises_regexp(exception, reg, run, *args, **kwargs):
"""Like assertRaisesRegexp in unittest"""
__tracebackhide__ = True

try:
run(*args, **kwargs)
assert False, f"{exception} should have been thrown"
except exception:
e = sys.exc_info()[1]
p = re.compile(reg)
assert p.search(str(e)), str(e)


@contextmanager
def requirements_file(contents, tmpdir):
"""Return a Path to a requirements file of given contents.
Expand Down
112 changes: 56 additions & 56 deletions tests/unit/test_req.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from pip._internal.req.req_tracker import get_requirement_tracker
from pip._internal.resolution.legacy.resolver import Resolver
from pip._internal.utils.urls import path_to_url
from tests.lib import assert_raises_regexp, make_test_finder, requirements_file
from tests.lib import make_test_finder, requirements_file


def get_processed_req_from_line(line, fname="file", lineno=1):
Expand Down Expand Up @@ -117,15 +117,15 @@ def test_no_reuse_existing_build_dir(self, data):
reqset.add_requirement(req)
finder = make_test_finder(find_links=[data.find_links])
with self._basic_resolver(finder) as resolver:
assert_raises_regexp(
with pytest.raises(
PreviousBuildDirError,
r"pip can't proceed with [\s\S]*{req}[\s\S]*{build_dir_esc}".format(
build_dir_esc=build_dir.replace("\\", "\\\\"), req=req
match=(
r"pip can't proceed with [\s\S]*{req}[\s\S]*{build_dir_esc}".format(
build_dir_esc=build_dir.replace("\\", "\\\\"), req=req
)
),
resolver.resolve,
reqset.all_requirements,
True,
)
):
resolver.resolve(reqset.all_requirements, True)

def test_environment_marker_extras(self, data):
"""
Expand All @@ -151,16 +151,16 @@ def test_missing_hash_with_require_hashes(self, data):
finder = make_test_finder(find_links=[data.find_links])

with self._basic_resolver(finder, require_hashes=True) as resolver:
assert_raises_regexp(
with pytest.raises(
HashErrors,
r"Hashes are required in --require-hashes mode, but they are "
r"missing .*\n"
r" simple==1.0 --hash=sha256:393043e672415891885c9a2a0929b1"
r"af95fb866d6ca016b42d2e6ce53619b653$",
resolver.resolve,
reqset.all_requirements,
True,
)
match=(
r"Hashes are required in --require-hashes mode, but they are "
r"missing .*\n"
r" simple==1.0 --hash=sha256:393043e672415891885c9a2a0929b1"
r"af95fb866d6ca016b42d2e6ce53619b653$"
),
):
resolver.resolve(reqset.all_requirements, True)

def test_missing_hash_with_require_hashes_in_reqs_file(self, data, tmpdir):
"""--require-hashes in a requirements file should make its way to the
Expand Down Expand Up @@ -203,20 +203,20 @@ def test_unsupported_hashes(self, data):
sep = "\\\\" # This needs to be escaped for the regex

with self._basic_resolver(finder, require_hashes=True) as resolver:
assert_raises_regexp(
with pytest.raises(
HashErrors,
r"Can't verify hashes for these requirements because we don't "
r"have a way to hash version control repositories:\n"
r" git\+git://github\.com/pypa/pip-test-package \(from -r "
r"file \(line 1\)\)\n"
r"Can't verify hashes for these file:// requirements because "
r"they point to directories:\n"
r" file://.*{sep}data{sep}packages{sep}FSPkg "
r"\(from -r file \(line 2\)\)".format(sep=sep),
resolver.resolve,
reqset.all_requirements,
True,
)
match=(
r"Can't verify hashes for these requirements because we don't "
r"have a way to hash version control repositories:\n"
r" git\+git://github\.com/pypa/pip-test-package \(from -r "
r"file \(line 1\)\)\n"
r"Can't verify hashes for these file:// requirements because "
r"they point to directories:\n"
r" file://.*{sep}data{sep}packages{sep}FSPkg "
r"\(from -r file \(line 2\)\)".format(sep=sep)
),
):
resolver.resolve(reqset.all_requirements, True)

def test_unpinned_hash_checking(self, data):
"""Make sure prepare_files() raises an error when a requirement is not
Expand All @@ -241,16 +241,16 @@ def test_unpinned_hash_checking(self, data):
)
finder = make_test_finder(find_links=[data.find_links])
with self._basic_resolver(finder, require_hashes=True) as resolver:
assert_raises_regexp(
with pytest.raises(
HashErrors,
# Make sure all failing requirements are listed:
r"versions pinned with ==. These do not:\n"
r" simple .* \(from -r file \(line 1\)\)\n"
r" simple2>1.0 .* \(from -r file \(line 2\)\)",
resolver.resolve,
reqset.all_requirements,
True,
)
match=(
r"versions pinned with ==. These do not:\n"
r" simple .* \(from -r file \(line 1\)\)\n"
r" simple2>1.0 .* \(from -r file \(line 2\)\)"
),
):
resolver.resolve(reqset.all_requirements, True)

def test_hash_mismatch(self, data):
"""A hash mismatch should raise an error."""
Expand All @@ -264,17 +264,17 @@ def test_hash_mismatch(self, data):
)
finder = make_test_finder(find_links=[data.find_links])
with self._basic_resolver(finder, require_hashes=True) as resolver:
assert_raises_regexp(
with pytest.raises(
HashErrors,
r"THESE PACKAGES DO NOT MATCH THE HASHES.*\n"
r" file:///.*/data/packages/simple-1\.0\.tar\.gz .*:\n"
r" Expected sha256 badbad\n"
r" Got 393043e672415891885c9a2a0929b1af95fb"
r"866d6ca016b42d2e6ce53619b653$",
resolver.resolve,
reqset.all_requirements,
True,
)
match=(
r"THESE PACKAGES DO NOT MATCH THE HASHES.*\n"
r" file:///.*/data/packages/simple-1\.0\.tar\.gz .*:\n"
r" Expected sha256 badbad\n"
r" Got 393043e672415891885c9a2a0929b1af95fb"
r"866d6ca016b42d2e6ce53619b653$"
),
):
resolver.resolve(reqset.all_requirements, True)

def test_unhashed_deps_on_require_hashes(self, data):
"""Make sure unhashed, unpinned, or otherwise unrepeatable
Expand All @@ -291,15 +291,15 @@ def test_unhashed_deps_on_require_hashes(self, data):
)

with self._basic_resolver(finder, require_hashes=True) as resolver:
assert_raises_regexp(
with pytest.raises(
HashErrors,
r"In --require-hashes mode, all requirements must have their "
r"versions pinned.*\n"
r" TopoRequires from .*$",
resolver.resolve,
reqset.all_requirements,
True,
)
match=(
r"In --require-hashes mode, all requirements must have their "
r"versions pinned.*\n"
r" TopoRequires from .*$"
),
):
resolver.resolve(reqset.all_requirements, True)

def test_hashed_deps_on_require_hashes(self):
"""Make sure hashed dependencies get installed when --require-hashes
Expand Down