Files
mikeandClaude Opus 5.5 df261910c5 Support Ghostty 1.4, version 1.0.2
The built-in schema now describes Ghostty 1.4.0: 212 options, the new
choices of copy-on-select, window-padding-balance = equal, number limits
that also take "unlimited", and categories for the new options.

genschema reads Ghostty's compatibility table, so old names and values
Ghostty still accepts are no longer reported as errors: a renamed key
such as scrollback-limit is shown in yellow and renamed in place with
enter, a replaced option names its successors, and old values such as
copy-on-select = true are accepted with a note.

Theme previews expand ~/ like Ghostty 1.4 and only look where Ghostty
looks for user themes. Theme names with parentheses of their own keep
them. A live test checks every ghostty command against the installed
Ghostty. Docs and screenshots updated for 1.4.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 10:44:23 +01:00

215 lines
5.9 KiB
Go

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 TestRenameKeepsLayout(t *testing.T) {
f := parse(t, "# limits\n scrollback-limit=100000\nfont-size = 15\nscrollback-limit = \"2000\"\n")
f.Rename("scrollback-limit", "scrollback-limit-bytes")
want := "# limits\n scrollback-limit-bytes=100000\nfont-size = 15\nscrollback-limit-bytes = \"2000\"\n"
if got := string(f.Bytes()); got != want {
t.Fatalf("got\n%s\nwant\n%s", got, want)
}
if f.Has("scrollback-limit") || len(f.GetAll("scrollback-limit-bytes")) != 2 {
t.Fatal("keys not renamed")
}
}
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)
}
}
}