~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

Move working tree initialisation out from  Branch.initialize, deprecated Branch.initialize to Branch.create.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import os
19
19
 
20
20
import bzrlib
21
 
from bzrlib.tests import TestCaseInTempDir
 
21
from bzrlib.tests import TestCaseWithTransport
22
22
from bzrlib.branch import Branch
23
23
from bzrlib.workingtree import WorkingTree
24
24
from bzrlib.commit import Commit
43
43
        return "bzrlib.ahook bzrlib.ahook"
44
44
 
45
45
 
46
 
class TestCommit(TestCaseInTempDir):
 
46
class TestCommit(TestCaseWithTransport):
47
47
 
48
48
    def test_simple_commit(self):
49
49
        """Commit and check two versions of a single file."""
50
 
        b = Branch.initialize(u'.')
 
50
        wt = self.make_branch_and_tree('.')
 
51
        b = wt.branch
51
52
        file('hello', 'w').write('hello world')
52
 
        b.working_tree().add('hello')
53
 
        b.working_tree().commit(message='add hello')
54
 
        file_id = b.working_tree().path2id('hello')
 
53
        wt.add('hello')
 
54
        wt.commit(message='add hello')
 
55
        file_id = wt.path2id('hello')
55
56
 
56
57
        file('hello', 'w').write('version 2')
57
 
        b.working_tree().commit(message='commit 2')
 
58
        wt.commit(message='commit 2')
58
59
 
59
60
        eq = self.assertEquals
60
61
        eq(b.revno(), 2)
71
72
 
72
73
    def test_delete_commit(self):
73
74
        """Test a commit with a deleted file"""
74
 
        b = Branch.initialize(u'.')
 
75
        wt = self.make_branch_and_tree('.')
 
76
        b = wt.branch
75
77
        file('hello', 'w').write('hello world')
76
 
        b.working_tree().add(['hello'], ['hello-id'])
77
 
        b.working_tree().commit(message='add hello')
 
78
        wt.add(['hello'], ['hello-id'])
 
79
        wt.commit(message='add hello')
78
80
 
79
81
        os.remove('hello')
80
 
        b.working_tree().commit('removed hello', rev_id='rev2')
 
82
        wt.commit('removed hello', rev_id='rev2')
81
83
 
82
84
        tree = b.revision_tree('rev2')
83
85
        self.assertFalse(tree.has_id('hello-id'))
84
86
 
85
87
    def test_pointless_commit(self):
86
88
        """Commit refuses unless there are changes or it's forced."""
87
 
        b = Branch.initialize(u'.')
 
89
        wt = self.make_branch_and_tree('.')
 
90
        b = wt.branch
88
91
        file('hello', 'w').write('hello')
89
 
        b.working_tree().add(['hello'])
90
 
        b.working_tree().commit(message='add hello')
 
92
        wt.add(['hello'])
 
93
        wt.commit(message='add hello')
91
94
        self.assertEquals(b.revno(), 1)
92
95
        self.assertRaises(PointlessCommit,
93
 
                          b.working_tree().commit,
 
96
                          wt.commit,
94
97
                          message='fails',
95
98
                          allow_pointless=False)
96
99
        self.assertEquals(b.revno(), 1)
97
100
        
98
101
    def test_commit_empty(self):
99
102
        """Commiting an empty tree works."""
100
 
        b = Branch.initialize(u'.')
101
 
        b.working_tree().commit(message='empty tree', allow_pointless=True)
 
103
        wt = self.make_branch_and_tree('.')
 
104
        b = wt.branch
 
105
        wt.commit(message='empty tree', allow_pointless=True)
102
106
        self.assertRaises(PointlessCommit,
103
 
                          b.working_tree().commit,
 
107
                          wt.commit,
104
108
                          message='empty tree',
105
109
                          allow_pointless=False)
106
 
        b.working_tree().commit(message='empty tree', allow_pointless=True)
 
110
        wt.commit(message='empty tree', allow_pointless=True)
107
111
        self.assertEquals(b.revno(), 2)
108
112
 
109
 
 
110
113
    def test_selective_delete(self):
111
114
        """Selective commit in tree with deletions"""
112
 
        b = Branch.initialize(u'.')
 
115
        wt = self.make_branch_and_tree('.')
 
116
        b = wt.branch
113
117
        file('hello', 'w').write('hello')
114
118
        file('buongia', 'w').write('buongia')
115
 
        b.working_tree().add(['hello', 'buongia'],
 
119
        wt.add(['hello', 'buongia'],
116
120
              ['hello-id', 'buongia-id'])
117
 
        b.working_tree().commit(message='add files',
 
121
        wt.commit(message='add files',
118
122
                 rev_id='test@rev-1')
119
123
        
120
124
        os.remove('hello')
121
125
        file('buongia', 'w').write('new text')
122
 
        b.working_tree().commit(message='update text',
 
126
        wt.commit(message='update text',
123
127
                 specific_files=['buongia'],
124
128
                 allow_pointless=False,
125
129
                 rev_id='test@rev-2')
126
130
 
127
 
        b.working_tree().commit(message='remove hello',
 
131
        wt.commit(message='remove hello',
128
132
                 specific_files=['hello'],
129
133
                 allow_pointless=False,
130
134
                 rev_id='test@rev-3')
141
145
        self.assertFalse(tree3.has_filename('hello'))
142
146
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
143
147
 
144
 
 
145
148
    def test_commit_rename(self):
146
149
        """Test commit of a revision where a file is renamed."""
147
 
        b = Branch.initialize(u'.')
148
 
        tree = WorkingTree(u'.', b)
 
150
        tree = self.make_branch_and_tree('.')
 
151
        b = tree.branch
149
152
        self.build_tree(['hello'], line_endings='binary')
150
153
        tree.add(['hello'], ['hello-id'])
151
154
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
171
174
 
172
175
    def test_reused_rev_id(self):
173
176
        """Test that a revision id cannot be reused in a branch"""
174
 
        b = Branch.initialize(u'.')
175
 
        b.working_tree().commit('initial', rev_id='test@rev-1', allow_pointless=True)
 
177
        wt = self.make_branch_and_tree('.')
 
178
        b = wt.branch
 
179
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
176
180
        self.assertRaises(Exception,
177
181
                          b.working_tree().commit,
178
182
                          message='reused id',
182
186
    def test_commit_move(self):
183
187
        """Test commit of revisions with moved files and directories"""
184
188
        eq = self.assertEquals
185
 
        b = Branch.initialize(u'.')
 
189
        wt = self.make_branch_and_tree('.')
 
190
        b = wt.branch
186
191
        r1 = 'test@rev-1'
187
192
        self.build_tree(['hello', 'a/', 'b/'])
188
 
        b.working_tree().add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
189
 
        b.working_tree().commit('initial', rev_id=r1, allow_pointless=False)
190
 
        b.working_tree().move(['hello'], 'a')
 
193
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
 
194
        wt.commit('initial', rev_id=r1, allow_pointless=False)
 
195
        wt.move(['hello'], 'a')
191
196
        r2 = 'test@rev-2'
192
 
        b.working_tree().commit('two', rev_id=r2, allow_pointless=False)
 
197
        wt.commit('two', rev_id=r2, allow_pointless=False)
193
198
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
194
199
                                   ['a', 'a/hello', 'b'])
195
200
 
196
 
        b.working_tree().move(['b'], 'a')
 
201
        wt.move(['b'], 'a')
197
202
        r3 = 'test@rev-3'
198
 
        b.working_tree().commit('three', rev_id=r3, allow_pointless=False)
199
 
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
 
203
        wt.commit('three', rev_id=r3, allow_pointless=False)
 
204
        self.check_inventory_shape(wt.read_working_inventory(),
200
205
                                   ['a', 'a/hello', 'a/b'])
201
206
        self.check_inventory_shape(b.get_revision_inventory(r3),
202
207
                                   ['a', 'a/hello', 'a/b'])
203
208
 
204
 
        b.working_tree().move(['a/hello'], 'a/b')
 
209
        wt.move(['a/hello'], 'a/b')
205
210
        r4 = 'test@rev-4'
206
 
        b.working_tree().commit('four', rev_id=r4, allow_pointless=False)
207
 
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
 
211
        wt.commit('four', rev_id=r4, allow_pointless=False)
 
212
        self.check_inventory_shape(wt.read_working_inventory(),
208
213
                                   ['a', 'a/b/hello', 'a/b'])
209
214
 
210
215
        inv = b.get_revision_inventory(r4)
214
219
        
215
220
    def test_removed_commit(self):
216
221
        """Commit with a removed file"""
217
 
        b = Branch.initialize(u'.')
218
 
        wt = b.working_tree()
 
222
        wt = self.make_branch_and_tree('.')
 
223
        b = wt.branch
219
224
        file('hello', 'w').write('hello world')
220
 
        b.working_tree().add(['hello'], ['hello-id'])
221
 
        b.working_tree().commit(message='add hello')
222
 
 
223
 
        wt = b.working_tree()  # FIXME: kludge for aliasing of working inventory
 
225
        wt.add(['hello'], ['hello-id'])
 
226
        wt.commit(message='add hello')
224
227
        wt.remove('hello')
225
 
        b.working_tree().commit('removed hello', rev_id='rev2')
 
228
        wt.commit('removed hello', rev_id='rev2')
226
229
 
227
230
        tree = b.revision_tree('rev2')
228
231
        self.assertFalse(tree.has_id('hello-id'))
229
232
 
230
 
 
231
233
    def test_committed_ancestry(self):
232
234
        """Test commit appends revisions to ancestry."""
233
 
        b = Branch.initialize(u'.')
 
235
        wt = self.make_branch_and_tree('.')
 
236
        b = wt.branch
234
237
        rev_ids = []
235
238
        for i in range(4):
236
239
            file('hello', 'w').write((str(i) * 4) + '\n')
237
240
            if i == 0:
238
 
                b.working_tree().add(['hello'], ['hello-id'])
 
241
                wt.add(['hello'], ['hello-id'])
239
242
            rev_id = 'test@rev-%d' % (i+1)
240
243
            rev_ids.append(rev_id)
241
 
            b.working_tree().commit(message='rev %d' % (i+1),
 
244
            wt.commit(message='rev %d' % (i+1),
242
245
                     rev_id=rev_id)
243
246
        eq = self.assertEquals
244
247
        eq(b.revision_history(), rev_ids)
247
250
            eq(anc, [None] + rev_ids[:i+1])
248
251
 
249
252
    def test_commit_new_subdir_child_selective(self):
250
 
        b = Branch.initialize(u'.')
 
253
        wt = self.make_branch_and_tree('.')
 
254
        b = wt.branch
251
255
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
252
 
        b.working_tree().add(['dir', 'dir/file1', 'dir/file2'],
 
256
        wt.add(['dir', 'dir/file1', 'dir/file2'],
253
257
              ['dirid', 'file1id', 'file2id'])
254
 
        b.working_tree().commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
 
258
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
255
259
        inv = b.get_inventory('1')
256
260
        self.assertEqual('1', inv['dirid'].revision)
257
261
        self.assertEqual('1', inv['file1id'].revision)
261
265
    def test_strict_commit(self):
262
266
        """Try and commit with unknown files and strict = True, should fail."""
263
267
        from bzrlib.errors import StrictCommitFailed
264
 
        b = Branch.initialize(u'.')
 
268
        wt = self.make_branch_and_tree('.')
 
269
        b = wt.branch
265
270
        file('hello', 'w').write('hello world')
266
 
        b.working_tree().add('hello')
 
271
        wt.add('hello')
267
272
        file('goodbye', 'w').write('goodbye cruel world!')
268
273
        self.assertRaises(StrictCommitFailed, b.working_tree().commit,
269
274
            message='add hello but not goodbye', strict=True)
272
277
        """Try and commit with no unknown files and strict = True,
273
278
        should work."""
274
279
        from bzrlib.errors import StrictCommitFailed
275
 
        b = Branch.initialize(u'.')
 
280
        wt = self.make_branch_and_tree('.')
 
281
        b = wt.branch
276
282
        file('hello', 'w').write('hello world')
277
 
        b.working_tree().add('hello')
278
 
        b.working_tree().commit(message='add hello', strict=True)
 
283
        wt.add('hello')
 
284
        wt.commit(message='add hello', strict=True)
279
285
 
280
286
    def test_nonstrict_commit(self):
281
287
        """Try and commit with unknown files and strict = False, should work."""
282
 
        b = Branch.initialize(u'.')
 
288
        wt = self.make_branch_and_tree('.')
 
289
        b = wt.branch
283
290
        file('hello', 'w').write('hello world')
284
 
        b.working_tree().add('hello')
 
291
        wt.add('hello')
285
292
        file('goodbye', 'w').write('goodbye cruel world!')
286
 
        b.working_tree().commit(message='add hello but not goodbye', strict=False)
 
293
        wt.commit(message='add hello but not goodbye', strict=False)
287
294
 
288
295
    def test_nonstrict_commit_without_unknowns(self):
289
296
        """Try and commit with no unknown files and strict = False,
290
297
        should work."""
291
 
        b = Branch.initialize(u'.')
 
298
        wt = self.make_branch_and_tree('.')
 
299
        b = wt.branch
292
300
        file('hello', 'w').write('hello world')
293
 
        b.working_tree().add('hello')
294
 
        b.working_tree().commit(message='add hello', strict=False)
 
301
        wt.add('hello')
 
302
        wt.commit(message='add hello', strict=False)
295
303
 
296
304
    def test_signed_commit(self):
297
305
        import bzrlib.gpg
298
306
        import bzrlib.commit as commit
299
307
        oldstrategy = bzrlib.gpg.GPGStrategy
300
 
        branch = Branch.initialize(u'.')
301
 
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
 
308
        wt = self.make_branch_and_tree('.')
 
309
        branch = wt.branch
 
310
        wt.commit("base", allow_pointless=True, rev_id='A')
302
311
        self.failIf(branch.revision_store.has_id('A', 'sig'))
303
312
        try:
304
313
            from bzrlib.testament import Testament
305
314
            # monkey patch gpg signing mechanism
306
315
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
307
 
            commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
 
316
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
308
317
                                                      allow_pointless=True,
309
 
                                                      rev_id='B')
 
318
                                                      rev_id='B',
 
319
                                                      working_tree=wt)
310
320
            self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
311
321
                             branch.revision_store.get('B', 'sig').read())
312
322
        finally:
316
326
        import bzrlib.gpg
317
327
        import bzrlib.commit as commit
318
328
        oldstrategy = bzrlib.gpg.GPGStrategy
319
 
        branch = Branch.initialize(u'.')
320
 
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
 
329
        wt = self.make_branch_and_tree('.')
 
330
        branch = wt.branch
 
331
        wt.commit("base", allow_pointless=True, rev_id='A')
321
332
        self.failIf(branch.revision_store.has_id('A', 'sig'))
322
333
        try:
323
334
            from bzrlib.testament import Testament
329
340
                              branch, "base",
330
341
                              allow_pointless=True,
331
342
                              rev_id='B')
332
 
            branch = Branch.open(u'.')
 
343
            branch = Branch.open(self.get_url('.'))
333
344
            self.assertEqual(branch.revision_history(), ['A'])
334
345
            self.failIf(branch.revision_store.has_id('B'))
335
346
        finally:
337
348
 
338
349
    def test_commit_invokes_hooks(self):
339
350
        import bzrlib.commit as commit
340
 
        branch = Branch.initialize(u'.')
 
351
        wt = self.make_branch_and_tree('.')
 
352
        branch = wt.branch
341
353
        calls = []
342
354
        def called(branch, rev_id):
343
355
            calls.append('called')
345
357
        try:
346
358
            config = BranchWithHooks(branch)
347
359
            commit.Commit(config=config).commit(
348
 
                            branch, "base",
 
360
                            message = "base",
349
361
                            allow_pointless=True,
350
 
                            rev_id='A')
 
362
                            rev_id='A', working_tree = wt)
351
363
            self.assertEqual(['called', 'called'], calls)
352
364
        finally:
353
365
            del bzrlib.ahook