1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
62
62
from bzrlib.merge import Merge3Merger, Merger
63
from bzrlib.mutabletree import MutableTree
63
64
from bzrlib.tests import (
69
from bzrlib.tests.features import (
70
73
from bzrlib.transform import (
87
90
super(TestTreeTransform, self).setUp()
88
self.wt = self.make_branch_and_tree('.', format='dirstate-with-subtree')
91
self.wt = self.make_branch_and_tree('.', format='development-subtree')
91
94
def get_transform(self):
321
324
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
322
325
self.assertRaises(ValueError, transform.fixup_new_roots)
327
def test_fixup_new_roots_permits_empty_tree(self):
328
transform, root = self.get_transform()
329
transform.delete_contents(root)
330
transform.unversion_file(root)
331
transform.fixup_new_roots()
332
self.assertIs(None, transform.final_kind(root))
333
self.assertIs(None, transform.final_file_id(root))
324
335
def test_apply_retains_root_directory(self):
325
336
# Do not attempt to delete the physical root directory, because that
333
344
self.assertContainsRe('TransformRenameFailed not raised', str(e))
346
def test_apply_retains_file_id(self):
347
transform, root = self.get_transform()
348
old_root_id = transform.tree_file_id(root)
349
transform.unversion_file(root)
351
self.assertEqual(old_root_id, self.wt.get_root_id())
335
353
def test_hardlink(self):
336
354
self.requireFeature(HardlinkFeature)
337
355
transform, root = self.get_transform()
382
400
self.addCleanup(tree.unlock)
383
401
self.assertEqual('subtree-revision',
384
tree.inventory['subtree-id'].reference_revision)
402
tree.root_inventory['subtree-id'].reference_revision)
386
404
def test_conflicts(self):
387
405
transform, root = self.get_transform()
438
456
newtip = transform2.new_file('tip', oz_id, 'other', 'tip-id')
439
457
result = transform2.find_conflicts()
440
458
fp = FinalPaths(transform2)
441
self.assert_('oz/tip' in transform2._tree_path_ids)
459
self.assertTrue('oz/tip' in transform2._tree_path_ids)
442
460
self.assertEqual(fp.get_path(newtip), pathjoin('oz', 'tip'))
443
461
self.assertEqual(len(result), 2)
444
462
self.assertEqual((result[0][0], result[0][1]),
764
782
'wizard2', 'behind_curtain')
766
784
def test_symlinks_unicode(self):
767
self.requireFeature(tests.UnicodeFilenameFeature)
785
self.requireFeature(features.UnicodeFilenameFeature)
768
786
self._test_symlinks(u'\N{Euro Sign}wizard',
769
787
u'wizard-targ\N{Euro Sign}t',
770
788
u'\N{Euro Sign}wizard2',
1459
1477
# The rename will fail because the target directory is not empty (but
1460
1478
# raises FileExists anyway).
1461
1479
err = self.assertRaises(errors.FileExists, tt_helper)
1462
self.assertContainsRe(str(err),
1463
"^File exists: .+/baz")
1480
self.assertEndsWith(err.path, "/baz")
1465
1482
def test_two_directories_clash(self):
1466
1483
def tt_helper():
1480
1497
err = self.assertRaises(errors.FileExists, tt_helper)
1481
self.assertContainsRe(str(err),
1482
"^File exists: .+/foo")
1498
self.assertEndsWith(err.path, "/foo")
1484
1500
def test_two_directories_clash_finalize(self):
1485
1501
def tt_helper():
1499
1515
err = self.assertRaises(errors.FileExists, tt_helper)
1500
self.assertContainsRe(str(err),
1501
"^File exists: .+/foo")
1516
self.assertEndsWith(err.path, "/foo")
1503
1518
def test_file_to_directory(self):
1504
1519
wt = self.make_branch_and_tree('.')
1540
1554
self.assertPathExists("foo")
1542
1556
self.addCleanup(wt.unlock)
1543
self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1557
self.assertEqual(wt.kind(wt.path2id("foo")), "symlink")
1546
1559
def test_dir_to_file(self):
1547
1560
wt = self.make_branch_and_tree('.')
1559
1572
self.assertPathExists("foo")
1561
1574
self.addCleanup(wt.unlock)
1562
self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1575
self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1565
1577
def test_dir_to_hardlink(self):
1566
1578
self.requireFeature(HardlinkFeature)
1581
1593
self.assertPathExists("baz")
1583
1595
self.addCleanup(wt.unlock)
1584
self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1596
self.assertEqual(wt.kind(wt.path2id("foo")), "file")
1587
1598
def test_no_final_path(self):
1588
1599
transform, root = self.get_transform()
1634
1645
def __init__(self, dirname, root_id):
1635
1646
self.name = dirname
1636
1647
os.mkdir(dirname)
1637
self.wt = BzrDir.create_standalone_workingtree(dirname)
1648
self.wt = ControlDir.create_standalone_workingtree(dirname)
1638
1649
self.wt.set_root_id(root_id)
1639
1650
self.b = self.wt.branch
1640
1651
self.tt = TreeTransform(self.wt)
1750
1761
merge_modified = this.wt.merge_modified()
1751
1762
self.assertSubset(merge_modified, modified)
1752
1763
self.assertEqual(len(merge_modified), len(modified))
1753
file(this.wt.id2abspath('a'), 'wb').write('booga')
1764
with file(this.wt.id2abspath('a'), 'wb') as f: f.write('booga')
1754
1765
modified.pop(0)
1755
1766
merge_modified = this.wt.merge_modified()
1756
1767
self.assertSubset(merge_modified, modified)
1870
1881
def test_build_tree_with_symlinks(self):
1871
1882
self.requireFeature(SymlinkFeature)
1873
a = BzrDir.create_standalone_workingtree('a')
1884
a = ControlDir.create_standalone_workingtree('a')
1874
1885
os.mkdir('a/foo')
1875
file('a/foo/bar', 'wb').write('contents')
1886
with file('a/foo/bar', 'wb') as f: f.write('contents')
1876
1887
os.symlink('a/foo/bar', 'a/foo/baz')
1877
1888
a.add(['foo', 'foo/bar', 'foo/baz'])
1878
1889
a.commit('initial commit')
1879
b = BzrDir.create_standalone_workingtree('b')
1890
b = ControlDir.create_standalone_workingtree('b')
1880
1891
basis = a.basis_tree()
1881
1892
basis.lock_read()
1882
1893
self.addCleanup(basis.unlock)
1888
1899
def test_build_with_references(self):
1889
1900
tree = self.make_branch_and_tree('source',
1890
format='dirstate-with-subtree')
1901
format='development-subtree')
1891
1902
subtree = self.make_branch_and_tree('source/subtree',
1892
format='dirstate-with-subtree')
1903
format='development-subtree')
1893
1904
tree.add_reference(subtree)
1894
1905
tree.commit('a revision')
1895
1906
tree.branch.create_checkout('target')
2173
2184
def rot13(chunks, context=None):
2174
2185
return [''.join(chunks).encode('rot13')]
2175
2186
rot13filter = filters.ContentFilter(rot13, rot13)
2176
filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
2187
filters.filter_stacks_registry.register(
2188
'rot13', {'yes': [rot13filter]}.get)
2177
2189
os.mkdir(self.test_home_dir + '/.bazaar')
2178
2190
rules_filename = self.test_home_dir + '/.bazaar/rules'
2179
2191
f = open(rules_filename, 'wb')
2209
2221
self.assertEqualStat(source_stat, target_stat)
2211
2223
def test_case_insensitive_build_tree_inventory(self):
2212
if (tests.CaseInsensitiveFilesystemFeature.available()
2213
or tests.CaseInsCasePresFilenameFeature.available()):
2224
if (features.CaseInsensitiveFilesystemFeature.available()
2225
or features.CaseInsCasePresFilenameFeature.available()):
2214
2226
raise tests.UnavailableFeature('Fully case sensitive filesystem')
2215
2227
source = self.make_branch_and_tree('source')
2216
2228
self.build_tree(['source/file', 'source/FILE'])
2399
2411
self.assertEqual('tree', revision.properties['branch-nick'])
2402
class TestBackupName(tests.TestCase):
2404
def test_deprecations(self):
2405
class MockTransform(object):
2407
def has_named_child(self, by_parent, parent_id, name):
2408
return name in by_parent.get(parent_id, [])
2410
class MockEntry(object):
2413
object.__init__(self)
2416
tt = MockTransform()
2417
name1 = self.applyDeprecated(
2418
symbol_versioning.deprecated_in((2, 3, 0)),
2419
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2420
self.assertEqual('name.~1~', name1)
2421
name2 = self.applyDeprecated(
2422
symbol_versioning.deprecated_in((2, 3, 0)),
2423
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2424
self.assertEqual('name.~2~', name2)
2427
2414
class TestFileMover(tests.TestCaseWithTransport):
2429
2416
def test_file_mover(self):
2782
2769
def test_iter_changes(self):
2783
2770
revision_tree, preview_tree = self.get_tree_and_preview_tree()
2784
root = revision_tree.inventory.root.file_id
2771
root = revision_tree.get_root_id()
2785
2772
self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
2786
2773
(root, root), ('a', 'a'), ('file', 'file'),
2787
2774
(False, False))],
2791
2778
revision_tree, preview_tree = self.get_tree_and_preview_tree()
2792
2779
changes = preview_tree.iter_changes(revision_tree,
2793
2780
include_unchanged=True)
2794
root = revision_tree.inventory.root.file_id
2781
root = revision_tree.get_root_id()
2796
2783
self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
3329
3316
self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
3331
3318
def test_ascii_limbo_paths(self):
3332
self.requireFeature(tests.UnicodeFilenameFeature)
3319
self.requireFeature(features.UnicodeFilenameFeature)
3333
3320
branch = self.make_branch('any')
3334
3321
tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
3335
3322
tt = TransformPreview(tree)
3353
3340
class TestSerializeTransform(tests.TestCaseWithTransport):
3355
_test_needs_features = [tests.UnicodeFilenameFeature]
3342
_test_needs_features = [features.UnicodeFilenameFeature]
3357
3344
def get_preview(self, tree=None):
3358
3345
if tree is None:
3433
3420
return self.make_records(attribs, contents)
3435
3422
def test_serialize_symlink_creation(self):
3436
self.requireFeature(tests.SymlinkFeature)
3423
self.requireFeature(features.SymlinkFeature)
3437
3424
tt = self.get_preview()
3438
3425
tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3439
3426
self.assertSerializesTo(self.symlink_creation_records(), tt)
3441
3428
def test_deserialize_symlink_creation(self):
3442
self.requireFeature(tests.SymlinkFeature)
3429
self.requireFeature(features.SymlinkFeature)
3443
3430
tt = self.get_preview()
3444
3431
tt.deserialize(iter(self.symlink_creation_records()))
3445
3432
abspath = tt._limbo_name('new-1')
3623
3610
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3625
3612
def _set_orphan_policy(self, wt, policy):
3626
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3613
wt.branch.get_config_stack().set('bzr.transform.orphan_policy',
3629
3616
def _prepare_orphan(self, wt):
3655
3642
warnings.append(args[0] % args[1:])
3656
3643
self.overrideAttr(trace, 'warning', warning)
3657
3644
remaining_conflicts = resolve_conflicts(tt)
3658
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3645
self.assertEqual(['dir/foo has been orphaned in bzr-orphans'],
3660
3647
# Yeah for resolved conflicts !
3661
3648
self.assertLength(0, remaining_conflicts)
3662
3649
# We have a new orphan
3663
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3664
self.assertEquals('bzr-orphans',
3650
self.assertEqual('foo.~1~', tt.final_name(orphan_tid))
3651
self.assertEqual('bzr-orphans',
3665
3652
tt.final_name(tt.final_parent(orphan_tid)))
3667
3654
def test_never_orphan(self):
3702
3689
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3703
3690
remaining_conflicts.pop())
3704
3691
self.assertLength(1, warnings)
3705
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')
3692
self.assertStartsWith(warnings[0], 'Value "donttouchmypreciouuus" ')
3695
class TestTransformHooks(tests.TestCaseWithTransport):
3698
super(TestTransformHooks, self).setUp()
3699
self.wt = self.make_branch_and_tree('.')
3702
def get_transform(self):
3703
transform = TreeTransform(self.wt)
3704
self.addCleanup(transform.finalize)
3705
return transform, transform.root
3707
def test_pre_commit_hooks(self):
3709
def record_pre_transform(tree, tt):
3710
calls.append((tree, tt))
3711
MutableTree.hooks.install_named_hook('pre_transform',
3712
record_pre_transform, "Pre transform")
3713
transform, root = self.get_transform()
3714
old_root_id = transform.tree_file_id(root)
3716
self.assertEqual(old_root_id, self.wt.get_root_id())
3717
self.assertEqual([(self.wt, transform)], calls)
3719
def test_post_commit_hooks(self):
3721
def record_post_transform(tree, tt):
3722
calls.append((tree, tt))
3723
MutableTree.hooks.install_named_hook('post_transform',
3724
record_post_transform, "Post transform")
3725
transform, root = self.get_transform()
3726
old_root_id = transform.tree_file_id(root)
3728
self.assertEqual(old_root_id, self.wt.get_root_id())
3729
self.assertEqual([(self.wt, transform)], calls)