Files
skillshare/cmd/skillshare/init_test.go
T
Willie 3e8b6e8bd9 fix(git): warn before staging first-level source links
Source links can record machine-local paths in Git. Share advisory indexed and ignored checks across CLI, init, and dashboard staging and previews, with scope-correct ignore patterns and manual untracking instructions, without changing staging behavior.
2026-10-05 14:11:57 +08:00

152 lines
5.0 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func envValue(env []string, key string) (string, bool) {
prefix := key + "="
for i := len(env) - 1; i >= 0; i-- {
item := env[i]
if strings.HasPrefix(item, prefix) {
return strings.TrimPrefix(item, prefix), true
}
}
return "", false
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v (%s)", args, err, strings.TrimSpace(string(out)))
}
return strings.TrimSpace(string(out))
}
func TestCommitSourceFiles_CommitFailureIsReturned(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
hookPath := filepath.Join(repo, ".git", "hooks", "pre-commit")
if err := os.WriteFile(hookPath, []byte("#!/bin/sh\nexit 1\n"), 0o755); err != nil {
t.Fatalf("write pre-commit hook: %v", err)
}
skillDir := filepath.Join(repo, "demo-skill")
if err := os.MkdirAll(skillDir, 0o755); err != nil {
t.Fatalf("mkdir skill dir: %v", err)
}
if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte("# Demo"), 0o644); err != nil {
t.Fatalf("write SKILL.md: %v", err)
}
err := commitSourceFiles(repo, repo)
if err == nil {
t.Fatal("expected commitSourceFiles to return error when git commit fails")
}
if !strings.Contains(err.Error(), "git commit failed") {
t.Fatalf("expected git commit failure message, got: %v", err)
}
}
func TestCommitSourceFiles_NoChangesReturnsNil(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
if err := os.WriteFile(filepath.Join(repo, ".gitignore"), []byte(".DS_Store\n"), 0o644); err != nil {
t.Fatalf("write .gitignore: %v", err)
}
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "initial")
if err := commitSourceFiles(repo, repo); err != nil {
t.Fatalf("expected nil error when nothing to commit, got: %v", err)
}
}
func TestRemoteFetchEnv_DisablesInteractivePrompts(t *testing.T) {
t.Setenv("GIT_SSH_COMMAND", "")
env := remoteFetchEnv("https://github.com/org/repo.git")
if v, ok := envValue(env, "GIT_TERMINAL_PROMPT"); !ok || v != "0" {
t.Fatalf("expected GIT_TERMINAL_PROMPT=0, got %q (present=%v)", v, ok)
}
if v, ok := envValue(env, "GIT_ASKPASS"); !ok || v != "" {
t.Fatalf("expected empty GIT_ASKPASS, got %q (present=%v)", v, ok)
}
if v, ok := envValue(env, "SSH_ASKPASS"); !ok || v != "" {
t.Fatalf("expected empty SSH_ASKPASS, got %q (present=%v)", v, ok)
}
if v, ok := envValue(env, "GIT_SSH_COMMAND"); !ok || !strings.Contains(v, "BatchMode=yes") {
t.Fatalf("expected default GIT_SSH_COMMAND with BatchMode=yes, got %q (present=%v)", v, ok)
}
if v, ok := envValue(env, "LC_ALL"); !ok || v != "C" {
t.Fatalf("expected LC_ALL=C for locale-independent git output, got %q (present=%v)", v, ok)
}
}
func TestRemoteFetchEnv_RespectsExistingSSHCommand(t *testing.T) {
custom := "ssh -i /tmp/custom-key"
t.Setenv("GIT_SSH_COMMAND", custom)
env := remoteFetchEnv("https://github.com/org/repo.git")
if v, ok := envValue(env, "GIT_SSH_COMMAND"); !ok || v != custom {
t.Fatalf("expected existing GIT_SSH_COMMAND to be preserved, got %q (present=%v)", v, ok)
}
}
// A skillshare folder that is a link (here a dangling one, which
// hasBuiltinSkill does not count) is refused before the download, so neither
// the install nor the fallback writes into its target.
func TestInstallBuiltinSkill_RefusesLinkedFolder(t *testing.T) {
source := t.TempDir()
external := filepath.Join(t.TempDir(), "elsewhere")
if err := os.Symlink(external, filepath.Join(source, "skillshare")); err != nil {
t.Fatal(err)
}
if _, err := installBuiltinSkill(source); err == nil || !strings.Contains(err.Error(), "is a link") {
t.Fatalf("installBuiltinSkill = %v, want a link refusal", err)
}
if _, err := os.Lstat(external); !os.IsNotExist(err) {
t.Fatalf("link target was created: %v", err)
}
}
func TestCommitSourceFiles_WarnsSourceLinkAtConfigRoot(t *testing.T) {
repo := t.TempDir()
skills := filepath.Join(repo, "skills")
if err := os.Mkdir(skills, 0755); err != nil {
t.Fatal(err)
}
runGit(t, repo, "init", "-q")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
if err := os.Symlink(t.TempDir(), filepath.Join(skills, "_dev-skills")); err != nil {
t.Fatal(err)
}
output := captureStdout(t, func() {
if err := commitSourceFiles(repo, skills); err != nil {
t.Fatal(err)
}
})
if strings.Count(output, "init will stage source link") != 1 || !strings.Contains(output, "/skills/_dev-skills") {
t.Fatalf("warning: %s", output)
}
if got := runGit(t, repo, "ls-tree", "--name-only", "HEAD", "skills/_dev-skills"); got != "skills/_dev-skills" {
t.Fatalf("link was not committed: %s", got)
}
}