package gittools import ( "context" "strings" "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 // 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{"lazygit"}, 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{"lazygit": true}} m := New([]string{"lazygit"}, 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{"lazygit"}, nil) p, _ := m.Plan(context.Background(), state{Missing: []string{"lazygit"}}) if len(p.Actions) != 1 { t.Fatalf("want 1 action, got %d", len(p.Actions)) } if !strings.Contains(p.Actions[0].Description, "lazygit") { t.Fatalf("desc: %s", p.Actions[0].Description) } } func TestPlanPresentEmpty(t *testing.T) { m := New([]string{"lazygit"}, 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{"lazygit"}, 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{"lazygit": true}} m := New([]string{"lazygit"}, platform.NewPacman(r)) if err := m.Verify(context.Background()); err != nil { t.Fatal(err) } } func TestPlanForceReinstallsAllPackages(t *testing.T) { r := &fakeRunner{installed: map[string]bool{"lazygit": true}} m := New([]string{"lazygit"}, platform.NewPacman(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 even when packages are installed") } 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 { 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 (-S --noconfirm, no --needed) call; calls=%v", r.runCalls) } } var _ module.Module = Module{}