From 8887daa3e717f48ac9a731c1e5cc1cd5e5ee8a45 Mon Sep 17 00:00:00 2001 From: "KADOTA, Kyohei" Date: Tue, 12 Jan 2021 15:35:57 +0900 Subject: [PATCH] Fix tests if there are hooks generated by templates (#434) If there are custom hooks in ~/.git_template/hooks, they can occur an error on such as pre-commit hooks. Co-authored-by: Casey Lee --- pkg/common/git_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/common/git_test.go b/pkg/common/git_test.go index 3c72b8e..bd70d87 100644 --- a/pkg/common/git_test.go +++ b/pkg/common/git_test.go @@ -49,6 +49,27 @@ func testDir(t *testing.T) string { return basedir } +func cleanGitHooks(dir string) error { + hooksDir := filepath.Join(dir, ".git", "hooks") + files, err := ioutil.ReadDir(hooksDir) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + for _, f := range files { + if f.IsDir() { + continue + } + relName := filepath.Join(hooksDir, f.Name()) + if err := os.Remove(relName); err != nil { + return err + } + } + return nil +} + func TestFindGitRemoteURL(t *testing.T) { assert := assert.New(t) @@ -56,6 +77,8 @@ func TestFindGitRemoteURL(t *testing.T) { gitConfig() err := gitCmd("init", basedir) assert.NoError(err) + err = cleanGitHooks(basedir) + assert.NoError(err) remoteURL := "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo-name" err = gitCmd("config", "-f", fmt.Sprintf("%s/.git/config", basedir), "--add", "remote.origin.url", remoteURL) @@ -138,6 +161,7 @@ func TestGitFindRef(t *testing.T) { dir := filepath.Join(basedir, name) require.NoError(t, os.MkdirAll(dir, 0755)) require.NoError(t, gitCmd("-C", dir, "init")) + require.NoError(t, cleanGitHooks(dir)) tt.Prepare(t, dir) ref, err := FindGitRef(dir) tt.Assert(t, ref, err)