vcom/internal/config/qa_rc2_test.go
vrubelroman 2e3c5959b6 test(qa): RC2 re-check of v0.2.8 (QA fixes, config, session, signals)
Re-verify QA-1..QA-11 fixes and the stage-2C config/session/signal changes
used by the old UI. New bugs QA-12..QA-19 are skip-tagged tests; QA-12
(SIGTERM can hang vcom: main and Bubble Tea both handle it) blocks the
release. The cross-device trash test now cleans the topdir trash it uses.
Report: plans/rework/05-qa-v0.2.8.md, section RC2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 08:56:17 +03:00

191 lines
6.3 KiB
Go

package config
// QA (v0.2.8 RC2): config loading and SetKey (stage 2C) from the user's
// side: symlinked/read-only/odd configs must never be lost or corrupted.
import (
"os"
"path/filepath"
"strings"
"testing"
)
func qaIsolate(t *testing.T) string {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_STATE_HOME", filepath.Join(home, ".local", "state"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("VCOM_CONFIG", "")
return home
}
func qaWrite(t *testing.T, p, s string, mode os.FileMode) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(s), mode); err != nil {
t.Fatal(err)
}
if err := os.Chmod(p, mode); err != nil {
t.Fatal(err)
}
}
// Dotfiles: the config is a symlink into a dotfiles repo. The link stays,
// the target gets the new theme, comments survive.
func TestQASetKeyThroughSymlink(t *testing.T) {
home := qaIsolate(t)
target := filepath.Join(home, "dotfiles", "vcom.toml")
qaWrite(t, target, "# mine\n[ui]\ntheme = \"old\" # c\n", 0o640)
link := filepath.Join(home, ".config", "vcom", "vcom.toml")
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink("../../dotfiles/vcom.toml", link); err != nil {
t.Fatal(err)
}
cfg, info, err := Load("")
if err != nil || !info.Found || cfg.UI.Theme != "old" {
t.Fatalf("load via symlink: %v %+v theme=%q", err, info, cfg.UI.Theme)
}
if _, err := SetKey(info.Path, "ui", "theme", "new"); err != nil {
t.Fatal(err)
}
if fi, err := os.Lstat(link); err != nil || fi.Mode()&os.ModeSymlink == 0 {
t.Fatal("symlink replaced by a regular file")
}
b, _ := os.ReadFile(target)
if string(b) != "# mine\n[ui]\ntheme = \"new\" # c\n" {
t.Fatalf("target content %q", b)
}
if fi, _ := os.Stat(target); fi.Mode().Perm() != 0o640 {
t.Fatalf("target mode changed to %o", fi.Mode().Perm())
}
}
// A user who made the config read-only (chmod a-w) expects vcom not to
// change it. v0.2.7 failed with EACCES; atomic rename now replaces it.
func TestQASetKeyReadOnlyConfig(t *testing.T) {
t.Skip("BUG QA-13 (P2): SetKey replaces a read-only (0444) config via temp+rename; v0.2.7 refused (EACCES). atomicfile.Write ignores the missing write permission (atomicfile.go:40-52)")
home := qaIsolate(t)
p := filepath.Join(home, ".config", "vcom", "vcom.toml")
qaWrite(t, p, "[ui]\ntheme = \"locked\"\n", 0o444)
if _, err := SetKey(p, "ui", "theme", "other"); err == nil {
b, _ := os.ReadFile(p)
t.Fatalf("read-only config was rewritten: %q", b)
}
}
// Valid TOML forms of the same key: dotted key at the root and an inline
// table. SetKey must either edit them or fail without touching the file;
// failing means the theme silently stops being saved for these users.
func TestQASetKeyDottedAndInlineForms(t *testing.T) {
t.Skip("BUG QA-14 (P2): SetKey cannot save the theme when the config uses `ui.theme = ...` at the root or `ui = { theme = ... }`: it appends a second [ui] table, verify() rejects the result, theme change reports 'save failed' (setkey.go:86-152)")
for name, content := range map[string]string{
"dotted": "ui.theme = \"old\"\nui.icon_mode = \"ascii\"\n",
"inline": "ui = { theme = \"old\" }\n",
} {
t.Run(name, func(t *testing.T) {
home := qaIsolate(t)
p := filepath.Join(home, "vcom.toml")
qaWrite(t, p, content, 0o644)
if _, _, err := Parse([]byte(content)); err != nil {
t.Fatalf("fixture must be valid: %v", err)
}
if _, err := SetKey(p, "ui", "theme", "new"); err != nil {
t.Fatalf("SetKey on a valid %s config: %v", name, err)
}
b, _ := os.ReadFile(p)
c, _, err := Parse(b)
if err != nil || c.UI.Theme != "new" {
t.Fatalf("result %q: theme=%q err=%v", b, c.UI.Theme, err)
}
})
}
}
// Even when SetKey fails on such a file, it must stay byte-identical.
func TestQASetKeyFailureLeavesFileIntact(t *testing.T) {
for _, content := range []string{
"ui.theme = \"old\"\n",
"ui = { theme = \"old\" }\n",
"[ui]\ntheme = \"\"\"multi\nline\"\"\"\n",
"[ui\ntheme = 1\n", // broken
} {
home := qaIsolate(t)
p := filepath.Join(home, "vcom.toml")
qaWrite(t, p, content, 0o600)
_, _ = SetKey(p, "ui", "theme", "new")
b, _ := os.ReadFile(p)
c, _, perr := Parse(b)
if string(b) != content && (perr != nil || c.UI.Theme != "new") {
t.Fatalf("file damaged: before %q after %q", content, b)
}
if left, _ := filepath.Glob(filepath.Join(home, ".vcom.toml.tmp-*")); len(left) != 0 {
t.Fatalf("temp files left: %v", left)
}
}
}
// UTF-8 BOM and CRLF (configs edited on Windows / by some editors).
func TestQASetKeyBOM(t *testing.T) {
home := qaIsolate(t)
p := filepath.Join(home, "vcom.toml")
content := "\ufeff[ui]\r\ntheme = \"old\"\r\n"
qaWrite(t, p, content, 0o644)
if _, _, err := Parse([]byte(content)); err != nil {
t.Skipf("go-toml does not accept a BOM: %v", err)
}
_, err := SetKey(p, "ui", "theme", "new")
b, _ := os.ReadFile(p)
if err != nil {
if string(b) != content {
t.Fatalf("failed SetKey changed the file: %q", b)
}
t.Logf("SetKey refuses a BOM config: %v", err)
return
}
if c, _, err := Parse(b); err != nil || c.UI.Theme != "new" || strings.Count(string(b), "[ui]") != 1 {
t.Fatalf("BOM config result %q (err %v)", b, err)
}
}
// Load: a directory or an unreadable file in place of the config gives
// defaults plus an error, never a crash.
func TestQALoadDirAndUnreadable(t *testing.T) {
home := qaIsolate(t)
p := filepath.Join(home, ".config", "vcom", "vcom.toml")
if err := os.MkdirAll(p, 0o755); err != nil {
t.Fatal(err)
}
cfg, _, err := Load("")
if err == nil {
t.Fatal("directory as config must report an error")
}
if cfg.UI.Theme != Default().UI.Theme {
t.Fatal("defaults not used")
}
os.Remove(p)
qaWrite(t, p, "[ui]\ntheme = \"x\"\n", 0o000)
if os.Geteuid() == 0 {
t.Skip("root reads 0000 files")
}
if _, _, err := Load(""); err == nil {
t.Fatal("unreadable config must report an error")
}
}
// -config / $VCOM_CONFIG pointing to a missing file: Load reports where it
// would be created (info.Path = that file).
func TestQALoadExplicitMissing(t *testing.T) {
home := qaIsolate(t)
want := filepath.Join(home, "custom", "vcom.toml")
t.Setenv("VCOM_CONFIG", want)
_, info, err := Load("")
if err != nil || info.Found || info.Path != want {
t.Fatalf("info=%+v err=%v", info, err)
}
}