~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_reconfigure.py

  • Committer: Jelmer Vernooij
  • Date: 2011-11-30 20:02:16 UTC
  • mto: This revision was merged to the branch mainline in revision 6333.
  • Revision ID: jelmer@samba.org-20111130200216-aoju21pdl20d1gkd
Consistently pass tree path when exporting.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007, 2008, 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
from bzrlib import (
 
18
    branch as _mod_branch,
 
19
    bzrdir,
 
20
    errors,
 
21
    reconfigure,
 
22
    repository,
 
23
    tests,
 
24
    workingtree,
 
25
    )
 
26
 
 
27
 
 
28
class TestReconfigure(tests.TestCaseWithTransport):
 
29
 
 
30
    def test_tree_to_branch(self):
 
31
        tree = self.make_branch_and_tree('tree')
 
32
        reconfiguration = reconfigure.Reconfigure.to_branch(tree.bzrdir)
 
33
        reconfiguration.apply()
 
34
        self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
 
35
                          'tree')
 
36
 
 
37
    def test_modified_tree_to_branch(self):
 
38
        tree = self.make_branch_and_tree('tree')
 
39
        self.build_tree(['tree/file'])
 
40
        tree.add('file')
 
41
        reconfiguration = reconfigure.Reconfigure.to_branch(tree.bzrdir)
 
42
        self.assertRaises(errors.UncommittedChanges, reconfiguration.apply)
 
43
        reconfiguration.apply(force=True)
 
44
        self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
 
45
                          'tree')
 
46
 
 
47
    def test_tree_with_pending_merge_to_branch(self):
 
48
        tree = self.make_branch_and_tree('tree')
 
49
        tree.commit('unchanged')
 
50
        other_tree = tree.bzrdir.sprout('other').open_workingtree()
 
51
        other_tree.commit('mergeable commit')
 
52
        tree.merge_from_branch(other_tree.branch)
 
53
        reconfiguration = reconfigure.Reconfigure.to_branch(tree.bzrdir)
 
54
        self.assertRaises(errors.UncommittedChanges, reconfiguration.apply)
 
55
        reconfiguration.apply(force=True)
 
56
        self.assertRaises(errors.NoWorkingTree, workingtree.WorkingTree.open,
 
57
                          'tree')
 
58
 
 
59
    def test_branch_to_branch(self):
 
60
        branch = self.make_branch('branch')
 
61
        self.assertRaises(errors.AlreadyBranch,
 
62
                          reconfigure.Reconfigure.to_branch, branch.bzrdir)
 
63
 
 
64
    def test_repo_to_branch(self):
 
65
        repo = self.make_repository('repo')
 
66
        reconfiguration = reconfigure.Reconfigure.to_branch(repo.bzrdir)
 
67
        reconfiguration.apply()
 
68
 
 
69
    def test_checkout_to_branch(self):
 
70
        branch = self.make_branch('branch')
 
71
        checkout = branch.create_checkout('checkout')
 
72
        reconfiguration = reconfigure.Reconfigure.to_branch(checkout.bzrdir)
 
73
        reconfiguration.apply()
 
74
        self.assertIs(None, checkout.branch.get_bound_location())
 
75
 
 
76
    def prepare_lightweight_checkout_to_branch(self):
 
77
        branch = self.make_branch('branch')
 
78
        checkout = branch.create_checkout('checkout', lightweight=True)
 
79
        checkout.commit('first commit', rev_id='rev1')
 
80
        reconfiguration = reconfigure.Reconfigure.to_branch(checkout.bzrdir)
 
81
        return reconfiguration, checkout
 
82
 
 
83
    def test_lightweight_checkout_to_branch(self):
 
84
        reconfiguration, checkout = \
 
85
            self.prepare_lightweight_checkout_to_branch()
 
86
        reconfiguration.apply()
 
87
        checkout_branch = checkout.bzrdir.open_branch()
 
88
        self.assertEqual(checkout_branch.bzrdir.root_transport.base,
 
89
                         checkout.bzrdir.root_transport.base)
 
90
        self.assertEqual('rev1', checkout_branch.last_revision())
 
91
        repo = checkout.bzrdir.open_repository()
 
92
        repo.get_revision('rev1')
 
93
 
 
94
    def test_lightweight_checkout_to_branch_tags(self):
 
95
        reconfiguration, checkout = \
 
96
            self.prepare_lightweight_checkout_to_branch()
 
97
        checkout.branch.tags.set_tag('foo', 'bar')
 
98
        reconfiguration.apply()
 
99
        checkout_branch = checkout.bzrdir.open_branch()
 
100
        self.assertEqual('bar', checkout_branch.tags.lookup_tag('foo'))
 
101
 
 
102
    def prepare_lightweight_checkout_to_checkout(self):
 
103
        branch = self.make_branch('branch')
 
104
        checkout = branch.create_checkout('checkout', lightweight=True)
 
105
        reconfiguration = reconfigure.Reconfigure.to_checkout(checkout.bzrdir)
 
106
        return reconfiguration, checkout
 
107
 
 
108
    def test_lightweight_checkout_to_checkout(self):
 
109
        reconfiguration, checkout = \
 
110
            self.prepare_lightweight_checkout_to_checkout()
 
111
        reconfiguration.apply()
 
112
        checkout_branch = checkout.bzrdir.open_branch()
 
113
        self.assertIsNot(checkout_branch.get_bound_location(), None)
 
114
 
 
115
    def test_lightweight_checkout_to_checkout_tags(self):
 
116
        reconfiguration, checkout = \
 
117
            self.prepare_lightweight_checkout_to_checkout()
 
118
        checkout.branch.tags.set_tag('foo', 'bar')
 
119
        reconfiguration.apply()
 
120
        checkout_branch = checkout.bzrdir.open_branch()
 
121
        self.assertEqual('bar', checkout_branch.tags.lookup_tag('foo'))
 
122
 
 
123
    def test_lightweight_conversion_uses_shared_repo(self):
 
124
        parent = self.make_branch('parent')
 
125
        shared_repo = self.make_repository('repo', shared=True)
 
126
        checkout = parent.create_checkout('repo/checkout', lightweight=True)
 
127
        reconfigure.Reconfigure.to_tree(checkout.bzrdir).apply()
 
128
        checkout_repo = checkout.bzrdir.open_branch().repository
 
129
        self.assertEqual(shared_repo.bzrdir.root_transport.base,
 
130
                         checkout_repo.bzrdir.root_transport.base)
 
131
 
 
132
    def test_branch_to_tree(self):
 
133
        branch = self.make_branch('branch')
 
134
        reconfiguration=reconfigure.Reconfigure.to_tree(branch.bzrdir)
 
135
        reconfiguration.apply()
 
136
        branch.bzrdir.open_workingtree()
 
137
 
 
138
    def test_tree_to_tree(self):
 
139
        tree = self.make_branch_and_tree('tree')
 
140
        self.assertRaises(errors.AlreadyTree, reconfigure.Reconfigure.to_tree,
 
141
                          tree.bzrdir)
 
142
 
 
143
    def test_select_bind_location(self):
 
144
        branch = self.make_branch('branch')
 
145
        reconfiguration = reconfigure.Reconfigure(branch.bzrdir)
 
146
        self.assertRaises(errors.NoBindLocation,
 
147
                          reconfiguration._select_bind_location)
 
148
        branch.set_parent('http://parent')
 
149
        self.assertEqual('http://parent',
 
150
                         reconfiguration._select_bind_location())
 
151
        branch.set_push_location('sftp://push')
 
152
        self.assertEqual('sftp://push',
 
153
                         reconfiguration._select_bind_location())
 
154
        branch.set_bound_location('bzr://foo/old-bound')
 
155
        branch.set_bound_location(None)
 
156
        self.assertEqual('bzr://foo/old-bound',
 
157
                         reconfiguration._select_bind_location())
 
158
        branch.set_bound_location('bzr://foo/cur-bound')
 
159
        self.assertEqual('bzr://foo/cur-bound',
 
160
                         reconfiguration._select_bind_location())
 
161
        reconfiguration.new_bound_location = 'ftp://user-specified'
 
162
        self.assertEqual('ftp://user-specified',
 
163
                         reconfiguration._select_bind_location())
 
164
 
 
165
    def test_select_reference_bind_location(self):
 
166
        branch = self.make_branch('branch')
 
167
        checkout = branch.create_checkout('checkout', lightweight=True)
 
168
        reconfiguration = reconfigure.Reconfigure(checkout.bzrdir)
 
169
        self.assertEqual(branch.base,
 
170
                         reconfiguration._select_bind_location())
 
171
 
 
172
    def test_tree_to_checkout(self):
 
173
        # A tree with no related branches and no supplied bind location cannot
 
174
        # become a checkout
 
175
        parent = self.make_branch('parent')
 
176
 
 
177
        tree = self.make_branch_and_tree('tree')
 
178
        reconfiguration = reconfigure.Reconfigure.to_checkout(tree.bzrdir)
 
179
        self.assertRaises(errors.NoBindLocation, reconfiguration.apply)
 
180
        # setting a parent allows it to become a checkout
 
181
        tree.branch.set_parent(parent.base)
 
182
        reconfiguration.apply()
 
183
        # supplying a location allows it to become a checkout
 
184
        tree2 = self.make_branch_and_tree('tree2')
 
185
        reconfiguration = reconfigure.Reconfigure.to_checkout(tree2.bzrdir,
 
186
                                                              parent.base)
 
187
        reconfiguration.apply()
 
188
 
 
189
    def test_tree_to_lightweight_checkout(self):
 
190
        # A tree with no related branches and no supplied bind location cannot
 
191
        # become a checkout
 
192
        parent = self.make_branch('parent')
 
193
 
 
194
        tree = self.make_branch_and_tree('tree')
 
195
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
196
            tree.bzrdir)
 
197
        self.assertRaises(errors.NoBindLocation, reconfiguration.apply)
 
198
        # setting a parent allows it to become a checkout
 
199
        tree.branch.set_parent(parent.base)
 
200
        reconfiguration.apply()
 
201
        # supplying a location allows it to become a checkout
 
202
        tree2 = self.make_branch_and_tree('tree2')
 
203
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
204
            tree2.bzrdir, parent.base)
 
205
        reconfiguration.apply()
 
206
 
 
207
    def test_checkout_to_checkout(self):
 
208
        parent = self.make_branch('parent')
 
209
        checkout = parent.create_checkout('checkout')
 
210
        self.assertRaises(errors.AlreadyCheckout,
 
211
                          reconfigure.Reconfigure.to_checkout, checkout.bzrdir)
 
212
 
 
213
    def make_unsynced_checkout(self):
 
214
        parent = self.make_branch('parent')
 
215
        checkout = parent.create_checkout('checkout')
 
216
        checkout.commit('test', rev_id='new-commit', local=True)
 
217
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
218
            checkout.bzrdir)
 
219
        return checkout, parent, reconfiguration
 
220
 
 
221
    def test_unsynced_checkout_to_lightweight(self):
 
222
        checkout, parent, reconfiguration = self.make_unsynced_checkout()
 
223
        self.assertRaises(errors.UnsyncedBranches, reconfiguration.apply)
 
224
 
 
225
    def test_synced_checkout_to_lightweight(self):
 
226
        checkout, parent, reconfiguration = self.make_unsynced_checkout()
 
227
        parent.pull(checkout.branch)
 
228
        reconfiguration.apply()
 
229
        wt = checkout.bzrdir.open_workingtree()
 
230
        self.assertTrue(parent.repository.has_same_location(
 
231
            wt.branch.repository))
 
232
        parent.repository.get_revision('new-commit')
 
233
        self.assertRaises(errors.NoRepositoryPresent,
 
234
                          checkout.bzrdir.open_repository)
 
235
 
 
236
    def prepare_branch_to_lightweight_checkout(self):
 
237
        parent = self.make_branch('parent')
 
238
        child = parent.bzrdir.sprout('child').open_workingtree()
 
239
        child.commit('test', rev_id='new-commit')
 
240
        parent.pull(child.branch)
 
241
        child.bzrdir.destroy_workingtree()
 
242
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
243
            child.bzrdir)
 
244
        return parent, child, reconfiguration
 
245
 
 
246
    def test_branch_to_lightweight_checkout(self):
 
247
        parent, child, reconfiguration = \
 
248
            self.prepare_branch_to_lightweight_checkout()
 
249
        reconfiguration.apply()
 
250
        self.assertTrue(reconfiguration._destroy_branch)
 
251
        wt = child.bzrdir.open_workingtree()
 
252
        self.assertTrue(parent.repository.has_same_location(
 
253
            wt.branch.repository))
 
254
        parent.repository.get_revision('new-commit')
 
255
        self.assertRaises(errors.NoRepositoryPresent,
 
256
                          child.bzrdir.open_repository)
 
257
 
 
258
    def test_branch_to_lightweight_checkout_failure(self):
 
259
        parent, child, reconfiguration = \
 
260
            self.prepare_branch_to_lightweight_checkout()
 
261
        old_Repository_fetch = repository.Repository.fetch
 
262
        repository.Repository.fetch = None
 
263
        try:
 
264
            self.assertRaises(TypeError, reconfiguration.apply)
 
265
        finally:
 
266
            repository.Repository.fetch = old_Repository_fetch
 
267
        child = _mod_branch.Branch.open('child')
 
268
        self.assertContainsRe(child.base, 'child/$')
 
269
 
 
270
    def test_branch_to_lightweight_checkout_fetch_tags(self):
 
271
        parent, child, reconfiguration = \
 
272
            self.prepare_branch_to_lightweight_checkout()
 
273
        child.branch.tags.set_tag('foo', 'bar')
 
274
        reconfiguration.apply()
 
275
        child = _mod_branch.Branch.open('child')
 
276
        self.assertEqual('bar', parent.tags.lookup_tag('foo'))
 
277
 
 
278
    def test_lightweight_checkout_to_lightweight_checkout(self):
 
279
        parent = self.make_branch('parent')
 
280
        checkout = parent.create_checkout('checkout', lightweight=True)
 
281
        self.assertRaises(errors.AlreadyLightweightCheckout,
 
282
                          reconfigure.Reconfigure.to_lightweight_checkout,
 
283
                          checkout.bzrdir)
 
284
 
 
285
    def test_repo_to_tree(self):
 
286
        repo = self.make_repository('repo')
 
287
        reconfiguration = reconfigure.Reconfigure.to_tree(repo.bzrdir)
 
288
        reconfiguration.apply()
 
289
        workingtree.WorkingTree.open('repo')
 
290
 
 
291
    def test_shared_repo_to_lightweight_checkout(self):
 
292
        repo = self.make_repository('repo', shared=True)
 
293
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
294
            repo.bzrdir)
 
295
        self.assertRaises(errors.NoBindLocation, reconfiguration.apply)
 
296
        branch = self.make_branch('branch')
 
297
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
298
            repo.bzrdir, 'branch')
 
299
        reconfiguration.apply()
 
300
        workingtree.WorkingTree.open('repo')
 
301
        repository.Repository.open('repo')
 
302
 
 
303
    def test_unshared_repo_to_lightweight_checkout(self):
 
304
        repo = self.make_repository('repo', shared=False)
 
305
        branch = self.make_branch('branch')
 
306
        reconfiguration = reconfigure.Reconfigure.to_lightweight_checkout(
 
307
            repo.bzrdir, 'branch')
 
308
        reconfiguration.apply()
 
309
        workingtree.WorkingTree.open('repo')
 
310
        self.assertRaises(errors.NoRepositoryPresent,
 
311
                          repository.Repository.open, 'repo')
 
312
 
 
313
    def test_standalone_to_use_shared(self):
 
314
        self.build_tree(['root/'])
 
315
        tree = self.make_branch_and_tree('root/tree')
 
316
        tree.commit('Hello', rev_id='hello-id')
 
317
        repo = self.make_repository('root', shared=True)
 
318
        reconfiguration = reconfigure.Reconfigure.to_use_shared(tree.bzrdir)
 
319
        reconfiguration.apply()
 
320
        tree = workingtree.WorkingTree.open('root/tree')
 
321
        self.assertTrue(repo.has_same_location(tree.branch.repository))
 
322
        self.assertEqual('Hello', repo.get_revision('hello-id').message)
 
323
 
 
324
    def add_dead_head(self, tree):
 
325
        revno, revision_id = tree.branch.last_revision_info()
 
326
        tree.commit('Dead head', rev_id='dead-head-id')
 
327
        tree.branch.set_last_revision_info(revno, revision_id)
 
328
        tree.set_last_revision(revision_id)
 
329
 
 
330
    def test_standalone_to_use_shared_preserves_dead_heads(self):
 
331
        self.build_tree(['root/'])
 
332
        tree = self.make_branch_and_tree('root/tree')
 
333
        self.add_dead_head(tree)
 
334
        tree.commit('Hello', rev_id='hello-id')
 
335
        repo = self.make_repository('root', shared=True)
 
336
        reconfiguration = reconfigure.Reconfigure.to_use_shared(tree.bzrdir)
 
337
        reconfiguration.apply()
 
338
        tree = workingtree.WorkingTree.open('root/tree')
 
339
        message = repo.get_revision('dead-head-id').message
 
340
        self.assertEqual('Dead head', message)
 
341
 
 
342
    def make_repository_tree(self):
 
343
        self.build_tree(['root/'])
 
344
        repo = self.make_repository('root', shared=True)
 
345
        tree = self.make_branch_and_tree('root/tree')
 
346
        reconfigure.Reconfigure.to_use_shared(tree.bzrdir).apply()
 
347
        return workingtree.WorkingTree.open('root/tree')
 
348
 
 
349
    def test_use_shared_to_use_shared(self):
 
350
        tree = self.make_repository_tree()
 
351
        self.assertRaises(errors.AlreadyUsingShared,
 
352
                          reconfigure.Reconfigure.to_use_shared, tree.bzrdir)
 
353
 
 
354
    def test_use_shared_to_standalone(self):
 
355
        tree = self.make_repository_tree()
 
356
        tree.commit('Hello', rev_id='hello-id')
 
357
        reconfigure.Reconfigure.to_standalone(tree.bzrdir).apply()
 
358
        tree = workingtree.WorkingTree.open('root/tree')
 
359
        repo = tree.branch.repository
 
360
        self.assertEqual(repo.bzrdir.root_transport.base,
 
361
                         tree.bzrdir.root_transport.base)
 
362
        self.assertEqual('Hello', repo.get_revision('hello-id').message)
 
363
 
 
364
    def test_use_shared_to_standalone_preserves_dead_heads(self):
 
365
        tree = self.make_repository_tree()
 
366
        self.add_dead_head(tree)
 
367
        tree.commit('Hello', rev_id='hello-id')
 
368
        reconfigure.Reconfigure.to_standalone(tree.bzrdir).apply()
 
369
        tree = workingtree.WorkingTree.open('root/tree')
 
370
        repo = tree.branch.repository
 
371
        self.assertRaises(errors.NoSuchRevision, repo.get_revision,
 
372
                          'dead-head-id')
 
373
 
 
374
    def test_standalone_to_standalone(self):
 
375
        tree = self.make_branch_and_tree('tree')
 
376
        self.assertRaises(errors.AlreadyStandalone,
 
377
                          reconfigure.Reconfigure.to_standalone, tree.bzrdir)
 
378
 
 
379
    def make_unsynced_branch_reconfiguration(self):
 
380
        parent = self.make_branch_and_tree('parent')
 
381
        parent.commit('commit 1')
 
382
        child = parent.bzrdir.sprout('child').open_workingtree()
 
383
        child.commit('commit 2')
 
384
        return reconfigure.Reconfigure.to_lightweight_checkout(child.bzrdir)
 
385
 
 
386
    def test_unsynced_branch_to_lightweight_checkout_unforced(self):
 
387
        reconfiguration = self.make_unsynced_branch_reconfiguration()
 
388
        self.assertRaises(errors.UnsyncedBranches, reconfiguration.apply)
 
389
 
 
390
    def test_unsynced_branch_to_lightweight_checkout_forced(self):
 
391
        reconfiguration = self.make_unsynced_branch_reconfiguration()
 
392
        reconfiguration.apply(force=True)
 
393
 
 
394
    def make_repository_with_without_trees(self, with_trees):
 
395
        repo = self.make_repository('repo', shared=True)
 
396
        repo.set_make_working_trees(with_trees)
 
397
        return repo
 
398
 
 
399
    def test_make_with_trees(self):
 
400
        repo = self.make_repository_with_without_trees(False)
 
401
        reconfiguration = reconfigure.Reconfigure.set_repository_trees(
 
402
            repo.bzrdir, True)
 
403
        reconfiguration.apply()
 
404
        self.assertIs(True, repo.make_working_trees())
 
405
 
 
406
    def test_make_without_trees(self):
 
407
        repo = self.make_repository_with_without_trees(True)
 
408
        reconfiguration = reconfigure.Reconfigure.set_repository_trees(
 
409
            repo.bzrdir, False)
 
410
        reconfiguration.apply()
 
411
        self.assertIs(False, repo.make_working_trees())
 
412
 
 
413
    def test_make_with_trees_already_with_trees(self):
 
414
        repo = self.make_repository_with_without_trees(True)
 
415
        e = self.assertRaises(errors.AlreadyWithTrees,
 
416
           reconfigure.Reconfigure.set_repository_trees, repo.bzrdir, True)
 
417
        self.assertContainsRe(str(e),
 
418
            r"Shared repository '.*' already creates working trees.")
 
419
 
 
420
    def test_make_without_trees_already_no_trees(self):
 
421
        repo = self.make_repository_with_without_trees(False)
 
422
        e = self.assertRaises(errors.AlreadyWithNoTrees,
 
423
            reconfigure.Reconfigure.set_repository_trees, repo.bzrdir, False)
 
424
        self.assertContainsRe(str(e),
 
425
            r"Shared repository '.*' already doesn't create working trees.")
 
426
 
 
427
    def test_repository_tree_reconfiguration_not_supported(self):
 
428
        tree = self.make_branch_and_tree('tree')
 
429
        e = self.assertRaises(errors.ReconfigurationNotSupported,
 
430
            reconfigure.Reconfigure.set_repository_trees, tree.bzrdir, None)
 
431
        self.assertContainsRe(str(e),
 
432
            r"Requested reconfiguration of '.*' is not supported.")
 
433
 
 
434
    def test_lightweight_checkout_to_tree_preserves_reference_locations(self):
 
435
        format = bzrdir.format_registry.make_bzrdir('1.9')
 
436
        format.set_branch_format(_mod_branch.BzrBranchFormat8())
 
437
        tree = self.make_branch_and_tree('tree', format=format)
 
438
        tree.branch.set_reference_info('file_id', 'path', '../location')
 
439
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
 
440
        reconfiguration = reconfigure.Reconfigure.to_tree(checkout.bzrdir)
 
441
        reconfiguration.apply()
 
442
        checkout_branch = checkout.bzrdir.open_branch()
 
443
        self.assertEqual(('path', '../location'),
 
444
                         checkout_branch.get_reference_info('file_id'))