~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/test_commit.py

  • Committer: Robert Collins
  • Date: 2005-11-04 14:33:19 UTC
  • Revision ID: robertc@robertcollins.net-20051104143319-5293770efa92f56d
Remove some unneeded shebangs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import os
19
19
 
 
20
import bzrlib
20
21
from bzrlib.selftest import TestCaseInTempDir
21
22
from bzrlib.branch import Branch
 
23
from bzrlib.workingtree import WorkingTree
22
24
from bzrlib.commit import Commit
23
 
from bzrlib.errors import PointlessCommit, BzrError
 
25
from bzrlib.config import BranchConfig
 
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
24
27
 
25
28
 
26
29
# TODO: Test commit with some added, and added-but-missing files
27
30
 
 
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
 
 
40
class BranchWithHooks(BranchConfig):
 
41
 
 
42
    def post_commit(self):
 
43
        return "bzrlib.ahook bzrlib.ahook"
 
44
 
 
45
 
28
46
class TestCommit(TestCaseInTempDir):
29
47
 
30
48
    def test_simple_commit(self):
51
69
        tree2 = b.revision_tree(rh[1])
52
70
        eq(tree2.get_file_text(file_id), 'version 2')
53
71
 
54
 
 
55
72
    def test_delete_commit(self):
56
73
        """Test a commit with a deleted file"""
57
74
        b = Branch.initialize('.')
179
196
        b.move(['hello'], 'a')
180
197
        r2 = 'test@rev-2'
181
198
        b.commit('two', rev_id=r2, allow_pointless=False)
182
 
        self.check_inventory_shape(b.inventory,
 
199
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
183
200
                                   ['a', 'a/hello', 'b'])
184
201
 
185
202
        b.move(['b'], 'a')
186
203
        r3 = 'test@rev-3'
187
204
        b.commit('three', rev_id=r3, allow_pointless=False)
188
 
        self.check_inventory_shape(b.inventory,
 
205
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
189
206
                                   ['a', 'a/hello', 'a/b'])
190
207
        self.check_inventory_shape(b.get_revision_inventory(r3),
191
208
                                   ['a', 'a/hello', 'a/b'])
194
211
               os.sep.join(['a', 'b']))
195
212
        r4 = 'test@rev-4'
196
213
        b.commit('four', rev_id=r4, allow_pointless=False)
197
 
        self.check_inventory_shape(b.inventory,
 
214
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
198
215
                                   ['a', 'a/b/hello', 'a/b'])
199
216
 
200
217
        inv = b.get_revision_inventory(r4)
204
221
 
205
222
        
206
223
    def test_removed_commit(self):
207
 
        """Test a commit with a removed file"""
 
224
        """Commit with a removed file"""
208
225
        b = Branch.initialize('.')
 
226
        wt = b.working_tree()
209
227
        file('hello', 'w').write('hello world')
210
228
        b.add(['hello'], ['hello-id'])
211
229
        b.commit(message='add hello')
212
230
 
213
 
        b.remove('hello')
 
231
        wt = b.working_tree()  # FIXME: kludge for aliasing of working inventory
 
232
        wt.remove('hello')
214
233
        b.commit('removed hello', rev_id='rev2')
215
234
 
216
235
        tree = b.revision_tree('rev2')
246
265
        self.assertEqual('1', inv['file1id'].revision)
247
266
        # FIXME: This should raise a KeyError I think, rbc20051006
248
267
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
 
268
 
 
269
    def test_strict_commit(self):
 
270
        """Try and commit with unknown files and strict = True, should fail."""
 
271
        from bzrlib.errors import StrictCommitFailed
 
272
        b = Branch.initialize('.')
 
273
        file('hello', 'w').write('hello world')
 
274
        b.add('hello')
 
275
        file('goodbye', 'w').write('goodbye cruel world!')
 
276
        self.assertRaises(StrictCommitFailed, b.commit,
 
277
            message='add hello but not goodbye', strict=True)
 
278
 
 
279
    def test_strict_commit_without_unknowns(self):
 
280
        """Try and commit with no unknown files and strict = True,
 
281
        should work."""
 
282
        from bzrlib.errors import StrictCommitFailed
 
283
        b = Branch.initialize('.')
 
284
        file('hello', 'w').write('hello world')
 
285
        b.add('hello')
 
286
        b.commit(message='add hello', strict=True)
 
287
 
 
288
    def test_nonstrict_commit(self):
 
289
        """Try and commit with unknown files and strict = False, should work."""
 
290
        b = Branch.initialize('.')
 
291
        file('hello', 'w').write('hello world')
 
292
        b.add('hello')
 
293
        file('goodbye', 'w').write('goodbye cruel world!')
 
294
        b.commit(message='add hello but not goodbye', strict=False)
 
295
 
 
296
    def test_nonstrict_commit_without_unknowns(self):
 
297
        """Try and commit with no unknown files and strict = False,
 
298
        should work."""
 
299
        b = Branch.initialize('.')
 
300
        file('hello', 'w').write('hello world')
 
301
        b.add('hello')
 
302
        b.commit(message='add hello', strict=False)
 
303
 
 
304
    def test_signed_commit(self):
 
305
        import bzrlib.gpg
 
306
        import bzrlib.commit as commit
 
307
        oldstrategy = bzrlib.gpg.GPGStrategy
 
308
        branch = Branch.initialize('.')
 
309
        branch.commit("base", allow_pointless=True, rev_id='A')
 
310
        self.failIf(branch.revision_store.has_id('A', 'sig'))
 
311
        try:
 
312
            from bzrlib.testament import Testament
 
313
            # monkey patch gpg signing mechanism
 
314
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
 
315
            commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
 
316
                                                      allow_pointless=True,
 
317
                                                      rev_id='B')
 
318
            self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
 
319
                             branch.revision_store.get('B', 'sig').read())
 
320
        finally:
 
321
            bzrlib.gpg.GPGStrategy = oldstrategy
 
322
 
 
323
    def test_commit_failed_signature(self):
 
324
        import bzrlib.gpg
 
325
        import bzrlib.commit as commit
 
326
        oldstrategy = bzrlib.gpg.GPGStrategy
 
327
        branch = Branch.initialize('.')
 
328
        branch.commit("base", allow_pointless=True, rev_id='A')
 
329
        self.failIf(branch.revision_store.has_id('A', 'sig'))
 
330
        try:
 
331
            from bzrlib.testament import Testament
 
332
            # monkey patch gpg signing mechanism
 
333
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
 
334
            config = MustSignConfig(branch)
 
335
            self.assertRaises(SigningFailed,
 
336
                              commit.Commit(config=config).commit,
 
337
                              branch, "base",
 
338
                              allow_pointless=True,
 
339
                              rev_id='B')
 
340
            branch = Branch.open('.')
 
341
            self.assertEqual(branch.revision_history(), ['A'])
 
342
            self.failIf(branch.revision_store.has_id('B'))
 
343
        finally:
 
344
            bzrlib.gpg.GPGStrategy = oldstrategy
 
345
 
 
346
    def test_commit_invokes_hooks(self):
 
347
        import bzrlib.commit as commit
 
348
        branch = Branch.initialize('.')
 
349
        calls = []
 
350
        def called(branch, rev_id):
 
351
            calls.append('called')
 
352
        bzrlib.ahook = called
 
353
        try:
 
354
            config = BranchWithHooks(branch)
 
355
            commit.Commit(config=config).commit(
 
356
                            branch, "base",
 
357
                            allow_pointless=True,
 
358
                            rev_id='A')
 
359
            self.assertEqual(['called', 'called'], calls)
 
360
        finally:
 
361
            del bzrlib.ahook