test_hooks.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 sys
  22. import tempfile
  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. super(ShellHookTests, self).setUp()
  33. if os.name != 'posix':
  34. self.skipTest('shell hook tests requires POSIX shell')
  35. def test_hook_pre_commit(self):
  36. pre_commit_fail = """#!/bin/sh
  37. exit 1
  38. """
  39. pre_commit_success = """#!/bin/sh
  40. exit 0
  41. """
  42. repo_dir = os.path.join(tempfile.mkdtemp())
  43. os.mkdir(os.path.join(repo_dir, 'hooks'))
  44. self.addCleanup(shutil.rmtree, repo_dir)
  45. pre_commit = os.path.join(repo_dir, 'hooks', 'pre-commit')
  46. hook = PreCommitShellHook(repo_dir)
  47. with open(pre_commit, 'w') as f:
  48. f.write(pre_commit_fail)
  49. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  50. self.assertRaises(errors.HookError, hook.execute)
  51. with open(pre_commit, 'w') as f:
  52. f.write(pre_commit_success)
  53. os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  54. hook.execute()
  55. def test_hook_commit_msg(self):
  56. commit_msg_fail = """#!/bin/sh
  57. exit 1
  58. """
  59. commit_msg_success = """#!/bin/sh
  60. exit 0
  61. """
  62. repo_dir = os.path.join(tempfile.mkdtemp())
  63. os.mkdir(os.path.join(repo_dir, 'hooks'))
  64. self.addCleanup(shutil.rmtree, repo_dir)
  65. commit_msg = os.path.join(repo_dir, 'hooks', 'commit-msg')
  66. hook = CommitMsgShellHook(repo_dir)
  67. with open(commit_msg, 'w') as f:
  68. f.write(commit_msg_fail)
  69. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  70. self.assertRaises(errors.HookError, hook.execute, b'failed commit')
  71. with open(commit_msg, 'w') as f:
  72. f.write(commit_msg_success)
  73. os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  74. hook.execute(b'empty commit')
  75. def test_hook_post_commit(self):
  76. (fd, path) = tempfile.mkstemp()
  77. os.close(fd)
  78. post_commit_msg = """#!/bin/sh
  79. rm """ + path + "\n"
  80. post_commit_msg_fail = """#!/bin/sh
  81. exit 1
  82. """
  83. repo_dir = os.path.join(tempfile.mkdtemp())
  84. os.mkdir(os.path.join(repo_dir, 'hooks'))
  85. self.addCleanup(shutil.rmtree, repo_dir)
  86. post_commit = os.path.join(repo_dir, 'hooks', 'post-commit')
  87. hook = PostCommitShellHook(repo_dir)
  88. with open(post_commit, 'w') as f:
  89. f.write(post_commit_msg_fail)
  90. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  91. self.assertRaises(errors.HookError, hook.execute)
  92. with open(post_commit, 'w') as f:
  93. f.write(post_commit_msg)
  94. os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
  95. hook.execute()
  96. self.assertFalse(os.path.exists(path))