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.
This commit is contained in:
Brian C. Lane 2023-05-16 12:15:58 -07:00 committed by Brian C. Lane
parent c7bc25cead
commit f378ff6367
2 changed files with 66 additions and 0 deletions

View file

@ -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 {

View file

@ -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())
}