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",