Blob Blame History Raw
From 2bd726d20f3e5d1502a2191d0d263d0b6132982b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Nosek?= <onosek@redhat.com>
Date: Thu, 12 Oct 2023 01:40:36 +0200
Subject: [PATCH] Unittests for "Undo rpmautospec processing"
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Merges: https://pagure.io/rpkg/pull-request/699
Signed-off-by: Ondřej Nosek <onosek@redhat.com>
---
 Jenkinsfile                                   |  2 +-
 jenkins_test.dockerfile                       |  3 +-
 pyrpkg/utils.py                               |  6 +--
 tests/fixtures/docpkg/docpkg-rpmautospec.spec | 53 +++++++++++++++++++
 tests/test_cli.py                             | 40 +++++++-------
 tests/test_utils.py                           | 36 ++++++++++++-
 6 files changed, 116 insertions(+), 24 deletions(-)
 create mode 100644 tests/fixtures/docpkg/docpkg-rpmautospec.spec

diff --git a/Jenkinsfile b/Jenkinsfile
index 7b6742f..e6d6394 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -50,7 +50,7 @@ git fetch proposed
 git checkout "origin/$params.BRANCH_TO"
 git merge --no-ff "proposed/$params.BRANCH" -m "Merge PR"
 
-podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/rpkg-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/rpkg-test:latest tox -e py36,py39,py311,flake8,bandit --workdir /tmp/tox ${TOX_POSARGS}
 # disabled py27 environment for now; keep just flake8 for Python 2
 podman run --rm -v .:/src:Z quay.io/exd-guild-source-tools/rpkg-test-py2:latest tox -e flake8python2 --workdir /tmp/tox ${TOX_POSARGS}
                         """
diff --git a/jenkins_test.dockerfile b/jenkins_test.dockerfile
index df8762e..d217a55 100644
--- a/jenkins_test.dockerfile
+++ b/jenkins_test.dockerfile
@@ -1,4 +1,4 @@
-FROM fedora:37
+FROM fedora:38
 LABEL \
     name="rpkg test" \
     description="Run tests using tox with Python 3" \
@@ -9,6 +9,7 @@ RUN dnf -y update && dnf -y install \
         python3-devel \
         python3-openidc-client \
         python3-libmodulemd \
+        python3-rpmautospec \
         python3-setuptools \
         rpmlint \
         rpm-build \
diff --git a/pyrpkg/utils.py b/pyrpkg/utils.py
index f3c8b25..fc01c7d 100644
--- a/pyrpkg/utils.py
+++ b/pyrpkg/utils.py
@@ -104,7 +104,7 @@ def log_result(log_func, result, level=0, indent=2):
     elif isinstance(result, dict):
         for key, value in result.items():
             _log_value(log_func, key, level, indent, ':')
-            log_result(log_func, value, level+1)
+            log_result(log_func, value, level + 1)
     else:
         _log_value(log_func, result, level, indent)
 
@@ -348,9 +348,9 @@ def _replace_lines(lines, startline, endline, replacement_lines=None, strip_endl
     else:
         # rpmautospec adds an empty line after the end
         # we want to remove it, but only if it is actually empty
-        if strip_endline and lines[end+1] == "\n":
+        if strip_endline and lines[end + 1] == "\n":
             end += 1
-        lines = lines[:start] + replacement_lines + lines[end+1:]
+        lines = lines[:start] + replacement_lines + lines[end + 1:]
         return lines, True
 
 
diff --git a/tests/fixtures/docpkg/docpkg-rpmautospec.spec b/tests/fixtures/docpkg/docpkg-rpmautospec.spec
new file mode 100644
index 0000000..5974b57
--- /dev/null
+++ b/tests/fixtures/docpkg/docpkg-rpmautospec.spec
@@ -0,0 +1,53 @@
+## START: Set by rpmautospec
+## (rpmautospec version 0.3.5)
+## RPMAUTOSPEC: autorelease, autochangelog
+%define autorelease(e:s:pb:n) %{?-p:0.}%{lua:
+    release_number = 8;
+    base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));
+    print(release_number + base_release_number - 1);
+}%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}
+## END: Set by rpmautospec
+
+# autogenerated specfile
+Summary: Dummy summary
+Name: docpkg-rpmautospec
+Version: 0.2
+Release: 1%{dist}
+License: GPL
+Group: Applications/Productivity
+
+BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
+Source0: hello-world.txt
+Source1: docpkg.tar.gz
+Source2: source-without-extension
+# added empty dir just to test import srpm `fedpkg import`. It should skip the dir.
+Source3: EMPTY_DIR
+
+%description
+This is a dummy description.
+
+%prep
+cp %{SOURCE0} .
+
+%build
+
+%clean
+rm -rf $$RPM_BUILD_ROOT
+%install
+rm -rf $RPM_BUILD_ROOT
+mkdir $RPM_BUILD_ROOT
+mkdir -p $RPM_BUILD_ROOT/usr/share/doc
+cp %{SOURCE0} $RPM_BUILD_ROOT/usr/share/doc/hello-world.txt
+
+%files
+%doc "/usr/share/doc/hello-world.txt"
+
+%changelog
+## START: Generated by rpmautospec
+* Sun Jan  1 2006 tester <tester@example.com> - 0.2-1
+- - New release 0.2-1
+
+* Sun Jan  1 2006 John Doe <jdoe@example.com> - 0.2-1
+- Initial version
+
+## END: Generated by rpmautospec
diff --git a/tests/test_cli.py b/tests/test_cli.py
index f2e68df..3d90f36 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -1706,10 +1706,10 @@ class TestFailureImportSrpm(CliTestCase):
 class TestImportSrpm(LookasideCacheMock, CliTestCase):
 
     @staticmethod
-    def build_srpm(srcrpmdir):
+    def build_srpm(srcrpmdir, specfile_name='docpkg.spec'):
         """Build a fake SRPM used by this test case"""
         docpkg_dir = os.path.join(fixtures_dir, 'docpkg')
-        specfile = os.path.join(docpkg_dir, 'docpkg.spec')
+        specfile = os.path.join(docpkg_dir, specfile_name)
         rpmbuild = [
             'rpmbuild', '-bs',
             '--define', '_topdir {0}'.format(srcrpmdir),
@@ -1735,6 +1735,9 @@ class TestImportSrpm(LookasideCacheMock, CliTestCase):
 
         self.srcrpmdir = tempfile.mkdtemp(prefix='test-import-srpm-topdir-')
         self.srpm_file = TestImportSrpm.build_srpm(self.srcrpmdir)
+        self.srpm_file_rpmautospec = TestImportSrpm.build_srpm(
+            self.srcrpmdir,
+            specfile_name='docpkg-rpmautospec.spec')
 
         self.chaos_repo = tempfile.mkdtemp(prefix='rpkg-tests-chaos-repo-')
         cmds = (
@@ -1827,40 +1830,29 @@ class TestImportSrpm(LookasideCacheMock, CliTestCase):
         self.assertFilesExist(['package.rpmlintrc'], search_dir=self.chaos_repo)
         self.assertFilesNotExist(['the_file_is_not_in_reserved.yaml'], search_dir=self.chaos_repo)
 
-    @patch('pyrpkg.spec_file_processed_by_rpmautospec')
-    def test_import_srpm_not_processed_by_rpmautospec(self, rpmautospec_processed):
+    def test_import_srpm_not_processed_by_rpmautospec(self):
         cli_cmd = ['rpkg', '--path', self.chaos_repo, '--name', 'docpkg',
                    'import', '--skip-diffs', self.srpm_file]
 
-        rpmautospec_processed.return_value = False
         with patch('sys.argv', new=cli_cmd):
             cli = self.new_cli()
             with patch('pyrpkg.lookaside.CGILookasideCache.upload', self.lookasidecache_upload):
                 cli.import_srpm()  # no exception should be raised
-            rpmautospec_processed.assert_called_once()
 
-    @patch('pyrpkg.spec_file_processed_by_rpmautospec')
-    def test_import_srpm_processed_by_rpmautospec(self, rpmautospec_processed):
+    def test_import_srpm_processed_by_rpmautospec(self):
         cli_cmd = ['rpkg', '--path', self.chaos_repo, '--name', 'docpkg',
-                   'import', '--skip-diffs', self.srpm_file]
+                   'import', '--skip-diffs', self.srpm_file_rpmautospec]
 
-        rpmautospec_processed.return_value = True
         with patch('sys.argv', new=cli_cmd):
             cli = self.new_cli()
             with patch('pyrpkg.lookaside.CGILookasideCache.upload', self.lookasidecache_upload):
-                six.assertRaisesRegex(
-                    self,
-                    rpkgError,
-                    'SRPM was processed by rpmautospec',
-                    cli.import_srpm)
-            rpmautospec_processed.assert_called_once()
+                cli.import_srpm()
 
-    @patch('pyrpkg.spec_file_processed_by_rpmautospec')
+    @patch('pyrpkg.spec_file_undo_rpmautospec')
     def test_import_srpm_processed_by_rpmautospec_allowed(self, rpmautospec_processed):
         cli_cmd = ['rpkg', '--path', self.chaos_repo, '--name', 'docpkg',
                    'import', '--skip-diffs', self.srpm_file]
 
-        rpmautospec_processed.return_value = True
         with patch('sys.argv', new=cli_cmd):
             cli = self.new_cli()
             cli.cmd.allow_pre_generated_srpm = True
@@ -1868,6 +1860,18 @@ class TestImportSrpm(LookasideCacheMock, CliTestCase):
                 cli.import_srpm()  # no exception should be raised
             rpmautospec_processed.assert_not_called()
 
+    @patch('pyrpkg.spec_file_undo_rpmautospec')
+    def test_import_srpm_processed_by_rpmautospec_not_allowed(self, rpmautospec_processed):
+        cli_cmd = ['rpkg', '--path', self.chaos_repo, '--name', 'docpkg',
+                   'import', '--skip-diffs', self.srpm_file]
+
+        with patch('sys.argv', new=cli_cmd):
+            cli = self.new_cli()
+            cli.cmd.allow_pre_generated_srpm = False  # or None
+            with patch('pyrpkg.lookaside.CGILookasideCache.upload', self.lookasidecache_upload):
+                cli.import_srpm()  # no exception should be raised
+            rpmautospec_processed.assert_called_once()
+
 
 class TestMockbuild(CliTestCase):
     """Test mockbuild command"""
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 34188c5..0cd62d8 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,15 +1,22 @@
 import os
+import shutil
 import tempfile
 import unittest
 import warnings
 
+try:
+    import rpmautospec
+except ImportError:
+    rpmautospec = None
+
 try:
     from unittest import mock
 except ImportError:
     import mock
 
 from pyrpkg.utils import (cached_property, is_file_in_directory,
-                          is_file_tracked, log_result, warn_deprecated)
+                          is_file_tracked, log_result,
+                          spec_file_undo_rpmautospec, warn_deprecated)
 
 from utils import CommandTestCase
 
@@ -287,3 +294,30 @@ class FileTrackedTestCase(CommandTestCase):
                 self.repo_path
             )
         )
+
+
+@unittest.skipIf(
+    rpmautospec is None,
+    "Skip test on releases where rpmautospec is not available (RHEL)")
+class SpecFileUndoRpmautospec(CommandTestCase):
+    def test_remove_autospec_from_specfile(self):
+        fixtures_dir = os.path.join(os.path.dirname(__file__), "fixtures", "docpkg")
+
+        specfile_without_rpmautospec = os.path.join(fixtures_dir, "docpkg.spec")
+        specfile_without_rpmautospec_copy = os.path.join(self.repo_path, "docpkg.spec")
+        shutil.copy2(specfile_without_rpmautospec, specfile_without_rpmautospec_copy)
+        self.assertFalse(rpmautospec.specfile_uses_rpmautospec(specfile_without_rpmautospec_copy))
+        self.assertFalse(spec_file_undo_rpmautospec(specfile_without_rpmautospec_copy))
+        self.assertFalse(rpmautospec.specfile_uses_rpmautospec(specfile_without_rpmautospec_copy))
+        os.remove(specfile_without_rpmautospec_copy)
+
+        specfile_with_rpmautospec = os.path.join(fixtures_dir, "docpkg-rpmautospec.spec")
+        specfile_with_rpmautospec_copy = os.path.join(self.repo_path, "docpkg-rpmautospec.spec")
+        shutil.copy2(specfile_with_rpmautospec, specfile_with_rpmautospec_copy)
+        # returns True if the specfile was modified
+        self.assertTrue(spec_file_undo_rpmautospec(specfile_with_rpmautospec_copy))
+        self.assertFalse(rpmautospec.specfile_uses_rpmautospec(
+            specfile_with_rpmautospec_copy, check_autochangelog=False, check_autorelease=True))
+        self.assertTrue(rpmautospec.specfile_uses_rpmautospec(
+            specfile_with_rpmautospec_copy, check_autochangelog=True, check_autorelease=False))
+        os.remove(specfile_with_rpmautospec_copy)
-- 
2.43.0