Skip to content

Commit

Permalink
feat(rpm): properly handle optional fields (#809)
Browse files Browse the repository at this point in the history
closes  #619

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 authored Apr 15, 2024
1 parent ccfa2f4 commit 409b516
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
9 changes: 7 additions & 2 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 58 additions & 1 deletion rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func exampleInfo() *nfpm.Info {
Priority: "extra",
Maintainer: "Carlos A Becker <pkg@carlosbecker.com>",
Version: "1.0.0",
Release: "1",
Epoch: "0",
Section: "default",
Homepage: "http://carlosbecker.com",
Vendor: "nope",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 409b516

Please sign in to comment.