Files
skillshare/internal/plugin/files_test.go
T
Willie ece7090b1b fix: write every YAML file with 2-space indentation
yaml.Marshal defaults to 4 spaces, and only the config package forced 2.
Saving a plugin re-encodes the whole config.yaml, so installing one
reindented a user's entire file; skill/agent frontmatter and
audit-rules.yaml were written with 4 spaces too.

Route all YAML writers (config, registry, plugins, MCP, frontmatter,
audit rules) through one utils.MarshalYAML so they cannot drift apart.
2026-09-21 16:54:13 +08:00

60 lines
2.3 KiB
Go

package plugin
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
func TestFilesListsTheSnapshotAndReadsOnlyInsideIt(t *testing.T) {
home := t.TempDir()
s := &Service{ConfigPath: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state")}
writePluginFile(t, home, "config.yaml", "plugins:\n packages:\n demo:\n bindings:\n claude:\n id: demo@skillshare-abc\n source: /src/demo\n plugin: demo\n imported:\n bindings:\n claude:\n id: other@market\n")
content := filepath.Join(s.snapshotPath(Binding{ID: "demo@skillshare-abc", Source: "/src/demo", Plugin: "demo"}, "claude"), "content")
for path, body := range map[string]string{"skills/hi/SKILL.md": "# hi", "node_modules/dep/index.js": "x", "../secret": "no"} {
full := filepath.Join(content, filepath.FromSlash(path))
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(full, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
files, err := s.Files("demo")
if err != nil || !slices.Equal(files, []string{"skills/hi/SKILL.md"}) {
t.Fatalf("files = %v, %v", files, err)
}
if data, err := s.ReadFile("demo", "skills/hi/SKILL.md"); err != nil || string(data) != "# hi" {
t.Fatalf("read = %q, %v", data, err)
}
if _, err := s.ReadFile("demo", "../secret"); err == nil {
t.Fatal("a path outside the snapshot was read")
}
if files, err := s.Files("imported"); err != nil || len(files) != 0 {
t.Fatalf("an imported plugin has no snapshot: %v, %v", files, err)
}
}
func TestSaveKeepsTwoSpaceIndent(t *testing.T) {
s := &Service{ConfigPath: filepath.Join(t.TempDir(), "config.yaml")}
raw := []byte("ignore:\n - '**/.git/**'\n")
if err := os.WriteFile(s.ConfigPath, raw, 0644); err != nil {
t.Fatal(err)
}
d, err := decodeDocument(raw)
if err != nil {
t.Fatal(err)
}
d.packages["demo"] = Package{Bindings: map[string]Binding{"claude": {ID: "demo@local", Components: []string{"skills"}}}}
if err := s.save(d); err != nil {
t.Fatal(err)
}
data, _ := os.ReadFile(s.ConfigPath)
want := "ignore:\n - '**/.git/**'\nplugins:\n packages:\n demo:\n bindings:\n claude:\n components:\n - skills\n"
if !strings.HasPrefix(string(data), want) {
t.Errorf("got:\n%s\nwant prefix:\n%s", data, want)
}
}