List every module's preferences in the settings verb (hq ADR 0262)
One verb is the interface to every preference, so no module builds a settings tool of its own: each key, its default and why, and every assigned machine's value with its source.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/novox/mesh-controller/internal/catalogue"
|
||||
@@ -19,3 +20,52 @@ func TestSettingsSayWhereEachValueComesFrom(t *testing.T) {
|
||||
t.Fatalf("said\n%s\nwant\n%s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// `settings` with list "preferences" is the one listing of every module's preferences; module and node
|
||||
// narrow it, and no other listing is taken.
|
||||
func TestSettingsListPreferences(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
args map[string]any
|
||||
want string
|
||||
}{
|
||||
{map[string]any{"list": "preferences"}, "settings preferences"},
|
||||
{map[string]any{"list": "preferences", "module": "dunst"}, "settings preferences dunst"},
|
||||
{map[string]any{"list": "preferences", "node": "laptop"}, "settings preferences --node laptop"},
|
||||
} {
|
||||
argv, err := argvFor("settings", c.args)
|
||||
if err != nil || strings.Join(argv, " ") != c.want {
|
||||
t.Errorf("%v: %v %v, want %s", c.args, argv, err, c.want)
|
||||
}
|
||||
}
|
||||
if _, err := argvFor("settings", map[string]any{"list": "everything"}); err == nil {
|
||||
t.Error("a listing other than preferences was taken")
|
||||
}
|
||||
if argv, err := argvFor("settings", map[string]any{}); err != nil || strings.Join(argv, " ") != "settings preferences" {
|
||||
t.Errorf("settings naming no module is the listing: %v %v", argv, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreferencesSayEachMachinesValueAndItsSource(t *testing.T) {
|
||||
m := catalogue.Manifest{Module: "dunst", Settings: map[string]catalogue.SettingDeclaration{
|
||||
"font-size": {Kind: catalogue.KindPreference, Default: float64(10), Why: "readable at 100 DPI"},
|
||||
"width": {Kind: catalogue.KindPreference, Default: float64(250), Why: "forty characters"},
|
||||
}}
|
||||
on := map[string][]catalogue.SettingSource{
|
||||
"laptop": catalogue.Effective(m, []catalogue.Layer{{From: "laptop", Values: map[string]any{"font-size": float64(16)}}}),
|
||||
"desk": catalogue.Effective(m, []catalogue.Layer{{From: catalogue.MeshWideLayer, Values: map[string]any{"width": float64(300)}}}),
|
||||
}
|
||||
got := describePreferences([]preferencesOf{{Manifest: m, Nodes: []string{"desk", "laptop"}, On: on}})
|
||||
want := "dunst (on desk, laptop)\n" +
|
||||
" font-size, default 10: readable at 100 DPI\n" +
|
||||
" desk: 10 (default)\n" +
|
||||
" laptop: 16 (the node)\n" +
|
||||
" width, default 250: forty characters\n" +
|
||||
" desk: 300 (the mesh)\n" +
|
||||
" laptop: 250 (default)\n"
|
||||
if got != want {
|
||||
t.Fatalf("said\n%s\nwant\n%s", got, want)
|
||||
}
|
||||
if describePreferences(nil) != "no module declares a preference\n" {
|
||||
t.Fatal("an empty listing")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user