~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branchbuilder.py

  • Committer: Andrew Bennetts
  • Date: 2008-08-07 00:25:38 UTC
  • mfrom: (3612 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3613.
  • Revision ID: andrew.bennetts@canonical.com-20080807002538-mtl1fcgy2fdabha4
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
from bzrlib import (
20
20
    branch as _mod_branch,
 
21
    errors,
21
22
    revision as _mod_revision,
22
23
    tests,
23
24
    )
71
72
        self.assertEqual(
72
73
            [rev_id1],
73
74
            branch.repository.get_revision(branch.last_revision()).parent_ids)
 
75
 
 
76
 
 
77
class TestBranchBuilderBuildSnapshot(tests.TestCaseWithMemoryTransport):
 
78
 
 
79
    def assertTreeShape(self, expected_shape, tree):
 
80
        """Check that the tree shape matches expectations."""
 
81
        tree.lock_read()
 
82
        try:
 
83
            entries = [(path, ie.file_id, ie.kind)
 
84
                       for path, ie in tree.iter_entries_by_dir()]
 
85
        finally:
 
86
            tree.unlock()
 
87
        self.assertEqual(expected_shape, entries)
 
88
 
 
89
    def build_a_rev(self):
 
90
        builder = BranchBuilder(self.get_transport().clone('foo'))
 
91
        rev_id1 = builder.build_snapshot('A-id', None,
 
92
            [('add', ('', 'a-root-id', 'directory', None)),
 
93
             ('add', ('a', 'a-id', 'file', 'contents'))])
 
94
        self.assertEqual('A-id', rev_id1)
 
95
        return builder
 
96
 
 
97
    def test_add_one_file(self):
 
98
        builder = self.build_a_rev()
 
99
        branch = builder.get_branch()
 
100
        self.assertEqual((1, 'A-id'), branch.last_revision_info())
 
101
        rev_tree = branch.repository.revision_tree('A-id')
 
102
        rev_tree.lock_read()
 
103
        self.addCleanup(rev_tree.unlock)
 
104
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
105
                              (u'a', 'a-id', 'file')], rev_tree)
 
106
        self.assertEqual('contents', rev_tree.get_file_text('a-id'))
 
107
 
 
108
    def test_add_second_file(self):
 
109
        builder = self.build_a_rev()
 
110
        rev_id2 = builder.build_snapshot('B-id', None,
 
111
            [('add', ('b', 'b-id', 'file', 'content_b'))])
 
112
        self.assertEqual('B-id', rev_id2)
 
113
        branch = builder.get_branch()
 
114
        self.assertEqual((2, rev_id2), branch.last_revision_info())
 
115
        rev_tree = branch.repository.revision_tree(rev_id2)
 
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'),
 
120
                              (u'b', 'b-id', 'file')], rev_tree)
 
121
        self.assertEqual('content_b', rev_tree.get_file_text('b-id'))
 
122
 
 
123
    def test_add_empty_dir(self):
 
124
        builder = self.build_a_rev()
 
125
        rev_id2 = builder.build_snapshot('B-id', None,
 
126
            [('add', ('b', 'b-id', 'directory', None))])
 
127
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
 
128
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
129
                              (u'a', 'a-id', 'file'),
 
130
                              (u'b', 'b-id', 'directory'),
 
131
                             ], rev_tree)
 
132
 
 
133
    def test_commit_message_default(self):
 
134
        builder = BranchBuilder(self.get_transport().clone('foo'))
 
135
        rev_id = builder.build_snapshot(None, None,
 
136
            [('add', (u'', None, 'directory', None))])
 
137
        branch = builder.get_branch()
 
138
        rev = branch.repository.get_revision(rev_id)
 
139
        self.assertEqual(u'commit 1', rev.message)
 
140
 
 
141
    def test_commit_message_supplied(self):
 
142
        builder = BranchBuilder(self.get_transport().clone('foo'))
 
143
        rev_id = builder.build_snapshot(None, None,
 
144
            [('add', (u'', None, 'directory', None))],
 
145
            message=u'Foo')
 
146
        branch = builder.get_branch()
 
147
        rev = branch.repository.get_revision(rev_id)
 
148
        self.assertEqual(u'Foo', rev.message)
 
149
 
 
150
    def test_modify_file(self):
 
151
        builder = self.build_a_rev()
 
152
        rev_id2 = builder.build_snapshot('B-id', None,
 
153
            [('modify', ('a-id', 'new\ncontent\n'))])
 
154
        self.assertEqual('B-id', rev_id2)
 
155
        branch = builder.get_branch()
 
156
        rev_tree = branch.repository.revision_tree(rev_id2)
 
157
        rev_tree.lock_read()
 
158
        self.addCleanup(rev_tree.unlock)
 
159
        self.assertEqual('new\ncontent\n', rev_tree.get_file_text('a-id'))
 
160
 
 
161
    def test_delete_file(self):
 
162
        builder = self.build_a_rev()
 
163
        rev_id2 = builder.build_snapshot('B-id', None,
 
164
            [('unversion', 'a-id')])
 
165
        self.assertEqual('B-id', rev_id2)
 
166
        branch = builder.get_branch()
 
167
        rev_tree = branch.repository.revision_tree(rev_id2)
 
168
        rev_tree.lock_read()
 
169
        self.addCleanup(rev_tree.unlock)
 
170
        self.assertTreeShape([(u'', 'a-root-id', 'directory')], rev_tree)
 
171
 
 
172
    def test_delete_directory(self):
 
173
        builder = self.build_a_rev()
 
174
        rev_id2 = builder.build_snapshot('B-id', None,
 
175
            [('add', ('b', 'b-id', 'directory', None)),
 
176
             ('add', ('b/c', 'c-id', 'file', 'foo\n')),
 
177
             ('add', ('b/d', 'd-id', 'directory', None)),
 
178
             ('add', ('b/d/e', 'e-id', 'file', 'eff\n')),
 
179
            ])
 
180
        rev_tree = builder.get_branch().repository.revision_tree('B-id')
 
181
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
182
                              (u'a', 'a-id', 'file'),
 
183
                              (u'b', 'b-id', 'directory'),
 
184
                              (u'b/c', 'c-id', 'file'),
 
185
                              (u'b/d', 'd-id', 'directory'),
 
186
                              (u'b/d/e', 'e-id', 'file')], rev_tree)
 
187
        # Removing a directory removes all child dirs
 
188
        builder.build_snapshot('C-id', None, [('unversion', 'b-id')])
 
189
        rev_tree = builder.get_branch().repository.revision_tree('C-id')
 
190
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
191
                              (u'a', 'a-id', 'file'),
 
192
                             ], rev_tree)
 
193
 
 
194
    def test_unknown_action(self):
 
195
        builder = self.build_a_rev()
 
196
        e = self.assertRaises(ValueError,
 
197
            builder.build_snapshot, 'B-id', None, [('weirdo', ('foo',))])
 
198
        self.assertEqual('Unknown build action: "weirdo"', str(e))
 
199
 
 
200
    # TODO: rename a file/directory, but rename isn't supported by the
 
201
    #       MemoryTree api yet, so for now we wait until it is used
 
202
 
 
203
    def test_set_parent(self):
 
204
        builder = self.build_a_rev()
 
205
        builder.start_series()
 
206
        self.addCleanup(builder.finish_series)
 
207
        builder.build_snapshot('B-id', ['A-id'],
 
208
            [('modify', ('a-id', 'new\ncontent\n'))])
 
209
        builder.build_snapshot('C-id', ['A-id'], 
 
210
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
 
211
        # We should now have a graph:
 
212
        #   A
 
213
        #   |\
 
214
        #   C B
 
215
        # And not A => B => C
 
216
        repo = builder.get_branch().repository
 
217
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',)},
 
218
                         repo.get_parent_map(['B-id', 'C-id']))
 
219
        b_tree = repo.revision_tree('B-id')
 
220
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
221
                              (u'a', 'a-id', 'file'),
 
222
                             ], b_tree)
 
223
        self.assertEqual('new\ncontent\n', b_tree.get_file_text('a-id'))
 
224
 
 
225
        # We should still be using the content from A in C, not from B
 
226
        c_tree = repo.revision_tree('C-id')
 
227
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
228
                              (u'a', 'a-id', 'file'),
 
229
                              (u'c', 'c-id', 'file'),
 
230
                             ], c_tree)
 
231
        self.assertEqual('contents', c_tree.get_file_text('a-id'))
 
232
        self.assertEqual('alt\ncontent\n', c_tree.get_file_text('c-id'))
 
233
 
 
234
    def test_set_merge_parent(self):
 
235
        builder = self.build_a_rev()
 
236
        builder.start_series()
 
237
        self.addCleanup(builder.finish_series)
 
238
        builder.build_snapshot('B-id', ['A-id'],
 
239
            [('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
 
240
        builder.build_snapshot('C-id', ['A-id'],
 
241
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
 
242
        builder.build_snapshot('D-id', ['B-id', 'C-id'], [])
 
243
        repo = builder.get_branch().repository
 
244
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
 
245
                          'D-id': ('B-id', 'C-id')},
 
246
                         repo.get_parent_map(['B-id', 'C-id', 'D-id']))
 
247
        d_tree = repo.revision_tree('D-id')
 
248
        # Note: by default a merge node does *not* pull in the changes from the
 
249
        #       merged tree, you have to supply it yourself.
 
250
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
251
                              (u'a', 'a-id', 'file'),
 
252
                              (u'b', 'b-id', 'file'),
 
253
                             ], d_tree)
 
254
 
 
255
    def test_set_merge_parent_and_contents(self):
 
256
        builder = self.build_a_rev()
 
257
        builder.start_series()
 
258
        self.addCleanup(builder.finish_series)
 
259
        builder.build_snapshot('B-id', ['A-id'],
 
260
            [('add', ('b', 'b-id', 'file', 'b\ncontent\n'))])
 
261
        builder.build_snapshot('C-id', ['A-id'],
 
262
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
 
263
        builder.build_snapshot('D-id', ['B-id', 'C-id'],
 
264
            [('add', ('c', 'c-id', 'file', 'alt\ncontent\n'))])
 
265
        repo = builder.get_branch().repository
 
266
        self.assertEqual({'B-id': ('A-id',), 'C-id': ('A-id',),
 
267
                          'D-id': ('B-id', 'C-id')},
 
268
                         repo.get_parent_map(['B-id', 'C-id', 'D-id']))
 
269
        d_tree = repo.revision_tree('D-id')
 
270
        self.assertTreeShape([(u'', 'a-root-id', 'directory'),
 
271
                              (u'a', 'a-id', 'file'),
 
272
                              (u'b', 'b-id', 'file'),
 
273
                              (u'c', 'c-id', 'file'),
 
274
                             ], d_tree)
 
275
        # Because we copied the exact text into *this* tree, the 'c' file
 
276
        # should look like it was not modified in the merge
 
277
        self.assertEqual('C-id', d_tree.inventory['c-id'].revision)
 
278
 
 
279
    def test_start_finish_series(self):
 
280
        builder = BranchBuilder(self.get_transport().clone('foo'))
 
281
        builder.start_series()
 
282
        try:
 
283
            self.assertIsNot(None, builder._tree)
 
284
            self.assertEqual('w', builder._tree._lock_mode)
 
285
            self.assertTrue(builder._branch.is_locked())
 
286
        finally:
 
287
            builder.finish_series()
 
288
        self.assertIs(None, builder._tree)
 
289
        self.assertFalse(builder._branch.is_locked())