mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
Merge-mode pruning for agents and extras removed any link whose name was not expected, so links a user made to agent or extra files outside the source were deleted on the next sync or `extras --remove-target --prune`. Agents and extras do not record their links in a manifest, so the #315 provenance check cannot be reused. Prune now removes a link only when it is broken or resolves inside the source. Relative links are resolved from the real parent directory, matching how createLink writes them, so orphans under a symlinked target directory are still found.
139 lines
3.8 KiB
Go
139 lines
3.8 KiB
Go
package sync
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestPruneExtraTarget_MergeRemovesSymlinksOnly verifies that merge-mode prune
|
|
// deletes skillshare symlinks but preserves the user's own real files.
|
|
func TestPruneExtraTarget_MergeRemovesSymlinksOnly(t *testing.T) {
|
|
src := t.TempDir()
|
|
tgt := t.TempDir()
|
|
|
|
srcFile := filepath.Join(src, "a.md")
|
|
if err := os.WriteFile(srcFile, []byte("x"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link := filepath.Join(tgt, "a.md")
|
|
if err := os.Symlink(srcFile, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := filepath.Join(tgt, "local.md")
|
|
if err := os.WriteFile(local, []byte("keep"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pruned, errs := PruneExtraTarget(tgt, src, "merge")
|
|
if len(errs) != 0 {
|
|
t.Fatalf("unexpected errors: %v", errs)
|
|
}
|
|
if pruned != 1 {
|
|
t.Errorf("expected 1 pruned, got %d", pruned)
|
|
}
|
|
if _, err := os.Lstat(link); !os.IsNotExist(err) {
|
|
t.Error("symlink should have been removed")
|
|
}
|
|
if _, err := os.Stat(local); err != nil {
|
|
t.Error("user's local file must be preserved")
|
|
}
|
|
}
|
|
|
|
// A link the user made to a file outside the extra's source is not skillshare's.
|
|
func TestPruneExtraTarget_MergeKeepsExternalLinks(t *testing.T) {
|
|
src := t.TempDir()
|
|
tgt := t.TempDir()
|
|
|
|
extFile := filepath.Join(t.TempDir(), "mine.md")
|
|
if err := os.WriteFile(extFile, []byte("mine"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link := filepath.Join(tgt, "mine.md")
|
|
if err := os.Symlink(extFile, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pruned, errs := PruneExtraTarget(tgt, src, "merge")
|
|
if len(errs) != 0 {
|
|
t.Fatalf("unexpected errors: %v", errs)
|
|
}
|
|
if pruned != 0 {
|
|
t.Errorf("expected 0 pruned, got %d", pruned)
|
|
}
|
|
if _, err := os.Lstat(link); err != nil {
|
|
t.Error("external link must be preserved")
|
|
}
|
|
}
|
|
|
|
func TestSyncExtra_MergeKeepsExternalLinks(t *testing.T) {
|
|
src, tgt := setupExtrasTest(t, map[string]string{"a.md": "a"})
|
|
|
|
extFile := filepath.Join(t.TempDir(), "mine.md")
|
|
if err := os.WriteFile(extFile, []byte("mine"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
link := filepath.Join(tgt, "mine.md")
|
|
if err := os.Symlink(extFile, link); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
result, err := SyncExtra(src, tgt, "merge", false, false, false, "", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Pruned != 0 {
|
|
t.Errorf("expected 0 pruned, got %d", result.Pruned)
|
|
}
|
|
if _, err := os.Lstat(link); err != nil {
|
|
t.Error("external link must be preserved")
|
|
}
|
|
}
|
|
|
|
func TestPruneExtraTargetFiles_CopyRemovesManagedOnly(t *testing.T) {
|
|
tgt := t.TempDir()
|
|
|
|
managed := filepath.Join(tgt, "managed.md")
|
|
if err := os.WriteFile(managed, []byte("managed"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
local := filepath.Join(tgt, "local.md")
|
|
if err := os.WriteFile(local, []byte("local"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pruned, errs := PruneExtraTargetFiles(tgt, "", "copy", map[string]bool{"managed.md": true})
|
|
if len(errs) != 0 {
|
|
t.Fatalf("unexpected errors: %v", errs)
|
|
}
|
|
if pruned != 1 {
|
|
t.Errorf("expected 1 pruned, got %d", pruned)
|
|
}
|
|
if _, err := os.Stat(managed); !os.IsNotExist(err) {
|
|
t.Errorf("managed file should have been removed, stat err = %v", err)
|
|
}
|
|
if _, err := os.Stat(local); err != nil {
|
|
t.Errorf("local file must be preserved, stat err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPruneExtraTargetFiles_SymlinkRequiresSymlink(t *testing.T) {
|
|
tgt := t.TempDir()
|
|
local := filepath.Join(tgt, "local.md")
|
|
if err := os.WriteFile(local, []byte("local"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
pruned, errs := PruneExtraTargetFiles(tgt, "", "symlink", nil)
|
|
if pruned != 0 {
|
|
t.Errorf("expected 0 pruned, got %d", pruned)
|
|
}
|
|
if len(errs) != 1 || !strings.Contains(errs[0], "target is not a symlink") {
|
|
t.Fatalf("expected target-is-not-symlink error, got %v", errs)
|
|
}
|
|
if _, err := os.Stat(local); err != nil {
|
|
t.Errorf("local file must be preserved, stat err = %v", err)
|
|
}
|
|
}
|