From f378ff6367878df1ae352b816e5418a4abf87940 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 16 May 2023 12:15:58 -0700 Subject: [PATCH] rpmmd: Add NEVRA helper functions to PackageSpec GetEVRA will return the Epoch:Version-Release.Arch string, and GetNEVRA will return Name-Epoch:Version-Release.Arch with Epoch being left off if it is zero. Also includes tests. --- internal/rpmmd/repository.go | 13 ++++++++ internal/rpmmd/repository_test.go | 53 +++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 internal/rpmmd/repository_test.go diff --git a/internal/rpmmd/repository.go b/internal/rpmmd/repository.go index b68ec5277..18f0fecd2 100644 --- a/internal/rpmmd/repository.go +++ b/internal/rpmmd/repository.go @@ -198,6 +198,19 @@ type PackageInfo struct { Dependencies []PackageSpec `json:"dependencies,omitempty"` } +// GetEVRA returns the package's Epoch:Version-Release.Arch string +func (ps *PackageSpec) GetEVRA() string { + if ps.Epoch == 0 { + return fmt.Sprintf("%s-%s.%s", ps.Version, ps.Release, ps.Arch) + } + return fmt.Sprintf("%d:%s-%s.%s", ps.Epoch, ps.Version, ps.Release, ps.Arch) +} + +// GetNEVRA returns the package's Name-Epoch:Version-Release.Arch string +func (ps *PackageSpec) GetNEVRA() string { + return fmt.Sprintf("%s-%s", ps.Name, ps.GetEVRA()) +} + func GetVerStrFromPackageSpecList(pkgs []PackageSpec, packageName string) (string, error) { for _, pkg := range pkgs { if pkg.Name == packageName { diff --git a/internal/rpmmd/repository_test.go b/internal/rpmmd/repository_test.go new file mode 100644 index 000000000..5ac270efe --- /dev/null +++ b/internal/rpmmd/repository_test.go @@ -0,0 +1,53 @@ +package rpmmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestPackageSpecGetEVRA(t *testing.T) { + specs := []PackageSpec{ + { + Name: "tmux", + Epoch: 0, + Version: "3.3a", + Release: "3.fc38", + Arch: "x86_64", + }, + { + Name: "grub2", + Epoch: 1, + Version: "2.06", + Release: "94.fc38", + Arch: "noarch", + }, + } + + assert.Equal(t, "3.3a-3.fc38.x86_64", specs[0].GetEVRA()) + assert.Equal(t, "1:2.06-94.fc38.noarch", specs[1].GetEVRA()) + +} + +func TestPackageSpecGetNEVRA(t *testing.T) { + specs := []PackageSpec{ + { + Name: "tmux", + Epoch: 0, + Version: "3.3a", + Release: "3.fc38", + Arch: "x86_64", + }, + { + Name: "grub2", + Epoch: 1, + Version: "2.06", + Release: "94.fc38", + Arch: "noarch", + }, + } + + assert.Equal(t, "tmux-3.3a-3.fc38.x86_64", specs[0].GetNEVRA()) + assert.Equal(t, "grub2-1:2.06-94.fc38.noarch", specs[1].GetNEVRA()) + +}