summaryrefslogtreecommitdiff
path: root/bzrlib/tests/blackbox/test_hooks.py
blob: 2d49a063595c23ac533650892c0ddc8ff5556c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (C) 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for commands related to hooks"""

from bzrlib.branch import Branch
from bzrlib.tests import TestCaseWithTransport

def _foo_hook():
    pass

class TestHooks(TestCaseWithTransport):

    def _check_hooks_output(self, command_output, hooks):
        for hook_type in Branch.hooks:
            s = "\n  ".join(hooks.get(hook_type, ["<no hooks installed>"]))
            self.assert_("%s:\n    %s" % (hook_type, s) in command_output)

    def test_hooks_with_no_hooks(self):
        self.make_branch('.')
        out, err = self.run_bzr('hooks')
        self.assertEqual(err, "")
        for hook_type in Branch.hooks:
            self._check_hooks_output(out, {})

    def test_hooks_with_unnamed_hook(self):
        self.make_branch('.')
        def foo(): return
        Branch.hooks.install_named_hook('post_push', foo, None)
        out, err = self.run_bzr('hooks')
        self._check_hooks_output(out, {'post_push': ["No hook name"]})

    def test_hooks_with_named_hook(self):
        self.make_branch('.')
        def foo(): return
        name = "Foo Bar Hook"
        Branch.hooks.install_named_hook('post_push', foo, name)
        out, err = self.run_bzr('hooks')
        self._check_hooks_output(out, {'post_push': [name]})

    def test_hooks_no_branch(self):
        self.run_bzr('hooks')

    def test_hooks_lazy_with_unnamed_hook(self):
        self.make_branch('.')
        def foo(): return
        Branch.hooks.install_named_hook_lazy('post_push',
            'bzrlib.tests.blackbox.test_hooks',
            '_foo_hook',
            None)
        out, err = self.run_bzr('hooks')
        self._check_hooks_output(out, {'post_push': ["No hook name"]})
        
    def test_hooks_lazy_with_named_hook(self):
        self.make_branch('.')
        def foo(): return
        Branch.hooks.install_named_hook_lazy('post_push',
            'bzrlib.tests.blackbox.test_hooks',
            '_foo_hook',
            'hook has a name')
        out, err = self.run_bzr('hooks')
        self._check_hooks_output(out, {'post_push': ["hook has a name"]})