vcom/internal/config/setkey_test.go
vrubelroman 28fd86b8d5 feat(config): non-fatal loading with warnings and in-place SetKey (2C.2)
Load finds the file through paths (no ./vcom.toml), returns defaults plus an
error for a broken file instead of exiting, warns about unknown and
deprecated keys (strict second pass), resets invalid values with a warning,
and aliases center_width_percent to ui.preview_width_percent. New keys from
design §9.2 get defaults. SetKey edits one line keeping comments, order and
unknown keys, appends missing keys/sections, creates the XDG file, refuses
a file that does not parse and verifies the result before an atomic write.
The old UI shows config errors in the status line and survives an unknown
theme name; the reference vcom.toml is documented and current.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:35:23 +03:00

152 lines
4.3 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)
}
}