wocao
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEnsureHookTemplatePassesTypeAndRepo(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
dir, err := EnsureHookTemplate(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := os.ReadFile(filepath.Join(dir, "hooks", "post-receive"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
text := string(body)
|
||||
for _, want := range []string{`post-receive "$repo" "$@"`, `repo=${repo_dir#"$SIMPLEGIT_REPO_ROOT"/}`, `repo=${repo%.git}`} {
|
||||
if !strings.Contains(text, want) {
|
||||
t.Errorf("generated hook missing %q:\n%s", want, text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureHookTemplateRefreshesExistingRepos(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
hook := filepath.Join(root, "repos", "alice", "demo.git", "hooks", "post-receive")
|
||||
if err := os.MkdirAll(filepath.Dir(hook), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(hook, []byte("old"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := EnsureHookTemplate(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := os.ReadFile(hook)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), `post-receive "$repo"`) {
|
||||
t.Fatalf("existing hook was not refreshed: %s", body)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user