|
@@ -21,6 +21,7 @@
|
|
|
|
|
|
from cStringIO import StringIO
|
|
|
import os
|
|
|
+import stat
|
|
|
import shutil
|
|
|
import tempfile
|
|
|
import warnings
|
|
@@ -51,6 +52,7 @@ from dulwich.tests import (
|
|
|
from dulwich.tests.utils import (
|
|
|
open_repo,
|
|
|
tear_down_repo,
|
|
|
+ setup_warning_catcher,
|
|
|
)
|
|
|
|
|
|
missing_sha = 'b91fa4d900e17e99b433218e988c4eb4a3e9a097'
|
|
@@ -412,6 +414,163 @@ class RepositoryTests(TestCase):
|
|
|
shutil.rmtree(r1_dir)
|
|
|
shutil.rmtree(r2_dir)
|
|
|
|
|
|
+ def test_shell_hook_pre_commit(self):
|
|
|
+ if os.name != 'posix':
|
|
|
+ self.skipTest('shell hook tests requires POSIX shell')
|
|
|
+
|
|
|
+ pre_commit_fail = """#!/bin/sh
|
|
|
+exit 1
|
|
|
+"""
|
|
|
+
|
|
|
+ pre_commit_success = """#!/bin/sh
|
|
|
+exit 0
|
|
|
+"""
|
|
|
+
|
|
|
+ repo_dir = os.path.join(tempfile.mkdtemp())
|
|
|
+ r = Repo.init(repo_dir)
|
|
|
+ self.addCleanup(shutil.rmtree, repo_dir)
|
|
|
+
|
|
|
+ pre_commit = os.path.join(r.controldir(), 'hooks', 'pre-commit')
|
|
|
+
|
|
|
+ f = open(pre_commit, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(pre_commit_fail)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12345, commit_timezone=0,
|
|
|
+ author_timestamp=12345, author_timezone=0)
|
|
|
+
|
|
|
+ f = open(pre_commit, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(pre_commit_success)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ commit_sha = r.do_commit(
|
|
|
+ 'empty commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12395, commit_timezone=0,
|
|
|
+ author_timestamp=12395, author_timezone=0)
|
|
|
+ self.assertEqual([], r[commit_sha].parents)
|
|
|
+
|
|
|
+ def test_shell_hook_commit_msg(self):
|
|
|
+ if os.name != 'posix':
|
|
|
+ self.skipTest('shell hook tests requires POSIX shell')
|
|
|
+
|
|
|
+ commit_msg_fail = """#!/bin/sh
|
|
|
+exit 1
|
|
|
+"""
|
|
|
+
|
|
|
+ commit_msg_success = """#!/bin/sh
|
|
|
+exit 0
|
|
|
+"""
|
|
|
+
|
|
|
+ repo_dir = os.path.join(tempfile.mkdtemp())
|
|
|
+ r = Repo.init(repo_dir)
|
|
|
+ self.addCleanup(shutil.rmtree, repo_dir)
|
|
|
+
|
|
|
+ commit_msg = os.path.join(r.controldir(), 'hooks', 'commit-msg')
|
|
|
+
|
|
|
+ f = open(commit_msg, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(commit_msg_fail)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12345, commit_timezone=0,
|
|
|
+ author_timestamp=12345, author_timezone=0)
|
|
|
+
|
|
|
+ f = open(commit_msg, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(commit_msg_success)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ commit_sha = r.do_commit(
|
|
|
+ 'empty commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12395, commit_timezone=0,
|
|
|
+ author_timestamp=12395, author_timezone=0)
|
|
|
+ self.assertEqual([], r[commit_sha].parents)
|
|
|
+
|
|
|
+ def test_shell_hook_post_commit(self):
|
|
|
+ if os.name != 'posix':
|
|
|
+ self.skipTest('shell hook tests requires POSIX shell')
|
|
|
+
|
|
|
+ repo_dir = os.path.join(tempfile.mkdtemp())
|
|
|
+ r = Repo.init(repo_dir)
|
|
|
+ self.addCleanup(shutil.rmtree, repo_dir)
|
|
|
+
|
|
|
+ (fd, path) = tempfile.mkstemp(dir=repo_dir)
|
|
|
+ post_commit_msg = """#!/bin/sh
|
|
|
+unlink %(file)s
|
|
|
+""" % {'file': path}
|
|
|
+
|
|
|
+ root_sha = r.do_commit(
|
|
|
+ 'empty commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12345, commit_timezone=0,
|
|
|
+ author_timestamp=12345, author_timezone=0)
|
|
|
+ self.assertEqual([], r[root_sha].parents)
|
|
|
+
|
|
|
+ post_commit = os.path.join(r.controldir(), 'hooks', 'post-commit')
|
|
|
+
|
|
|
+ f = open(post_commit, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(post_commit_msg)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ commit_sha = r.do_commit(
|
|
|
+ 'empty commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12345, commit_timezone=0,
|
|
|
+ author_timestamp=12345, author_timezone=0)
|
|
|
+ self.assertEqual([root_sha], r[commit_sha].parents)
|
|
|
+
|
|
|
+ self.assertFalse(os.path.exists(path))
|
|
|
+
|
|
|
+ post_commit_msg_fail = """#!/bin/sh
|
|
|
+exit 1
|
|
|
+"""
|
|
|
+ f = open(post_commit, 'wb')
|
|
|
+ try:
|
|
|
+ f.write(post_commit_msg_fail)
|
|
|
+ finally:
|
|
|
+ f.close()
|
|
|
+ os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
|
|
|
+
|
|
|
+ warnings.simplefilter("always", UserWarning)
|
|
|
+ self.addCleanup(warnings.resetwarnings)
|
|
|
+ warnings_list = setup_warning_catcher()
|
|
|
+
|
|
|
+ commit_sha2 = r.do_commit(
|
|
|
+ 'empty commit',
|
|
|
+ committer='Test Committer <test@nodomain.com>',
|
|
|
+ author='Test Author <test@nodomain.com>',
|
|
|
+ commit_timestamp=12345, commit_timezone=0,
|
|
|
+ author_timestamp=12345, author_timezone=0)
|
|
|
+ self.assertEqual(len(warnings_list), 1)
|
|
|
+ self.assertIsInstance(warnings_list[-1], UserWarning)
|
|
|
+ self.assertTrue("post-commit hook failed: " in str(warnings_list[-1]))
|
|
|
+ self.assertEqual([commit_sha], r[commit_sha2].parents)
|
|
|
+
|
|
|
|
|
|
class BuildRepoTests(TestCase):
|
|
|
"""Tests that build on-disk repos from scratch.
|