|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestLoadConfigCopiesDefaultKeys(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + contents string |
| 13 | + customCtrl string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "no key configuration", |
| 17 | + }, |
| 18 | + { |
| 19 | + name: "omitted key category", |
| 20 | + contents: "[keys.global]\nCtrlR = \"customSubmit\"\n", |
| 21 | + customCtrl: "customSubmit", |
| 22 | + }, |
| 23 | + } |
| 24 | + |
| 25 | + for _, test := range tests { |
| 26 | + t.Run(test.name, func(t *testing.T) { |
| 27 | + original := DefaultKeys["url"]["Enter"] |
| 28 | + t.Cleanup(func() { |
| 29 | + DefaultKeys["url"]["Enter"] = original |
| 30 | + }) |
| 31 | + |
| 32 | + configFile := filepath.Join(t.TempDir(), "config.toml") |
| 33 | + if err := os.WriteFile(configFile, []byte(test.contents), 0600); err != nil { |
| 34 | + t.Fatal(err) |
| 35 | + } |
| 36 | + |
| 37 | + loaded, err := LoadConfig(configFile) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + if test.customCtrl != "" && loaded.Keys["global"]["CtrlR"] != test.customCtrl { |
| 42 | + t.Fatalf("custom CtrlR binding = %q, want %q", loaded.Keys["global"]["CtrlR"], test.customCtrl) |
| 43 | + } |
| 44 | + |
| 45 | + loaded.Keys["url"]["Enter"] = "changed" |
| 46 | + if DefaultKeys["url"]["Enter"] != original { |
| 47 | + t.Fatalf("DefaultKeys changed to %q after loaded config mutation", DefaultKeys["url"]["Enter"]) |
| 48 | + } |
| 49 | + |
| 50 | + reloaded, err := LoadConfig(configFile) |
| 51 | + if err != nil { |
| 52 | + t.Fatal(err) |
| 53 | + } |
| 54 | + if reloaded.Keys["url"]["Enter"] != original { |
| 55 | + t.Fatalf("subsequent load inherited %q, want %q", reloaded.Keys["url"]["Enter"], original) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
0 commit comments