vcom/internal/config/setkey_test.go
vrubelroman 98aaa09a2b fix(config): keep read-only configs; edit dotted and inline ui tables (QA-13, QA-14)
atomicfile.Write refuses a target the process cannot write (EACCES, as a
plain write in v0.2.7 did) instead of replacing it by rename. SetKey edits
a section written in the root table as dotted keys (ui.theme = ...) or as
a one-line inline table (ui = { theme = ... }), adding the key in the same
form when missing; multi-line inline tables are refused without touching
the file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 09:10:05 +03:00

178 lines
5.5 KiB
Go

package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func writeFile(t *testing.T, p, s string) {
t.Helper()
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
func readFile(t *testing.T, p string) string {
t.Helper()
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
return string(b)
}
// diffLines returns the indexes of lines that differ (files of equal length).
func diffLines(t *testing.T, a, b string) []int {
t.Helper()
la, lb := strings.Split(a, "\n"), strings.Split(b, "\n")
if len(la) != len(lb) {
t.Fatalf("line count changed: %d -> %d\n%s", len(la), len(lb), b)
}
var d []int
for i := range la {
if la[i] != lb[i] {
d = append(d, i)
}
}
return d
}
func TestSetKeyPreservesComments(t *testing.T) {
orig, err := os.ReadFile("../../vcom.toml")
if err != nil {
t.Fatal(err)
}
// Plus an unknown key and an extra comment the user added.
before := string(orig) + "\n# my own note\n[extra]\nmine = 1 # keep\n"
p := filepath.Join(t.TempDir(), "vcom.toml")
writeFile(t, p, before)
if _, err := SetKey(p, "ui", "theme", "nord"); err != nil {
t.Fatal(err)
}
after := readFile(t, p)
d := diffLines(t, before, after)
if len(d) != 1 {
t.Fatalf("changed lines %v, want only the theme line:\n%s", d, after)
}
line := strings.Split(after, "\n")[d[0]]
if !strings.HasPrefix(line, "theme = 'nord'") {
t.Fatalf("theme line = %q (style of the original literal string expected)", line)
}
c, _, err := Parse([]byte(after))
if err != nil || c.UI.Theme != "nord" {
t.Fatalf("result: %v %+v", err, c.UI)
}
// A value with a trailing comment keeps the comment; a nested section works.
if _, err := SetKey(p, "browser.sort", "by", "size"); err != nil {
t.Fatal(err)
}
if !strings.Contains(readFile(t, p), "by = 'size' # name | modified") {
t.Fatalf("trailing comment lost:\n%s", readFile(t, p))
}
if _, err := SetKey(p, "ui", "pane_gap", 2); err != nil {
t.Fatal(err)
}
if !strings.Contains(readFile(t, p), "pane_gap = 2 # 0..4") {
t.Fatalf("int edit:\n%s", readFile(t, p))
}
}
func TestSetKeyMissingSection(t *testing.T) {
p := filepath.Join(t.TempDir(), "vcom.toml")
before := "# top comment\n[browser]\nshow_hidden = false # mine\n"
writeFile(t, p, before)
if _, err := SetKey(p, "ui", "theme", "nord"); err != nil {
t.Fatal(err)
}
after := readFile(t, p)
if !strings.HasPrefix(after, before) {
t.Fatalf("existing content changed:\n%s", after)
}
if !strings.HasSuffix(after, "\n[ui]\ntheme = \"nord\"\n") {
t.Fatalf("section not appended:\n%q", after)
}
// Missing key in an existing section: inserted after its last key.
writeFile(t, p, "[ui]\nborder = 'thin' # b\n\n# about browser\n[browser]\nshow_hidden = true\n")
if _, err := SetKey(p, "ui", "theme", "nord"); err != nil {
t.Fatal(err)
}
want := "[ui]\nborder = 'thin' # b\ntheme = \"nord\"\n\n# about browser\n[browser]\nshow_hidden = true\n"
if got := readFile(t, p); got != want {
t.Fatalf("got:\n%s\nwant:\n%s", got, want)
}
}
func TestSetKeyNoFile(t *testing.T) {
xdg := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", xdg)
t.Setenv("VCOM_CONFIG", "")
path, err := SetKey("", "ui", "theme", "nord")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(xdg, "vcom", "vcom.toml"); path != want {
t.Fatalf("created %s, want %s", path, want)
}
if got := readFile(t, path); got != "[ui]\ntheme = \"nord\"\n" {
t.Fatalf("content %q", got)
}
}
func TestSetKeyRefusesBrokenFile(t *testing.T) {
p := filepath.Join(t.TempDir(), "vcom.toml")
broken := "[ui]\ntheme = \"nord\nborder = 'x'\n"
writeFile(t, p, broken)
if _, err := SetKey(p, "ui", "theme", "dracula"); err == nil {
t.Fatal("SetKey edited a file that does not parse")
}
if readFile(t, p) != broken {
t.Fatal("broken file was modified")
}
// Unsupported shapes are refused, not mangled.
writeFile(t, p, "[ui]\ntheme = \"\"\"\nnord\"\"\"\n")
if _, err := SetKey(p, "ui", "theme", "x"); err == nil {
t.Fatal("multi-line string must be refused")
}
}
func TestSetKeyQuotingAndCRLF(t *testing.T) {
p := filepath.Join(t.TempDir(), "vcom.toml")
writeFile(t, p, "[ui]\r\ntheme = \"a#b\" # c\r\n")
if _, err := SetKey(p, "ui", "theme", `it's "q"`); err != nil {
t.Fatal(err)
}
if got := readFile(t, p); got != "[ui]\r\ntheme = \"it's \\\"q\\\"\" # c\r\n" {
t.Fatalf("got %q", got)
}
}
func TestSetKeyRootForms(t *testing.T) {
cases := map[string]struct{ in, want string }{
"dotted": {"ui.theme = 'old' # c\nui.icon_mode = \"ascii\"\n", "ui.theme = 'new' # c\nui.icon_mode = \"ascii\"\n"},
"dotted missing": {"ui.icon_mode = \"ascii\"\n[browser]\nshow_hidden = true\n", "ui.icon_mode = \"ascii\"\nui.theme = \"new\"\n[browser]\nshow_hidden = true\n"},
"inline": {"ui = { theme = \"old\", border = 'x' }\n", "ui = { theme = \"new\", border = 'x' }\n"},
"inline missing": {"ui = { border = 'x' }\n", "ui = { border = 'x', theme = \"new\" }\n"},
"inline empty": {"ui = {}\n", "ui = { theme = \"new\" }\n"},
}
for name, c := range cases {
p := filepath.Join(t.TempDir(), "vcom.toml")
writeFile(t, p, c.in)
if _, err := SetKey(p, "ui", "theme", "new"); err != nil {
t.Fatalf("%s: %v", name, err)
}
if got := readFile(t, p); got != c.want {
t.Errorf("%s:\n got %q\nwant %q", name, got, c.want)
}
}
// A multi-line inline table is refused, file untouched.
p := filepath.Join(t.TempDir(), "vcom.toml")
writeFile(t, p, "ui = {\n}\n")
if _, err := SetKey(p, "ui", "theme", "new"); err == nil {
t.Fatal("multi-line inline accepted")
}
}