#252 Mostly, add the `-n` flag to `%autorelease` to suppress the dist tag, plus fixes and more/better testing
Merged 2 years ago by nphilipp. Opened 2 years ago by nphilipp.
fedora-infra/ nphilipp/rpmautospec main--suppress-dist-tag-etc  into  main

file modified
+1
@@ -16,6 +16,7 @@ 

  the different portions of the release field:

  

  * ``-b <baserelease>``: Allows specifying a custom base release number (i.e. other than 1).

+ * ``-n``: Don’t render the dist tag, e.g. for use in macros, if the dist tag is added later.

  

  

  .. important::

@@ -1,8 +1,8 @@ 

- %autorelease(e:s:pb:) %{?-p:0.}%{lua:

+ %autorelease(e:s:pb:n) %{?-p:0.}%{lua:

      release_number = tonumber(rpm.expand("%{?_rpmautospec_release_number}%{!?_rpmautospec_release_number:1}"));

      base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));

      print(release_number + base_release_number - 1);

- }%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{?dist}

+ }%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}

  %autochangelog %{lua:

      locale = os.setlocale(nil)

      os.setlocale("C.utf8")

file modified
+4 -2
@@ -6,9 +6,11 @@ 

  

  

  # the %autorelease macro including parameters

- AUTORELEASE_MACRO = "autorelease(e:s:pb:)"

+ AUTORELEASE_MACRO = "autorelease(e:s:pb:n)"

  

- autorelease_re = re.compile(r"\s*(?i:Release)\s*:.*%(?:autorelease(?:\s|$)|\{\??autorelease\})")

+ autorelease_re = re.compile(

+     r"\s*(?i:Release)\s*:.*%(?:autorelease(?:\s|$)|\{\??autorelease(?:\s+[^\}]*)?\})"

+ )

  changelog_re = re.compile(r"^%changelog(?:\s.*)?$", re.IGNORECASE)

  autochangelog_re = re.compile(r"\s*%(?:autochangelog|\{\??autochangelog\})\s*")

  

@@ -15,11 +15,11 @@ 

  

  autorelease_template = """## START: Set by rpmautospec

  ## (rpmautospec version {version})

- %define autorelease(e:s:pb:) %{{?-p:0.}}%{{lua:

+ %define autorelease(e:s:pb:n) %{{?-p:0.}}%{{lua:

      release_number = {autorelease_number:d};

      base_release_number = tonumber(rpm.expand("%{{?-b*}}%{{!?-b:1}}"));

      print(release_number + base_release_number - 1);

- }}%{{?-e:.%{{-e*}}}}%{{?-s:.%{{-s*}}}}%{{?dist}}

+ }}%{{?-e:.%{{-e*}}}}%{{?-s:.%{{-s*}}}}%{{!?-n:%{{?dist}}}}

  ## END: Set by rpmautospec

  """  # noqa: E501

  

@@ -1,5 +1,6 @@ 

  import logging

  import os

+ from pathlib import Path

  from tempfile import NamedTemporaryFile

  from typing import TextIO

  
@@ -18,14 +19,24 @@ 

          specfile: TextIO,

          *,

          with_autorelease=True,

+         with_autorelease_braces=False,

          autorelease_flags="",

          with_changelog=True,

          with_autochangelog=True,

      ):

+         if autorelease_flags and not autorelease_flags.startswith(" "):

+             autorelease_flags = " " + autorelease_flags

+ 

          contents = [

              "Line 1",

              "Line 2",

-             f"Release: %autorelease {autorelease_flags}" if with_autorelease else "Release: 1",

+             (

+                 f"Release: %{{autorelease{autorelease_flags}}}"

+                 if with_autorelease_braces

+                 else f"Release: %autorelease{autorelease_flags}"

+             )

+             if with_autorelease

+             else "Release: 1",

              "Line 4",

              "Line 5",

          ]
@@ -40,16 +51,26 @@ 

          specfile.flush()

  

      @pytest.mark.parametrize(

-         "with_autorelease,autorelease_flags,with_changelog,with_autochangelog",

+         "with_autorelease, with_autorelease_braces, autorelease_flags, with_changelog,"

+         + " with_autochangelog",

          (

-             pytest.param(True, "", True, True, id="all features"),

-             pytest.param(True, "-b 200", True, True, id="with non standard base release number"),

-             pytest.param(False, "", False, False, id="nothing"),

+             pytest.param(True, False, "", True, True, id="all features"),

+             pytest.param(

+                 True, False, "-b 200", True, True, id="with non standard base release number"

+             ),

+             pytest.param(True, True, "", True, True, id="all features, braces"),

+             pytest.param(

+                 True, True, "-b 200", True, True, id="with non standard base release number, braces"

+             ),

+             pytest.param(False, False, "", False, False, id="nothing"),

          ),

      )

+     @pytest.mark.parametrize("specpath_type", (str, Path))

      def test_check_specfile_features(

          self,

+         specpath_type,

          with_autorelease,

+         with_autorelease_braces,

          autorelease_flags,

          with_changelog,

          with_autochangelog,
@@ -58,12 +79,13 @@ 

              self._generate_spec_with_features(

                  specfile,

                  with_autorelease=with_autorelease,

+                 with_autorelease_braces=with_autorelease_braces,

                  autorelease_flags=autorelease_flags,

                  with_changelog=with_changelog,

                  with_autochangelog=with_autochangelog,

              )

  

-             features = misc.check_specfile_features(specfile.name)

+             features = misc.check_specfile_features(specpath_type(specfile.name))

  

              assert features.has_autorelease == with_autorelease

              if with_changelog:

@@ -1,10 +1,10 @@ 

  ## START: Set by rpmautospec

  ## (rpmautospec version 0.2.5)

- %define autorelease(e:s:pb:) %{?-p:0.}%{lua:

+ %define autorelease(e:s:pb:n) %{?-p:0.}%{lua:

      release_number = 11;

      base_release_number = tonumber(rpm.expand("%{?-b*}%{!?-b:1}"));

      print(release_number + base_release_number - 1);

- }%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{?dist}

+ }%{?-e:.%{-e*}}%{?-s:.%{-s*}}%{!?-n:%{?dist}}

  ## END: Set by rpmautospec

  

  # Our dummy-test-packages are named after canary varieties, meet Gloster, Rubino and Crested

@@ -0,0 +1,115 @@ 

+ import datetime as dt

+ import subprocess

+ import time

+ 

+ from pathlib import Path

+ 

+ import pytest

+ 

+ 

+ HERE = Path(__file__).parent

+ MACROS_FILE = HERE.parent / "rpm" / "macros.d" / "macros.rpmautospec"

+ 

+ 

+ @pytest.mark.parametrize("nodisttag", (False, True))

+ @pytest.mark.parametrize("base", (None, 10))

+ @pytest.mark.parametrize("prerelease", (False, True))

+ @pytest.mark.parametrize("snapshot", (None, "19700101"))

+ @pytest.mark.parametrize("extra", (None, "ilikeunsortableversions"))

+ @pytest.mark.parametrize("braces", (False, True))

+ def test_autorelease(braces, extra, snapshot, prerelease, base, nodisttag):

+     expected = ""

+     if braces:

+         autorelease_macro = "%{autorelease"

+     else:

+         autorelease_macro = "%autorelease"

+ 

+     if extra:

+         autorelease_macro += f" -e {extra}"

+         expected += f".{extra}"

+ 

+     if snapshot:

+         autorelease_macro += f" -s {snapshot}"

+         expected += f".{snapshot}"

+ 

+     if base:

+         autorelease_macro += f" -b {base}"

+         expected = "10" + expected

+     else:

+         expected = "1" + expected

+ 

+     if prerelease:

+         autorelease_macro += " -p"

+         expected = "0." + expected

+ 

+     if nodisttag:

+         autorelease_macro += " -n"

+     else:

+         expected += ".DIST"

+ 

+     if braces:

+         autorelease_macro += "}"

+ 

+     rpm_result = (

+         subprocess.check_output(

+             (

+                 "rpm",

+                 "--load",

+                 str(MACROS_FILE.absolute()),

+                 "--define",

+                 "%dist .DIST",

+                 "--eval",

+                 autorelease_macro,

+             )

+         )

+         .decode("UTF-8")

+         .strip()

+     )

+ 

+     assert rpm_result == expected

+ 

+ 

+ @pytest.mark.parametrize("epoch", (None, 2))

+ @pytest.mark.parametrize("packager", (None, "Foo Bar <foo@bar.com>"))

+ def test_autochangelog(packager, epoch):

+     rpm_params = (

+         "rpm",

+         "--load",

+         str(MACROS_FILE.absolute()),

+         "--define",

+         "%version 1",

+         "--define",

+         "%release 1",

+     )

+ 

+     if packager:

+         rpm_params += ("--define", f"%packager {packager}")

+     else:

+         rpm_params += ("--undefine", "%packager")

+     expected_packager = packager or "John Doe <packager@example.com>"

+ 

+     if epoch:

+         rpm_params += ("--define", f"%epoch {epoch}")

+         expected_evr = "2:1-1"

+     else:

+         expected_evr = "1-1"

+ 

+     rpm_params += ("--eval", "%autochangelog")

+ 

+     now = dt.datetime.now()

+ 

+     # If closer than 5 seconds to local midnight, delay running the test until after.

+     midnight = (now + dt.timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)

+     remaining = midnight - now

+     if remaining < dt.timedelta(seconds=5):

+         time.sleep(remaining.total_seconds() + 1)

+         now = dt.datetime.now()

+ 

+     expected_date = now.strftime("%a %b %d %Y")

+ 

+     rpm_result = subprocess.check_output(rpm_params).decode("UTF-8").strip()

+ 

+     header, entry = rpm_result.split("\n")

+ 

+     assert header == f"* {expected_date} {expected_packager} - {expected_evr}"

+     assert entry == "- local build"

commit 5270774469c8a88253f7e2d59b475325ac1408ac
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Jul 11 16:50:57 2022 +0200

    Recognize %{autorelease …} with flags

    Previously, the regex only recognized flagless %{autorelease} (if the
    macro was surrounded by braces).

    Fixes: #250

    Signed-off-by: Nils Philippsen <nils@redhat.com>

commit 0152f62f60147e00e26de6a13bfd7fbec0f11d11
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Jul 11 16:56:34 2022 +0200

    Cover all of rpmautospec.misc in unit tests

    Previously, the missing lines were covered by other tests.

    Signed-off-by: Nils Philippsen <nils@redhat.com>

commit 7594eeecd980f7bc8ce7c6652e15daae474dd17e
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Jul 11 18:01:09 2022 +0200

    Test RPM macros file functionality

    This went completely untested before.

    Fixes: #251

    Signed-off-by: Nils Philippsen <nils@redhat.com>

commit 95399a2be634a39bc7f6357e0980ac80972ca643
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Jul 11 18:08:04 2022 +0200

    Add `-n` flag to `%autorelease`

    This flag prevents the dist tag macro from being rendered, this is
    useful if `%autorelease` is itself used in a macro which is later
    plugged into `Release:`, with the dist tag explicitly added.

    Fixes: #248

    Signed-off-by: Nils Philippsen <nils@redhat.com>

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

rebased onto b135f36

2 years ago

Build succeeded.

Pull-Request has been merged by nphilipp

2 years ago