Files
Archduino/internal/modules/zellij/zellij_test.go

85 lines
2.2 KiB
Go

package zellij
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/config"
"gitlab.hive-brain.de/MDietermann/Archduino/internal/module"
"gitlab.hive-brain.de/MDietermann/Archduino/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{}