Files
skillshare/tests/integration/git_hooks_test.go
T
Rusty ShacklefordandClaude Opus 5.5 51cecbeab4 test(testutil): align sandbox paths with symlinked temp dirs (#610)
On macOS, TMPDIR sits under /var, a symlink to /private/var. Sandbox
paths kept the unresolved form while the CLI reports canonical paths,
so tests needed per-case EvalSymlinks workarounds. NewSandbox now
resolves its root once.

RunCLIInDir and RunCLIInDirWithInput pass an explicit Env, so os/exec
does not set PWD and the child inherits the test process's PWD. The CLI
then falls back to getcwd and reports the canonical path even when
invoked through a symlinked directory, unlike a real shell. Set PWD to
the requested dir so the helper matches shell behavior.

The affected integration tests now create explicit symlink aliases so
they keep covering aliased working directories and config paths.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 17:58:31 +08:00

79 lines
3.2 KiB
Go

//go:build !online
package integration
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"skillshare/internal/hooks"
)
func TestGitHooksCLILifecycle(t *testing.T) {
sb := newHooksSandbox(t)
defer sb.Cleanup()
gitConfigPath := filepath.Join(sb.Home, "custom.gitconfig")
if runtime.GOOS != "windows" {
aliasedHome := filepath.Join(sb.Root, "home-alias")
sb.CreateSymlink(sb.Home, aliasedHome)
gitConfigPath = filepath.Join(aliasedHome, "custom.gitconfig")
}
sb.SetEnv("GIT_CONFIG_GLOBAL", gitConfigPath)
sb.SetEnv("GIT_CONFIG_SYSTEM", filepath.Join(sb.Home, "system.gitconfig"))
sb.SetEnv("GIT_CONFIG_NOSYSTEM", "1")
path := filepath.Join(sb.Root, "git-entry.yaml")
sb.WriteFile(path, "bindings:\n git:\n commands:\n check:\n events: [pre-commit]\n command: '{files}/check.sh'\n files:\n check.sh: |\n #!/bin/sh\n exit 0\n")
file := filepath.Join(sb.Home, ".config", "git", "skillshare", "hooks.gitconfig")
sb.RunCLI("hooks", "add", "guard", "--file", path, "-g").AssertSuccess(t)
sb.RunCLI("hooks", "sync", "--dry-run", "-g").AssertSuccess(t)
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatal("source-only add or preview published hooks")
}
sb.RunCLI("hooks", "sync", "-g").AssertSuccess(t)
list := sb.RunCLI("hooks", "list", "--json", "-g")
list.AssertSuccess(t)
var inv hooks.Inventory
if err := json.Unmarshal([]byte(list.Stdout), &inv); err != nil {
t.Fatal(err)
}
// Destinations resolve existing parent aliases before reporting their target.
resolvedHome, err := filepath.EvalSymlinks(sb.Home)
if err != nil {
t.Fatal(err)
}
if inv.Git == nil || !inv.Git.Include.Owned || inv.Git.Include.Target != filepath.Join(resolvedHome, "custom.gitconfig") {
t.Fatalf("CLI inventory: %+v", inv.Git)
}
sb.RunCLI("hooks", "disable", "guard", "--sync", "-g").AssertSuccess(t)
if _, err := os.Stat(file); !os.IsNotExist(err) {
t.Fatal("disable did not remove owned hooks file")
}
sb.RunCLI("hooks", "enable", "guard", "--sync", "-g").AssertSuccess(t)
r := sb.RunCLI("hooks", "remove", "guard", "--keep-files", "-g")
r.AssertFailure(t)
r.AssertAnyOutputContains(t, "cannot keep files")
r = sb.RunCLI("hooks", "import", "--from", "git", "-g")
r.AssertFailure(t)
r.AssertAnyOutputContains(t, "not supported yet")
sb.RunCLI("hooks", "remove", "guard", "--sync", "-g").AssertSuccess(t)
// Project mode publishes only in common-dir config. Repository initialization
// runs before any hooks are present and never creates a commit.
root := filepath.Join(sb.Root, "repo")
sb.WriteFile(filepath.Join(root, ".skillshare", "config.yaml"), "targets: []\n")
cmd := exec.Command("git", "init", "--quiet", root)
cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL="+filepath.Join(sb.Home, "custom.gitconfig"), "GIT_CONFIG_NOSYSTEM=1")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("init: %v %s", err, out)
}
sb.RunCLIInDir(root, "hooks", "add", "guard", "--file", path, "--sync", "-p").AssertSuccess(t)
if !strings.Contains(sb.ReadFile(filepath.Join(root, ".git", "config")), "skillshare/hooks.gitconfig") {
t.Fatal("project include missing")
}
sb.RunCLIInDir(root, "hooks", "remove", "guard", "--sync", "-p").AssertSuccess(t)
}