~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

  • Committer: Richard Wilbur
  • Date: 2016-02-04 19:07:28 UTC
  • mto: This revision was merged to the branch mainline in revision 6618.
  • Revision ID: richard.wilbur@gmail.com-20160204190728-p0zvfii6zase0fw7
Update COPYING.txt from the original http://www.gnu.org/licenses/gpl-2.0.txt  (Only differences were in whitespace.)  Thanks to Petr Stodulka for pointing out the discrepancy.

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
 
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed,
30
 
                           LockContention)
 
29
from bzrlib.errors import (
 
30
    PointlessCommit,
 
31
    BzrError,
 
32
    SigningFailed,
 
33
    LockContention,
 
34
    )
31
35
from bzrlib.tests import (
32
 
    SymlinkFeature,
33
36
    TestCaseWithTransport,
34
37
    test_foreign,
35
38
    )
 
39
from bzrlib.tests.features import (
 
40
    SymlinkFeature,
 
41
    )
 
42
from bzrlib.tests.matchers import MatchesAncestry
36
43
 
37
44
 
38
45
# TODO: Test commit with some added, and added-but-missing files
39
46
 
40
 
class MustSignConfig(BranchConfig):
41
 
 
42
 
    def signature_needed(self):
43
 
        return True
44
 
 
45
 
    def gpg_signing_command(self):
46
 
        return ['cat', '-']
47
 
 
48
 
 
49
 
class BranchWithHooks(BranchConfig):
50
 
 
51
 
    def post_commit(self):
52
 
        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
''')
53
54
 
54
55
 
55
56
class CapturingReporter(NullCommitReporter):
81
82
        """Commit and check two versions of a single file."""
82
83
        wt = self.make_branch_and_tree('.')
83
84
        b = wt.branch
84
 
        file('hello', 'w').write('hello world')
 
85
        with file('hello', 'w') as f: f.write('hello world')
85
86
        wt.add('hello')
86
 
        wt.commit(message='add hello')
 
87
        rev1 = wt.commit(message='add hello')
87
88
        file_id = wt.path2id('hello')
88
89
 
89
 
        file('hello', 'w').write('version 2')
90
 
        wt.commit(message='commit 2')
 
90
        with file('hello', 'w') as f: f.write('version 2')
 
91
        rev2 = wt.commit(message='commit 2')
91
92
 
92
93
        eq = self.assertEquals
93
94
        eq(b.revno(), 2)
94
 
        rh = b.revision_history()
95
 
        rev = b.repository.get_revision(rh[0])
 
95
        rev = b.repository.get_revision(rev1)
96
96
        eq(rev.message, 'add hello')
97
97
 
98
 
        tree1 = b.repository.revision_tree(rh[0])
 
98
        tree1 = b.repository.revision_tree(rev1)
99
99
        tree1.lock_read()
100
100
        text = tree1.get_file_text(file_id)
101
101
        tree1.unlock()
102
102
        self.assertEqual('hello world', text)
103
103
 
104
 
        tree2 = b.repository.revision_tree(rh[1])
 
104
        tree2 = b.repository.revision_tree(rev2)
105
105
        tree2.lock_read()
106
106
        text = tree2.get_file_text(file_id)
107
107
        tree2.unlock()
111
111
        """Attempt a lossy commit to a native branch."""
112
112
        wt = self.make_branch_and_tree('.')
113
113
        b = wt.branch
114
 
        file('hello', 'w').write('hello world')
 
114
        with file('hello', 'w') as f: f.write('hello world')
115
115
        wt.add('hello')
116
116
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
117
117
        self.assertEquals('revid', revid)
122
122
        wt = self.make_branch_and_tree('.',
123
123
            format=test_foreign.DummyForeignVcsDirFormat())
124
124
        b = wt.branch
125
 
        file('hello', 'w').write('hello world')
 
125
        with file('hello', 'w') as f: f.write('hello world')
126
126
        wt.add('hello')
127
127
        revid = wt.commit(message='add hello', lossy=True,
128
128
            timestamp=1302659388, timezone=0)
135
135
            format=test_foreign.DummyForeignVcsDirFormat())
136
136
        wt = foreign_branch.create_checkout("local")
137
137
        b = wt.branch
138
 
        file('local/hello', 'w').write('hello world')
 
138
        with file('local/hello', 'w') as f: f.write('hello world')
139
139
        wt.add('hello')
140
140
        revid = wt.commit(message='add hello', lossy=True,
141
141
            timestamp=1302659388, timezone=0)
149
149
        """Test a commit with a missing file"""
150
150
        wt = self.make_branch_and_tree('.')
151
151
        b = wt.branch
152
 
        file('hello', 'w').write('hello world')
 
152
        with file('hello', 'w') as f: f.write('hello world')
153
153
        wt.add(['hello'], ['hello-id'])
154
154
        wt.commit(message='add hello')
155
155
 
156
156
        os.remove('hello')
157
 
        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)
158
162
 
159
163
        tree = b.repository.revision_tree('rev2')
160
164
        self.assertFalse(tree.has_id('hello-id'))
183
187
        """Commit refuses unless there are changes or it's forced."""
184
188
        wt = self.make_branch_and_tree('.')
185
189
        b = wt.branch
186
 
        file('hello', 'w').write('hello')
 
190
        with file('hello', 'w') as f: f.write('hello')
187
191
        wt.add(['hello'])
188
192
        wt.commit(message='add hello')
189
193
        self.assertEquals(b.revno(), 1)
209
213
        """Selective commit in tree with deletions"""
210
214
        wt = self.make_branch_and_tree('.')
211
215
        b = wt.branch
212
 
        file('hello', 'w').write('hello')
213
 
        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')
214
218
        wt.add(['hello', 'buongia'],
215
219
              ['hello-id', 'buongia-id'])
216
220
        wt.commit(message='add files',
217
221
                 rev_id='test@rev-1')
218
222
 
219
223
        os.remove('hello')
220
 
        file('buongia', 'w').write('new text')
 
224
        with file('buongia', 'w') as f: f.write('new text')
221
225
        wt.commit(message='update text',
222
226
                 specific_files=['buongia'],
223
227
                 allow_pointless=False,
332
336
        """Commit with a removed file"""
333
337
        wt = self.make_branch_and_tree('.')
334
338
        b = wt.branch
335
 
        file('hello', 'w').write('hello world')
 
339
        with file('hello', 'w') as f: f.write('hello world')
336
340
        wt.add(['hello'], ['hello-id'])
337
341
        wt.commit(message='add hello')
338
342
        wt.remove('hello')
347
351
        b = wt.branch
348
352
        rev_ids = []
349
353
        for i in range(4):
350
 
            file('hello', 'w').write((str(i) * 4) + '\n')
 
354
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
351
355
            if i == 0:
352
356
                wt.add(['hello'], ['hello-id'])
353
357
            rev_id = 'test@rev-%d' % (i+1)
354
358
            rev_ids.append(rev_id)
355
359
            wt.commit(message='rev %d' % (i+1),
356
360
                     rev_id=rev_id)
357
 
        eq = self.assertEquals
358
 
        eq(b.revision_history(), rev_ids)
359
361
        for i in range(4):
360
 
            anc = b.repository.get_ancestry(rev_ids[i])
361
 
            eq(anc, [None] + rev_ids[:i+1])
 
362
            self.assertThat(rev_ids[:i+1],
 
363
                MatchesAncestry(b.repository, rev_ids[i]))
362
364
 
363
365
    def test_commit_new_subdir_child_selective(self):
364
366
        wt = self.make_branch_and_tree('.')
378
380
        from bzrlib.errors import StrictCommitFailed
379
381
        wt = self.make_branch_and_tree('.')
380
382
        b = wt.branch
381
 
        file('hello', 'w').write('hello world')
 
383
        with file('hello', 'w') as f: f.write('hello world')
382
384
        wt.add('hello')
383
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
385
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
384
386
        self.assertRaises(StrictCommitFailed, wt.commit,
385
387
            message='add hello but not goodbye', strict=True)
386
388
 
389
391
        should work."""
390
392
        wt = self.make_branch_and_tree('.')
391
393
        b = wt.branch
392
 
        file('hello', 'w').write('hello world')
 
394
        with file('hello', 'w') as f: f.write('hello world')
393
395
        wt.add('hello')
394
396
        wt.commit(message='add hello', strict=True)
395
397
 
397
399
        """Try and commit with unknown files and strict = False, should work."""
398
400
        wt = self.make_branch_and_tree('.')
399
401
        b = wt.branch
400
 
        file('hello', 'w').write('hello world')
 
402
        with file('hello', 'w') as f: f.write('hello world')
401
403
        wt.add('hello')
402
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
404
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
403
405
        wt.commit(message='add hello but not goodbye', strict=False)
404
406
 
405
407
    def test_nonstrict_commit_without_unknowns(self):
407
409
        should work."""
408
410
        wt = self.make_branch_and_tree('.')
409
411
        b = wt.branch
410
 
        file('hello', 'w').write('hello world')
 
412
        with file('hello', 'w') as f: f.write('hello world')
411
413
        wt.add('hello')
412
414
        wt.commit(message='add hello', strict=False)
413
415
 
423
425
            from bzrlib.testament import Testament
424
426
            # monkey patch gpg signing mechanism
425
427
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
426
 
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
427
 
                                                      allow_pointless=True,
428
 
                                                      rev_id='B',
429
 
                                                      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)
430
435
            def sign(text):
431
436
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
432
437
            self.assertEqual(sign(Testament.from_revision(branch.repository,
433
 
                             'B').as_short_text()),
 
438
                                                          'B').as_short_text()),
434
439
                             branch.repository.get_signature_text('B'))
435
440
        finally:
436
441
            bzrlib.gpg.GPGStrategy = oldstrategy
446
451
        try:
447
452
            # monkey patch gpg signing mechanism
448
453
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
449
 
            config = MustSignConfig(branch)
 
454
            conf = config.MemoryStack('''
 
455
gpg_signing_command=cat -
 
456
create_signatures=always
 
457
''')
450
458
            self.assertRaises(SigningFailed,
451
 
                              commit.Commit(config=config).commit,
 
459
                              commit.Commit(config_stack=conf).commit,
452
460
                              message="base",
453
461
                              allow_pointless=True,
454
462
                              rev_id='B',
455
463
                              working_tree=wt)
456
464
            branch = Branch.open(self.get_url('.'))
457
 
            self.assertEqual(branch.revision_history(), ['A'])
 
465
            self.assertEqual(branch.last_revision(), 'A')
458
466
            self.assertFalse(branch.repository.has_revision('B'))
459
467
        finally:
460
468
            bzrlib.gpg.GPGStrategy = oldstrategy
468
476
            calls.append('called')
469
477
        bzrlib.ahook = called
470
478
        try:
471
 
            config = BranchWithHooks(branch)
472
 
            commit.Commit(config=config).commit(
473
 
                            message = "base",
474
 
                            allow_pointless=True,
475
 
                            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)
476
483
            self.assertEqual(['called', 'called'], calls)
477
484
        finally:
478
485
            del bzrlib.ahook
828
835
    def test_commit_with_checkout_and_branch_sharing_repo(self):
829
836
        repo = self.make_repository('repo', shared=True)
830
837
        # make_branch_and_tree ignores shared repos
831
 
        branch = bzrdir.BzrDir.create_branch_convenience('repo/branch')
 
838
        branch = controldir.ControlDir.create_branch_convenience('repo/branch')
832
839
        tree2 = branch.create_checkout('repo/tree2')
833
840
        tree2.commit('message', rev_id='rev1')
834
841
        self.assertTrue(tree2.branch.repository.has_revision('rev1'))