initial commit [141.14.140.180,mike]

This commit is contained in:
mike committed 2026-10-22 13:21:28 +02:00
commit 156bb18c91
50 files changed
+13466

No files matched your search

+202
View File
@@ -0,0 +1,202 @@
package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
const sample = `# My Ghostty config
font-family = Hack Nerd Font Mono
font-size=15
keybind = a=new_tab
# split navigation
keybind = b=goto_split:next
theme = "Dracula"
# trailing comment
`
func parse(t *testing.T, s string) *File {
t.Helper()
return &File{Lines: Parse([]byte(s))}
}
func TestRoundTripPreservesEverything(t *testing.T) {
f := parse(t, sample)
if got := string(f.Bytes()); got != sample {
t.Fatalf("round trip changed the file:\n%s", got)
}
if v, _ := f.Get("font-size"); v != "15" {
t.Errorf("font-size = %q", v)
}
if v, _ := f.Get("theme"); v != `"Dracula"` {
t.Errorf("theme should keep its quotes, got %q", v)
}
if got := f.Keys(); strings.Join(got, ",") != "font-family,font-size,keybind,theme" {
t.Errorf("keys = %v", got)
}
}
func TestSetUpdatesInPlace(t *testing.T) {
f := parse(t, sample)
f.Set("font-size", "16")
want := strings.Replace(sample, "font-size=15", "font-size = 16", 1)
if got := string(f.Bytes()); got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
}
}
func TestSetAppendsAfterLastEntry(t *testing.T) {
f := parse(t, sample)
f.Set("cursor-style", "bar")
want := strings.Replace(sample, "theme = \"Dracula\"\n", "theme = \"Dracula\"\ncursor-style = bar\n", 1)
if got := string(f.Bytes()); got != want {
t.Fatalf("got:\n%s", got)
}
}
func TestSetRemovesDuplicates(t *testing.T) {
f := parse(t, "a = 1\nb = 2\na = 3\n")
f.Set("a", "4")
if got := string(f.Bytes()); got != "a = 4\nb = 2\n" {
t.Fatalf("got:\n%s", got)
}
}
func TestSetAllKeepsPositions(t *testing.T) {
tests := []struct {
name string
values []string
want string
}{
{
name: "append",
values: []string{"a=new_tab", "b=goto_split:next", "c=close_surface"},
want: "keybind = a=new_tab\n# split navigation\nkeybind = b=goto_split:next\nkeybind = c=close_surface\n",
},
{
name: "edit second",
values: []string{"a=new_tab", "b=goto_split:previous"},
want: "keybind = a=new_tab\n# split navigation\nkeybind = b=goto_split:previous\n",
},
{
name: "delete first",
values: []string{"b=goto_split:next"},
want: "# split navigation\nkeybind = b=goto_split:next\n",
},
{
name: "insert at front",
values: []string{"z=reset", "a=new_tab", "b=goto_split:next"},
want: "keybind = z=reset\nkeybind = a=new_tab\n# split navigation\nkeybind = b=goto_split:next\n",
},
{
name: "swap",
values: []string{"b=goto_split:next", "a=new_tab"},
// The comment stays attached to the entry it describes.
want: "# split navigation\nkeybind = b=goto_split:next\nkeybind = a=new_tab\n",
},
}
const base = "keybind = a=new_tab\n# split navigation\nkeybind = b=goto_split:next\n"
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := parse(t, base)
f.SetAll("keybind", tt.values)
if got := string(f.Bytes()); got != tt.want {
t.Fatalf("got:\n%s\nwant:\n%s", got, tt.want)
}
if got := f.GetAll("keybind"); strings.Join(got, "|") != strings.Join(tt.values, "|") {
t.Fatalf("values = %v", got)
}
})
}
}
func TestUnset(t *testing.T) {
f := parse(t, sample)
f.Unset("keybind")
if f.Has("keybind") {
t.Fatal("keybind still present")
}
if !strings.Contains(string(f.Bytes()), "# split navigation") {
t.Fatal("comments must be kept")
}
}
func TestFormatLineQuoting(t *testing.T) {
cases := map[string]string{
"bar": "k = bar",
"": `k = ""`,
" x": `k = " x"`,
`"quoted"`: `k = "quoted"`,
}
for in, want := range cases {
if got := FormatLine("k", in); got != want {
t.Errorf("FormatLine(%q) = %q, want %q", in, got, want)
}
}
}
func TestSaveBackupAndSymlink(t *testing.T) {
dir := t.TempDir()
real := filepath.Join(dir, "real.ghostty")
link := filepath.Join(dir, "config.ghostty")
if err := os.WriteFile(real, []byte("font-size = 12\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(real, link); err != nil {
t.Fatal(err)
}
f, err := Load(link)
if err != nil {
t.Fatal(err)
}
f.Set("font-size", "14")
if err := f.Save(true); err != nil {
t.Fatal(err)
}
if fi, err := os.Lstat(link); err != nil || fi.Mode()&os.ModeSymlink == 0 {
t.Fatal("symlink was replaced")
}
got, _ := os.ReadFile(real)
if string(got) != "font-size = 14\n" {
t.Fatalf("saved content = %q", got)
}
bak, _ := os.ReadFile(real + ".bak")
if string(bak) != "font-size = 12\n" {
t.Fatalf("backup content = %q", bak)
}
if fi, _ := os.Stat(real); fi.Mode().Perm() != 0o600 {
t.Fatalf("permissions changed to %v", fi.Mode().Perm())
}
if f.ChangedOnDisk() {
t.Fatal("file should not be reported as changed after our own save")
}
}
func TestLoadMissingFile(t *testing.T) {
f, err := Load(filepath.Join(t.TempDir(), "nope"))
if err != nil {
t.Fatal(err)
}
if f.Exists || len(f.Lines) != 0 {
t.Fatal("expected empty, non-existing file")
}
}
func TestDiffListsOnlyChanges(t *testing.T) {
old := []string{"super+d=new_split:right", "super+shift+d=new_split:down", "super+k=unbind"}
new := []string{"super+shift+d=new_split:down", "super+k=unbind", "super+d=unbind", `super+\=new_split:right`}
got := Diff(old, new)
want := []Change{{true, "super+d=new_split:right"}, {false, "super+d=unbind"}, {false, `super+\=new_split:right`}}
if len(got) != len(want) {
t.Fatalf("Diff = %v, want %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("Diff = %v, want %v", got, want)
}
}
}