1
# Copyright (C) 2005 by Canonical Ltd
2
# -*- coding: utf-8 -*-
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Black-box tests for bzr sign-my-commits."""
23
from bzrlib.testament import Testament
24
from bzrlib.tests import TestCaseWithTransport
25
from bzrlib.workingtree import WorkingTree
28
class SignMyCommits(TestCaseWithTransport):
30
def monkey_patch_gpg(self):
31
"""Monkey patch the gpg signing strategy to be a loopback.
33
This also registers the cleanup, so that we will revert to
34
the original gpg strategy when done.
36
self._oldstrategy = bzrlib.gpg.GPGStrategy
38
# monkey patch gpg signing mechanism
39
bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
41
self.addCleanup(self._fix_gpg_strategy)
43
def _fix_gpg_strategy(self):
44
bzrlib.gpg.GPGStrategy = self._oldstrategy
46
def setup_tree(self, location='.'):
47
wt = self.make_branch_and_tree(location)
48
wt.commit("base A", allow_pointless=True, rev_id='A')
49
wt.commit("base B", allow_pointless=True, rev_id='B')
50
wt.commit("base C", allow_pointless=True, rev_id='C')
51
wt.commit("base D", allow_pointless=True, rev_id='D',
52
committer='Alternate <alt@foo.com>')
56
def assertUnsigned(self, repo, revision_id):
57
"""Assert that revision_id is not signed in repo."""
58
self.assertFalse(repo.has_signature_for_revision_id(revision_id))
60
def assertSigned(self, repo, revision_id):
61
"""Assert that revision_id is signed in repo."""
62
self.assertTrue(repo.has_signature_for_revision_id(revision_id))
64
def test_sign_my_commits(self):
65
#Test re signing of data.
66
wt = self.setup_tree()
67
repo = wt.branch.repository
69
self.monkey_patch_gpg()
71
self.assertUnsigned(repo, 'A')
72
self.assertUnsigned(repo, 'B')
73
self.assertUnsigned(repo, 'C')
74
self.assertUnsigned(repo, 'D')
76
self.run_bzr('sign-my-commits')
78
self.assertSigned(repo, 'A')
79
self.assertSigned(repo, 'B')
80
self.assertSigned(repo, 'C')
81
self.assertUnsigned(repo, 'D')
83
def test_sign_my_commits_location(self):
84
wt = self.setup_tree('other')
85
repo = wt.branch.repository
87
self.monkey_patch_gpg()
89
self.run_bzr('sign-my-commits', 'other')
91
self.assertSigned(repo, 'A')
92
self.assertSigned(repo, 'B')
93
self.assertSigned(repo, 'C')
94
self.assertUnsigned(repo, 'D')
96
def test_sign_diff_committer(self):
97
wt = self.setup_tree()
98
repo = wt.branch.repository
100
self.monkey_patch_gpg()
102
self.run_bzr('sign-my-commits', '.', 'Alternate <alt@foo.com>')
104
self.assertUnsigned(repo, 'A')
105
self.assertUnsigned(repo, 'B')
106
self.assertUnsigned(repo, 'C')
107
self.assertSigned(repo, 'D')
109
def test_sign_dry_run(self):
110
wt = self.setup_tree()
111
repo = wt.branch.repository
113
self.monkey_patch_gpg()
115
out = self.run_bzr('sign-my-commits', '--dry-run')[0]
117
self.assertEquals('A\nB\nC\nSigned 3 revisions\n', out)
118
self.assertUnsigned(repo, 'A')
119
self.assertUnsigned(repo, 'B')
120
self.assertUnsigned(repo, 'C')
121
self.assertUnsigned(repo, 'D')