Files
magpie/internal/library/symlink_test.go
T
yetone ce9fc6758a library: on Windows without the right to make symlinks, skills are linked with junctions (#973) — with Developer Mode off and magpie not elevated, os.Symlink fails with "A required privilege is not held by the client": a skill installed from a folder became a detached copy, and bringing in a skill from ~/.agents/skills failed outright. The library's folder links (an agent's link to the library's skill, a folder source, a skill brought in from a link or the shared folder, a carried skill's folder, relink) now go through dirLink, which on that error makes a directory junction instead: one every user may make, followed by every program as a symlink to a folder is, made natively (FSCTL_SET_REPARSE_POINT, no mklink). A target a junction can't reach (a network share) still gets the old copy. Go reports a junction as irregular, not a symlink, so ours, unlink, carry's clearing of the library, a project's place and the CC Switch update now tell a link by linkEntry (symlink or junction) and take it away with os.Remove, leaving what it points at, even when where it points can't be read. On the Windows box with every privilege stripped from the token (CreateRestrictedToken DISABLE_MAX_PRIVILEGE, whoami /priv shows SeChangeNotify only) and an explicit sandbox environment, the library's tests went from 16 failures to none (TestSkillHow, TestProjectSkillsLinked, TestCarry, TestSkillCopyEditReachesTheOthers, TestImportSharedSkillForKimi …); with the privilege they pass too. TestJunctionIsALink (Windows: magpie's own, read through, unlinked without its target, none to a share) is new; the folder fixtures that ignored a failed os.Symlink now use dirLink, and the tests' link checks accept a junction. go test -race ./internal/library/ on the Mac, go vet, windows/linux(nogui) builds pass.
2026-10-06 13:32:03 +08:00

69 lines
2.1 KiB
Go

package library
import (
"os"
"path/filepath"
"strings"
"testing"
)
func linkIn(t *testing.T, target, link string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(target, link); err != nil {
t.Skip("no symlinks here:", err)
}
}
func stillLink(t *testing.T, p string) {
t.Helper()
fi, err := os.Lstat(p)
if err != nil || !linkEntry(fi) {
t.Fatalf("%s is no longer a symlink (%v)", p, err)
}
}
// library.json, library/instructions.md and an agent's own AGENTS.md
// linked in from a dotfiles repo (#323): saving writes the files they
// point at, and clearing the text empties them, the links staying.
func TestLinkedFilesStayLinked(t *testing.T) {
h := sandbox(t)
repo := filepath.Join(h, "dotfiles")
write(t, filepath.Join(repo, "library.json"), "{}\n")
write(t, filepath.Join(repo, "instructions.md"), "Old.\n")
write(t, filepath.Join(repo, "AGENTS.md"), "")
linkIn(t, filepath.Join(repo, "library.json"), path())
linkIn(t, filepath.Join(repo, "instructions.md"), setPath(defaultSet))
cx := filepath.Join(h, ".codex/AGENTS.md")
linkIn(t, filepath.Join(repo, "AGENTS.md"), cx)
shared := "Use tabs."
ok(t)(SaveInstructions(InstructionsChange{Shared: &shared, Agents: []string{"codex"}}))
for _, p := range []string{path(), setPath(defaultSet), cx} {
stillLink(t, p)
}
if s := read(t, filepath.Join(repo, "instructions.md")); s != "Use tabs.\n" {
t.Errorf("instructions.md target: %q", s)
}
if s := read(t, filepath.Join(repo, "library.json")); !strings.Contains(s, "codex") {
t.Errorf("library.json target: %s", s)
}
if s := read(t, filepath.Join(repo, "AGENTS.md")); !strings.Contains(s, "Use tabs.") {
t.Errorf("AGENTS.md target: %q", s)
}
empty := ""
ok(t)(SaveInstructions(InstructionsChange{Shared: &empty, Agents: []string{}}))
for _, p := range []string{path(), setPath(defaultSet), cx} {
stillLink(t, p)
}
if s := read(t, filepath.Join(repo, "instructions.md")); s != "" {
t.Errorf("cleared instructions.md target kept %q", s)
}
if s := read(t, filepath.Join(repo, "AGENTS.md")); s != "" {
t.Errorf("AGENTS.md target kept %q", s)
}
}