package fonts import ( "context" "testing" "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 TestFontsPlanApplyUsesYay(t *testing.T) { r := &fakeRunner{installed: map[string]bool{"yay": true}} m := New([]string{"ttf-jetbrains-mono-nerd"}, platform.NewPacman(r), platform.NewYay(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) } sawYay := false for _, c := range r.runCalls { if len(c) >= 2 && c[0] == "yay" && c[1] == "-S" { sawYay = true } } if !sawYay { t.Fatalf("no yay install; calls=%v", r.runCalls) } } func TestFontsDetectPresentNoActions(t *testing.T) { r := &fakeRunner{installed: map[string]bool{"yay": true, "ttf-jetbrains-mono-nerd": true}} m := New([]string{"ttf-jetbrains-mono-nerd"}, platform.NewPacman(r), platform.NewYay(r)) st, _ := m.Detect(context.Background()) p, _ := m.Plan(context.Background(), st) if !p.IsEmpty() { t.Fatal("want empty plan when already installed") } } func TestPlanForceReinstallsAllFonts(t *testing.T) { r := &fakeRunner{installed: map[string]bool{"yay": true, "ttf-jetbrains-mono-nerd": true}} m := New([]string{"ttf-jetbrains-mono-nerd"}, platform.NewPacman(r), platform.NewYay(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 { // yay -S --noconfirm ... (no --needed) if len(c) >= 3 && c[0] == "yay" && c[1] == "-S" && c[2] == "--noconfirm" { sawReinstall = true } } if !sawReinstall { t.Fatalf("no yay reinstall (-S --noconfirm, no --needed) call; calls=%v", r.runCalls) } } var _ module.Module = Module{}