Files
skillshare/internal/sourcefs/follow_test.go
T
Willie cd6e0165dd fix(uninstall): refuse skills at followed link roots
A root skill discovered through a followed source link occupies the link entry itself. Moving that entry to trash silently unlinks every nested skill instead of removing one skill.

Share the root-skill guard across single and batch API handlers and CLI uninstall, report the unlink action clearly, and preserve explicit unlink and nested skill removal.
2026-10-05 20:02:58 +08:00

151 lines
5.1 KiB
Go

package sourcefs
import (
"errors"
"os"
"path/filepath"
"testing"
"skillshare/internal/sourcewalk"
)
// followFixture reuses fixture's src/_f -> ext and opens src again with a
// policy that follows _f. ext/child holds a nested link escaping ext.
func followFixture(t *testing.T) (followed, plain *Root, ext string) {
t.Helper()
plain, ext = fixture(t)
outside := filepath.Join(filepath.Dir(ext), "outside")
mustWrite(t, filepath.Join(outside, "SKILL.md"), "outside")
if err := os.Symlink(outside, filepath.Join(ext, "child", "escape")); err != nil {
t.Fatal(err)
}
followed, err := Open(plain.Dir(), sourcewalk.NewFollow(plain.Dir(), nil))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { followed.Close() })
return followed, plain, ext
}
func TestFollowWritesLandInTarget(t *testing.T) {
r, _, ext := followFixture(t)
name := filepath.Join("_f", "child", "SKILL.md")
if err := r.WriteFileAtomic(name, []byte("edited"), 0o644); err != nil {
t.Fatal(err)
}
if data, _ := os.ReadFile(filepath.Join(ext, "child", "SKILL.md")); string(data) != "edited" {
t.Fatalf("checkout file = %q", data)
}
if err := r.MkdirAll("_f", 0o755); err != nil {
t.Fatalf("MkdirAll of the followed link: %v", err)
}
if err := r.MkdirAll(filepath.Join("_f", "new", "deep"), 0o755); err != nil {
t.Fatal(err)
}
if err := r.Rename(filepath.Join("_f", "new"), filepath.Join("_f", "renamed")); err != nil {
t.Fatal(err)
}
if err := r.RemoveAll(filepath.Join("_f", "renamed")); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(ext, "renamed")); !os.IsNotExist(err) {
t.Fatalf("renamed dir still exists: %v", err)
}
if err := CheckMoveOut(r.Dir(), filepath.Join(r.Dir(), "_f", "child"), sourcewalk.NewFollow(r.Dir(), nil)); err != nil {
t.Fatalf("nested move out: %v", err)
}
}
func TestFollowMoveInStaysInOpenedTargetAfterRebind(t *testing.T) {
r, _, ext := followFixture(t)
name := filepath.Join("_f", "child", "new")
if err := r.CheckNoLink(name); err != nil {
t.Fatal(err)
}
// Rebind the target pathname after the second root was opened.
other := filepath.Join(filepath.Dir(ext), "other")
mustWrite(t, filepath.Join(other, "child", "SKILL.md"), "other")
saved := ext + "-saved"
if err := os.Rename(ext, saved); err != nil {
t.Fatal(err)
}
if err := os.Symlink(other, ext); err != nil {
t.Fatal(err)
}
staged := filepath.Join(t.TempDir(), "staged")
mustWrite(t, filepath.Join(staged, "SKILL.md"), "staged")
if err := r.MoveIn(staged, name); err != nil {
t.Fatal(err)
}
if data, _ := os.ReadFile(filepath.Join(saved, "child", "new", "SKILL.md")); string(data) != "staged" {
t.Fatalf("opened checkout file = %q", data)
}
if _, err := os.Lstat(filepath.Join(other, "child", "new")); !os.IsNotExist(err) {
t.Fatalf("move landed in the rebound directory: %v", err)
}
if _, err := os.Lstat(staged); !os.IsNotExist(err) {
t.Fatalf("staged dir left behind: %v", err)
}
}
func TestFollowStillRefusesEscapes(t *testing.T) {
r, _, _ := followFixture(t)
for _, name := range []string{
"_f" + string(filepath.Separator) + ".." + string(filepath.Separator) + "x",
filepath.Join("_f", "child", "escape", "SKILL.md"),
} {
if err := r.WriteFile(name, []byte("x"), 0o644); err == nil {
t.Errorf("%s: write escaped", name)
}
}
if err := r.Rename(filepath.Join("_f", "child"), "moved"); err == nil {
t.Error("rename across the link boundary was allowed")
}
}
func TestFollowKeepsLinkEntrySemantics(t *testing.T) {
r, _, ext := followFixture(t)
// The link itself is never written through or removed through.
if err := r.RemoveAll("_f"); !errors.Is(err, ErrLink) {
t.Fatalf("RemoveAll(_f) = %v, want ErrLink", err)
}
if _, err := os.Stat(filepath.Join(ext, "SKILL.md")); err != nil {
t.Fatalf("checkout touched: %v", err)
}
if err := CheckMoveOut(r.Dir(), filepath.Join(r.Dir(), "_f"), sourcewalk.NewFollow(r.Dir(), nil)); err != nil {
t.Fatalf("link entry must stay movable to trash: %v", err)
}
}
func TestCheckSkillMoveOut_RefusesFollowedRootSkill(t *testing.T) {
r, _, ext := followFixture(t)
follow := sourcewalk.NewFollow(r.Dir(), nil)
link := filepath.Join(r.Dir(), "_f")
if err := CheckSkillMoveOut(r.Dir(), link, follow); !errors.Is(err, ErrLinkedSkillRoot) {
t.Fatalf("root skill move = %v, want ErrLinkedSkillRoot", err)
}
if err := CheckSkillMoveOut(r.Dir(), filepath.Join(link, "child"), follow); err != nil {
t.Fatalf("nested skill move: %v", err)
}
if err := CheckMoveOut(r.Dir(), link, follow); err != nil {
t.Fatalf("explicit link move: %v", err)
}
if err := os.Remove(filepath.Join(ext, "SKILL.md")); err != nil {
t.Fatal(err)
}
if err := CheckSkillMoveOut(r.Dir(), link, follow); err != nil {
t.Fatalf("container link move: %v", err)
}
}
func TestNoPolicyKeepsRefusals(t *testing.T) {
_, plain, _ := followFixture(t)
name := filepath.Join("_f", "child", "SKILL.md")
if err := plain.WriteFile(name, []byte("x"), 0o644); !errors.Is(err, ErrLink) {
t.Fatalf("WriteFile without policy = %v, want ErrLink", err)
}
if err := CheckMoveOut(plain.Dir(), filepath.Join(plain.Dir(), "_f", "child")); !errors.Is(err, ErrLink) {
t.Fatalf("CheckMoveOut without policy = %v, want ErrLink", err)
}
}