Repository: packit/packit
Issue: rhbz#1963155: local_build of a git branch with starts with "rpm" fails: "created successfully, but can't be found"

Source https://bugzilla.redhat.com/show_bug.cgi?id=1963155

I presume our glob/regex pattern doesn't account for this.

Changed files:
packit/upstream.py, tests/unit/test_upstream.py

diff --git a/packit/upstream.py b/packit/upstream.py
index 7e678317..00864b6a 100644
--- a/packit/upstream.py
+++ b/packit/upstream.py
@@ -617,21 +617,27 @@ class Upstream(PackitRepositoryBase):
                 f"The `rpmbuild` command failed:\\n{ex}"
             ) from ex
 
-        rpms = self._get_rpms_from_rpmbuild_output(out)
+        rpms = Upstream._get_rpms_from_rpmbuild_output(out)
         return [Path(rpm) for rpm in rpms]
 
-    def _get_rpms_from_rpmbuild_output(self, output: str) -> List[str]:
+    @staticmethod
+    def _get_rpms_from_rpmbuild_output(output: str) -> List[str]:
         """
         Try to find the rpm files in the `rpmbuild -bb` command output.
 
-        :param output: output of the `rpmbuild -bb` command
-        :return: the names of the RPM files
+        Args:
+            output: Output of the `rpmbuild -bb` command.
+
+        Returns:
+            List of names of the RPM files.
         """
         logger.debug(f"The `rpmbuild` command output: {output}")
-        reg = r": (.+\\.rpm)"
-        logger.debug(re.findall(reg, output))
+        reg = r": (\\S+\\.rpm)(\\s|$)"
         rpms = re.findall(reg, output)
 
+        rpms = [rpm for rpm, _ in rpms]
+        logger.debug(rpms)
+
         if not rpms:
             raise PackitRPMNotFoundException(
                 "RPMs cannot be found, something is wrong."
@@ -770,7 +776,7 @@ class SRPMBuilder:
         """
         logger.debug(f"The `rpmbuild` command output: {output}")
         # not doing 'Wrote: (.+)' since people can have different locales; hi Franto!
-        reg = r": (.+\\.src\\.rpm)$"
+        reg = r": (\\S+\\.src\\.rpm)$"
         try:
             the_srpm = re.findall(reg, output)[0]
         except IndexError:
diff --git a/tests/unit/test_upstream.py b/tests/unit/test_upstream.py
index d108fa3a..ee42c45b 100644
--- a/tests/unit/test_upstream.py
+++ b/tests/unit/test_upstream.py
@@ -339,3 +339,37 @@ def test_convert_version_to_tag(
     with expectation:
         upstream_mock.package_config.upstream_tag_template = tag_template
         assert upstream_mock.convert_version_to_tag(version) == expected_output
+
+
+@pytest.mark.parametrize(
+    "output, expected",
+    (
+        (
+            "Wrote: packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm",
+            [
+                "packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm"
+            ],
+        ),
+        (
+            "Processing files: python3-packit-0.37.1.dev14+g860168a.d20211004-1."
+            "20211004105435567001.rpm.regex.broken.14.g860168a.fc35.noarch\\n"
+            "\\nAnother false positive: random_rpm_named_with_space .rpm"
+            "Wrote: packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm",
+            [
+                "packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm"
+            ],
+        ),
+        (
+            "Wrote: packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm"
+            "\\n\\n\\nWrote: packit-0.38.0.rpma.fc35.noarch.rpm\\n"
+            "Wrote: packit-0.38.0.rpm_hmm.fc35.noarch.rpm\\n",
+            [
+                "packit-0.37.1.dev13+gd57da48.rpm.regex.broken.13.gd57da48.fc35.noarch.rpm",
+                "packit-0.38.0.rpma.fc35.noarch.rpm",
+                "packit-0.38.0.rpm_hmm.fc35.noarch.rpm",
+            ],
+        ),
+    ),
+)
+def test_get_rpms_from_rpmbuild_output(output, expected):
+    assert Upstream._get_rpms_from_rpmbuild_output(output) == expected