From 98afc53431d01e1aadbcf33c48da673246029410 Mon Sep 17 00:00:00 2001
From: Diego Herrera <dherrera@redhat.com>
Date: May 28 2024 00:58:54 +0000
Subject: Fix tests on Python 3.12 by fixing pkg_resources dependency


Signed-off-by: Diego Herrera <dherrera@redhat.com>

---

diff --git a/Jenkinsfile b/Jenkinsfile
index 6c196f7..4603a84 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -53,7 +53,7 @@ git merge --no-ff "proposed/$params.BRANCH" -m "Merge PR"
 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/
                         """
diff --git a/Makefile b/Makefile
index f0008b3..4668b9a 100644
--- a/Makefile
+++ b/Makefile
@@ -5,5 +5,5 @@ test: $(default_targets)
 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
diff --git a/fedpkg/__init__.py b/fedpkg/__init__.py
index ea76877..aa84bc1 100644
--- a/fedpkg/__init__.py
+++ b/fedpkg/__init__.py
@@ -21,7 +21,15 @@ from datetime import datetime, timedelta
 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 @@ except ImportError:
     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:
diff --git a/fedpkg/cli.py b/fedpkg/cli.py
index d4ea258..d6c7715 100644
--- a/fedpkg/cli.py
+++ b/fedpkg/cli.py
@@ -21,8 +21,11 @@ import re
 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 @@ require_testcases=%(require_testcases)s
 
 
 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):
diff --git a/jenkins_test.dockerfile b/jenkins_test.dockerfile
index fdd0a50..9628fc5 100644
--- a/jenkins_test.dockerfile
+++ b/jenkins_test.dockerfile
@@ -22,4 +22,4 @@ WORKDIR /src
 
 ENV PYTHONPATH "${PYTHONPATH}:./rpkg"
 
-CMD ["tox", "-e", "py36,py39,flake8,bandit"]
+CMD ["tox", "-e", "py36,py39,py312,flake8,bandit"]
diff --git a/test/test_cli.py b/test/test_cli.py
index 0b68dc8..b6a11d1 100644
--- a/test/test_cli.py
+++ b/test/test_cli.py
@@ -21,13 +21,18 @@ from os import rmdir
 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 @@ class TestRequestTestsRepo(CliTestCase):
             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')
diff --git a/tox.ini b/tox.ini
index 2d53a26..1f5b36e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -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 @@ basepython=
     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}

