~bzr-pqm/bzr/bzr.dev

1246 by Martin Pool
- add very simple commit tests
1
# Copyright (C) 2005 by Canonical Ltd
2
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
18
import os
19
1472 by Robert Collins
post commit hook, first pass implementation
20
import bzrlib
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
21
from bzrlib.tests import TestCaseInTempDir
1246 by Martin Pool
- add very simple commit tests
22
from bzrlib.branch import Branch
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
23
from bzrlib.workingtree import WorkingTree
1246 by Martin Pool
- add very simple commit tests
24
from bzrlib.commit import Commit
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
25
from bzrlib.config import BranchConfig
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
1246 by Martin Pool
- add very simple commit tests
27
28
1257 by Martin Pool
doc
29
# TODO: Test commit with some added, and added-but-missing files
30
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
31
class MustSignConfig(BranchConfig):
32
33
    def signature_needed(self):
34
        return True
35
36
    def gpg_signing_command(self):
37
        return ['cat', '-']
38
39
1472 by Robert Collins
post commit hook, first pass implementation
40
class BranchWithHooks(BranchConfig):
41
42
    def post_commit(self):
43
        return "bzrlib.ahook bzrlib.ahook"
44
45
1246 by Martin Pool
- add very simple commit tests
46
class TestCommit(TestCaseInTempDir):
1390 by Robert Collins
pair programming worx... merge integration and weave
47
1246 by Martin Pool
- add very simple commit tests
48
    def test_simple_commit(self):
49
        """Commit and check two versions of a single file."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
50
        b = Branch.initialize(u'.')
1246 by Martin Pool
- add very simple commit tests
51
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
52
        b.working_tree().add('hello')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
53
        b.working_tree().commit(message='add hello')
1246 by Martin Pool
- add very simple commit tests
54
        file_id = b.working_tree().path2id('hello')
55
56
        file('hello', 'w').write('version 2')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
57
        b.working_tree().commit(message='commit 2')
1246 by Martin Pool
- add very simple commit tests
58
59
        eq = self.assertEquals
60
        eq(b.revno(), 2)
61
        rh = b.revision_history()
62
        rev = b.get_revision(rh[0])
63
        eq(rev.message, 'add hello')
64
65
        tree1 = b.revision_tree(rh[0])
66
        text = tree1.get_file_text(file_id)
67
        eq(text, 'hello world')
68
69
        tree2 = b.revision_tree(rh[1])
70
        eq(tree2.get_file_text(file_id), 'version 2')
71
72
    def test_delete_commit(self):
73
        """Test a commit with a deleted file"""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
74
        b = Branch.initialize(u'.')
1247 by Martin Pool
- tests for deletion and removal of files in commits
75
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
76
        b.working_tree().add(['hello'], ['hello-id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
77
        b.working_tree().commit(message='add hello')
1247 by Martin Pool
- tests for deletion and removal of files in commits
78
79
        os.remove('hello')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
80
        b.working_tree().commit('removed hello', rev_id='rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
81
82
        tree = b.revision_tree('rev2')
83
        self.assertFalse(tree.has_id('hello-id'))
84
1253 by Martin Pool
- test that pointless commits are trapped
85
    def test_pointless_commit(self):
86
        """Commit refuses unless there are changes or it's forced."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
87
        b = Branch.initialize(u'.')
1253 by Martin Pool
- test that pointless commits are trapped
88
        file('hello', 'w').write('hello')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
89
        b.working_tree().add(['hello'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
90
        b.working_tree().commit(message='add hello')
1253 by Martin Pool
- test that pointless commits are trapped
91
        self.assertEquals(b.revno(), 1)
92
        self.assertRaises(PointlessCommit,
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
93
                          b.working_tree().commit,
1253 by Martin Pool
- test that pointless commits are trapped
94
                          message='fails',
95
                          allow_pointless=False)
96
        self.assertEquals(b.revno(), 1)
97
        
1252 by Martin Pool
- add test for commit of an empty tree
98
    def test_commit_empty(self):
1253 by Martin Pool
- test that pointless commits are trapped
99
        """Commiting an empty tree works."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
100
        b = Branch.initialize(u'.')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
101
        b.working_tree().commit(message='empty tree', allow_pointless=True)
1253 by Martin Pool
- test that pointless commits are trapped
102
        self.assertRaises(PointlessCommit,
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
103
                          b.working_tree().commit,
1253 by Martin Pool
- test that pointless commits are trapped
104
                          message='empty tree',
105
                          allow_pointless=False)
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
106
        b.working_tree().commit(message='empty tree', allow_pointless=True)
1252 by Martin Pool
- add test for commit of an empty tree
107
        self.assertEquals(b.revno(), 2)
108
109
110
    def test_selective_delete(self):
111
        """Selective commit in tree with deletions"""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
112
        b = Branch.initialize(u'.')
1254 by Martin Pool
- fix handling of selective commit with deleted files
113
        file('hello', 'w').write('hello')
114
        file('buongia', 'w').write('buongia')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
115
        b.working_tree().add(['hello', 'buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
116
              ['hello-id', 'buongia-id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
117
        b.working_tree().commit(message='add files',
1255 by Martin Pool
- more tests for selective commit of deletion
118
                 rev_id='test@rev-1')
1254 by Martin Pool
- fix handling of selective commit with deleted files
119
        
120
        os.remove('hello')
121
        file('buongia', 'w').write('new text')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
122
        b.working_tree().commit(message='update text',
1254 by Martin Pool
- fix handling of selective commit with deleted files
123
                 specific_files=['buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
124
                 allow_pointless=False,
125
                 rev_id='test@rev-2')
1254 by Martin Pool
- fix handling of selective commit with deleted files
126
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
127
        b.working_tree().commit(message='remove hello',
1254 by Martin Pool
- fix handling of selective commit with deleted files
128
                 specific_files=['hello'],
1255 by Martin Pool
- more tests for selective commit of deletion
129
                 allow_pointless=False,
130
                 rev_id='test@rev-3')
1254 by Martin Pool
- fix handling of selective commit with deleted files
131
132
        eq = self.assertEquals
133
        eq(b.revno(), 3)
1255 by Martin Pool
- more tests for selective commit of deletion
134
135
        tree2 = b.revision_tree('test@rev-2')
136
        self.assertTrue(tree2.has_filename('hello'))
137
        self.assertEquals(tree2.get_file_text('hello-id'), 'hello')
138
        self.assertEquals(tree2.get_file_text('buongia-id'), 'new text')
139
        
140
        tree3 = b.revision_tree('test@rev-3')
141
        self.assertFalse(tree3.has_filename('hello'))
142
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
143
144
145
    def test_commit_rename(self):
146
        """Test commit of a revision where a file is renamed."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
147
        b = Branch.initialize(u'.')
148
        tree = WorkingTree(u'.', b)
1185.38.7 by John Arbash Meinel
Updated build_tree to use fixed line-endings for tests which read the file contents and compare
149
        self.build_tree(['hello'], line_endings='binary')
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
150
        tree.add(['hello'], ['hello-id'])
151
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
152
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
153
        tree.rename_one('hello', 'fruity')
154
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
155
1303 by Martin Pool
- commit updates entry_version
156
        eq = self.assertEquals
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
157
        tree1 = b.revision_tree('test@rev-1')
1303 by Martin Pool
- commit updates entry_version
158
        eq(tree1.id2path('hello-id'), 'hello')
159
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
160
        self.assertFalse(tree1.has_filename('fruity'))
1291 by Martin Pool
- add test for moving files between directories
161
        self.check_inventory_shape(tree1.inventory, ['hello'])
1303 by Martin Pool
- commit updates entry_version
162
        ie = tree1.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
163
        eq(ie.revision, 'test@rev-1')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
164
165
        tree2 = b.revision_tree('test@rev-2')
1303 by Martin Pool
- commit updates entry_version
166
        eq(tree2.id2path('hello-id'), 'fruity')
167
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
1291 by Martin Pool
- add test for moving files between directories
168
        self.check_inventory_shape(tree2.inventory, ['fruity'])
1303 by Martin Pool
- commit updates entry_version
169
        ie = tree2.inventory['hello-id']
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
170
        eq(ie.revision, 'test@rev-2')
1291 by Martin Pool
- add test for moving files between directories
171
172
    def test_reused_rev_id(self):
1292 by Martin Pool
- add check that revision ids cannot be committed twice
173
        """Test that a revision id cannot be reused in a branch"""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
174
        b = Branch.initialize(u'.')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
175
        b.working_tree().commit('initial', rev_id='test@rev-1', allow_pointless=True)
1292 by Martin Pool
- add check that revision ids cannot be committed twice
176
        self.assertRaises(Exception,
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
177
                          b.working_tree().commit,
1292 by Martin Pool
- add check that revision ids cannot be committed twice
178
                          message='reused id',
179
                          rev_id='test@rev-1',
180
                          allow_pointless=True)
1291 by Martin Pool
- add test for moving files between directories
181
182
    def test_commit_move(self):
183
        """Test commit of revisions with moved files and directories"""
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
184
        eq = self.assertEquals
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
185
        b = Branch.initialize(u'.')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
186
        r1 = 'test@rev-1'
1291 by Martin Pool
- add test for moving files between directories
187
        self.build_tree(['hello', 'a/', 'b/'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
188
        b.working_tree().add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
189
        b.working_tree().commit('initial', rev_id=r1, allow_pointless=False)
1508.1.8 by Robert Collins
move move() from Branch to WorkingTree.
190
        b.working_tree().move(['hello'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
191
        r2 = 'test@rev-2'
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
192
        b.working_tree().commit('two', rev_id=r2, allow_pointless=False)
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
193
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
194
                                   ['a', 'a/hello', 'b'])
195
1508.1.8 by Robert Collins
move move() from Branch to WorkingTree.
196
        b.working_tree().move(['b'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
197
        r3 = 'test@rev-3'
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
198
        b.working_tree().commit('three', rev_id=r3, allow_pointless=False)
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
199
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
200
                                   ['a', 'a/hello', 'a/b'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
201
        self.check_inventory_shape(b.get_revision_inventory(r3),
1291 by Martin Pool
- add test for moving files between directories
202
                                   ['a', 'a/hello', 'a/b'])
203
1185.31.34 by John Arbash Meinel
Removing instances of os.sep
204
        b.working_tree().move(['a/hello'], 'a/b')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
205
        r4 = 'test@rev-4'
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
206
        b.working_tree().commit('four', rev_id=r4, allow_pointless=False)
1497 by Robert Collins
Move Branch.read_working_inventory to WorkingTree.
207
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
1291 by Martin Pool
- add test for moving files between directories
208
                                   ['a', 'a/b/hello', 'a/b'])
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
209
210
        inv = b.get_revision_inventory(r4)
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
211
        eq(inv['hello-id'].revision, r4)
212
        eq(inv['a-id'].revision, r1)
213
        eq(inv['b-id'].revision, r3)
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
214
        
1246 by Martin Pool
- add very simple commit tests
215
    def test_removed_commit(self):
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
216
        """Commit with a removed file"""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
217
        b = Branch.initialize(u'.')
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
218
        wt = b.working_tree()
1247 by Martin Pool
- tests for deletion and removal of files in commits
219
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
220
        b.working_tree().add(['hello'], ['hello-id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
221
        b.working_tree().commit(message='add hello')
1247 by Martin Pool
- tests for deletion and removal of files in commits
222
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
223
        wt = b.working_tree()  # FIXME: kludge for aliasing of working inventory
224
        wt.remove('hello')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
225
        b.working_tree().commit('removed hello', rev_id='rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
226
227
        tree = b.revision_tree('rev2')
228
        self.assertFalse(tree.has_id('hello-id'))
1246 by Martin Pool
- add very simple commit tests
229
230
1256 by Martin Pool
- test that commits append to the tree's ancestry
231
    def test_committed_ancestry(self):
232
        """Test commit appends revisions to ancestry."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
233
        b = Branch.initialize(u'.')
1256 by Martin Pool
- test that commits append to the tree's ancestry
234
        rev_ids = []
235
        for i in range(4):
236
            file('hello', 'w').write((str(i) * 4) + '\n')
237
            if i == 0:
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
238
                b.working_tree().add(['hello'], ['hello-id'])
1256 by Martin Pool
- test that commits append to the tree's ancestry
239
            rev_id = 'test@rev-%d' % (i+1)
240
            rev_ids.append(rev_id)
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
241
            b.working_tree().commit(message='rev %d' % (i+1),
1256 by Martin Pool
- test that commits append to the tree's ancestry
242
                     rev_id=rev_id)
243
        eq = self.assertEquals
244
        eq(b.revision_history(), rev_ids)
245
        for i in range(4):
246
            anc = b.get_ancestry(rev_ids[i])
1390 by Robert Collins
pair programming worx... merge integration and weave
247
            eq(anc, [None] + rev_ids[:i+1])
1416 by Robert Collins
when committing a specific file, include all its parents
248
249
    def test_commit_new_subdir_child_selective(self):
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
250
        b = Branch.initialize(u'.')
1416 by Robert Collins
when committing a specific file, include all its parents
251
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
252
        b.working_tree().add(['dir', 'dir/file1', 'dir/file2'],
1416 by Robert Collins
when committing a specific file, include all its parents
253
              ['dirid', 'file1id', 'file2id'])
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
254
        b.working_tree().commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
1416 by Robert Collins
when committing a specific file, include all its parents
255
        inv = b.get_inventory('1')
256
        self.assertEqual('1', inv['dirid'].revision)
257
        self.assertEqual('1', inv['file1id'].revision)
258
        # FIXME: This should raise a KeyError I think, rbc20051006
259
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
260
261
    def test_strict_commit(self):
262
        """Try and commit with unknown files and strict = True, should fail."""
263
        from bzrlib.errors import StrictCommitFailed
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
264
        b = Branch.initialize(u'.')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
265
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
266
        b.working_tree().add('hello')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
267
        file('goodbye', 'w').write('goodbye cruel world!')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
268
        self.assertRaises(StrictCommitFailed, b.working_tree().commit,
1185.16.65 by mbp at sourcefrog
- new commit --strict option
269
            message='add hello but not goodbye', strict=True)
270
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
271
    def test_strict_commit_without_unknowns(self):
272
        """Try and commit with no unknown files and strict = True,
273
        should work."""
274
        from bzrlib.errors import StrictCommitFailed
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
275
        b = Branch.initialize(u'.')
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
276
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
277
        b.working_tree().add('hello')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
278
        b.working_tree().commit(message='add hello', strict=True)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
279
1185.16.65 by mbp at sourcefrog
- new commit --strict option
280
    def test_nonstrict_commit(self):
281
        """Try and commit with unknown files and strict = False, should work."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
282
        b = Branch.initialize(u'.')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
283
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
284
        b.working_tree().add('hello')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
285
        file('goodbye', 'w').write('goodbye cruel world!')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
286
        b.working_tree().commit(message='add hello but not goodbye', strict=False)
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
287
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
288
    def test_nonstrict_commit_without_unknowns(self):
289
        """Try and commit with no unknown files and strict = False,
290
        should work."""
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
291
        b = Branch.initialize(u'.')
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
292
        file('hello', 'w').write('hello world')
1508.1.5 by Robert Collins
Move add from Branch to WorkingTree.
293
        b.working_tree().add('hello')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
294
        b.working_tree().commit(message='add hello', strict=False)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
295
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
296
    def test_signed_commit(self):
297
        import bzrlib.gpg
298
        import bzrlib.commit as commit
299
        oldstrategy = bzrlib.gpg.GPGStrategy
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
300
        branch = Branch.initialize(u'.')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
301
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
302
        self.failIf(branch.revision_store.has_id('A', 'sig'))
303
        try:
304
            from bzrlib.testament import Testament
305
            # monkey patch gpg signing mechanism
306
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
307
            commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
308
                                                      allow_pointless=True,
309
                                                      rev_id='B')
310
            self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
311
                             branch.revision_store.get('B', 'sig').read())
312
        finally:
313
            bzrlib.gpg.GPGStrategy = oldstrategy
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
314
315
    def test_commit_failed_signature(self):
316
        import bzrlib.gpg
317
        import bzrlib.commit as commit
318
        oldstrategy = bzrlib.gpg.GPGStrategy
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
319
        branch = Branch.initialize(u'.')
1457.1.17 by Robert Collins
Branch.commit() has moved to WorkingTree.commit(). (Robert Collins)
320
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
321
        self.failIf(branch.revision_store.has_id('A', 'sig'))
322
        try:
323
            from bzrlib.testament import Testament
324
            # monkey patch gpg signing mechanism
325
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
326
            config = MustSignConfig(branch)
327
            self.assertRaises(SigningFailed,
328
                              commit.Commit(config=config).commit,
329
                              branch, "base",
330
                              allow_pointless=True,
331
                              rev_id='B')
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
332
            branch = Branch.open(u'.')
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
333
            self.assertEqual(branch.revision_history(), ['A'])
334
            self.failIf(branch.revision_store.has_id('B'))
335
        finally:
336
            bzrlib.gpg.GPGStrategy = oldstrategy
1472 by Robert Collins
post commit hook, first pass implementation
337
338
    def test_commit_invokes_hooks(self):
339
        import bzrlib.commit as commit
1185.33.66 by Martin Pool
[patch] use unicode literals for all hardcoded paths (Alexander Belchenko)
340
        branch = Branch.initialize(u'.')
1472 by Robert Collins
post commit hook, first pass implementation
341
        calls = []
342
        def called(branch, rev_id):
343
            calls.append('called')
344
        bzrlib.ahook = called
345
        try:
346
            config = BranchWithHooks(branch)
347
            commit.Commit(config=config).commit(
348
                            branch, "base",
349
                            allow_pointless=True,
350
                            rev_id='A')
351
            self.assertEqual(['called', 'called'], calls)
352
        finally:
353
            del bzrlib.ahook