A project is often a repository someone else wrote. A link in it was followed wherever it pointed: CLAUDE.md linked to ~/.zshrc had the user's project instructions (`make` in backticks included) appended to the shell's startup file, a .mcp.json linked out had the project's servers and their keys written where the link's author chose, and a .claude folder linked to a shared one got the skills and settings. Only the agents' user-wide instruction files were refused. Project.inside now checks every file magpie writes or removes in a project, links followed, before the write: instructions, MCP files, Codex's per-project off list, Claude Code's settings.local.json, subagents, the old .gitignore lines, and the folder a skill's link is placed in (the entry itself is magpie's link to the library). A link to nothing is refused too, since where a write through it lands can't be told first. A link that stays inside the project (AGENTS.md to docs/CLAUDE.md) still works. Each refusal is a problem on the project, and what magpie wrote before is kept on record. Run (fix worktree at origin/main7b552eef3plus this change): - go test -count=1 ./internal/library/ ok on mica (macOS) and on WSL magpie-desktop (Linux). - Windows (xiaomi-gpu): every test passes but TestDsbotLibraryTarget, TestDshNotOnDsbotHome and TestEveryAgentInLibrary, which fail the same at origin/main7b552eef3run alone there; filed as #6995. - go vet ./internal/library/ for darwin, windows and linux; gofmt -l clean. - Broken by hand: with inside() returning nil, the three new *StayInTheProject tests go red (the user's text lands in ~/.zshrc, a link to nothing is made, .claude's target gets skills/, the outside file gets the fs server). Not tried: the GUI; a real repository with such links beyond the tests.
108 lines
3.0 KiB
Go
108 lines
3.0 KiB
Go
package library
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func symlinkAt(t *testing.T, to, at string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(at), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(to, at); err != nil {
|
|
t.Skip("no symlinks here:", err)
|
|
}
|
|
}
|
|
|
|
func problemAbout(r *Result, what string) bool {
|
|
for _, p := range r.Problems {
|
|
if strings.HasPrefix(p.What, what) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// A project is often a repository someone else wrote. A link in it that
|
|
// leads out of the project (CLAUDE.md to the user's ~/.zshrc) never has
|
|
// magpie write the user's instructions where it points, and a link to
|
|
// nothing never has magpie make the file it names.
|
|
func TestProjectInstructionsStayInTheProject(t *testing.T) {
|
|
h, dir := instrProject(t)
|
|
rc := filepath.Join(h, ".zshrc")
|
|
write(t, rc, "export A=1\n")
|
|
symlinkAt(t, rc, filepath.Join(dir, "CLAUDE.md"))
|
|
nothing := filepath.Join(h, "made-by-magpie")
|
|
symlinkAt(t, nothing, filepath.Join(dir, "AGENTS.md"))
|
|
text := "Run `make` first."
|
|
r, err := ProjectInstructions(dir, ProjectInstr{Text: &text, Agents: &[]string{"claude", "codex"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !problemAbout(r, "project:instructions") {
|
|
t.Errorf("problems: %+v", r.Problems)
|
|
}
|
|
if got := read(t, rc); got != "export A=1\n" {
|
|
t.Errorf("~/.zshrc = %q", got)
|
|
}
|
|
gone(t, nothing)
|
|
}
|
|
|
|
// A link inside the project (AGENTS.md to CLAUDE.md) still takes them.
|
|
func TestProjectInstructionsThroughALinkInside(t *testing.T) {
|
|
_, dir := instrProject(t)
|
|
write(t, filepath.Join(dir, "docs", "CLAUDE.md"), "Mine.\n")
|
|
symlinkAt(t, filepath.Join("docs", "CLAUDE.md"), filepath.Join(dir, "CLAUDE.md"))
|
|
setInstr(t, dir, "Here.", "claude")
|
|
if got := read(t, filepath.Join(dir, "docs", "CLAUDE.md")); !strings.Contains(got, block("Here.")) {
|
|
t.Errorf("docs/CLAUDE.md = %q", got)
|
|
}
|
|
}
|
|
|
|
// A folder of the project's that is a link out of it (.claude to a folder
|
|
// anyone can read) gets no skill and no settings of the user's.
|
|
func TestProjectSkillsStayInTheProject(t *testing.T) {
|
|
_, proj := projectLib(t)
|
|
out := filepath.Join(t.TempDir(), "shared")
|
|
symlinkAt(t, out, filepath.Join(proj, ".claude"))
|
|
if err := os.MkdirAll(out, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := ProjectSkill(proj, "pdf", []string{"claude"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !problemAbout(r, "project:pdf") {
|
|
t.Errorf("problems: %+v", r.Problems)
|
|
}
|
|
if es, _ := os.ReadDir(out); len(es) != 0 {
|
|
t.Errorf("%s has %v", out, es)
|
|
}
|
|
}
|
|
|
|
// A project's .mcp.json that leads out of it gets none of the servers
|
|
// (and none of their keys).
|
|
func TestProjectServersStayInTheProject(t *testing.T) {
|
|
h, proj := mcpProject(t)
|
|
out := filepath.Join(h, "public.json")
|
|
write(t, out, "{}\n")
|
|
mcp := filepath.Join(proj, ".mcp.json")
|
|
if err := os.Remove(mcp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
symlinkAt(t, out, mcp)
|
|
r, err := ProjectServer(proj, "fs", []string{"claude"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(r.Problems) == 0 {
|
|
t.Error("no problem said")
|
|
}
|
|
if got := read(t, out); got != "{}\n" {
|
|
t.Errorf("%s = %q", out, got)
|
|
}
|
|
}
|