~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branchbuilder.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-27 04:48:39 UTC
  • mto: (1711.2.26 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 1734.
  • Revision ID: john@arbash-meinel.com-20060527044839-83fdd64ce7d708bb
Instead of iterating randomly in both trees, _compare_trees now iterates in order on both trees simultaneously.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007, 2009 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Tests for the BranchBuilder class."""
18
 
 
19
 
from bzrlib import (
20
 
    branch as _mod_branch,
21
 
    errors,
22
 
    revision as _mod_revision,
23
 
    tests,
24
 
    )
25
 
from bzrlib.branchbuilder import BranchBuilder
26
 
 
27
 
 
28
 
class TestBranchBuilder(tests.TestCaseWithMemoryTransport):
29
 
 
30
 
    def test_create(self):
31
 
        """Test the constructor api."""
32
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
33
 
        # we dont care if the branch has been built or not at this point.
34
 
 
35
 
    def test_get_branch(self):
36
 
        """get_branch returns the created branch."""
37
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
38
 
        branch = builder.get_branch()
39
 
        self.assertIsInstance(branch, _mod_branch.Branch)
40
 
        self.assertEqual(self.get_transport().clone('foo').base,
41
 
            branch.base)
42
 
        self.assertEqual(
43
 
            (0, _mod_revision.NULL_REVISION),
44
 
            branch.last_revision_info())
45
 
 
46
 
    def test_format(self):
47
 
        """Making a BranchBuilder with a format option sets the branch type."""
48
 
        builder = BranchBuilder(self.get_transport(), format='dirstate-tags')
49
 
        branch = builder.get_branch()
50
 
        self.assertIsInstance(branch, _mod_branch.BzrBranch6)
51
 
 
52
 
    def test_build_one_commit(self):
53
 
        """doing build_commit causes a commit to happen."""
54
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
55
 
        rev_id = builder.build_commit()
56
 
        branch = builder.get_branch()
57
 
        self.assertEqual((1, rev_id), branch.last_revision_info())
58
 
        self.assertEqual(
59
 
            'commit 1',
60
 
            branch.repository.get_revision(branch.last_revision()).message)
61
 
 
62
 
    def test_build_commit_timestamp(self):
63
 
        """You can set a date when committing."""
64
 
        builder = self.make_branch_builder('foo')
65
 
        rev_id = builder.build_commit(timestamp=1236043340)
66
 
        branch = builder.get_branch()
67
 
        self.assertEqual((1, rev_id), branch.last_revision_info())
68
 
        rev = branch.repository.get_revision(branch.last_revision())
69
 
        self.assertEqual(
70
 
            'commit 1',
71
 
            rev.message)
72
 
        self.assertEqual(
73
 
            1236043340,
74
 
            int(rev.timestamp))
75
 
 
76
 
    def test_build_two_commits(self):
77
 
        """The second commit has the right parents and message."""
78
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
79
 
        rev_id1 = builder.build_commit()
80
 
        rev_id2 = builder.build_commit()
81
 
        branch = builder.get_branch()
82
 
        self.assertEqual((2, rev_id2), branch.last_revision_info())
83
 
        self.assertEqual(
84
 
            'commit 2',
85
 
            branch.repository.get_revision(branch.last_revision()).message)
86
 
        self.assertEqual(
87
 
            [rev_id1],
88
 
            branch.repository.get_revision(branch.last_revision()).parent_ids)
89
 
 
90
 
 
91
 
class TestBranchBuilderBuildSnapshot(tests.TestCaseWithMemoryTransport):
92
 
 
93
 
    def assertTreeShape(self, expected_shape, tree):
94
 
        """Check that the tree shape matches expectations."""
95
 
        tree.lock_read()
96
 
        try:
97
 
            entries = [(path, ie.file_id, ie.kind)
98
 
                       for path, ie in tree.iter_entries_by_dir()]
99
 
        finally:
100
 
            tree.unlock()
101
 
        self.assertEqual(expected_shape, entries)
102
 
 
103
 
    def build_a_rev(self):
104
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
105
 
        rev_id1 = builder.build_snapshot('A-id', None,
106
 
            [('add', ('', 'a-root-id', 'directory', None)),
107
 
             ('add', ('a', 'a-id', 'file', 'contents'))])
108
 
        self.assertEqual('A-id', rev_id1)
109
 
        return builder
110
 
 
111
 
    def test_add_one_file(self):
112
 
        builder = self.build_a_rev()
113
 
        branch = builder.get_branch()
114
 
        self.assertEqual((1, 'A-id'), branch.last_revision_info())
115
 
        rev_tree = branch.repository.revision_tree('A-id')
116
 
        rev_tree.lock_read()
117
 
        self.addCleanup(rev_tree.unlock)
118
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
119
 
                              (u'a', 'a-id', 'file')], rev_tree)
120
 
        self.assertEqual('contents', rev_tree.get_file_text('a-id'))
121
 
 
122
 
    def test_add_second_file(self):
123
 
        builder = self.build_a_rev()
124
 
        rev_id2 = builder.build_snapshot('B-id', None,
125
 
            [('add', ('b', 'b-id', 'file', 'content_b'))])
126
 
        self.assertEqual('B-id', rev_id2)
127
 
        branch = builder.get_branch()
128
 
        self.assertEqual((2, rev_id2), branch.last_revision_info())
129
 
        rev_tree = branch.repository.revision_tree(rev_id2)
130
 
        rev_tree.lock_read()
131
 
        self.addCleanup(rev_tree.unlock)
132
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
133
 
                              (u'a', 'a-id', 'file'),
134
 
                              (u'b', 'b-id', 'file')], rev_tree)
135
 
        self.assertEqual('content_b', rev_tree.get_file_text('b-id'))
136
 
 
137
 
    def test_add_empty_dir(self):
138
 
        builder = self.build_a_rev()
139
 
        rev_id2 = builder.build_snapshot('B-id', None,
140
 
            [('add', ('b', 'b-id', 'directory', None))])
141
 
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
142
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
143
 
                              (u'a', 'a-id', 'file'),
144
 
                              (u'b', 'b-id', 'directory'),
145
 
                             ], rev_tree)
146
 
 
147
 
    def test_commit_timestamp(self):
148
 
        builder = self.make_branch_builder('foo')
149
 
        rev_id = builder.build_snapshot(None, None,
150
 
            [('add', (u'', None, 'directory', None))],
151
 
            timestamp=1234567890)
152
 
        rev = builder.get_branch().repository.get_revision(rev_id)
153
 
        self.assertEqual(
154
 
            1234567890,
155
 
            int(rev.timestamp))
156
 
 
157
 
    def test_commit_message_default(self):
158
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
159
 
        rev_id = builder.build_snapshot(None, None,
160
 
            [('add', (u'', None, 'directory', None))])
161
 
        branch = builder.get_branch()
162
 
        rev = branch.repository.get_revision(rev_id)
163
 
        self.assertEqual(u'commit 1', rev.message)
164
 
 
165
 
    def test_commit_message_supplied(self):
166
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
167
 
        rev_id = builder.build_snapshot(None, None,
168
 
            [('add', (u'', None, 'directory', None))],
169
 
            message=u'Foo')
170
 
        branch = builder.get_branch()
171
 
        rev = branch.repository.get_revision(rev_id)
172
 
        self.assertEqual(u'Foo', rev.message)
173
 
 
174
 
    def test_commit_message_callback(self):
175
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
176
 
        rev_id = builder.build_snapshot(None, None,
177
 
            [('add', (u'', None, 'directory', None))],
178
 
            message_callback=lambda x:u'Foo')
179
 
        branch = builder.get_branch()
180
 
        rev = branch.repository.get_revision(rev_id)
181
 
        self.assertEqual(u'Foo', rev.message)
182
 
 
183
 
    def test_modify_file(self):
184
 
        builder = self.build_a_rev()
185
 
        rev_id2 = builder.build_snapshot('B-id', None,
186
 
            [('modify', ('a-id', 'new\ncontent\n'))])
187
 
        self.assertEqual('B-id', rev_id2)
188
 
        branch = builder.get_branch()
189
 
        rev_tree = branch.repository.revision_tree(rev_id2)
190
 
        rev_tree.lock_read()
191
 
        self.addCleanup(rev_tree.unlock)
192
 
        self.assertEqual('new\ncontent\n', rev_tree.get_file_text('a-id'))
193
 
 
194
 
    def test_delete_file(self):
195
 
        builder = self.build_a_rev()
196
 
        rev_id2 = builder.build_snapshot('B-id', None,
197
 
            [('unversion', 'a-id')])
198
 
        self.assertEqual('B-id', rev_id2)
199
 
        branch = builder.get_branch()
200
 
        rev_tree = branch.repository.revision_tree(rev_id2)
201
 
        rev_tree.lock_read()
202
 
        self.addCleanup(rev_tree.unlock)
203
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory')], rev_tree)
204
 
 
205
 
    def test_delete_directory(self):
206
 
        builder = self.build_a_rev()
207
 
        rev_id2 = builder.build_snapshot('B-id', None,
208
 
            [('add', ('b', 'b-id', 'directory', None)),
209
 
             ('add', ('b/c', 'c-id', 'file', 'foo\n')),
210
 
             ('add', ('b/d', 'd-id', 'directory', None)),
211
 
             ('add', ('b/d/e', 'e-id', 'file', 'eff\n')),
212
 
            ])
213
 
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
214
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
215
 
                              (u'a', 'a-id', 'file'),
216
 
                              (u'b', 'b-id', 'directory'),
217
 
                              (u'b/c', 'c-id', 'file'),
218
 
                              (u'b/d', 'd-id', 'directory'),
219
 
                              (u'b/d/e', 'e-id', 'file')], rev_tree)
220
 
        # Removing a directory removes all child dirs
221
 
        builder.build_snapshot('C-id', None, [('unversion', 'b-id')])
222
 
        rev_tree = builder.get_branch().repository.revision_tree('C-id')
223
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
224
 
                              (u'a', 'a-id', 'file'),
225
 
                             ], rev_tree)
226
 
 
227
 
    def test_unknown_action(self):
228
 
        builder = self.build_a_rev()
229
 
        e = self.assertRaises(ValueError,
230
 
            builder.build_snapshot, 'B-id', None, [('weirdo', ('foo',))])
231
 
        self.assertEqual('Unknown build action: "weirdo"', str(e))
232
 
 
233
 
    def test_rename(self):
234
 
        builder = self.build_a_rev()
235
 
        builder.build_snapshot('B-id', None,
236
 
            [('rename', ('a', 'b'))])
237
 
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
238
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
239
 
                              (u'b', 'a-id', 'file')], rev_tree)
240
 
 
241
 
    def test_rename_into_subdir(self):
242
 
        builder = self.build_a_rev()
243
 
        builder.build_snapshot('B-id', None,
244
 
            [('add', ('dir', 'dir-id', 'directory', None)),
245
 
             ('rename', ('a', 'dir/a'))])
246
 
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
247
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
248
 
                              (u'dir', 'dir-id', 'directory'),
249
 
                              (u'dir/a', 'a-id', 'file')], rev_tree)
250
 
 
251
 
    def test_set_parent(self):
252
 
        builder = self.build_a_rev()
253
 
        builder.start_series()
254
 
        self.addCleanup(builder.finish_series)
255
 
        builder.build_snapshot('B-id', ['A-id'],
256
 
            [('modify', ('a-id', 'new\ncontent\n'))])
257
 
        builder.build_snapshot('C-id', ['A-id'],
258
 
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
259
 
        # We should now have a graph:
260
 
        #   A
261
 
        #   |\
262
 
        #   C B
263
 
        # And not A => B => C
264
 
        repo = builder.get_branch().repository
265
 
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',)},
266
 
                         repo.get_parent_map(['B-id', 'C-id']))
267
 
        b_tree = repo.revision_tree('B-id')
268
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
269
 
                              (u'a', 'a-id', 'file'),
270
 
                             ], b_tree)
271
 
        self.assertEqual('new\ncontent\n', b_tree.get_file_text('a-id'))
272
 
 
273
 
        # We should still be using the content from A in C, not from B
274
 
        c_tree = repo.revision_tree('C-id')
275
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
276
 
                              (u'a', 'a-id', 'file'),
277
 
                              (u'c', 'c-id', 'file'),
278
 
                             ], c_tree)
279
 
        self.assertEqual('contents', c_tree.get_file_text('a-id'))
280
 
        self.assertEqual('alt\ncontent\n', c_tree.get_file_text('c-id'))
281
 
 
282
 
    def test_set_merge_parent(self):
283
 
        builder = self.build_a_rev()
284
 
        builder.start_series()
285
 
        self.addCleanup(builder.finish_series)
286
 
        builder.build_snapshot('B-id', ['A-id'],
287
 
            [('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
288
 
        builder.build_snapshot('C-id', ['A-id'],
289
 
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
290
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
291
 
        repo = builder.get_branch().repository
292
 
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
293
 
                          'D-id': ('B-id', 'C-id')},
294
 
                         repo.get_parent_map(['B-id', 'C-id', 'D-id']))
295
 
        d_tree = repo.revision_tree('D-id')
296
 
        # Note: by default a merge node does *not* pull in the changes from the
297
 
        #       merged tree, you have to supply it yourself.
298
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
299
 
                              (u'a', 'a-id', 'file'),
300
 
                              (u'b', 'b-id', 'file'),
301
 
                             ], d_tree)
302
 
 
303
 
    def test_set_merge_parent_and_contents(self):
304
 
        builder = self.build_a_rev()
305
 
        builder.start_series()
306
 
        self.addCleanup(builder.finish_series)
307
 
        builder.build_snapshot('B-id', ['A-id'],
308
 
            [('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
309
 
        builder.build_snapshot('C-id', ['A-id'],
310
 
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
311
 
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
312
 
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
313
 
        repo = builder.get_branch().repository
314
 
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
315
 
                          'D-id': ('B-id', 'C-id')},
316
 
                         repo.get_parent_map(['B-id', 'C-id', 'D-id']))
317
 
        d_tree = repo.revision_tree('D-id')
318
 
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
319
 
                              (u'a', 'a-id', 'file'),
320
 
                              (u'b', 'b-id', 'file'),
321
 
                              (u'c', 'c-id', 'file'),
322
 
                             ], d_tree)
323
 
        # Because we copied the exact text into *this* tree, the 'c' file
324
 
        # should look like it was not modified in the merge
325
 
        self.assertEqual('C-id', d_tree.inventory['c-id'].revision)
326
 
 
327
 
    def test_start_finish_series(self):
328
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
329
 
        builder.start_series()
330
 
        try:
331
 
            self.assertIsNot(None, builder._tree)
332
 
            self.assertEqual('w', builder._tree._lock_mode)
333
 
            self.assertTrue(builder._branch.is_locked())
334
 
        finally:
335
 
            builder.finish_series()
336
 
        self.assertIs(None, builder._tree)
337
 
        self.assertFalse(builder._branch.is_locked())
338
 
 
339
 
    def test_ghost_mainline_history(self):
340
 
        builder = BranchBuilder(self.get_transport().clone('foo'))
341
 
        builder.start_series()
342
 
        try:
343
 
            builder.build_snapshot('tip', ['ghost'],
344
 
                [('add', ('', 'ROOT_ID', 'directory', ''))],
345
 
                allow_leftmost_as_ghost=True)
346
 
        finally:
347
 
            builder.finish_series()
348
 
        b = builder.get_branch()
349
 
        b.lock_read()
350
 
        self.addCleanup(b.unlock)
351
 
        self.assertEqual(('ghost',),
352
 
            b.repository.get_graph().get_parent_map(['tip'])['tip'])