From 409b51628a1ce84657da5bdeb8f55842d276e978 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 15 Apr 2024 08:02:58 -0300 Subject: [PATCH] feat(rpm): properly handle optional fields (#809) closes #619 Signed-off-by: Carlos Alexandro Becker --- go.mod | 2 +- go.sum | 4 ++-- rpm/rpm.go | 9 ++++++-- rpm/rpm_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 67f419a9..d4de690c 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/blakesmith/ar v0.0.0-20190502131153-809d4375e1fb github.com/caarlos0/go-rpmutils v0.2.1-0.20240105125627-01185134a559 github.com/caarlos0/go-version v0.1.1 - github.com/google/rpmpack v0.6.0 + github.com/google/rpmpack v0.6.1-0.20240329070804-c2247cbb881a github.com/goreleaser/chglog v0.5.0 github.com/goreleaser/fileglob v1.3.0 github.com/invopop/jsonschema v0.12.0 diff --git a/go.sum b/go.sum index 80299438..2488af6f 100644 --- a/go.sum +++ b/go.sum @@ -66,8 +66,8 @@ github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJA github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/rpmpack v0.6.0 h1:LoQuqlw6kHRwg25n3M0xtYrW+z2pTkR0ae1xx11hRw8= -github.com/google/rpmpack v0.6.0/go.mod h1:uqVAUVQLq8UY2hCDfmJ/+rtO3aw7qyhc90rCVEabEfI= +github.com/google/rpmpack v0.6.1-0.20240329070804-c2247cbb881a h1:JJBdjSfqSy3mnDT0940ASQFghwcZ4y4cb6ttjAoXqwE= +github.com/google/rpmpack v0.6.1-0.20240329070804-c2247cbb881a/go.mod h1:uqVAUVQLq8UY2hCDfmJ/+rtO3aw7qyhc90rCVEabEfI= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/rpm/rpm.go b/rpm/rpm.go index 8dfed1a6..5ec04d14 100644 --- a/rpm/rpm.go +++ b/rpm/rpm.go @@ -208,8 +208,13 @@ func buildRPMMeta(info *nfpm.Info) (*rpmpack.RPMMetaData, error) { if info.RPM.Compression == "" { info.RPM.Compression = "gzip:-1" } - if epoch, err = strconv.ParseUint(defaultTo(info.Epoch, "0"), 10, 32); err != nil { - return nil, err + + if info.Epoch == "" { + epoch = uint64(rpmpack.NoEpoch) + } else { + if epoch, err = strconv.ParseUint(info.Epoch, 10, 32); err != nil { + return nil, err + } } if provides, err = toRelation(info.Provides); err != nil { return nil, err diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go index 9af8cc6d..ac02ab18 100644 --- a/rpm/rpm_test.go +++ b/rpm/rpm_test.go @@ -29,6 +29,8 @@ func exampleInfo() *nfpm.Info { Priority: "extra", Maintainer: "Carlos A Becker ", Version: "1.0.0", + Release: "1", + Epoch: "0", Section: "default", Homepage: "http://carlosbecker.com", Vendor: "nope", @@ -78,6 +80,7 @@ func exampleInfo() *nfpm.Info { PostRemove: "../testdata/scripts/postremove.sh", }, RPM: nfpm.RPM{ + Group: "foo", Prefixes: []string{"/opt"}, Scripts: nfpm.RPMScripts{ PreTrans: "../testdata/scripts/pretrans.sh", @@ -134,7 +137,7 @@ func TestRPM(t *testing.T) { group, err := rpm.Header.GetString(rpmutils.GROUP) require.NoError(t, err) - require.Equal(t, "", group) + require.Equal(t, "foo", group) summary, err := rpm.Header.GetString(rpmutils.SUMMARY) require.NoError(t, err) @@ -145,6 +148,60 @@ func TestRPM(t *testing.T) { require.Equal(t, "Foo does things", description) } +func TestRPMMandatoryFieldsOnly(t *testing.T) { + f, err := os.CreateTemp(t.TempDir(), "test.rpm") + require.NoError(t, err) + require.NoError(t, Default.Package(&nfpm.Info{ + Name: "foo", + Arch: "amd64", + Version: "1.2", + Release: "1", + Description: "summary\nfoo bar\nlong description", + License: "MIT", + }, f)) + + file, err := os.OpenFile(f.Name(), os.O_RDONLY, 0o600) //nolint:gosec + require.NoError(t, err) + defer func() { + f.Close() + file.Close() + err = os.Remove(file.Name()) + require.NoError(t, err) + }() + rpm, err := rpmutils.ReadRpm(file) + require.NoError(t, err) + + os, err := rpm.Header.GetString(rpmutils.OS) + require.NoError(t, err) + require.Equal(t, "linux", os) + + arch, err := rpm.Header.GetString(rpmutils.ARCH) + require.NoError(t, err) + require.Equal(t, archToRPM["amd64"], arch) + + version, err := rpm.Header.GetString(rpmutils.VERSION) + require.NoError(t, err) + require.Equal(t, "1.2", version) + + release, err := rpm.Header.GetString(rpmutils.RELEASE) + require.NoError(t, err) + require.Equal(t, "1", release) + + _, err = rpm.Header.Get(rpmutils.EPOCH) + require.Error(t, err, "epoch should not be set") + + _, err = rpm.Header.GetString(rpmutils.GROUP) + require.Error(t, err, "group should not be set") + + summary, err := rpm.Header.GetString(rpmutils.SUMMARY) + require.NoError(t, err) + require.Equal(t, "summary", summary) + + description, err := rpm.Header.GetString(rpmutils.DESCRIPTION) + require.NoError(t, err) + require.Equal(t, "summary\nfoo bar\nlong description", description) +} + func TestRPMPlatform(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "test*.rpm") require.NoError(t, err)