~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-20 12:19:29 UTC
  • mfrom: (6437.23.11 2.5)
  • mto: (6581.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6582.
  • Revision ID: jelmer@samba.org-20120220121929-7ni2psvjoatm1yp4
Merge bzr/2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
import bzrlib
21
21
from bzrlib import (
22
22
    bzrdir,
 
23
    config,
23
24
    errors,
24
25
    )
25
26
from bzrlib.branch import Branch
26
27
from bzrlib.bzrdir import BzrDirMetaFormat1
27
28
from bzrlib.commit import Commit, NullCommitReporter
28
 
from bzrlib.config import BranchConfig
29
29
from bzrlib.errors import (
30
30
    PointlessCommit,
31
31
    BzrError,
44
44
 
45
45
# TODO: Test commit with some added, and added-but-missing files
46
46
 
47
 
class MustSignConfig(BranchConfig):
48
 
 
49
 
    def signature_needed(self):
50
 
        return True
51
 
 
52
 
    def gpg_signing_command(self):
53
 
        return ['cat', '-']
54
 
 
55
 
 
56
 
class BranchWithHooks(BranchConfig):
57
 
 
58
 
    def post_commit(self):
59
 
        return "bzrlib.ahook bzrlib.ahook"
 
47
class MustSignConfig(config.MemoryStack):
 
48
 
 
49
    def __init__(self):
 
50
        super(MustSignConfig, self).__init__('''
 
51
gpg_signing_command=cat -
 
52
create_signatures=always
 
53
''')
60
54
 
61
55
 
62
56
class CapturingReporter(NullCommitReporter):
88
82
        """Commit and check two versions of a single file."""
89
83
        wt = self.make_branch_and_tree('.')
90
84
        b = wt.branch
91
 
        file('hello', 'w').write('hello world')
 
85
        with file('hello', 'w') as f: f.write('hello world')
92
86
        wt.add('hello')
93
 
        wt.commit(message='add hello')
 
87
        rev1 = wt.commit(message='add hello')
94
88
        file_id = wt.path2id('hello')
95
89
 
96
 
        file('hello', 'w').write('version 2')
97
 
        wt.commit(message='commit 2')
 
90
        with file('hello', 'w') as f: f.write('version 2')
 
91
        rev2 = wt.commit(message='commit 2')
98
92
 
99
93
        eq = self.assertEquals
100
94
        eq(b.revno(), 2)
101
 
        rh = b.revision_history()
102
 
        rev = b.repository.get_revision(rh[0])
 
95
        rev = b.repository.get_revision(rev1)
103
96
        eq(rev.message, 'add hello')
104
97
 
105
 
        tree1 = b.repository.revision_tree(rh[0])
 
98
        tree1 = b.repository.revision_tree(rev1)
106
99
        tree1.lock_read()
107
100
        text = tree1.get_file_text(file_id)
108
101
        tree1.unlock()
109
102
        self.assertEqual('hello world', text)
110
103
 
111
 
        tree2 = b.repository.revision_tree(rh[1])
 
104
        tree2 = b.repository.revision_tree(rev2)
112
105
        tree2.lock_read()
113
106
        text = tree2.get_file_text(file_id)
114
107
        tree2.unlock()
118
111
        """Attempt a lossy commit to a native branch."""
119
112
        wt = self.make_branch_and_tree('.')
120
113
        b = wt.branch
121
 
        file('hello', 'w').write('hello world')
 
114
        with file('hello', 'w') as f: f.write('hello world')
122
115
        wt.add('hello')
123
116
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
124
117
        self.assertEquals('revid', revid)
129
122
        wt = self.make_branch_and_tree('.',
130
123
            format=test_foreign.DummyForeignVcsDirFormat())
131
124
        b = wt.branch
132
 
        file('hello', 'w').write('hello world')
 
125
        with file('hello', 'w') as f: f.write('hello world')
133
126
        wt.add('hello')
134
127
        revid = wt.commit(message='add hello', lossy=True,
135
128
            timestamp=1302659388, timezone=0)
142
135
            format=test_foreign.DummyForeignVcsDirFormat())
143
136
        wt = foreign_branch.create_checkout("local")
144
137
        b = wt.branch
145
 
        file('local/hello', 'w').write('hello world')
 
138
        with file('local/hello', 'w') as f: f.write('hello world')
146
139
        wt.add('hello')
147
140
        revid = wt.commit(message='add hello', lossy=True,
148
141
            timestamp=1302659388, timezone=0)
156
149
        """Test a commit with a missing file"""
157
150
        wt = self.make_branch_and_tree('.')
158
151
        b = wt.branch
159
 
        file('hello', 'w').write('hello world')
 
152
        with file('hello', 'w') as f: f.write('hello world')
160
153
        wt.add(['hello'], ['hello-id'])
161
154
        wt.commit(message='add hello')
162
155
 
163
156
        os.remove('hello')
164
 
        wt.commit('removed hello', rev_id='rev2')
 
157
        reporter = CapturingReporter()
 
158
        wt.commit('removed hello', rev_id='rev2', reporter=reporter)
 
159
        self.assertEquals(
 
160
            [('missing', u'hello'), ('deleted', u'hello')],
 
161
            reporter.calls)
165
162
 
166
163
        tree = b.repository.revision_tree('rev2')
167
164
        self.assertFalse(tree.has_id('hello-id'))
190
187
        """Commit refuses unless there are changes or it's forced."""
191
188
        wt = self.make_branch_and_tree('.')
192
189
        b = wt.branch
193
 
        file('hello', 'w').write('hello')
 
190
        with file('hello', 'w') as f: f.write('hello')
194
191
        wt.add(['hello'])
195
192
        wt.commit(message='add hello')
196
193
        self.assertEquals(b.revno(), 1)
216
213
        """Selective commit in tree with deletions"""
217
214
        wt = self.make_branch_and_tree('.')
218
215
        b = wt.branch
219
 
        file('hello', 'w').write('hello')
220
 
        file('buongia', 'w').write('buongia')
 
216
        with file('hello', 'w') as f: f.write('hello')
 
217
        with file('buongia', 'w') as f: f.write('buongia')
221
218
        wt.add(['hello', 'buongia'],
222
219
              ['hello-id', 'buongia-id'])
223
220
        wt.commit(message='add files',
224
221
                 rev_id='test@rev-1')
225
222
 
226
223
        os.remove('hello')
227
 
        file('buongia', 'w').write('new text')
 
224
        with file('buongia', 'w') as f: f.write('new text')
228
225
        wt.commit(message='update text',
229
226
                 specific_files=['buongia'],
230
227
                 allow_pointless=False,
339
336
        """Commit with a removed file"""
340
337
        wt = self.make_branch_and_tree('.')
341
338
        b = wt.branch
342
 
        file('hello', 'w').write('hello world')
 
339
        with file('hello', 'w') as f: f.write('hello world')
343
340
        wt.add(['hello'], ['hello-id'])
344
341
        wt.commit(message='add hello')
345
342
        wt.remove('hello')
354
351
        b = wt.branch
355
352
        rev_ids = []
356
353
        for i in range(4):
357
 
            file('hello', 'w').write((str(i) * 4) + '\n')
 
354
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
358
355
            if i == 0:
359
356
                wt.add(['hello'], ['hello-id'])
360
357
            rev_id = 'test@rev-%d' % (i+1)
361
358
            rev_ids.append(rev_id)
362
359
            wt.commit(message='rev %d' % (i+1),
363
360
                     rev_id=rev_id)
364
 
        eq = self.assertEquals
365
 
        eq(b.revision_history(), rev_ids)
366
361
        for i in range(4):
367
362
            self.assertThat(rev_ids[:i+1],
368
363
                MatchesAncestry(b.repository, rev_ids[i]))
385
380
        from bzrlib.errors import StrictCommitFailed
386
381
        wt = self.make_branch_and_tree('.')
387
382
        b = wt.branch
388
 
        file('hello', 'w').write('hello world')
 
383
        with file('hello', 'w') as f: f.write('hello world')
389
384
        wt.add('hello')
390
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
385
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
391
386
        self.assertRaises(StrictCommitFailed, wt.commit,
392
387
            message='add hello but not goodbye', strict=True)
393
388
 
396
391
        should work."""
397
392
        wt = self.make_branch_and_tree('.')
398
393
        b = wt.branch
399
 
        file('hello', 'w').write('hello world')
 
394
        with file('hello', 'w') as f: f.write('hello world')
400
395
        wt.add('hello')
401
396
        wt.commit(message='add hello', strict=True)
402
397
 
404
399
        """Try and commit with unknown files and strict = False, should work."""
405
400
        wt = self.make_branch_and_tree('.')
406
401
        b = wt.branch
407
 
        file('hello', 'w').write('hello world')
 
402
        with file('hello', 'w') as f: f.write('hello world')
408
403
        wt.add('hello')
409
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
404
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
410
405
        wt.commit(message='add hello but not goodbye', strict=False)
411
406
 
412
407
    def test_nonstrict_commit_without_unknowns(self):
414
409
        should work."""
415
410
        wt = self.make_branch_and_tree('.')
416
411
        b = wt.branch
417
 
        file('hello', 'w').write('hello world')
 
412
        with file('hello', 'w') as f: f.write('hello world')
418
413
        wt.add('hello')
419
414
        wt.commit(message='add hello', strict=False)
420
415
 
430
425
            from bzrlib.testament import Testament
431
426
            # monkey patch gpg signing mechanism
432
427
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
433
 
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
434
 
                                                      allow_pointless=True,
435
 
                                                      rev_id='B',
436
 
                                                      working_tree=wt)
 
428
            conf = config.MemoryStack('''
 
429
gpg_signing_command=cat -
 
430
create_signatures=always
 
431
''')
 
432
            commit.Commit(config_stack=conf).commit(
 
433
                message="base", allow_pointless=True, rev_id='B',
 
434
                working_tree=wt)
437
435
            def sign(text):
438
436
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
439
437
            self.assertEqual(sign(Testament.from_revision(branch.repository,
440
 
                             'B').as_short_text()),
 
438
                                                          'B').as_short_text()),
441
439
                             branch.repository.get_signature_text('B'))
442
440
        finally:
443
441
            bzrlib.gpg.GPGStrategy = oldstrategy
453
451
        try:
454
452
            # monkey patch gpg signing mechanism
455
453
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
456
 
            config = MustSignConfig(branch)
 
454
            conf = config.MemoryStack('''
 
455
gpg_signing_command=cat -
 
456
create_signatures=always
 
457
''')
457
458
            self.assertRaises(SigningFailed,
458
 
                              commit.Commit(config=config).commit,
 
459
                              commit.Commit(config_stack=conf).commit,
459
460
                              message="base",
460
461
                              allow_pointless=True,
461
462
                              rev_id='B',
462
463
                              working_tree=wt)
463
464
            branch = Branch.open(self.get_url('.'))
464
 
            self.assertEqual(branch.revision_history(), ['A'])
 
465
            self.assertEqual(branch.last_revision(), 'A')
465
466
            self.assertFalse(branch.repository.has_revision('B'))
466
467
        finally:
467
468
            bzrlib.gpg.GPGStrategy = oldstrategy
475
476
            calls.append('called')
476
477
        bzrlib.ahook = called
477
478
        try:
478
 
            config = BranchWithHooks(branch)
479
 
            commit.Commit(config=config).commit(
480
 
                            message = "base",
481
 
                            allow_pointless=True,
482
 
                            rev_id='A', working_tree = wt)
 
479
            conf = config.MemoryStack('post_commit=bzrlib.ahook bzrlib.ahook')
 
480
            commit.Commit(config_stack=conf).commit(
 
481
                message = "base", allow_pointless=True, rev_id='A',
 
482
                working_tree = wt)
483
483
            self.assertEqual(['called', 'called'], calls)
484
484
        finally:
485
485
            del bzrlib.ahook