Skip to content

Commit 6e1dcc8

Browse files
committed
SetHook tests
1 parent 9b8b39e commit 6e1dcc8

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ gtm
6060

6161

6262

63+
6364
.gtm/

scm/git_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package scm
33
import (
44
"io/ioutil"
55
"os"
6+
"path"
67
"strings"
78
"testing"
89

@@ -329,3 +330,49 @@ func TestIgnoreSet_GitignoreError(t *testing.T) {
329330
)
330331
}
331332
}
333+
334+
func TestSetGitHooks(t *testing.T) {
335+
repo := util.NewTestRepo(t, false)
336+
defer repo.Remove()
337+
338+
repoPath := repo.PathIn("")
339+
hooks := map[string]string{"post-commit": "gtm commit --yes"}
340+
341+
// test when hook exists
342+
err := ioutil.WriteFile(path.Join(repoPath, ".git", "hooks", "post-commit"), []byte{}, 0755)
343+
if err != nil {
344+
t.Fatalf("SetHooks(hooks) expect error nil, got %s", err)
345+
}
346+
347+
err = SetHooks(hooks, repoPath)
348+
if err != nil {
349+
t.Fatalf("SetHooks(hooks) expect error nil, got %s", err)
350+
}
351+
b, err := ioutil.ReadFile(path.Join(repoPath, ".git", "hooks", "post-commit"))
352+
if err != nil {
353+
t.Fatalf("SetHooks(hooks) expect error nil, got %s", err)
354+
}
355+
output := string(b)
356+
if !strings.Contains(output, hooks["post-commit"]) {
357+
t.Errorf("SetHooks(hooks) expected post-commit to contain %s, got %s", hooks["post-commit"], output)
358+
}
359+
360+
// test if hook doesn't exist
361+
err = os.Remove(path.Join(repoPath, ".git", "hooks", "post-commit"))
362+
if err != nil {
363+
t.Fatalf("SetHooks(hooks) expect error nil, got %s", err)
364+
}
365+
366+
err = SetHooks(hooks, repoPath)
367+
if err != nil {
368+
t.Errorf("SetHooks(hooks) expect error nil, got %s", err)
369+
}
370+
b, err = ioutil.ReadFile(path.Join(repoPath, ".git", "hooks", "post-commit"))
371+
if err != nil {
372+
t.Fatalf("SetHooks(hooks) expect error nil, got %s", err)
373+
}
374+
output = string(b)
375+
if !strings.Contains(output, hooks["post-commit"]) {
376+
t.Errorf("SetHooks(hooks) expected post-commit to contain %s, got %s", hooks["post-commit"], output)
377+
}
378+
}

0 commit comments

Comments
 (0)