Skip to content

Commit

Permalink
fix(rpm): conventional file name should always have the release
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Sep 1, 2023
1 parent b50fc3a commit 31f43bd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
17 changes: 7 additions & 10 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,26 @@ 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
}

// ConventionalFileName returns a file name according
// 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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
43 changes: 22 additions & 21 deletions rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -85,7 +85,7 @@ func exampleInfo() *nfpm.Info {
},
},
},
})
}))
}

func TestConventionalExtension(t *testing.T) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -504,15 +505,15 @@ 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)
})
}

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)
})
}
Expand Down Expand Up @@ -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: "",
Expand All @@ -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",
Expand Down

0 comments on commit 31f43bd

Please sign in to comment.