package procprio import ( "errors" "reflect" "testing" ) func withTools(t *testing.T, tools map[string]string) { t.Helper() previous := lookPath lookPath = func(name string) (string, error) { if path, ok := tools[name]; ok { return path, nil } return "", errors.New("not found") } t.Cleanup(func() { lookPath = previous }) } func TestCommandUnchangedWithoutLowPriority(t *testing.T) { withTools(t, map[string]string{"ionice": "/usr/bin/ionice", "nice": "/usr/bin/nice"}) name, args := Command(false, "/bin/restic", []string{"backup", "/src"}) if name != "/bin/restic" || !reflect.DeepEqual(args, []string{"backup", "/src"}) { t.Fatalf("got %q %q", name, args) } } func TestCommandWrapsWithIoniceAndNice(t *testing.T) { withTools(t, map[string]string{"ionice": "/usr/bin/ionice", "nice": "/usr/bin/nice"}) name, args := Command(true, "/bin/restic", []string{"backup", "/src"}) want := []string{"-c", "3", "/usr/bin/nice", "-n", "19", "/bin/restic", "backup", "/src"} if name != "/usr/bin/ionice" || !reflect.DeepEqual(args, want) { t.Fatalf("got %q %q", name, args) } } func TestCommandSkipsMissingTools(t *testing.T) { withTools(t, map[string]string{"nice": "/usr/bin/nice"}) name, args := Command(true, "/bin/rsync", []string{"-a"}) if name != "/usr/bin/nice" || !reflect.DeepEqual(args, []string{"-n", "19", "/bin/rsync", "-a"}) { t.Fatalf("got %q %q", name, args) } withTools(t, map[string]string{}) name, args = Command(true, "/bin/rsync", []string{"-a"}) if name != "/bin/rsync" || !reflect.DeepEqual(args, []string{"-a"}) { t.Fatalf("got %q %q", name, args) } }