feat(zellij): implement PlanForce
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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{}
|
||||
|
||||
Reference in New Issue
Block a user