Files
skillshare/internal/install/source_write_test.go
T
Willie a502e9316b feat(sourcefs): write through followed first-level source links
With follow_source_links on, discovery reports <source>/_dev-skills/foo,
but every write to it was refused: sourcefs rejects a link in the path, and
its source-root os.Root refuses descendants outside the source. Dashboard
edits, nested uninstall, regular skill replacement, and tracked installs
under such a link all failed, although writing through a link the user
placed is the agreed behavior (#410).

sourcefs.Open, Create, MkdirAllIn, and CheckMoveOut take the operation's
optional sourcewalk.Follow. A clean name <link>/<rest> whose first
component the policy follows is handled by a second os.Root opened at the
link's canonical target, with the same per-component link check on <rest>,
so a nested link or ".." still cannot escape the target. Names with ".."
keep the source root, which refuses them through the link. Without a
policy nothing changes.

The link entry itself stays a link: RemoveAll, Rename, and writes on it are
refused, install --track --force still refuses to replace it, and
CheckMoveOut still lets trash rename the link without following it.
MkdirAll on a followed link is a no-op, since it already is that directory;
install --into needs that. A rename across the link boundary is refused.

The install, update, uninstall, and server callers pass the same policy
their discovery uses, through InstallOptions.SourceFollow and the update
context. No raw-write allowance was needed.

Refs #274
2026-10-05 14:42:09 +08:00

93 lines
3.0 KiB
Go

package install
import (
"errors"
"os"
"path/filepath"
"testing"
"skillshare/internal/sourcefs"
"skillshare/internal/sourcewalk"
)
func TestSwapStagedIntoSource(t *testing.T) {
source := t.TempDir()
external := t.TempDir()
if err := os.WriteFile(filepath.Join(external, "SKILL.md"), []byte("external"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(external, filepath.Join(source, "linked")); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(source, "plain"), 0o755); err != nil {
t.Fatal(err)
}
stage := func() string {
dir := filepath.Join(t.TempDir(), "skill")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("staged"), 0o644); err != nil {
t.Fatal(err)
}
return dir
}
for _, dest := range []string{
filepath.Join(source, "linked"), // the skill is a link
filepath.Join(source, "linked", "child"), // the skill is below one
} {
if err := swapStagedIntoSource(source, stage(), dest, nil); !errors.Is(err, sourcefs.ErrLink) {
t.Errorf("swap into %s: got %v, want ErrLink", dest, err)
}
}
if data, _ := os.ReadFile(filepath.Join(external, "SKILL.md")); string(data) != "external" {
t.Fatalf("external skill changed: %q", data)
}
if entries, _ := os.ReadDir(external); len(entries) != 1 {
t.Fatalf("external tree changed: %v", entries)
}
if err := swapStagedIntoSource(source, stage(), filepath.Join(source, "plain"), nil); err != nil {
t.Fatalf("swap into a real directory: %v", err)
}
if data, _ := os.ReadFile(filepath.Join(source, "plain", "SKILL.md")); string(data) != "staged" {
t.Fatalf("plain skill = %q, want staged content", data)
}
}
func TestSwapStagedIntoSource_FollowedLinkReplacesInCheckout(t *testing.T) {
source := t.TempDir()
checkout := t.TempDir()
if err := os.MkdirAll(filepath.Join(checkout, "foo"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(checkout, "foo", "old.md"), []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Symlink(checkout, filepath.Join(source, "_dev-skills")); err != nil {
t.Fatal(err)
}
staged := filepath.Join(t.TempDir(), "foo")
if err := os.MkdirAll(staged, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(staged, "SKILL.md"), []byte("staged"), 0o644); err != nil {
t.Fatal(err)
}
follow := sourcewalk.NewFollow(source, nil)
if err := swapStagedIntoSource(source, staged, filepath.Join(source, "_dev-skills", "foo"), follow); err != nil {
t.Fatalf("swap below a followed link: %v", err)
}
if data, _ := os.ReadFile(filepath.Join(checkout, "foo", "SKILL.md")); string(data) != "staged" {
t.Fatalf("checkout skill = %q, want staged content", data)
}
if _, err := os.Stat(filepath.Join(checkout, "foo", "old.md")); !os.IsNotExist(err) {
t.Fatalf("old content kept: %v", err)
}
if info, err := os.Lstat(filepath.Join(source, "_dev-skills")); err != nil || info.Mode()&os.ModeSymlink == 0 {
t.Fatalf("link was replaced: %v", err)
}
}