~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

(vila) Provide an ``ssl.ca_certs`` default value for supported platforms.
 (Vincent Ladeuil)

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
87
        rev1 = wt.commit(message='add hello')
94
88
        file_id = wt.path2id('hello')
95
89
 
96
 
        file('hello', 'w').write('version 2')
 
90
        with file('hello', 'w') as f: f.write('version 2')
97
91
        rev2 = wt.commit(message='commit 2')
98
92
 
99
93
        eq = self.assertEquals
117
111
        """Attempt a lossy commit to a native branch."""
118
112
        wt = self.make_branch_and_tree('.')
119
113
        b = wt.branch
120
 
        file('hello', 'w').write('hello world')
 
114
        with file('hello', 'w') as f: f.write('hello world')
121
115
        wt.add('hello')
122
116
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
123
117
        self.assertEquals('revid', revid)
128
122
        wt = self.make_branch_and_tree('.',
129
123
            format=test_foreign.DummyForeignVcsDirFormat())
130
124
        b = wt.branch
131
 
        file('hello', 'w').write('hello world')
 
125
        with file('hello', 'w') as f: f.write('hello world')
132
126
        wt.add('hello')
133
127
        revid = wt.commit(message='add hello', lossy=True,
134
128
            timestamp=1302659388, timezone=0)
141
135
            format=test_foreign.DummyForeignVcsDirFormat())
142
136
        wt = foreign_branch.create_checkout("local")
143
137
        b = wt.branch
144
 
        file('local/hello', 'w').write('hello world')
 
138
        with file('local/hello', 'w') as f: f.write('hello world')
145
139
        wt.add('hello')
146
140
        revid = wt.commit(message='add hello', lossy=True,
147
141
            timestamp=1302659388, timezone=0)
155
149
        """Test a commit with a missing file"""
156
150
        wt = self.make_branch_and_tree('.')
157
151
        b = wt.branch
158
 
        file('hello', 'w').write('hello world')
 
152
        with file('hello', 'w') as f: f.write('hello world')
159
153
        wt.add(['hello'], ['hello-id'])
160
154
        wt.commit(message='add hello')
161
155
 
193
187
        """Commit refuses unless there are changes or it's forced."""
194
188
        wt = self.make_branch_and_tree('.')
195
189
        b = wt.branch
196
 
        file('hello', 'w').write('hello')
 
190
        with file('hello', 'w') as f: f.write('hello')
197
191
        wt.add(['hello'])
198
192
        wt.commit(message='add hello')
199
193
        self.assertEquals(b.revno(), 1)
219
213
        """Selective commit in tree with deletions"""
220
214
        wt = self.make_branch_and_tree('.')
221
215
        b = wt.branch
222
 
        file('hello', 'w').write('hello')
223
 
        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')
224
218
        wt.add(['hello', 'buongia'],
225
219
              ['hello-id', 'buongia-id'])
226
220
        wt.commit(message='add files',
227
221
                 rev_id='test@rev-1')
228
222
 
229
223
        os.remove('hello')
230
 
        file('buongia', 'w').write('new text')
 
224
        with file('buongia', 'w') as f: f.write('new text')
231
225
        wt.commit(message='update text',
232
226
                 specific_files=['buongia'],
233
227
                 allow_pointless=False,
342
336
        """Commit with a removed file"""
343
337
        wt = self.make_branch_and_tree('.')
344
338
        b = wt.branch
345
 
        file('hello', 'w').write('hello world')
 
339
        with file('hello', 'w') as f: f.write('hello world')
346
340
        wt.add(['hello'], ['hello-id'])
347
341
        wt.commit(message='add hello')
348
342
        wt.remove('hello')
357
351
        b = wt.branch
358
352
        rev_ids = []
359
353
        for i in range(4):
360
 
            file('hello', 'w').write((str(i) * 4) + '\n')
 
354
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
361
355
            if i == 0:
362
356
                wt.add(['hello'], ['hello-id'])
363
357
            rev_id = 'test@rev-%d' % (i+1)
386
380
        from bzrlib.errors import StrictCommitFailed
387
381
        wt = self.make_branch_and_tree('.')
388
382
        b = wt.branch
389
 
        file('hello', 'w').write('hello world')
 
383
        with file('hello', 'w') as f: f.write('hello world')
390
384
        wt.add('hello')
391
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
385
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
392
386
        self.assertRaises(StrictCommitFailed, wt.commit,
393
387
            message='add hello but not goodbye', strict=True)
394
388
 
397
391
        should work."""
398
392
        wt = self.make_branch_and_tree('.')
399
393
        b = wt.branch
400
 
        file('hello', 'w').write('hello world')
 
394
        with file('hello', 'w') as f: f.write('hello world')
401
395
        wt.add('hello')
402
396
        wt.commit(message='add hello', strict=True)
403
397
 
405
399
        """Try and commit with unknown files and strict = False, should work."""
406
400
        wt = self.make_branch_and_tree('.')
407
401
        b = wt.branch
408
 
        file('hello', 'w').write('hello world')
 
402
        with file('hello', 'w') as f: f.write('hello world')
409
403
        wt.add('hello')
410
 
        file('goodbye', 'w').write('goodbye cruel world!')
 
404
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
411
405
        wt.commit(message='add hello but not goodbye', strict=False)
412
406
 
413
407
    def test_nonstrict_commit_without_unknowns(self):
415
409
        should work."""
416
410
        wt = self.make_branch_and_tree('.')
417
411
        b = wt.branch
418
 
        file('hello', 'w').write('hello world')
 
412
        with file('hello', 'w') as f: f.write('hello world')
419
413
        wt.add('hello')
420
414
        wt.commit(message='add hello', strict=False)
421
415
 
431
425
            from bzrlib.testament import Testament
432
426
            # monkey patch gpg signing mechanism
433
427
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
434
 
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
435
 
                                                      allow_pointless=True,
436
 
                                                      rev_id='B',
437
 
                                                      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)
438
435
            def sign(text):
439
436
                return bzrlib.gpg.LoopbackGPGStrategy(None).sign(text)
440
437
            self.assertEqual(sign(Testament.from_revision(branch.repository,
441
 
                             'B').as_short_text()),
 
438
                                                          'B').as_short_text()),
442
439
                             branch.repository.get_signature_text('B'))
443
440
        finally:
444
441
            bzrlib.gpg.GPGStrategy = oldstrategy
454
451
        try:
455
452
            # monkey patch gpg signing mechanism
456
453
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.DisabledGPGStrategy
457
 
            config = MustSignConfig(branch)
 
454
            conf = config.MemoryStack('''
 
455
gpg_signing_command=cat -
 
456
create_signatures=always
 
457
''')
458
458
            self.assertRaises(SigningFailed,
459
 
                              commit.Commit(config=config).commit,
 
459
                              commit.Commit(config_stack=conf).commit,
460
460
                              message="base",
461
461
                              allow_pointless=True,
462
462
                              rev_id='B',
476
476
            calls.append('called')
477
477
        bzrlib.ahook = called
478
478
        try:
479
 
            config = BranchWithHooks(branch)
480
 
            commit.Commit(config=config).commit(
481
 
                            message = "base",
482
 
                            allow_pointless=True,
483
 
                            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)
484
483
            self.assertEqual(['called', 'called'], calls)
485
484
        finally:
486
485
            del bzrlib.ahook