feat(neovim): implement PlanForce

This commit is contained in:
MDietermann
2026-04-14 22:15:32 +02:00
parent 77b767aa0e
commit 25b4eefecd
2 changed files with 198 additions and 2 deletions

View File

@@ -207,4 +207,93 @@ func TestWriteChadrcNoopWithoutConfigDir(t *testing.T) {
}
}
func TestPlanForceReinstallsAndRewrites(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
// Pre-populate an "installed" NvChad clone with a local user tweak so we
// can verify PlanForce does NOT wipe the clone.
cfg := filepath.Join(home, ".config", "nvim")
os.MkdirAll(filepath.Join(cfg, "lua", "plugins"), 0o755)
os.WriteFile(filepath.Join(cfg, "lua", "plugins", "init.lua"),
[]byte("return {\n \"other\",\n},\n"), 0o644)
userTweak := filepath.Join(cfg, "lua", "user_tweak.lua")
os.WriteFile(userTweak, []byte("-- my custom stuff"), 0o644)
r := &fakeRunner{installed: map[string]bool{"neovim": true}}
m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "dracula", platform.NewPacman(r), platform.GitClient{Runner: 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 %q: %v", a.Description, err)
}
}
// User tweak must survive.
if _, err := os.Stat(userTweak); err != nil {
t.Fatalf("PlanForce wiped user NvChad modification: %v", err)
}
// No `git clone` call because starter init.lua was already present.
for _, c := range r.runCalls {
if c[0] == "git" && len(c) >= 2 && c[1] == "clone" {
t.Fatalf("PlanForce re-cloned existing NvChad: %v", c)
}
}
// pacman reinstall must have fired.
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)
}
// chadrc.lua was rewritten with the requested theme.
chadrc, err := os.ReadFile(filepath.Join(cfg, "lua", "chadrc.lua"))
if err != nil {
t.Fatalf("chadrc missing after force: %v", err)
}
if !strings.Contains(string(chadrc), `theme = "dracula"`) {
t.Fatalf("chadrc theme not applied: %s", chadrc)
}
// assets rewritten
for _, name := range []string{"arduino.lua", "dap.lua"} {
if _, err := os.Stat(filepath.Join(cfg, "lua", "plugins", name)); err != nil {
t.Errorf("asset %s missing after force: %v", name, err)
}
}
}
func TestPlanForceClonesIfMissing(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
r := &fakeRunner{installed: map[string]bool{"neovim": true}}
m := New(config.NeovimConfig{NvChadStarterRepo: "https://x/starter"}, "gruvbox", platform.NewPacman(r), platform.GitClient{Runner: r})
plan, err := m.PlanForce(context.Background())
if err != nil {
t.Fatalf("PlanForce: %v", err)
}
for _, a := range plan.Actions {
if err := a.Exec(context.Background()); err != nil {
t.Fatalf("Exec %q: %v", a.Description, err)
}
}
sawClone := false
for _, c := range r.runCalls {
if c[0] == "git" && len(c) >= 2 && c[1] == "clone" {
sawClone = true
}
}
if !sawClone {
t.Fatalf("expected git clone when NvChad absent; calls=%v", r.runCalls)
}
}
var _ module.Module = Module{}