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 <cplee@nektos.com>
This commit is contained in:
KADOTA, Kyohei 2021-01-12 15:35:57 +09:00 committed by GitHub
parent 80a245652e
commit 8887daa3e7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)