@@ -3,6 +3,7 @@ package scm
33import (
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