From f4b96795044efd15b5756e7dbc7f97aea5613fca Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Thu, 26 Feb 2026 02:40:11 +0000 Subject: [PATCH] feat(dnf): fix dnf5 addRepo compatibility, expand test coverage - Fix addRepo to use 'config-manager addrepo --from-repofile=' for dnf5 (dnf4 uses 'config-manager --add-repo') - Add fedora:latest (dnf5) to containertest matrix - Add IsDNF5 detection integration test - Add RepoManager and NameNormalizer integration test coverage - Rename containertest fedora entry to fedora-dnf4 for clarity --- dnf/capabilities.go | 2 +- dnf/capabilities_linux.go | 10 ++++++++-- dnf/capabilities_other.go | 2 +- dnf/dnf_integration_test.go | 20 ++++++++++++++++++++ integration_test.go | 8 +++++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/dnf/capabilities.go b/dnf/capabilities.go index 1df600d..d23a769 100644 --- a/dnf/capabilities.go +++ b/dnf/capabilities.go @@ -90,7 +90,7 @@ func (d *DNF) ListRepos(ctx context.Context) ([]snack.Repository, error) { func (d *DNF) AddRepo(ctx context.Context, repo snack.Repository) error { d.Lock() defer d.Unlock() - return addRepo(ctx, repo) + return addRepo(ctx, repo, d.v5) } // RemoveRepo removes a configured repository. diff --git a/dnf/capabilities_linux.go b/dnf/capabilities_linux.go index 64c5aad..48b891d 100644 --- a/dnf/capabilities_linux.go +++ b/dnf/capabilities_linux.go @@ -171,8 +171,14 @@ func listRepos(ctx context.Context, v5 bool) ([]snack.Repository, error) { return parseRepoList(out), nil } -func addRepo(ctx context.Context, repo snack.Repository) error { - _, err := run(ctx, []string{"config-manager", "--add-repo", repo.URL}, snack.Options{}) +func addRepo(ctx context.Context, repo snack.Repository, v5 bool) error { + var args []string + if v5 { + args = []string{"config-manager", "addrepo", "--from-repofile=" + repo.URL} + } else { + args = []string{"config-manager", "--add-repo", repo.URL} + } + _, err := run(ctx, args, snack.Options{}) return err } diff --git a/dnf/capabilities_other.go b/dnf/capabilities_other.go index 17a06ea..64f93e2 100644 --- a/dnf/capabilities_other.go +++ b/dnf/capabilities_other.go @@ -56,7 +56,7 @@ func listRepos(_ context.Context, _ bool) ([]snack.Repository, error) { return nil, snack.ErrUnsupportedPlatform } -func addRepo(_ context.Context, _ snack.Repository) error { +func addRepo(_ context.Context, _ snack.Repository, _ bool) error { return snack.ErrUnsupportedPlatform } diff --git a/dnf/dnf_integration_test.go b/dnf/dnf_integration_test.go index c491006..2850407 100644 --- a/dnf/dnf_integration_test.go +++ b/dnf/dnf_integration_test.go @@ -12,6 +12,14 @@ import ( "github.com/stretchr/testify/require" ) +func TestIntegration_DNF_Detection(t *testing.T) { + d := dnf.New() + if !d.Available() { + t.Skip("dnf not available") + } + t.Logf("IsDNF5: %v", d.IsDNF5()) +} + func TestIntegration_DNF(t *testing.T) { var mgr snack.Manager = dnf.New() if !mgr.Available() { @@ -106,5 +114,17 @@ func TestIntegration_DNF(t *testing.T) { require.NoError(t, err) _ = groups } + + if rm, ok := mgr.(snack.RepoManager); ok { + repos, err := rm.ListRepos(ctx) + require.NoError(t, err) + require.NotEmpty(t, repos, "should have at least one repo") + t.Logf("repos: %d", len(repos)) + } + + if nn, ok := mgr.(snack.NameNormalizer); ok { + got := nn.NormalizeName("curl.x86_64") + assert.Equal(t, "curl", got) + } }) } diff --git a/integration_test.go b/integration_test.go index cf20513..21bd88f 100644 --- a/integration_test.go +++ b/integration_test.go @@ -57,11 +57,17 @@ var distros = []distroTest{ packages: "./pacman/ ./detect/", }, { - name: "fedora-dnf", + name: "fedora-dnf4", image: "fedora:39", setup: installGo("dnf install -y tree sudo wget"), packages: "./dnf/ ./rpm/ ./detect/", }, + { + name: "fedora-dnf5", + image: "fedora:latest", + setup: installGo("dnf install -y tree sudo wget"), + packages: "./dnf/ ./rpm/ ./detect/", + }, } func TestContainers(t *testing.T) {