Fix pkg_resources exception handling for py312

The exception that is generated on parsing errors has changed in Python
3.12, match on the generic ValueError instead. Also switch to using
the simpler assertRaises() call instead of explicitly dealing with
exceptions.

Note that this is just a temporary workaround, we should really move
away for using pkg_resources completely, see [0] and [1].

[0] https://setuptools.pypa.io/en/latest/pkg_resources.html
[1] https://importlib-resources.readthedocs.io/en/latest/migration.html

Change-Id: I20422194f6fc6d1c4ba49da754a2c3aab99a282c
This commit is contained in:
Dr. Jens Harbott 2024-06-18 07:52:07 +02:00 committed by Jens Harbott
parent b557b9580a
commit c7d98564d0

View File

@ -12,8 +12,6 @@
import textwrap
import pkg_resources
import pkg_resources.extern.packaging.requirements as pkg_resources_reqs
import testscenarios
import testtools
@ -107,13 +105,7 @@ class TestParseRequirementFailures(testtools.TestCase):
('-f', dict(line='-f http://tarballs.openstack.org/'))]
def test_does_not_parse(self):
try:
requirement.parse_line(self.line)
except (pkg_resources.RequirementParseError,
pkg_resources_reqs.InvalidRequirement):
pass
else:
self.fail('No exception triggered')
self.assertRaises(ValueError, requirement.parse_line, self.line)
class TestToContent(testtools.TestCase):
@ -153,13 +145,8 @@ class TestToReqs(testtools.TestCase):
self.assertEqual(reqs, [(req, line)])
def test_not_urls(self):
try:
list(requirement.to_reqs('file:///foo#egg=foo'))
except (pkg_resources.RequirementParseError,
pkg_resources_reqs.InvalidRequirement):
pass
else:
self.fail('No exception triggered')
self.assertRaises(
ValueError, list, requirement.to_reqs('file:///foo#egg=foo'))
def test_multiline(self):
content = textwrap.dedent("""\