Files
Archduino/internal/modules/yazi/yazi_test.go
2026-04-14 08:47:00 +02:00

114 lines
2.9 KiB
Go

package yazi
import (
"context"
"strings"
"testing"
"github.com/MDietermann/ArduinoArchSetup/internal/module"
"github.com/MDietermann/ArduinoArchSetup/internal/platform"
)
type fakeRunner struct {
runCalls [][]string
outCalls [][]string
installed map[string]bool // key: pkg name
}
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(args[1] + " 1.0-1\n"), nil
}
return nil, errExitOne
}
return nil, nil
}
type exitOneErr struct{}
func (exitOneErr) Error() string { return "exit status 1" }
var errExitOne = exitOneErr{}
type nopReporter struct{}
func (nopReporter) Progress(string, int, int, string) {}
func (nopReporter) Log(string, string) {}
func TestDetectAbsent(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, err := m.Detect(context.Background())
if err != nil {
t.Fatal(err)
}
s := st.(state)
if len(s.Missing) != 1 {
t.Fatalf("want 1 missing, got %v", s.Missing)
}
}
func TestDetectPresent(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{"yazi": true}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
if len(st.(state).Missing) != 0 {
t.Fatalf("want 0 missing, got %v", st.(state).Missing)
}
}
func TestPlanAbsentEmitsInstallAction(t *testing.T) {
m := New([]string{"yazi"}, nil)
p, _ := m.Plan(context.Background(), state{Missing: []string{"yazi"}})
if len(p.Actions) != 1 {
t.Fatalf("want 1 action, got %d", len(p.Actions))
}
if !strings.Contains(p.Actions[0].Description, "yazi") {
t.Fatalf("desc: %s", p.Actions[0].Description)
}
}
func TestPlanPresentEmpty(t *testing.T) {
m := New([]string{"yazi"}, nil)
p, _ := m.Plan(context.Background(), state{Missing: nil})
if !p.IsEmpty() {
t.Fatal("want empty plan")
}
}
func TestApplyShellsOut(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{}}
m := New([]string{"yazi"}, platform.NewPacman(r))
st, _ := m.Detect(context.Background())
p, _ := m.Plan(context.Background(), st)
if err := m.Apply(context.Background(), p, nopReporter{}); err != nil {
t.Fatal(err)
}
sawInstall := false
for _, c := range r.runCalls {
if len(c) >= 4 && c[0] == "sudo" && c[1] == "pacman" && c[2] == "-S" {
sawInstall = true
}
}
if !sawInstall {
t.Fatalf("no pacman install call; calls=%v", r.runCalls)
}
}
func TestVerifyAfterApply(t *testing.T) {
r := &fakeRunner{installed: map[string]bool{"yazi": true}}
m := New([]string{"yazi"}, platform.NewPacman(r))
if err := m.Verify(context.Background()); err != nil {
t.Fatal(err)
}
}
var _ module.Module = Module{}