Go · 2044 bytes Raw Blame History
1 // SPDX-License-Identifier: AGPL-3.0-or-later
2
3 package hooks_test
4
5 import (
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10
11 "github.com/tenseleyFlow/shithub/internal/git/hooks"
12 )
13
14 func TestInstall_WritesExecutableShim(t *testing.T) {
15 t.Parallel()
16 gitDir := t.TempDir()
17 bin := "/usr/local/bin/shithubd"
18
19 if err := hooks.Install(gitDir, bin); err != nil {
20 t.Fatalf("Install: %v", err)
21 }
22
23 for _, name := range hooks.SupportedHooks {
24 path := filepath.Join(gitDir, "hooks", name)
25 st, err := os.Stat(path)
26 if err != nil {
27 t.Fatalf("stat %s: %v", path, err)
28 }
29 if st.Mode().Perm()&0o111 == 0 {
30 t.Errorf("%s: not executable, mode = %v", path, st.Mode())
31 }
32 body, err := os.ReadFile(path)
33 if err != nil {
34 t.Fatalf("read %s: %v", path, err)
35 }
36 s := string(body)
37 if !strings.Contains(s, "#!/bin/sh") {
38 t.Errorf("%s: missing shebang", path)
39 }
40 if !strings.Contains(s, "hook "+name) {
41 t.Errorf("%s: missing 'hook %s' subcommand reference", path, name)
42 }
43 if !strings.Contains(s, bin) {
44 t.Errorf("%s: missing binary path %q", path, bin)
45 }
46 }
47 }
48
49 func TestInstall_Idempotent(t *testing.T) {
50 t.Parallel()
51 gitDir := t.TempDir()
52 bin := "/usr/local/bin/shithubd"
53
54 if err := hooks.Install(gitDir, bin); err != nil {
55 t.Fatalf("first Install: %v", err)
56 }
57 if err := hooks.Install(gitDir, bin); err != nil {
58 t.Fatalf("second Install: %v", err)
59 }
60 }
61
62 func TestInstall_OverwritesStale(t *testing.T) {
63 t.Parallel()
64 gitDir := t.TempDir()
65 hooksDir := filepath.Join(gitDir, "hooks")
66 if err := os.MkdirAll(hooksDir, 0o755); err != nil {
67 t.Fatal(err)
68 }
69 stale := filepath.Join(hooksDir, "pre-receive")
70 if err := os.WriteFile(stale, []byte("#!/bin/sh\nold contents\n"), 0o755); err != nil {
71 t.Fatal(err)
72 }
73
74 if err := hooks.Install(gitDir, "/usr/local/bin/shithubd-new"); err != nil {
75 t.Fatalf("Install: %v", err)
76 }
77 body, err := os.ReadFile(stale)
78 if err != nil {
79 t.Fatal(err)
80 }
81 if !strings.Contains(string(body), "shithubd-new") {
82 t.Errorf("stale hook not overwritten: %q", string(body))
83 }
84 }
85