~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

  • Committer: Patch Queue Manager
  • Date: 2015-12-17 18:39:00 UTC
  • mfrom: (6606.1.2 fix-float)
  • Revision ID: pqm@pqm.ubuntu.com-20151217183900-0719du2uv1kwu3lc
(vila) Inline testtools private method to fix an issue in xenial (the
 private implementation has changed in an backward incompatible way).
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import bzrlib
21
21
from bzrlib import (
22
 
    bzrdir,
 
22
    config,
 
23
    controldir,
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
 
194
187
        """Commit refuses unless there are changes or it's forced."""
195
188
        wt = self.make_branch_and_tree('.')
196
189
        b = wt.branch
197
 
        file('hello', 'w').write('hello')
 
190
        with file('hello', 'w') as f: f.write('hello')
198
191
        wt.add(['hello'])
199
192
        wt.commit(message='add hello')
200
193
        self.assertEquals(b.revno(), 1)
220
213
        """Selective commit in tree with deletions"""
221
214
        wt = self.make_branch_and_tree('.')
222
215
        b = wt.branch
223
 
        file('hello', 'w').write('hello')
224
 
        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')
225
218
        wt.add(['hello', 'buongia'],
226
219
              ['hello-id', 'buongia-id'])
227
220
        wt.commit(message='add files',
228
221
                 rev_id='test@rev-1')
229
222
 
230
223
        os.remove('hello')
231
 
        file('buongia', 'w').write('new text')
 
224
        with file('buongia', 'w') as f: f.write('new text')
232
225
        wt.commit(message='update text',
233
226
                 specific_files=['buongia'],
234
227
                 allow_pointless=False,
343
336
        """Commit with a removed file"""
344
337
        wt = self.make_branch_and_tree('.')
345
338
        b = wt.branch
346
 
        file('hello', 'w').write('hello world')
 
339
        with file('hello', 'w') as f: f.write('hello world')
347
340
        wt.add(['hello'], ['hello-id'])
348
341
        wt.commit(message='add hello')
349
342
        wt.remove('hello')
358
351
        b = wt.branch
359
352
        rev_ids = []
360
353
        for i in range(4):
361
 
            file('hello', 'w').write((str(i) * 4) + '\n')
 
354
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
362
355
            if i == 0:
363
356
                wt.add(['hello'], ['hello-id'])
364
357
            rev_id = 'test@rev-%d' % (i+1)
365
358
            rev_ids.append(rev_id)
366
359
            wt.commit(message='rev %d' % (i+1),
367
360
                     rev_id=rev_id)
368
 
        eq = self.assertEquals
369
 
        eq(b.revision_history(), rev_ids)
370
361
        for i in range(4):
371
362
            self.assertThat(rev_ids[:i+1],
372
363
                MatchesAncestry(b.repository, rev_ids[i]))
389
380
        from bzrlib.errors import StrictCommitFailed
390
381
        wt = self.make_branch_and_tree('.')
391
382
        b = wt.branch
392
 
        file('hello', 'w').write('hello world')
 
383
        with file('hello', 'w') as f: f.write('hello world')
393
384
        wt.add('hello')
394
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
385
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
395
386
        self.assertRaises(StrictCommitFailed, wt.commit,
396
387
            message='add hello but not goodbye', strict=True)
397
388
 
400
391
        should work."""
401
392
        wt = self.make_branch_and_tree('.')
402
393
        b = wt.branch
403
 
        file('hello', 'w').write('hello world')
 
394
        with file('hello', 'w') as f: f.write('hello world')
404
395
        wt.add('hello')
405
396
        wt.commit(message='add hello', strict=True)
406
397
 
408
399
        """Try and commit with unknown files and strict = False, should work."""
409
400
        wt = self.make_branch_and_tree('.')
410
401
        b = wt.branch
411
 
        file('hello', 'w').write('hello world')
 
402
        with file('hello', 'w') as f: f.write('hello world')
412
403
        wt.add('hello')
413
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
404
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
414
405
        wt.commit(message='add hello but not goodbye', strict=False)
415
406
 
416
407
    def test_nonstrict_commit_without_unknowns(self):
418
409
        should work."""
419
410
        wt = self.make_branch_and_tree('.')
420
411
        b = wt.branch
421
 
        file('hello', 'w').write('hello world')
 
412
        with file('hello', 'w') as f: f.write('hello world')
422
413
        wt.add('hello')
423
414
        wt.commit(message='add hello', strict=False)
424
415
 
434
425
            from bzrlib.testament import Testament
435
426
            # monkey patch gpg signing mechanism
436
427
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
437
 
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
438
 
                                                      allow_pointless=True,
439
 
                                                      rev_id='B',
440
 
                                                      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)
441
435
            def sign(text):
442
436
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
443
437
            self.assertEqual(sign(Testament.from_revision(branch.repository,
444
 
                             'B').as_short_text()),
 
438
                                                          'B').as_short_text()),
445
439
                             branch.repository.get_signature_text('B'))
446
440
        finally:
447
441
            bzrlib.gpg.GPGStrategy = oldstrategy
457
451
        try:
458
452
            # monkey patch gpg signing mechanism
459
453
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
460
 
            config = MustSignConfig(branch)
 
454
            conf = config.MemoryStack('''
 
455
gpg_signing_command=cat -
 
456
create_signatures=always
 
457
''')
461
458
            self.assertRaises(SigningFailed,
462
 
                              commit.Commit(config=config).commit,
 
459
                              commit.Commit(config_stack=conf).commit,
463
460
                              message="base",
464
461
                              allow_pointless=True,
465
462
                              rev_id='B',
466
463
                              working_tree=wt)
467
464
            branch = Branch.open(self.get_url('.'))
468
 
            self.assertEqual(branch.revision_history(), ['A'])
 
465
            self.assertEqual(branch.last_revision(), 'A')
469
466
            self.assertFalse(branch.repository.has_revision('B'))
470
467
        finally:
471
468
            bzrlib.gpg.GPGStrategy = oldstrategy
479
476
            calls.append('called')
480
477
        bzrlib.ahook = called
481
478
        try:
482
 
            config = BranchWithHooks(branch)
483
 
            commit.Commit(config=config).commit(
484
 
                            message = "base",
485
 
                            allow_pointless=True,
486
 
                            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)
487
483
            self.assertEqual(['called', 'called'], calls)
488
484
        finally:
489
485
            del bzrlib.ahook
839
835
    def test_commit_with_checkout_and_branch_sharing_repo(self):
840
836
        repo = self.make_repository('repo', shared=True)
841
837
        # make_branch_and_tree ignores shared repos
842
 
        branch = bzrdir.BzrDir.create_branch_convenience('repo/branch')
 
838
        branch = controldir.ControlDir.create_branch_convenience('repo/branch')
843
839
        tree2 = branch.create_checkout('repo/tree2')
844
840
        tree2.commit('message', rev_id='rev1')
845
841
        self.assertTrue(tree2.branch.repository.has_revision('rev1'))