Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / tests / test_hooks.py @ 959

History | View | Annotate | Download (3.82 KB)

1
# test_hooks.py -- Tests for executing hooks
2
#
3
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
4
# General Public License as public by the Free Software Foundation; version 2.0
5
# or (at your option) any later version. You can redistribute it and/or
6
# modify it under the terms of either of these two licenses.
7
#
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS,
10
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
# See the License for the specific language governing permissions and
12
# limitations under the License.
13
#
14
# You should have received a copy of the licenses; if not, see
15
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
16
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
17
# License, Version 2.0.
18
#
19

    
20
"""Tests for executing hooks."""
21

    
22
import os
23
import stat
24
import shutil
25
import tempfile
26

    
27
from dulwich import errors
28

    
29
from dulwich.hooks import (
30
    PreCommitShellHook,
31
    PostCommitShellHook,
32
    CommitMsgShellHook,
33
)
34

    
35
from dulwich.tests import TestCase
36

    
37

    
38
class ShellHookTests(TestCase):
39

    
40
    def setUp(self):
41
        super(ShellHookTests, self).setUp()
42
        if os.name != 'posix':
43
            self.skipTest('shell hook tests requires POSIX shell')
44

    
45
    def test_hook_pre_commit(self):
46
        pre_commit_fail = """#!/bin/sh
47
exit 1
48
"""
49

    
50
        pre_commit_success = """#!/bin/sh
51
exit 0
52
"""
53

    
54
        repo_dir = os.path.join(tempfile.mkdtemp())
55
        os.mkdir(os.path.join(repo_dir, 'hooks'))
56
        self.addCleanup(shutil.rmtree, repo_dir)
57

    
58
        pre_commit = os.path.join(repo_dir, 'hooks', 'pre-commit')
59
        hook = PreCommitShellHook(repo_dir)
60

    
61
        with open(pre_commit, 'w') as f:
62
            f.write(pre_commit_fail)
63
        os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
64

    
65
        self.assertRaises(errors.HookError, hook.execute)
66

    
67
        with open(pre_commit, 'w') as f:
68
            f.write(pre_commit_success)
69
        os.chmod(pre_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
70

    
71
        hook.execute()
72

    
73
    def test_hook_commit_msg(self):
74

    
75
        commit_msg_fail = """#!/bin/sh
76
exit 1
77
"""
78

    
79
        commit_msg_success = """#!/bin/sh
80
exit 0
81
"""
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

    
87
        commit_msg = os.path.join(repo_dir, 'hooks', 'commit-msg')
88
        hook = CommitMsgShellHook(repo_dir)
89

    
90
        with open(commit_msg, 'w') as f:
91
            f.write(commit_msg_fail)
92
        os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
93

    
94
        self.assertRaises(errors.HookError, hook.execute, b'failed commit')
95

    
96
        with open(commit_msg, 'w') as f:
97
            f.write(commit_msg_success)
98
        os.chmod(commit_msg, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
99

    
100
        hook.execute(b'empty commit')
101

    
102
    def test_hook_post_commit(self):
103

    
104
        (fd, path) = tempfile.mkstemp()
105
        os.close(fd)
106

    
107
        post_commit_msg = """#!/bin/sh
108
rm """ + path + "\n"
109

    
110
        post_commit_msg_fail = """#!/bin/sh
111
exit 1
112
"""
113

    
114
        repo_dir = os.path.join(tempfile.mkdtemp())
115
        os.mkdir(os.path.join(repo_dir, 'hooks'))
116
        self.addCleanup(shutil.rmtree, repo_dir)
117

    
118
        post_commit = os.path.join(repo_dir, 'hooks', 'post-commit')
119
        hook = PostCommitShellHook(repo_dir)
120

    
121
        with open(post_commit, 'w') as f:
122
            f.write(post_commit_msg_fail)
123
        os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
124

    
125
        self.assertRaises(errors.HookError, hook.execute)
126

    
127
        with open(post_commit, 'w') as f:
128
            f.write(post_commit_msg)
129
        os.chmod(post_commit, stat.S_IREAD | stat.S_IWRITE | stat.S_IEXEC)
130

    
131
        hook.execute()
132
        self.assertFalse(os.path.exists(path))