From 58bd532dba25c3d14143eafe048f2dd5c5eecc15 Mon Sep 17 00:00:00 2001 From: MDietermann Date: Tue, 14 Apr 2026 21:55:08 +0200 Subject: [PATCH] feat(packages): implement PlanForce to reinstall all packages Adds Reinstall method to PackageManager and AURHelper interfaces so modules can invoke the concrete Reinstall helpers behind the interface. *Pacman and *Yay already satisfy it. --- internal/modules/packages/packages.go | 11 +++++++-- internal/modules/packages/packages_test.go | 26 ++++++++++++++++++++++ internal/platform/aur.go | 1 + internal/platform/pkgmgr.go | 1 + 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/internal/modules/packages/packages.go b/internal/modules/packages/packages.go index b276052..426375f 100644 --- a/internal/modules/packages/packages.go +++ b/internal/modules/packages/packages.go @@ -3,7 +3,6 @@ package packages import ( "context" - "errors" "fmt" "strings" @@ -75,5 +74,13 @@ func (m Module) Verify(ctx context.Context) error { } func (m Module) PlanForce(ctx context.Context) (module.Plan, error) { - return module.Plan{}, errors.New("PlanForce not implemented") + if len(m.pkgs) == 0 { + return module.Plan{}, nil + } + pkgs := append([]string{}, m.pkgs...) + return module.Plan{Actions: []module.Action{{ + Description: "pacman -S (force) " + strings.Join(pkgs, ", "), + Kind: module.ActionPackageInstall, + Exec: func(ctx context.Context) error { return m.pm.Reinstall(ctx, pkgs) }, + }}}, nil } diff --git a/internal/modules/packages/packages_test.go b/internal/modules/packages/packages_test.go index d2a9f85..d2da36c 100644 --- a/internal/modules/packages/packages_test.go +++ b/internal/modules/packages/packages_test.go @@ -110,4 +110,30 @@ func TestVerifyAfterApply(t *testing.T) { } } +func TestPlanForceReinstallsAllPackages(t *testing.T) { + r := &fakeRunner{installed: map[string]bool{"curl": true, "wget": true}} + m := New([]string{"curl", "wget"}, platform.NewPacman(r)) + plan, err := m.PlanForce(context.Background()) + if err != nil { + t.Fatalf("PlanForce: %v", err) + } + if plan.IsEmpty() { + t.Fatal("force plan must not be empty even when packages are installed") + } + for _, a := range plan.Actions { + if err := a.Exec(context.Background()); err != nil { + t.Fatalf("Exec: %v", err) + } + } + sawReinstall := false + for _, c := range r.runCalls { + if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" && c[3] == "--noconfirm" { + sawReinstall = true + } + } + if !sawReinstall { + t.Fatalf("no pacman reinstall (-S --noconfirm, no --needed) call; calls=%v", r.runCalls) + } +} + var _ module.Module = Module{} diff --git a/internal/platform/aur.go b/internal/platform/aur.go index 903c535..133c1bb 100644 --- a/internal/platform/aur.go +++ b/internal/platform/aur.go @@ -11,6 +11,7 @@ import ( type AURHelper interface { EnsureInstalled(ctx context.Context) error Install(ctx context.Context, pkgs []string) error + Reinstall(ctx context.Context, pkgs []string) error } type Yay struct { diff --git a/internal/platform/pkgmgr.go b/internal/platform/pkgmgr.go index f7016cd..7b261b1 100644 --- a/internal/platform/pkgmgr.go +++ b/internal/platform/pkgmgr.go @@ -8,6 +8,7 @@ type PackageManager interface { Name() string IsInstalled(ctx context.Context, pkg string) (bool, error) Install(ctx context.Context, pkgs []string) error + Reinstall(ctx context.Context, pkgs []string) error Update(ctx context.Context) error }