From 4ed257bbd1b29b660b5d7ad268f62fbec67f3e86 Mon Sep 17 00:00:00 2001 From: MDietermann Date: Tue, 14 Apr 2026 22:06:10 +0200 Subject: [PATCH] feat(zellij): implement PlanForce --- internal/modules/zellij/zellij.go | 24 +++++++++++++-- internal/modules/zellij/zellij_test.go | 41 ++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/internal/modules/zellij/zellij.go b/internal/modules/zellij/zellij.go index 4462666..2c3fb63 100644 --- a/internal/modules/zellij/zellij.go +++ b/internal/modules/zellij/zellij.go @@ -3,7 +3,6 @@ package zellij import ( "context" - "errors" "fmt" "os" "path/filepath" @@ -111,5 +110,26 @@ 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") + actions := []module.Action{{ + Description: "pacman -S (force) zellij", + Kind: module.ActionPackageInstall, + Exec: func(ctx context.Context) error { return m.pm.Reinstall(ctx, []string{"zellij"}) }, + }} + if m.cfg.ConfigURL != "" { + p, err := configPath() + if err != nil { + return module.Plan{}, err + } + actions = append(actions, module.Action{ + Description: "redownload zellij config (force)", + Kind: module.ActionFileWrite, + Exec: func(ctx context.Context) error { + if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil { + return err + } + return platform.Download(ctx, m.cfg.ConfigURL, p, 0o644) + }, + }) + } + return module.Plan{Actions: actions}, nil } diff --git a/internal/modules/zellij/zellij_test.go b/internal/modules/zellij/zellij_test.go index 2ae8d51..8f74c29 100644 --- a/internal/modules/zellij/zellij_test.go +++ b/internal/modules/zellij/zellij_test.go @@ -81,4 +81,45 @@ func TestZellijInstalledPresentEmptyPlan(t *testing.T) { } } +func TestPlanForceReinstallsAndRewritesConfig(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + os.MkdirAll(filepath.Join(home, ".config", "zellij"), 0o755) + os.WriteFile(filepath.Join(home, ".config", "zellij", "config.kdl"), []byte("old\n"), 0o644) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Write([]byte("fresh {}\n")) + })) + defer srv.Close() + r := &fakeRunner{installed: map[string]bool{"zellij": true}} + m := New(config.ZellijConfig{ConfigURL: srv.URL}, 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") + } + 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 call; calls=%v", r.runCalls) + } + got, err := os.ReadFile(filepath.Join(home, ".config", "zellij", "config.kdl")) + if err != nil { + t.Fatalf("config not written: %v", err) + } + if string(got) != "fresh {}\n" { + t.Fatalf("config not overwritten: %q", got) + } +} + var _ module.Module = Module{}