Files
magpie/internal/edit/symlink_test.go
T
yetone 0361189f9e test: every package's tests pass on Windows, and none writes outside its sandbox (the user: 把 Windows 上那几个失败的测试修掉) — on a Windows box the suite failed in most packages, and the root package hung for 25 minutes. Tests set HOME, which os.UserHomeDir ignores on Windows (it reads USERPROFILE), so they read and wrote the runner's own home; each now sets USERPROFILE beside HOME, and Devin's, Goose's, Crush's and Claude Desktop's tests set APPDATA/LOCALAPPDATA, where those keep their files there. said() read the command's output only once it returned, and a pipe holds a few KB on Windows, so magpie model --help blocked forever; it reads while the command writes. Unix permission bits (the gateway key store's 0600 too), exec bits and symlinks Windows lacks are checked only off Windows; TOML strings hold Windows paths as literal strings, JSON as marshalled ones; a URL's path is compared as one. The provider and usage packages run in a home of their own (TestMain, as the gateway's already does), since a CLI's identity kept by a refresh still going when its test ended landed in the runner's ~/.config/magpie — and on a Mac in the user's own. Two product fixes came out of it: imagemcp took a file:///C:/… root or image for the path /C:/…, which doesn't exist on Windows, and now makes a Windows path of it (a host for a UNC share); and session discovery could keep a new Cowork session hidden on Linux, where a folder's mtime is a kernel tick and its size doesn't change, so an entry added within the tick of the last listing left both as they were — a listing made within 2s of its folder's last change is read again, as git does for racy files. TestFilePath and TestRacyDirectoryListedAgain fail without the change. On Windows 11 every package passes (32 of 32) and a run of each package in a fresh home leaves nothing in home, APPDATA or LOCALAPPDATA; on Linux (WSL) and macOS go test -tags nogui ./... passes. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build pass.
2026-10-01 23:15:07 +08:00

158 lines
4.3 KiB
Go

package edit
import (
"errors"
"os"
"path/filepath"
"runtime"
"testing"
)
// linkTo makes link a symlink to target, skipping where symlinks can't be
// made (Windows without the privilege).
func linkTo(t *testing.T, target, link string) {
t.Helper()
if err := os.Symlink(target, link); err != nil {
t.Skip("no symlinks here:", err)
}
}
func isLink(t *testing.T, p string) {
t.Helper()
fi, err := os.Lstat(p)
if err != nil || fi.Mode()&os.ModeSymlink == 0 {
t.Fatalf("%s is no longer a symlink (%v)", p, err)
}
}
// A config kept in a dotfiles repo and linked into place (#323): a write
// goes to the file it points at, the link stays, and the mode is the
// target's.
func TestWriteAtomicFollowsSymlink(t *testing.T) {
d := t.TempDir()
repo := filepath.Join(d, "dotfiles", "settings.json")
if err := os.MkdirAll(filepath.Dir(repo), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(repo, []byte("{\n \"model\": \"old\"\n}\n"), 0o600); err != nil {
t.Fatal(err)
}
conf := filepath.Join(d, "home", ".claude")
if err := os.MkdirAll(conf, 0o755); err != nil {
t.Fatal(err)
}
p := filepath.Join(conf, "settings.json")
linkTo(t, "../../dotfiles/settings.json", p) // relative, as stow makes them
if err := SetJSON(p, KV{Path: "model", Value: "new"}); err != nil {
t.Fatal(err)
}
isLink(t, p)
if got := read(t, repo); got != "{\n \"model\": \"new\"\n}\n" {
t.Fatalf("target: %q", got)
}
if st, _ := os.Stat(repo); runtime.GOOS != "windows" && st.Mode().Perm() != 0o600 { // Windows has no such bits
t.Errorf("mode %v", st.Mode().Perm())
}
if es, _ := os.ReadDir(conf); len(es) != 1 {
t.Errorf("left beside the link: %v", es)
}
// a link through a link, to a file not made yet: the file is made
// where the last one points
next := filepath.Join(d, "dotfiles", "new.md")
mid := filepath.Join(d, "mid.md")
linkTo(t, next, mid)
q := filepath.Join(conf, "CLAUDE.md")
linkTo(t, mid, q)
if err := WriteAtomic(q, []byte("hi\n")); err != nil {
t.Fatal(err)
}
isLink(t, q)
isLink(t, mid)
if got := read(t, next); got != "hi\n" {
t.Fatalf("dangling target: %q", got)
}
// links in a loop: an error, and the link left as it is
a, b := filepath.Join(d, "a"), filepath.Join(d, "b")
linkTo(t, b, a)
linkTo(t, a, b)
if err := WriteAtomic(a, []byte("x")); err == nil {
t.Error("a loop of links was written")
}
isLink(t, a)
}
// Emptying a linked file empties its target and keeps the link; a plain
// file still goes, and a link to nothing is left be.
func TestRemoveKeepsSymlink(t *testing.T) {
d := t.TempDir()
target := filepath.Join(d, "instructions.md")
if err := os.WriteFile(target, []byte("old\n"), 0o644); err != nil {
t.Fatal(err)
}
p := filepath.Join(d, "link.md")
linkTo(t, target, p)
if err := Remove(p); err != nil {
t.Fatal(err)
}
isLink(t, p)
if got := read(t, target); got != "" {
t.Fatalf("target kept %q", got)
}
gone := filepath.Join(d, "gone.md")
dangling := filepath.Join(d, "dangling.md")
linkTo(t, gone, dangling)
if err := Remove(dangling); err != nil {
t.Fatal(err)
}
isLink(t, dangling)
if _, err := os.Stat(gone); !errors.Is(err, os.ErrNotExist) {
t.Error("emptying a link to nothing made its target")
}
plain := tmpFile(t, "plain.md", "x")
if err := Remove(plain); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(plain); !errors.Is(err, os.ErrNotExist) {
t.Error("a plain file stayed")
}
}
// A failed edit puts a linked file back through its link, and takes away
// only the file it made behind a link to nothing, not the link.
func TestAtomicallyKeepsSymlink(t *testing.T) {
d := t.TempDir()
target := filepath.Join(d, "real.json")
if err := os.WriteFile(target, []byte("{}"), 0o644); err != nil {
t.Fatal(err)
}
p := filepath.Join(d, "link.json")
linkTo(t, target, p)
missing := filepath.Join(d, "missing.json")
q := filepath.Join(d, "dangling.json")
linkTo(t, missing, q)
err := Atomically(func() error {
if err := WriteAtomic(p, []byte("half")); err != nil {
return err
}
if err := WriteAtomic(q, []byte("half")); err != nil {
return err
}
return errors.New("fail")
}, p, q)
if err == nil || err.Error() != "fail" {
t.Fatalf("err %v", err)
}
isLink(t, p)
isLink(t, q)
if got := read(t, target); got != "{}" {
t.Errorf("target %q", got)
}
if _, err := os.Stat(missing); !errors.Is(err, os.ErrNotExist) {
t.Error("the file made behind the link stayed")
}
}