From cb2d7bd5d5479d746ff062bfddf40f90cf233334 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 12 Sep 2023 09:59:28 -0300 Subject: [PATCH] fix(rpm): conventional file name should always have the release (#701) * fix(rpm): conventional file name should always have the release Signed-off-by: Carlos Alexandro Becker * test: fix acceptance test --------- Signed-off-by: Carlos Alexandro Becker --- rpm/rpm.go | 17 +++++------- rpm/rpm_test.go | 43 +++++++++++++++--------------- testdata/acceptance/rpm.dockerfile | 12 ++++----- 3 files changed, 35 insertions(+), 37 deletions(-) diff --git a/rpm/rpm.go b/rpm/rpm.go index 97953bff..311dce96 100644 --- a/rpm/rpm.go +++ b/rpm/rpm.go @@ -69,13 +69,15 @@ var archToRPM = map[string]string{ // TODO: other arches } -func ensureValidArch(info *nfpm.Info) *nfpm.Info { +func setDefaults(info *nfpm.Info) *nfpm.Info { if info.RPM.Arch != "" { info.Arch = info.RPM.Arch } else if arch, ok := archToRPM[info.Arch]; ok { info.Arch = arch } + info.Release = defaultTo(info.Release, "1") + return info } @@ -83,15 +85,10 @@ func ensureValidArch(info *nfpm.Info) *nfpm.Info { // to the conventions for RPM packages. See: // http://ftp.rpm.org/max-rpm/ch-rpm-file-format.html func (*RPM) ConventionalFileName(info *nfpm.Info) string { - info = ensureValidArch(info) - - version := formatVersion(info) - if info.Release != "" { - version += "-" + info.Release - } + info = setDefaults(info) // name-version-release.architecture.rpm - return fmt.Sprintf("%s-%s.%s.rpm", info.Name, version, info.Arch) + return fmt.Sprintf("%s-%s.%s.rpm", info.Name, formatVersion(info), info.Arch) } // ConventionalExtension returns the file name conventionally used for RPM packages @@ -105,7 +102,7 @@ func (*RPM) Package(info *nfpm.Info, w io.Writer) (err error) { meta *rpmpack.RPMMetaData rpm *rpmpack.RPM ) - info = ensureValidArch(info) + info = setDefaults(info) err = nfpm.PrepareForPackager(info, packagerName) if err != nil { @@ -269,7 +266,7 @@ func formatVersion(info *nfpm.Info) string { version += "+" + info.VersionMetadata } - return version + return version + "-" + info.Release } func defaultTo(in, def string) string { diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go index 314da90c..07ac2e0d 100644 --- a/rpm/rpm_test.go +++ b/rpm/rpm_test.go @@ -22,7 +22,7 @@ import ( ) func exampleInfo() *nfpm.Info { - return nfpm.WithDefaults(&nfpm.Info{ + return setDefaults(nfpm.WithDefaults(&nfpm.Info{ Name: "foo", Arch: "amd64", Description: "Foo does things", @@ -85,7 +85,7 @@ func exampleInfo() *nfpm.Info { }, }, }, - }) + })) } func TestConventionalExtension(t *testing.T) { @@ -118,7 +118,7 @@ func TestRPM(t *testing.T) { version, err := rpm.Header.GetString(rpmutils.VERSION) require.NoError(t, err) - require.Equal(t, "1.0.0", version) + require.Equal(t, "1.0.0-1", version) release, err := rpm.Header.GetString(rpmutils.RELEASE) require.NoError(t, err) @@ -298,7 +298,7 @@ func TestWithRPMTags(t *testing.T) { version, err := rpm.Header.GetString(rpmutils.VERSION) require.NoError(t, err) - require.Equal(t, "1.0.0", version) + require.Equal(t, "1.0.0-3", version) release, err := rpm.Header.GetString(rpmutils.RELEASE) require.NoError(t, err) @@ -329,7 +329,7 @@ func TestRPMVersion(t *testing.T) { info.Version = "1.0.0" //nolint:golint,goconst meta, err := buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0", meta.Version) + require.Equal(t, "1.0.0-1", meta.Version) require.Equal(t, "1", meta.Release) } @@ -339,7 +339,7 @@ func TestRPMVersionWithRelease(t *testing.T) { info.Release = "2" meta, err := buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0", meta.Version) + require.Equal(t, "1.0.0-2", meta.Version) require.Equal(t, "2", meta.Release) } @@ -351,14 +351,14 @@ func TestRPMVersionWithPrerelease(t *testing.T) { info.Prerelease = "rc1" // nolint:goconst meta, err := buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0~rc1", meta.Version) + require.Equal(t, "1.0.0~rc1-1", meta.Version) require.Equal(t, "1", meta.Release) info.Version = "1.0.0~rc1" info.Prerelease = "" meta, err = buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0~rc1", meta.Version) + require.Equal(t, "1.0.0~rc1-1", meta.Version) require.Equal(t, "1", meta.Release) } @@ -367,20 +367,20 @@ func TestRPMVersionWithReleaseAndPrerelease(t *testing.T) { info := exampleInfo() info.Version = "1.0.0" - info.Release = "0.2" + info.Release = "2" info.Prerelease = "rc1" meta, err := buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0~rc1", meta.Version) - require.Equal(t, "0.2", meta.Release) + require.Equal(t, "1.0.0~rc1-2", meta.Version) + require.Equal(t, "2", meta.Release) info.Version = "1.0.0~rc1" - info.Release = "0.2" + info.Release = "3" info.Prerelease = "" meta, err = buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0~rc1", meta.Version) - require.Equal(t, "0.2", meta.Release) + require.Equal(t, "1.0.0~rc1-3", meta.Version) + require.Equal(t, "3", meta.Release) } func TestRPMVersionWithVersionMetadata(t *testing.T) { @@ -389,15 +389,16 @@ func TestRPMVersionWithVersionMetadata(t *testing.T) { info.Version = "1.0.0+meta" info.VersionMetadata = "" - meta, err := buildRPMMeta(nfpm.WithDefaults(info)) + meta, err := buildRPMMeta(info) require.NoError(t, err) - require.Equal(t, "1.0.0+meta", meta.Version) + require.Equal(t, "1.0.0+meta-1", meta.Version) info.Version = "1.0.0" info.VersionMetadata = "meta" + info.Release = "10" meta, err = buildRPMMeta(nfpm.WithDefaults(info)) require.NoError(t, err) - require.Equal(t, "1.0.0+meta", meta.Version) + require.Equal(t, "1.0.0+meta-10", meta.Version) } func TestWithInvalidEpoch(t *testing.T) { @@ -504,7 +505,7 @@ func TestArches(t *testing.T) { t.Run(k, func(t *testing.T) { info := exampleInfo() info.Arch = k - info = ensureValidArch(info) + info = setDefaults(info) require.Equal(t, archToRPM[k], info.Arch) }) } @@ -512,7 +513,7 @@ func TestArches(t *testing.T) { t.Run("override", func(t *testing.T) { info := exampleInfo() info.RPM.Arch = "foo64" - info = ensureValidArch(info) + info = setDefaults(info) require.Equal(t, "foo64", info.Arch) }) } @@ -569,7 +570,7 @@ func TestRPMConventionalFileName(t *testing.T) { }{ { Version: "1.2.3", Release: "", Prerelease: "", Metadata: "", - Expected: fmt.Sprintf("%s-1.2.3.%s.rpm", info.Name, info.Arch), + Expected: fmt.Sprintf("%s-1.2.3-1.%s.rpm", info.Name, info.Arch), }, { Version: "1.2.3", Release: "4", Prerelease: "", Metadata: "", @@ -581,7 +582,7 @@ func TestRPMConventionalFileName(t *testing.T) { }, { Version: "1.2.3", Release: "", Prerelease: "5", Metadata: "", - Expected: fmt.Sprintf("%s-1.2.3~5.%s.rpm", info.Name, info.Arch), + Expected: fmt.Sprintf("%s-1.2.3~5-1.%s.rpm", info.Name, info.Arch), }, { Version: "1.2.3", Release: "1", Prerelease: "5", Metadata: "git", diff --git a/testdata/acceptance/rpm.dockerfile b/testdata/acceptance/rpm.dockerfile index 5f517f71..0efaed5b 100644 --- a/testdata/acceptance/rpm.dockerfile +++ b/testdata/acceptance/rpm.dockerfile @@ -123,16 +123,16 @@ RUN command -v zsh # ---- env-var-version test ---- FROM min AS env-var-version -ENV EXPECTVER="Version : 1.0.0~0.1.b1+git.abcdefgh" \ - EXPECTREL="Release : 1" +ENV EXPECTVER="Version : 1.0.0~0.1.b1+git.abcdefgh-1" \ + EXPECTREL="Release : 1" RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Version" > found.ver RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Release" > found.rel RUN export FOUND_VER="$(cat found.ver)" && \ - echo "Expected: ${EXPECTVER}' :: Found: '${FOUND_VER}'" && \ - test "${FOUND_VER}" = "${EXPECTVER}" + echo "Expected: ${EXPECTVER}' :: Found: '${FOUND_VER}'" && \ + test "${FOUND_VER}" = "${EXPECTVER}" RUN export FOUND_REL="$(cat found.rel)" && \ - echo "Expected: '${EXPECTREL}' :: Found: '${FOUND_REL}'" && \ - test "${FOUND_REL}" = "${EXPECTREL}" + echo "Expected: '${EXPECTREL}' :: Found: '${FOUND_REL}'" && \ + test "${FOUND_REL}" = "${EXPECTREL}" # ---- changelog test ----