#538 Fix tests on Python 3.12 by fixing pkg_resources dependency
Opened 2 months ago by dherrera. Modified 17 hours ago
dherrera/fedpkg python312-tests  into  master

file modified
+1 -1
@@ -53,7 +53,7 @@ 

  git clone https://pagure.io/rpkg.git

  # docker image will contain ENV: PYTHONPATH=./rpkg

  

- podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test:latest tox -e py36,py39,flake8,bandit --workdir /tmp/tox ${TOX_POSARGS}

+ podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test:latest tox -e py36,py39,py312,flake8,bandit --workdir /tmp/tox ${TOX_POSARGS}

  podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test-py2:latest python2.7 -m pytest test/

  podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/fedpkg-test-py2:latest flake8 fedpkg/ test/

                          """

file modified
+1 -1
@@ -5,5 +5,5 @@ 

  tox:

  	@python3 -m venv .env

  	@.env/bin/pip install tox

- 	@.env/bin/tox -e py27,py36,py39,flake8,flake8python2 --parallel=auto ${TOX_POSARGS}

+ 	@.env/bin/tox -e py27,py36,py39,py312,flake8,flake8python2 --parallel=auto ${TOX_POSARGS}

  .PHONY: tox

file modified
+10 -2
@@ -21,7 +21,15 @@ 

  from . import cli  # noqa

  from .lookaside import FedoraLookasideCache

  from pyrpkg.utils import cached_property

- from pkg_resources import get_distribution, parse_version

+ # Use deprecated pkg_resources if importlib isn't available (python 3.6)

+ try:

+     from importlib.metadata import distribution

+ except ImportError:

+     from pkg_resources import get_distribution as distribution

+ try:

+     from packaging.version import parse as parse_version

+ except ImportError:

+     from pkg_resources import parse_version

  

  try:

      from distro import linux_distribution  # noqa
@@ -29,7 +37,7 @@ 

      from platform import linux_distribution  # noqa

  

  

- bodhi_version = get_distribution('bodhi-client').version

+ bodhi_version = distribution('bodhi-client').version

  if parse_version(bodhi_version) < parse_version("6.0.0"):

      from .bodhi_5 import BodhiClient, UPDATE_TYPES, REQUEST_TYPES, SUGGEST_TYPES

  else:

file modified
+15 -5
@@ -21,8 +21,11 @@ 

  import shutil

  import textwrap

  from datetime import datetime

- 

- import pkg_resources

+ # Use deprecated pkg_resources if importlib isn't available (python 3.6)

+ try:

+     import importlib.metadata

+ except ImportError:

+     import pkg_resources

  import six

  from pyrpkg import rpkgError

  from pyrpkg.cli import cliClient
@@ -92,10 +95,17 @@ 

  

  

  def check_bodhi_version():

+     # Use deprecated pkg_resources if importlib isn't available (python 3.6)

      try:

-         pkg_resources.get_distribution('bodhi_client')

-     except pkg_resources.DistributionNotFound:

-         raise rpkgError('bodhi-client < 2.0 is not supported.')

+         try:

+             importlib.metadata.distribution('bodhi_client')

+         except importlib.metadata.PackageNotFoundError:

+             raise rpkgError('bodhi-client < 2.0 is not supported.')

+     except NameError:

+         try:

+             pkg_resources.get_distribution('bodhi_client')

+         except pkg_resources.DistributionNotFound:

+             raise rpkgError('bodhi-client < 2.0 is not supported.')

  

  

  class fedpkgClient(cliClient):

file modified
+1 -1
@@ -22,4 +22,4 @@ 

  

  ENV PYTHONPATH "${PYTHONPATH}:./rpkg"

  

- CMD ["tox", "-e", "py36,py39,flake8,bandit"]

+ CMD ["tox", "-e", "py36,py39,py312,flake8,bandit"]

file modified
+33 -10
@@ -21,13 +21,18 @@ 

  from tempfile import mkdtemp, mkstemp

  

  import git

- import pkg_resources

+ # Use deprecated pkg_resources if packaging library isn't available (python 3.6)

+ try:

+     from packaging.version import parse as parse_version

+ except ImportError:

+     from pkg_resources import parse_version

+ 

  import six

  from six.moves import StringIO

  from six.moves.configparser import NoOptionError, NoSectionError

  

  import fedpkg.cli

- from fedpkg import parse_version, bodhi_version

+ from fedpkg import bodhi_version

  from fedpkg.bugzilla import BugzillaClient

  from fedpkg.cli import check_bodhi_version

  from freezegun import freeze_time
@@ -1609,16 +1614,34 @@ 

              self.assertEqual(str(error), expected_error)

  

  

- class TestCheckBodhiVersion(unittest.TestCase):

-     """Test check_bodhi_version"""

+ # Use deprecated pkg_resources if importlib isn't available (python 3.6)

+ try:

+     import importlib.metadata

  

-     @patch('pkg_resources.get_distribution')

-     def test_no_2_x_version_installed(self, get_distribution):

-         get_distribution.side_effect = pkg_resources.DistributionNotFound

+     class TestCheckBodhiVersion(unittest.TestCase):

+         """Test check_bodhi_version"""

  

-         six.assertRaisesRegex(

-             self, rpkgError, r'bodhi-client < 2\.0 is not supported\.',

-             check_bodhi_version)

+         @patch('importlib.metadata.distribution')

+         def test_no_2_x_version_installed(self, distribution):

+             distribution.side_effect = importlib.metadata.PackageNotFoundError

+ 

+             six.assertRaisesRegex(

+                 self, rpkgError, r'bodhi-client < 2\.0 is not supported\.',

+                 check_bodhi_version)

+ 

+ except ImportError:

+     import pkg_resources

+ 

+     class TestCheckBodhiVersion(unittest.TestCase):

+         """Test check_bodhi_version"""

+ 

+         @patch('pkg_resources.get_distribution')

+         def test_no_2_x_version_installed(self, get_distribution):

+             get_distribution.side_effect = pkg_resources.DistributionNotFound

+ 

+             six.assertRaisesRegex(

+                 self, rpkgError, r'bodhi-client < 2\.0 is not supported\.',

+                 check_bodhi_version)

  

  

  @unittest.skipUnless(bodhi, 'Skip if no supported bodhi-client is available')

file modified
+3 -2
@@ -1,5 +1,5 @@ 

  [tox]

- envlist = py27,py36,py39,py310,py311,flake8,doc,bandit

+ envlist = py27,py36,py39,py310,py311,py312,flake8,doc,bandit

  

  [testenv]

  sitepackages=false
@@ -8,7 +8,8 @@ 

      py36: {env:TOXPYTHON:python3.6}

      py39: {env:TOXPYTHON:python3.9}

      py310: {env:TOXPYTHON:python3.10}

-     py310: {env:TOXPYTHON:python3.11}

+     py311: {env:TOXPYTHON:python3.11}

+     py312: {env:TOXPYTHON:python3.12}

      flake8: {env:TOXPYTHON:python3.6}

      flake8python2: {env:TOXPYTHON:python2.7}

      doc: {env:TOXPYTHON:python3}

I've been studying fedpkg code and noticed that the tests were failing to run on python 3.12. The main cause of this is that the pkg_resources library can't be found during the testing process when testing on python 3.12.

Digging around, I found out that the thing that causes this problem is that python 3.12 stopped preinstalling the setuptools library in virtual environments created with venv [0]. This can be easily fixed by adding the setuptools requirement to the requirements.txt file, but I also noticed that the pkg_resources API being used in fedpkg was a deprecated API [1], so I changed the code so that fedpkg prioritizes using the importlib library when available instead as recommended by upstream[1].

[0] https://docs.python.org/3/whatsnew/3.12.html
[1] https://setuptools.pypa.io/en/latest/pkg_resources.html

@onosek would you mind taking a look at this PR and providing feedback?