test_hooks.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # test_hooks.py -- Tests for executing hooks
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # or (at your option) a later version of the License.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. # MA 02110-1301, USA.
  17. """Tests for executing hooks."""
  18. import os
  19. import stat
  20. import shutil
  21. import tempfile
  22. import warnings
  23. from dulwich import errors
  24. from dulwich.hooks import (
  25. PreCommitShellHook,
  26. PostCommitShellHook,
  27. CommitMsgShellHook,
  28. )
  29. from dulwich.tests import TestCase
  30. class ShellHookTests(TestCase):
  31. def setUp(self):
  32. if os.name != 'posix':
  33. self.skipTest('shell hook tests requires POSIX shell')
  34. def test_hook_pre_commit(self):
  35. pre_commit_fail = """#!/bin/sh
  36. exit 1
  37. """
  38. pre_commit_success = """#!/bin/sh
  39. exit 0
  40. """
  41. repo_dir = os.path.join(tempfile.mkdtemp())
  42. os.mkdir(os.path.join(repo_dir, 'hooks'))
  43. self.addCleanup(shutil.rmtree, repo_dir)
  44. pre_commit = os.path.join(repo_dir, 'hooks', 'pre-commit')
  45. hook = PreCommitShellHook(repo_dir)
  46. f = open(pre_commit, 'wb')
  47. try:
  48. f.write(pre_commit_fail)
  49. finally:
  50. f.close()
  51. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  52. self.assertRaises(errors.HookError, hook.execute)
  53. f = open(pre_commit, 'wb')
  54. try:
  55. f.write(pre_commit_success)
  56. finally:
  57. f.close()
  58. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  59. hook.execute()
  60. def test_hook_commit_msg(self):
  61. commit_msg_fail = """#!/bin/sh
  62. exit 1
  63. """
  64. commit_msg_success = """#!/bin/sh
  65. exit 0
  66. """
  67. repo_dir = os.path.join(tempfile.mkdtemp())
  68. os.mkdir(os.path.join(repo_dir, 'hooks'))
  69. self.addCleanup(shutil.rmtree, repo_dir)
  70. commit_msg = os.path.join(repo_dir, 'hooks', 'commit-msg')
  71. hook = CommitMsgShellHook(repo_dir)
  72. f = open(commit_msg, 'wb')
  73. try:
  74. f.write(commit_msg_fail)
  75. finally:
  76. f.close()
  77. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  78. self.assertRaises(errors.HookError, hook.execute, 'failed commit')
  79. f = open(commit_msg, 'wb')
  80. try:
  81. f.write(commit_msg_success)
  82. finally:
  83. f.close()
  84. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  85. hook.execute('empty commit')
  86. def test_hook_post_commit(self):
  87. (fd, path) = tempfile.mkstemp()
  88. post_commit_msg = """#!/bin/sh
  89. rm %(file)s
  90. """ % {'file': path}
  91. post_commit_msg_fail = """#!/bin/sh
  92. exit 1
  93. """
  94. repo_dir = os.path.join(tempfile.mkdtemp())
  95. os.mkdir(os.path.join(repo_dir, 'hooks'))
  96. self.addCleanup(shutil.rmtree, repo_dir)
  97. post_commit = os.path.join(repo_dir, 'hooks', 'post-commit')
  98. hook = PostCommitShellHook(repo_dir)
  99. f = open(post_commit, 'wb')
  100. try:
  101. f.write(post_commit_msg_fail)
  102. finally:
  103. f.close()
  104. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  105. self.assertRaises(errors.HookError, hook.execute)
  106. f = open(post_commit, 'wb')
  107. try:
  108. f.write(post_commit_msg)
  109. finally:
  110. f.close()
  111. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  112. hook.execute()
  113. self.assertFalse(os.path.exists(path))