Files
skillshare/internal/git/scope_test.go
T
Willie 16c6e8ec8d feat(server): git operations honor git_root scope + POST /git/root to switch scope
- extract shared git.InitScopeRepo/WriteScopeGitignore/EnsureLocalIdentity (UI-free)
- cmd init delegates to the shared helpers (no logic duplication)
- handler_git operations use EffectiveGitRoot; status exposes the scope
- new POST /api/git/root inits a repo at the target scope dir + persists git_root
2026-05-30 19:23:12 +08:00

72 lines
1.8 KiB
Go

package git
import (
"os"
"path/filepath"
"strings"
"testing"
)
func readGitignore(t *testing.T, dir string) string {
t.Helper()
b, err := os.ReadFile(filepath.Join(dir, ".gitignore"))
if err != nil {
t.Fatalf("read .gitignore: %v", err)
}
return string(b)
}
func TestWriteScopeGitignore_FreshRootExcludesConfig(t *testing.T) {
dir := t.TempDir()
if err := WriteScopeGitignore(dir, "root"); err != nil {
t.Fatal(err)
}
got := readGitignore(t, dir)
if !strings.Contains(got, "config.yaml") {
t.Errorf("root .gitignore must exclude config.yaml, got:\n%s", got)
}
}
func TestWriteScopeGitignore_FreshNonRootOmitsConfig(t *testing.T) {
dir := t.TempDir()
if err := WriteScopeGitignore(dir, "skills"); err != nil {
t.Fatal(err)
}
got := readGitignore(t, dir)
if strings.Contains(got, "config.yaml") {
t.Errorf("non-root .gitignore must not mention config.yaml, got:\n%s", got)
}
}
func TestWriteScopeGitignore_AppendsToExistingAtRoot(t *testing.T) {
dir := t.TempDir()
gi := filepath.Join(dir, ".gitignore")
if err := os.WriteFile(gi, []byte("node_modules/\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := WriteScopeGitignore(dir, "root"); err != nil {
t.Fatal(err)
}
got := readGitignore(t, dir)
if !strings.Contains(got, "config.yaml") {
t.Errorf("existing root .gitignore must gain config.yaml, got:\n%s", got)
}
if !strings.Contains(got, "node_modules/") {
t.Errorf("existing entries must be preserved, got:\n%s", got)
}
}
func TestWriteScopeGitignore_IdempotentAtRoot(t *testing.T) {
dir := t.TempDir()
if err := WriteScopeGitignore(dir, "root"); err != nil {
t.Fatal(err)
}
if err := WriteScopeGitignore(dir, "root"); err != nil {
t.Fatal(err)
}
got := readGitignore(t, dir)
if n := strings.Count(got, "config.yaml"); n != 1 {
t.Errorf("config.yaml must appear exactly once, got %d:\n%s", n, got)
}
}