mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
A --track install with an explicit kind skips discovery, so check the subdir after the tracked clone and remove it on refusal. Match a case variant of a gitlink path only when it opens the same directory, so Linux keeps a real Vendor/Up next to a vendor/up gitlink. Strip everything before the last @ in a submodule URL so malformed and scheme-relative values fail closed.
148 lines
5.3 KiB
Go
148 lines
5.3 KiB
Go
package install
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// newHubWithSubmodule builds a bare hub repo with its own skill at own/ and an
|
|
// upstream repo mounted as a submodule at vendor/up (holding skills/a).
|
|
func newHubWithSubmodule(t *testing.T) (hubURL, upURL string) {
|
|
t.Helper()
|
|
if _, err := exec.LookPath("git"); err != nil {
|
|
t.Skip("git not available")
|
|
}
|
|
tmp := t.TempDir()
|
|
commit := func(dir string, files map[string]string) {
|
|
mustRunGit(t, "", "init", "-q", dir)
|
|
mustRunGit(t, dir, "config", "user.email", "test@test.com")
|
|
mustRunGit(t, dir, "config", "user.name", "Test")
|
|
for name, body := range files {
|
|
path := filepath.Join(dir, name)
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
mustRunGit(t, dir, "add", ".")
|
|
mustRunGit(t, dir, "commit", "-q", "-m", "init")
|
|
}
|
|
|
|
up := filepath.Join(tmp, "up")
|
|
commit(up, map[string]string{"skills/a/SKILL.md": "# a"})
|
|
upURL = "file://" + filepath.ToSlash(up) // git records forward slashes on Windows too
|
|
|
|
work := filepath.Join(tmp, "work")
|
|
commit(work, map[string]string{"own/SKILL.md": "# own"})
|
|
mustRunGit(t, work, "-c", "protocol.file.allow=always", "submodule", "add", "-q", upURL, "vendor/up")
|
|
mustRunGit(t, work, "commit", "-q", "-m", "add submodule")
|
|
|
|
hub := filepath.Join(tmp, "hub.git")
|
|
mustRunGit(t, "", "clone", "-q", "--bare", work, hub)
|
|
return "file://" + hub, upURL
|
|
}
|
|
|
|
func hubSource(hubURL, subdir string) *Source {
|
|
return &Source{
|
|
Type: SourceTypeGitHTTPS,
|
|
Raw: hubURL + "/" + subdir,
|
|
CloneURL: hubURL,
|
|
Subdir: subdir,
|
|
Name: filepath.Base(subdir),
|
|
}
|
|
}
|
|
|
|
func TestDiscoverFromGitSubdir_RefusesSubmodulePaths(t *testing.T) {
|
|
hubURL, upURL := newHubWithSubmodule(t)
|
|
|
|
for _, subdir := range []string{"vendor/up", "vendor/up/skills/a", "vendor//up"} {
|
|
t.Run(subdir, func(t *testing.T) {
|
|
result, err := DiscoverFromGitSubdir(hubSource(hubURL, subdir))
|
|
if err == nil {
|
|
CleanupDiscovery(result)
|
|
t.Fatalf("expected an error, got %d skill(s)", len(result.Skills))
|
|
}
|
|
if !strings.Contains(err.Error(), "git submodule 'vendor/up'") || !strings.Contains(err.Error(), upURL) {
|
|
t.Fatalf("error should name the submodule and its upstream, got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInstall_RefusesSubmoduleSubdir(t *testing.T) {
|
|
hubURL, _ := newHubWithSubmodule(t)
|
|
dest := filepath.Join(t.TempDir(), "up")
|
|
|
|
_, err := Install(hubSource(hubURL, "vendor/up"), dest, InstallOptions{SkipAudit: true})
|
|
if err == nil || !strings.Contains(err.Error(), "git submodule") {
|
|
t.Fatalf("expected a submodule error, got: %v", err)
|
|
}
|
|
if _, statErr := os.Stat(dest); !os.IsNotExist(statErr) {
|
|
t.Fatalf("nothing should be installed at %s", dest)
|
|
}
|
|
}
|
|
|
|
func TestSubmoduleError_CaseVariantFollowsFilesystem(t *testing.T) {
|
|
hubURL, _ := newHubWithSubmodule(t)
|
|
repo := filepath.Join(t.TempDir(), "repo")
|
|
mustRunGit(t, "", "clone", "-q", hubURL, repo) // leaves vendor/up as an empty dir
|
|
|
|
_, statErr := os.Stat(filepath.Join(repo, "Vendor", "Up"))
|
|
foldsCase := statErr == nil
|
|
if err := submoduleError(repo, "Vendor/Up", nil); (err != nil) != foldsCase {
|
|
t.Fatalf("case-insensitive filesystem = %v, but submoduleError(Vendor/Up) = %v", foldsCase, err)
|
|
}
|
|
}
|
|
|
|
func TestInstallTrackedRepo_RefusesSubmoduleSubdir(t *testing.T) {
|
|
hubURL, _ := newHubWithSubmodule(t)
|
|
sourceDir := t.TempDir()
|
|
|
|
_, err := InstallTrackedRepo(hubSource(hubURL, "vendor/up"), sourceDir, InstallOptions{Name: "up", Force: true})
|
|
if err == nil || !strings.Contains(err.Error(), "git submodule") {
|
|
t.Fatalf("expected a submodule error, got: %v", err)
|
|
}
|
|
if _, statErr := os.Stat(filepath.Join(sourceDir, "_up")); !os.IsNotExist(statErr) {
|
|
t.Fatalf("the refused tracked clone should be removed, stat err = %v", statErr)
|
|
}
|
|
}
|
|
|
|
func TestGitlinkString_HidesURLCredentials(t *testing.T) {
|
|
for raw, want := range map[string]string{
|
|
"https://user:s3cret@example.com/org/up.git": "https://example.com/org/up.git",
|
|
"https://s3cret@example.com/org/up.git?token=s3cret": "https://example.com/org/up.git",
|
|
"https://user:s3 cret@example.com/org/up.git#s3cret": "https://example.com/org/up.git",
|
|
"../up.git?token=s3cret": "../up.git",
|
|
"s3cret@example.com:org/up.git": "example.com:org/up.git",
|
|
"https:/user:s3cret@example.com/org/up.git": "example.com/org/up.git",
|
|
"//user:s3cret@example.com/org/up.git": "example.com/org/up.git",
|
|
} {
|
|
got := gitlink{Path: "vendor/up", Commit: "abc", URL: raw}.String()
|
|
if strings.Contains(got, "s3cret") || !strings.Contains(got, want) {
|
|
t.Errorf("String() for %q = %q, want %q without credentials", raw, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDiscoverFromGit_WarnsSkippedSubmodule(t *testing.T) {
|
|
hubURL, upURL := newHubWithSubmodule(t)
|
|
|
|
result, err := DiscoverFromGit(&Source{Type: SourceTypeGitHTTPS, Raw: hubURL, CloneURL: hubURL, Name: "hub"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer CleanupDiscovery(result)
|
|
|
|
if len(result.Skills) != 1 || result.Skills[0].Name != "own" {
|
|
t.Fatalf("expected only the hub's own skill, got %+v", result.Skills)
|
|
}
|
|
if len(result.Warnings) != 1 || !strings.Contains(result.Warnings[0], "skipped git submodule 'vendor/up'") || !strings.Contains(result.Warnings[0], upURL) {
|
|
t.Fatalf("expected one submodule warning, got %q", result.Warnings)
|
|
}
|
|
}
|