Files
skillshare/internal/sync/relative_test.go
T
Willie 8e9ef1c806 test(sync): make the tests pass on Windows and run them in CI
Ten tests assumed POSIX behaviour:

- Five asserted os.ModeSymlink, but Windows links with a junction;
  check utils.IsLinkMode instead.
- Two used `/abs/...` as an absolute path, which has no volume on
  Windows; give the fixtures one.
- Windows has no permission bits to preserve, so the 0600 check is
  skipped there.
- Reading a file as a directory reports not-exist on Windows, which
  prune ignores, so that prune-error test is skipped there.
- A single-file extension relies on the shebang and the executable bit,
  so ListExtensions finds only directory extensions on Windows, as the
  extras docs describe.

Add ./internal/sync and ./internal/trash to the windows-test package
list so they stay green.

Refs #344
2026-10-06 09:40:50 +08:00

232 lines
6.5 KiB
Go

package sync
import (
"os"
"path/filepath"
"testing"
)
func TestShouldUseRelative(t *testing.T) {
tests := []struct {
name string
projectRoot string
sourcePath string
targetPath string
want bool
}{
{
name: "both under project root",
projectRoot: "/home/user/project",
sourcePath: "/home/user/project/.skillshare/skills/foo",
targetPath: "/home/user/project/.claude/skills",
want: true,
},
{
name: "source outside project root",
projectRoot: "/home/user/project",
sourcePath: "/opt/shared/skills/foo",
targetPath: "/home/user/project/.claude/skills",
want: false,
},
{
name: "target outside project root",
projectRoot: "/home/user/project",
sourcePath: "/home/user/project/.skillshare/skills/foo",
targetPath: "/opt/shared/claude/skills",
want: false,
},
{
name: "empty project root (global mode)",
projectRoot: "",
sourcePath: "/home/user/.config/skillshare/skills/foo",
targetPath: "/home/user/.claude/skills",
want: false,
},
{
name: "both outside project root",
projectRoot: "/home/user/project",
sourcePath: "/opt/a",
targetPath: "/opt/b",
want: false,
},
{
name: "project root itself as source",
projectRoot: "/home/user/project",
sourcePath: "/home/user/project",
targetPath: "/home/user/project/.claude/skills",
want: true,
},
{
name: "filesystem root as project root",
projectRoot: "/",
sourcePath: "/skills/foo",
targetPath: "/claude/skills",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := shouldUseRelative(tt.projectRoot, tt.sourcePath, tt.targetPath)
if got != tt.want {
t.Errorf("shouldUseRelative(%q, %q, %q) = %v, want %v",
tt.projectRoot, tt.sourcePath, tt.targetPath, got, tt.want)
}
})
}
}
func TestShouldUseRelative_CleansPaths(t *testing.T) {
got := shouldUseRelative(
"/home/user/project/",
"/home/user/project/.skillshare/skills/../skills/foo",
"/home/user/project/.claude/skills/",
)
if !got {
t.Error("expected true for unclean but valid paths under project root")
}
}
func TestShouldUseRelative_SymlinkedProjectRoot(t *testing.T) {
tmp, _ := filepath.EvalSymlinks(t.TempDir())
realProject := filepath.Join(tmp, "real-project")
os.MkdirAll(filepath.Join(realProject, ".skillshare", "skills"), 0755)
os.MkdirAll(filepath.Join(realProject, ".claude", "skills"), 0755)
symlinkProject := filepath.Join(tmp, "workspace")
if err := os.Symlink(realProject, symlinkProject); err != nil {
t.Skip("symlinks not supported:", err)
}
// Symlinked root: both resolve under the real project
got := shouldUseRelative(
symlinkProject,
filepath.Join(symlinkProject, ".skillshare", "skills"),
filepath.Join(symlinkProject, ".claude", "skills"),
)
if !got {
t.Error("expected true: both paths under symlinked project root")
}
}
func TestShouldUseRelative_DivergentSymlink(t *testing.T) {
tmp, _ := filepath.EvalSymlinks(t.TempDir())
realProject := filepath.Join(tmp, "project")
otherDir := filepath.Join(tmp, "other")
os.MkdirAll(filepath.Join(realProject, ".skillshare", "skills"), 0755)
os.MkdirAll(filepath.Join(otherDir, "skills"), 0755)
// Symlink .claude/skills to outside the project
os.MkdirAll(filepath.Join(realProject, ".claude"), 0755)
if err := os.Symlink(filepath.Join(otherDir, "skills"), filepath.Join(realProject, ".claude", "skills")); err != nil {
t.Skip("symlinks not supported:", err)
}
// Target resolves to outside project root → should use absolute
got := shouldUseRelative(
realProject,
filepath.Join(realProject, ".skillshare", "skills"),
filepath.Join(realProject, ".claude", "skills"),
)
if got {
t.Error("expected false: target symlink resolves outside project root")
}
}
// posixAbs gives a slash-form path the volume it needs to be absolute on Windows.
func posixAbs(p string) string {
return filepath.VolumeName(os.TempDir()) + filepath.FromSlash(p)
}
func TestResolveReadlink(t *testing.T) {
tests := []struct {
name string
dest string
linkPath string
want string
}{
{
"absolute dest unchanged",
posixAbs("/abs/source/skill"),
posixAbs("/project/.claude/skills/skill"),
posixAbs("/abs/source/skill"),
},
{
"relative dest resolved from link parent",
"../../.skillshare/skills/skill",
posixAbs("/project/.claude/skills/skill"),
posixAbs("/project/.skillshare/skills/skill"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := resolveReadlink(tt.dest, tt.linkPath)
if got != tt.want {
t.Errorf("resolveReadlink(%q, %q) = %q, want %q", tt.dest, tt.linkPath, got, tt.want)
}
})
}
}
func TestLinkNeedsReformat(t *testing.T) {
tests := []struct {
name string
dest string
wantRelative bool
expected bool
}{
{"absolute dest wants relative", posixAbs("/abs/path/to/skill"), true, true},
{"absolute dest wants absolute", posixAbs("/abs/path/to/skill"), false, false},
{"relative dest wants relative", "../../.skillshare/skills/foo", true, false},
{"relative dest wants absolute", "../../.skillshare/skills/foo", false, true},
{"empty dest", "", true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := linkNeedsReformat(tt.dest, tt.wantRelative); got != tt.expected {
t.Errorf("linkNeedsReformat(%q, %v) = %v, want %v", tt.dest, tt.wantRelative, got, tt.expected)
}
})
}
}
func TestReformatLink(t *testing.T) {
tmp, _ := filepath.EvalSymlinks(t.TempDir())
source := filepath.Join(tmp, "source")
os.MkdirAll(source, 0755)
link := filepath.Join(tmp, "link")
os.Symlink(source, link)
// Verify setup: absolute link
dest, _ := os.Readlink(link)
if !filepath.IsAbs(dest) {
t.Fatal("setup: link should be absolute")
}
// Reformat to relative (atomic)
if err := reformatLink(link, source, true); err != nil {
t.Fatalf("reformatLink: %v", err)
}
// Verify: now relative
dest, _ = os.Readlink(link)
if filepath.IsAbs(dest) {
t.Errorf("after reformat, link should be relative, got %q", dest)
}
// Verify: still resolves correctly
resolved, err := filepath.EvalSymlinks(link)
if err != nil {
t.Fatalf("symlink should resolve: %v", err)
}
if resolved != source {
t.Errorf("resolved = %q, want %q", resolved, source)
}
// Verify: no temp file left behind
if _, err := os.Lstat(link + ".ss-reformat"); err == nil {
t.Error("temp file should be cleaned up")
}
}