Files
Archduino/internal/platform/pacman_test.go
MDietermann 8b43a0f273 fix: address final-review findings before closing Plan 1
- privilege: wire sudo -v to real stdin/stdout/stderr so it can prompt
- cmd: validate --only/--skip IDs against registry (prevents silent
  run-all on typo); gate install to Arch-only (spec requirement);
  mark --only and --skip mutually exclusive
- platform: Pacman.Update now runs -Syu --noconfirm, not -Sy (avoids
  Arch partial-upgrade anti-pattern)
- test/integration: trap-clean the named docker volume on any exit
- go.mod: tidy — yaml.v3 is a direct dep
2026-04-13 21:35:14 +02:00

88 lines
2.0 KiB
Go

package platform
import (
"context"
"errors"
"reflect"
"testing"
)
type fakeRunner struct {
runCalls [][]string
outputCalls [][]string
outputErr error
output []byte
}
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.outputCalls = append(f.outputCalls, append([]string{name}, args...))
return f.output, f.outputErr
}
func TestPacmanInstallShellsOutCorrectly(t *testing.T) {
r := &fakeRunner{}
p := NewPacman(r)
if err := p.Install(context.Background(), []string{"tio", "git"}); err != nil {
t.Fatal(err)
}
if len(r.runCalls) != 1 {
t.Fatalf("run calls = %d", len(r.runCalls))
}
want := []string{"sudo", "pacman", "-S", "--needed", "--noconfirm", "tio", "git"}
if !reflect.DeepEqual(r.runCalls[0], want) {
t.Fatalf("args = %v, want %v", r.runCalls[0], want)
}
}
func TestPacmanInstallEmptyIsNoop(t *testing.T) {
r := &fakeRunner{}
p := NewPacman(r)
if err := p.Install(context.Background(), nil); err != nil {
t.Fatal(err)
}
if len(r.runCalls) != 0 {
t.Fatalf("expected no calls, got %v", r.runCalls)
}
}
func TestPacmanIsInstalledTrue(t *testing.T) {
r := &fakeRunner{output: []byte("tio 3.2-1")}
p := NewPacman(r)
ok, err := p.IsInstalled(context.Background(), "tio")
if err != nil {
t.Fatal(err)
}
if !ok {
t.Fatal("want installed=true")
}
}
func TestPacmanIsInstalledFalseOnError(t *testing.T) {
r := &fakeRunner{outputErr: errors.New("not found")}
p := NewPacman(r)
ok, err := p.IsInstalled(context.Background(), "ghost")
if err != nil {
t.Fatal(err)
}
if ok {
t.Fatal("want installed=false")
}
}
func TestPacmanUpdate(t *testing.T) {
r := &fakeRunner{}
p := NewPacman(r)
if err := p.Update(context.Background()); err != nil {
t.Fatal(err)
}
want := []string{"sudo", "pacman", "-Syu", "--noconfirm"}
if !reflect.DeepEqual(r.runCalls[0], want) {
t.Fatalf("args = %v, want %v", r.runCalls[0], want)
}
}