feat(modules): port zellij module to Go

This commit is contained in:
MDietermann
2026-04-14 08:59:28 +02:00
parent 4b3ce22491
commit 78ad784c7e
2 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
package zellij
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/MDietermann/ArduinoArchSetup/internal/config"
"github.com/MDietermann/ArduinoArchSetup/internal/module"
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
)
type fakeRunner struct {
runCalls [][]string
outCalls [][]string
installed map[string]bool
}
func (f *fakeRunner) Run(_ context.Context, name string, args ...string) error {
f.runCalls = append(f.runCalls, append([]string{name}, args...))
return nil
}
func (f *fakeRunner) Output(_ context.Context, name string, args ...string) ([]byte, error) {
f.outCalls = append(f.outCalls, append([]string{name}, args...))
if name == "pacman" && len(args) == 2 && args[0] == "-Q" {
if f.installed[args[1]] {
return []byte("ok"), nil
}
return nil, errOne
}
return nil, nil
}
type eo struct{}
func (eo) Error() string { return "exit 1" }
var errOne = eo{}
type nop struct{}
func (nop) Progress(string, int, int, string) {}
func (nop) Log(string, string) {}
func TestZellijPlanAppliesInstallAndConfig(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte("keybinds {}\n"))
}))
defer srv.Close()
r := &fakeRunner{installed: map[string]bool{}}
m := New(config.ZellijConfig{ConfigURL: srv.URL}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
if err := m.Apply(context.Background(), p, nop{}); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(filepath.Join(home, ".config", "zellij", "config.kdl"))
if err != nil {
t.Fatalf("config not written: %v", err)
}
if string(got) != "keybinds {}\n" {
t.Fatalf("unexpected content %q", got)
}
}
func TestZellijInstalledPresentEmptyPlan(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
r := &fakeRunner{installed: map[string]bool{"zellij": true}}
m := New(config.ZellijConfig{ConfigURL: ""}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
if !p.IsEmpty() {
t.Fatalf("want empty plan, got %+v", p)
}
}
var _ module.Module = Module{}