~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-05-09 09:54:24 UTC
  • mfrom: (5840.1.1 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20110509095424-go6263smv6wzc5u1
(vila) Use a proper version for the weave_format plugin (Vincent
 Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import errno
17
18
import os
18
19
from StringIO import StringIO
19
20
import sys
27
28
    osutils,
28
29
    revision as _mod_revision,
29
30
    rules,
 
31
    symbol_versioning,
30
32
    tests,
 
33
    trace,
 
34
    transform,
31
35
    urlutils,
32
36
    )
33
37
from bzrlib.bzrdir import BzrDir
49
53
    ImmortalPendingDeletion,
50
54
    LockError,
51
55
    MalformedTransform,
52
 
    NoSuchFile,
53
56
    ReusingTransform,
54
57
)
55
58
from bzrlib.osutils import (
58
61
)
59
62
from bzrlib.merge import Merge3Merger, Merger
60
63
from bzrlib.tests import (
 
64
    features,
61
65
    HardlinkFeature,
62
66
    SymlinkFeature,
63
 
    TestCase,
64
67
    TestCaseInTempDir,
65
68
    TestSkipped,
66
69
)
70
73
    cook_conflicts,
71
74
    _FileMover,
72
75
    FinalPaths,
73
 
    get_backup_name,
74
76
    resolve_conflicts,
75
77
    resolve_checkout,
76
78
    ROOT_PARENT,
91
93
        self.addCleanup(transform.finalize)
92
94
        return transform, transform.root
93
95
 
 
96
    def get_transform_for_sha1_test(self):
 
97
        trans, root = self.get_transform()
 
98
        self.wt.lock_tree_write()
 
99
        self.addCleanup(self.wt.unlock)
 
100
        contents = ['just some content\n']
 
101
        sha1 = osutils.sha_strings(contents)
 
102
        # Roll back the clock
 
103
        trans._creation_mtime = time.time() - 20.0
 
104
        return trans, root, contents, sha1
 
105
 
94
106
    def test_existing_limbo(self):
95
107
        transform, root = self.get_transform()
96
108
        limbo_name = transform._limbodir
159
171
        transform.finalize()
160
172
        transform.finalize()
161
173
 
 
174
    def test_apply_informs_tree_of_observed_sha1(self):
 
175
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
 
176
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
 
177
                                  sha1=sha1)
 
178
        calls = []
 
179
        orig = self.wt._observed_sha1
 
180
        def _observed_sha1(*args):
 
181
            calls.append(args)
 
182
            orig(*args)
 
183
        self.wt._observed_sha1 = _observed_sha1
 
184
        trans.apply()
 
185
        self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
 
186
                         calls)
 
187
 
 
188
    def test_create_file_caches_sha1(self):
 
189
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
 
190
        trans_id = trans.create_path('file1', root)
 
191
        trans.create_file(contents, trans_id, sha1=sha1)
 
192
        st_val = osutils.lstat(trans._limbo_name(trans_id))
 
193
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
 
194
        self.assertEqual(o_sha1, sha1)
 
195
        self.assertEqualStat(o_st_val, st_val)
 
196
 
 
197
    def test__apply_insertions_updates_sha1(self):
 
198
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
 
199
        trans_id = trans.create_path('file1', root)
 
200
        trans.create_file(contents, trans_id, sha1=sha1)
 
201
        st_val = osutils.lstat(trans._limbo_name(trans_id))
 
202
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
 
203
        self.assertEqual(o_sha1, sha1)
 
204
        self.assertEqualStat(o_st_val, st_val)
 
205
        creation_mtime = trans._creation_mtime + 10.0
 
206
        # We fake a time difference from when the file was created until now it
 
207
        # is being renamed by using os.utime. Note that the change we actually
 
208
        # want to see is the real ctime change from 'os.rename()', but as long
 
209
        # as we observe a new stat value, we should be fine.
 
210
        os.utime(trans._limbo_name(trans_id), (creation_mtime, creation_mtime))
 
211
        trans.apply()
 
212
        new_st_val = osutils.lstat(self.wt.abspath('file1'))
 
213
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
 
214
        self.assertEqual(o_sha1, sha1)
 
215
        self.assertEqualStat(o_st_val, new_st_val)
 
216
        self.assertNotEqual(st_val.st_mtime, new_st_val.st_mtime)
 
217
 
 
218
    def test_new_file_caches_sha1(self):
 
219
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
 
220
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
 
221
                                  sha1=sha1)
 
222
        st_val = osutils.lstat(trans._limbo_name(trans_id))
 
223
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
 
224
        self.assertEqual(o_sha1, sha1)
 
225
        self.assertEqualStat(o_st_val, st_val)
 
226
 
 
227
    def test_cancel_creation_removes_observed_sha1(self):
 
228
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
 
229
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
 
230
                                  sha1=sha1)
 
231
        self.assertTrue(trans_id in trans._observed_sha1s)
 
232
        trans.cancel_creation(trans_id)
 
233
        self.assertFalse(trans_id in trans._observed_sha1s)
 
234
 
162
235
    def test_create_files_same_timestamp(self):
163
236
        transform, root = self.get_transform()
164
237
        self.wt.lock_tree_write()
222
295
        trans_id = target_transform.create_path('file1', target_transform.root)
223
296
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
224
297
        target_transform.apply()
225
 
        self.failUnlessExists('target/file1')
 
298
        self.assertPathExists('target/file1')
226
299
        source_stat = os.stat(self.wt.abspath('file1'))
227
300
        target_stat = os.stat('target/file1')
228
301
        self.assertEqual(source_stat, target_stat)
394
467
        transform.new_file('FiLe', transform.root, 'content')
395
468
        resolve_conflicts(transform)
396
469
        transform.apply()
397
 
        self.failUnlessExists('tree/file')
398
 
        self.failUnlessExists('tree/FiLe.moved')
 
470
        self.assertPathExists('tree/file')
 
471
        self.assertPathExists('tree/FiLe.moved')
399
472
 
400
473
    def test_resolve_checkout_case_conflict(self):
401
474
        tree = self.make_branch_and_tree('tree')
410
483
        resolve_conflicts(transform,
411
484
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
412
485
        transform.apply()
413
 
        self.failUnlessExists('tree/file')
414
 
        self.failUnlessExists('tree/FiLe.moved')
 
486
        self.assertPathExists('tree/file')
 
487
        self.assertPathExists('tree/FiLe.moved')
415
488
 
416
489
    def test_apply_case_conflict(self):
417
490
        """Ensure that a transform with case conflicts can always be applied"""
425
498
        transform.new_file('dirFiLe', dir, 'content')
426
499
        resolve_conflicts(transform)
427
500
        transform.apply()
428
 
        self.failUnlessExists('tree/file')
 
501
        self.assertPathExists('tree/file')
429
502
        if not os.path.exists('tree/FiLe.moved'):
430
 
            self.failUnlessExists('tree/FiLe')
431
 
        self.failUnlessExists('tree/dir/dirfile')
 
503
            self.assertPathExists('tree/FiLe')
 
504
        self.assertPathExists('tree/dir/dirfile')
432
505
        if not os.path.exists('tree/dir/dirFiLe.moved'):
433
 
            self.failUnlessExists('tree/dir/dirFiLe')
 
506
            self.assertPathExists('tree/dir/dirFiLe')
434
507
 
435
508
    def test_case_insensitive_limbo(self):
436
509
        tree = self.make_branch_and_tree('tree')
814
887
        self.assertIs(None, self.wt.path2id('parent'))
815
888
        self.assertIs(None, self.wt.path2id('parent.new'))
816
889
 
 
890
    def test_resolve_conflicts_missing_parent(self):
 
891
        wt = self.make_branch_and_tree('.')
 
892
        tt = TreeTransform(wt)
 
893
        self.addCleanup(tt.finalize)
 
894
        parent = tt.trans_id_file_id('parent-id')
 
895
        tt.new_file('file', parent, 'Contents')
 
896
        raw_conflicts = resolve_conflicts(tt)
 
897
        # Since the directory doesn't exist it's seen as 'missing'.  So
 
898
        # 'resolve_conflicts' create a conflict asking for it to be created.
 
899
        self.assertLength(1, raw_conflicts)
 
900
        self.assertEqual(('missing parent', 'Created directory', 'new-1'),
 
901
                         raw_conflicts.pop())
 
902
        # apply fail since the missing directory doesn't exist
 
903
        self.assertRaises(errors.NoFinalPath, tt.apply)
 
904
 
817
905
    def test_moving_versioned_directories(self):
818
906
        create, root = self.get_transform()
819
907
        kansas = create.new_directory('kansas', root, 'kansas-id')
853
941
        rename.apply()
854
942
 
855
943
    def test_rename_fails(self):
 
944
        self.requireFeature(features.not_running_as_root)
856
945
        # see https://bugs.launchpad.net/bzr/+bug/491763
857
946
        create, root_id = self.get_transform()
858
947
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
880
969
        # On windows looks like:
881
970
        # "Failed to rename .../work/myfile to 
882
971
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
883
 
        # The strerror will vary per OS and language so it's not checked here
884
 
        self.assertContainsRe(str(e),
885
 
            "Failed to rename .*(first-dir.newname:|myfile)")
 
972
        # This test isn't concerned with exactly what the error looks like,
 
973
        # and the strerror will vary across OS and locales, but the assert
 
974
        # that the exeception attributes are what we expect
 
975
        self.assertEqual(e.errno, errno.EACCES)
 
976
        if os.name == "posix":
 
977
            self.assertEndsWith(e.to_path, "/first-dir/newname")
 
978
        else:
 
979
            self.assertEqual(os.path.basename(e.from_path), "myfile")
886
980
 
887
981
    def test_set_executability_order(self):
888
982
        """Ensure that executability behaves the same, no matter what order.
1161
1255
        parent2 = transform.new_directory('parent2', root)
1162
1256
        transform.adjust_path('child1', parent2, child1)
1163
1257
        transform.apply()
1164
 
        self.failIfExists(self.wt.abspath('parent1/child1'))
1165
 
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1258
        self.assertPathDoesNotExist(self.wt.abspath('parent1/child1'))
 
1259
        self.assertPathExists(self.wt.abspath('parent2/child1'))
1166
1260
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
1167
1261
        # no rename for child1 (counting only renames during apply)
1168
 
        self.failUnlessEqual(2, transform.rename_count)
 
1262
        self.assertEqual(2, transform.rename_count)
1169
1263
 
1170
1264
    def test_cancel_parent(self):
1171
1265
        """Cancelling a parent doesn't cause deletion of a non-empty directory
1194
1288
        parent2 = transform.new_directory('parent2', root)
1195
1289
        transform.adjust_path('child1', parent2, child1)
1196
1290
        transform.apply()
1197
 
        self.failIfExists(self.wt.abspath('parent1'))
1198
 
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
 
1291
        self.assertPathDoesNotExist(self.wt.abspath('parent1'))
 
1292
        self.assertPathExists(self.wt.abspath('parent2/child1'))
1199
1293
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
1200
 
        self.failUnlessEqual(2, transform.rename_count)
 
1294
        self.assertEqual(2, transform.rename_count)
1201
1295
 
1202
1296
    def test_adjust_and_cancel(self):
1203
1297
        """Make sure adjust_path keeps track of limbo children properly"""
1236
1330
        child = transform.new_directory('child', parent)
1237
1331
        transform.adjust_path('parent', root, parent)
1238
1332
        transform.apply()
1239
 
        self.failUnlessExists(self.wt.abspath('parent/child'))
 
1333
        self.assertPathExists(self.wt.abspath('parent/child'))
1240
1334
        self.assertEqual(1, transform.rename_count)
1241
1335
 
1242
1336
    def test_reuse_name(self):
1373
1467
        tt.create_file(["aa\n"], bar_trans_id)
1374
1468
        tt.version_file("bar-1", bar_trans_id)
1375
1469
        tt.apply()
1376
 
        self.failUnlessExists("foo/bar")
 
1470
        self.assertPathExists("foo/bar")
1377
1471
        wt.lock_read()
1378
1472
        try:
1379
1473
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1396
1490
        tt.delete_contents(foo_trans_id)
1397
1491
        tt.create_symlink("bar", foo_trans_id)
1398
1492
        tt.apply()
1399
 
        self.failUnlessExists("foo")
 
1493
        self.assertPathExists("foo")
1400
1494
        wt.lock_read()
1401
1495
        self.addCleanup(wt.unlock)
1402
1496
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1415
1509
        tt.delete_versioned(bar_trans_id)
1416
1510
        tt.create_file(["aa\n"], foo_trans_id)
1417
1511
        tt.apply()
1418
 
        self.failUnlessExists("foo")
 
1512
        self.assertPathExists("foo")
1419
1513
        wt.lock_read()
1420
1514
        self.addCleanup(wt.unlock)
1421
1515
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1436
1530
        self.build_tree(['baz'])
1437
1531
        tt.create_hardlink("baz", foo_trans_id)
1438
1532
        tt.apply()
1439
 
        self.failUnlessExists("foo")
1440
 
        self.failUnlessExists("baz")
 
1533
        self.assertPathExists("foo")
 
1534
        self.assertPathExists("baz")
1441
1535
        wt.lock_read()
1442
1536
        self.addCleanup(wt.unlock)
1443
1537
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1720
1814
        tree.add_reference(subtree)
1721
1815
        tree.commit('a revision')
1722
1816
        tree.branch.create_checkout('target')
1723
 
        self.failUnlessExists('target')
1724
 
        self.failUnlessExists('target/subtree')
 
1817
        self.assertPathExists('target')
 
1818
        self.assertPathExists('target/subtree')
1725
1819
 
1726
1820
    def test_file_conflict_handling(self):
1727
1821
        """Ensure that when building trees, conflict handling is done"""
1774
1868
        source.commit('added file')
1775
1869
        build_tree(source.basis_tree(), target)
1776
1870
        self.assertEqual([], target.conflicts())
1777
 
        self.failUnlessExists('target/dir1/file')
 
1871
        self.assertPathExists('target/dir1/file')
1778
1872
 
1779
1873
        # Ensure contents are merged
1780
1874
        target = self.make_branch_and_tree('target2')
1781
1875
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1782
1876
        build_tree(source.basis_tree(), target)
1783
1877
        self.assertEqual([], target.conflicts())
1784
 
        self.failUnlessExists('target2/dir1/file2')
1785
 
        self.failUnlessExists('target2/dir1/file')
 
1878
        self.assertPathExists('target2/dir1/file2')
 
1879
        self.assertPathExists('target2/dir1/file')
1786
1880
 
1787
1881
        # Ensure new contents are suppressed for existing branches
1788
1882
        target = self.make_branch_and_tree('target3')
1789
1883
        self.make_branch('target3/dir1')
1790
1884
        self.build_tree(['target3/dir1/file2'])
1791
1885
        build_tree(source.basis_tree(), target)
1792
 
        self.failIfExists('target3/dir1/file')
1793
 
        self.failUnlessExists('target3/dir1/file2')
1794
 
        self.failUnlessExists('target3/dir1.diverted/file')
 
1886
        self.assertPathDoesNotExist('target3/dir1/file')
 
1887
        self.assertPathExists('target3/dir1/file2')
 
1888
        self.assertPathExists('target3/dir1.diverted/file')
1795
1889
        self.assertEqual([DuplicateEntry('Diverted to',
1796
1890
            'dir1.diverted', 'dir1', 'new-dir1', None)],
1797
1891
            target.conflicts())
1800
1894
        self.build_tree(['target4/dir1/'])
1801
1895
        self.make_branch('target4/dir1/file')
1802
1896
        build_tree(source.basis_tree(), target)
1803
 
        self.failUnlessExists('target4/dir1/file')
 
1897
        self.assertPathExists('target4/dir1/file')
1804
1898
        self.assertEqual('directory', file_kind('target4/dir1/file'))
1805
 
        self.failUnlessExists('target4/dir1/file.diverted')
 
1899
        self.assertPathExists('target4/dir1/file.diverted')
1806
1900
        self.assertEqual([DuplicateEntry('Diverted to',
1807
1901
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
1808
1902
            target.conflicts())
1876
1970
        self.addCleanup(target.unlock)
1877
1971
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1878
1972
 
 
1973
    def test_build_tree_accelerator_tree_observes_sha1(self):
 
1974
        source = self.create_ab_tree()
 
1975
        sha1 = osutils.sha_string('A')
 
1976
        target = self.make_branch_and_tree('target')
 
1977
        target.lock_write()
 
1978
        self.addCleanup(target.unlock)
 
1979
        state = target.current_dirstate()
 
1980
        state._cutoff_time = time.time() + 60
 
1981
        build_tree(source.basis_tree(), target, source)
 
1982
        entry = state._get_entry(0, path_utf8='file1')
 
1983
        self.assertEqual(sha1, entry[1][0][1])
 
1984
 
1879
1985
    def test_build_tree_accelerator_tree_missing_file(self):
1880
1986
        source = self.create_ab_tree()
1881
1987
        os.unlink('source/file1')
2039
2145
        self.assertEqual('file.moved', target.id2path('lower-id'))
2040
2146
        self.assertEqual('FILE', target.id2path('upper-id'))
2041
2147
 
 
2148
    def test_build_tree_observes_sha(self):
 
2149
        source = self.make_branch_and_tree('source')
 
2150
        self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
 
2151
        source.add(['file1', 'dir', 'dir/file2'],
 
2152
                   ['file1-id', 'dir-id', 'file2-id'])
 
2153
        source.commit('new files')
 
2154
        target = self.make_branch_and_tree('target')
 
2155
        target.lock_write()
 
2156
        self.addCleanup(target.unlock)
 
2157
        # We make use of the fact that DirState caches its cutoff time. So we
 
2158
        # set the 'safe' time to one minute in the future.
 
2159
        state = target.current_dirstate()
 
2160
        state._cutoff_time = time.time() + 60
 
2161
        build_tree(source.basis_tree(), target)
 
2162
        entry1_sha = osutils.sha_file_by_name('source/file1')
 
2163
        entry2_sha = osutils.sha_file_by_name('source/dir/file2')
 
2164
        # entry[1] is the state information, entry[1][0] is the state of the
 
2165
        # working tree, entry[1][0][1] is the sha value for the current working
 
2166
        # tree
 
2167
        entry1 = state._get_entry(0, path_utf8='file1')
 
2168
        self.assertEqual(entry1_sha, entry1[1][0][1])
 
2169
        # The 'size' field must also be set.
 
2170
        self.assertEqual(25, entry1[1][0][2])
 
2171
        entry1_state = entry1[1][0]
 
2172
        entry2 = state._get_entry(0, path_utf8='dir/file2')
 
2173
        self.assertEqual(entry2_sha, entry2[1][0][1])
 
2174
        self.assertEqual(29, entry2[1][0][2])
 
2175
        entry2_state = entry2[1][0]
 
2176
        # Now, make sure that we don't have to re-read the content. The
 
2177
        # packed_stat should match exactly.
 
2178
        self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
 
2179
        self.assertEqual(entry2_sha,
 
2180
                         target.get_file_sha1('file2-id', 'dir/file2'))
 
2181
        self.assertEqual(entry1_state, entry1[1][0])
 
2182
        self.assertEqual(entry2_state, entry2[1][0])
 
2183
 
2042
2184
 
2043
2185
class TestCommitTransform(tests.TestCaseWithTransport):
2044
2186
 
2178
2320
        self.assertEqual('tree', revision.properties['branch-nick'])
2179
2321
 
2180
2322
 
2181
 
class MockTransform(object):
2182
 
 
2183
 
    def has_named_child(self, by_parent, parent_id, name):
2184
 
        for child_id in by_parent[parent_id]:
2185
 
            if child_id == '0':
2186
 
                if name == "name~":
2187
 
                    return True
2188
 
            elif name == "name.~%s~" % child_id:
2189
 
                return True
2190
 
        return False
2191
 
 
2192
 
 
2193
 
class MockEntry(object):
2194
 
    def __init__(self):
2195
 
        object.__init__(self)
2196
 
        self.name = "name"
2197
 
 
2198
 
 
2199
 
class TestGetBackupName(TestCase):
2200
 
    def test_get_backup_name(self):
 
2323
class TestBackupName(tests.TestCase):
 
2324
 
 
2325
    def test_deprecations(self):
 
2326
        class MockTransform(object):
 
2327
 
 
2328
            def has_named_child(self, by_parent, parent_id, name):
 
2329
                return name in by_parent.get(parent_id, [])
 
2330
 
 
2331
        class MockEntry(object):
 
2332
 
 
2333
            def __init__(self):
 
2334
                object.__init__(self)
 
2335
                self.name = "name"
 
2336
 
2201
2337
        tt = MockTransform()
2202
 
        name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
2203
 
        self.assertEqual(name, 'name.~1~')
2204
 
        name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2205
 
        self.assertEqual(name, 'name.~2~')
2206
 
        name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2207
 
        self.assertEqual(name, 'name.~1~')
2208
 
        name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2209
 
        self.assertEqual(name, 'name.~1~')
2210
 
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2211
 
        self.assertEqual(name, 'name.~4~')
 
2338
        name1 = self.applyDeprecated(
 
2339
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2340
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
 
2341
        self.assertEqual('name.~1~', name1)
 
2342
        name2 = self.applyDeprecated(
 
2343
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2344
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
 
2345
        self.assertEqual('name.~2~', name2)
2212
2346
 
2213
2347
 
2214
2348
class TestFileMover(tests.TestCaseWithTransport):
2217
2351
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2218
2352
        mover = _FileMover()
2219
2353
        mover.rename('a', 'q')
2220
 
        self.failUnlessExists('q')
2221
 
        self.failIfExists('a')
2222
 
        self.failUnlessExists('q/b')
2223
 
        self.failUnlessExists('c')
2224
 
        self.failUnlessExists('c/d')
 
2354
        self.assertPathExists('q')
 
2355
        self.assertPathDoesNotExist('a')
 
2356
        self.assertPathExists('q/b')
 
2357
        self.assertPathExists('c')
 
2358
        self.assertPathExists('c/d')
2225
2359
 
2226
2360
    def test_pre_delete_rollback(self):
2227
2361
        self.build_tree(['a/'])
2228
2362
        mover = _FileMover()
2229
2363
        mover.pre_delete('a', 'q')
2230
 
        self.failUnlessExists('q')
2231
 
        self.failIfExists('a')
 
2364
        self.assertPathExists('q')
 
2365
        self.assertPathDoesNotExist('a')
2232
2366
        mover.rollback()
2233
 
        self.failIfExists('q')
2234
 
        self.failUnlessExists('a')
 
2367
        self.assertPathDoesNotExist('q')
 
2368
        self.assertPathExists('a')
2235
2369
 
2236
2370
    def test_apply_deletions(self):
2237
2371
        self.build_tree(['a/', 'b/'])
2238
2372
        mover = _FileMover()
2239
2373
        mover.pre_delete('a', 'q')
2240
2374
        mover.pre_delete('b', 'r')
2241
 
        self.failUnlessExists('q')
2242
 
        self.failUnlessExists('r')
2243
 
        self.failIfExists('a')
2244
 
        self.failIfExists('b')
 
2375
        self.assertPathExists('q')
 
2376
        self.assertPathExists('r')
 
2377
        self.assertPathDoesNotExist('a')
 
2378
        self.assertPathDoesNotExist('b')
2245
2379
        mover.apply_deletions()
2246
 
        self.failIfExists('q')
2247
 
        self.failIfExists('r')
2248
 
        self.failIfExists('a')
2249
 
        self.failIfExists('b')
 
2380
        self.assertPathDoesNotExist('q')
 
2381
        self.assertPathDoesNotExist('r')
 
2382
        self.assertPathDoesNotExist('a')
 
2383
        self.assertPathDoesNotExist('b')
2250
2384
 
2251
2385
    def test_file_mover_rollback(self):
2252
2386
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2257
2391
            mover.rename('a', 'c')
2258
2392
        except errors.FileExists, e:
2259
2393
            mover.rollback()
2260
 
        self.failUnlessExists('a')
2261
 
        self.failUnlessExists('c/d')
 
2394
        self.assertPathExists('a')
 
2395
        self.assertPathExists('c/d')
2262
2396
 
2263
2397
 
2264
2398
class Bogus(Exception):
2294
2428
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2295
2429
        self.assertRaises(Bogus, tt.apply,
2296
2430
                          _mover=self.ExceptionFileMover(bad_source='a'))
2297
 
        self.failUnlessExists('a')
2298
 
        self.failUnlessExists('a/b')
 
2431
        self.assertPathExists('a')
 
2432
        self.assertPathExists('a/b')
2299
2433
        tt.apply()
2300
 
        self.failUnlessExists('c')
2301
 
        self.failUnlessExists('c/d')
 
2434
        self.assertPathExists('c')
 
2435
        self.assertPathExists('c/d')
2302
2436
 
2303
2437
    def test_rollback_rename_into_place(self):
2304
2438
        tree = self.make_branch_and_tree('.')
2310
2444
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2311
2445
        self.assertRaises(Bogus, tt.apply,
2312
2446
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
2313
 
        self.failUnlessExists('a')
2314
 
        self.failUnlessExists('a/b')
 
2447
        self.assertPathExists('a')
 
2448
        self.assertPathExists('a/b')
2315
2449
        tt.apply()
2316
 
        self.failUnlessExists('c')
2317
 
        self.failUnlessExists('c/d')
 
2450
        self.assertPathExists('c')
 
2451
        self.assertPathExists('c/d')
2318
2452
 
2319
2453
    def test_rollback_deletion(self):
2320
2454
        tree = self.make_branch_and_tree('.')
2326
2460
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2327
2461
        self.assertRaises(Bogus, tt.apply,
2328
2462
                          _mover=self.ExceptionFileMover(bad_target='d'))
2329
 
        self.failUnlessExists('a')
2330
 
        self.failUnlessExists('a/b')
2331
 
 
2332
 
    def test_resolve_no_parent(self):
 
2463
        self.assertPathExists('a')
 
2464
        self.assertPathExists('a/b')
 
2465
 
 
2466
 
 
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
 
2468
 
 
2469
    def make_tt_with_versioned_dir(self):
2333
2470
        wt = self.make_branch_and_tree('.')
 
2471
        self.build_tree(['dir/',])
 
2472
        wt.add(['dir'], ['dir-id'])
 
2473
        wt.commit('Create dir')
2334
2474
        tt = TreeTransform(wt)
2335
2475
        self.addCleanup(tt.finalize)
2336
 
        parent = tt.trans_id_file_id('parent-id')
2337
 
        tt.new_file('file', parent, 'Contents')
2338
 
        resolve_conflicts(tt)
 
2476
        return wt, tt
 
2477
 
 
2478
    def test_resolve_create_parent_for_versioned_file(self):
 
2479
        wt, tt = self.make_tt_with_versioned_dir()
 
2480
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2481
        file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
 
2482
        tt.delete_contents(dir_tid)
 
2483
        tt.unversion_file(dir_tid)
 
2484
        conflicts = resolve_conflicts(tt)
 
2485
        # one conflict for the missing directory, one for the unversioned
 
2486
        # parent
 
2487
        self.assertLength(2, conflicts)
 
2488
 
 
2489
    def test_non_versioned_file_create_conflict(self):
 
2490
        wt, tt = self.make_tt_with_versioned_dir()
 
2491
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2492
        tt.new_file('file', dir_tid, 'Contents')
 
2493
        tt.delete_contents(dir_tid)
 
2494
        tt.unversion_file(dir_tid)
 
2495
        conflicts = resolve_conflicts(tt)
 
2496
        # no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
 
2497
        self.assertLength(1, conflicts)
 
2498
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
2499
                         conflicts.pop())
2339
2500
 
2340
2501
 
2341
2502
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2342
2503
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2343
2504
                  (False, False))
2344
2505
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2345
 
              ('', ''), ('directory', 'directory'), (False, None))
 
2506
              ('', ''), ('directory', 'directory'), (False, False))
2346
2507
 
2347
2508
 
2348
2509
class TestTransformPreview(tests.TestCaseWithTransport):
2435
2596
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2436
2597
        changes = preview_tree.iter_changes(revision_tree,
2437
2598
                                            specific_files=[''])
2438
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
2599
        self.assertEqual([A_ENTRY], list(changes))
2439
2600
 
2440
2601
    def test_want_unversioned(self):
2441
2602
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2442
2603
        changes = preview_tree.iter_changes(revision_tree,
2443
2604
                                            want_unversioned=True)
2444
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
2605
        self.assertEqual([A_ENTRY], list(changes))
2445
2606
 
2446
2607
    def test_ignore_extra_trees_no_specific_files(self):
2447
2608
        # extra_trees is harmless without specific_files, so we'll silently
2490
2651
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2491
2652
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
2492
2653
 
 
2654
    def test_get_file_size(self):
 
2655
        work_tree = self.make_branch_and_tree('tree')
 
2656
        self.build_tree_contents([('tree/old', 'old')])
 
2657
        work_tree.add('old', 'old-id')
 
2658
        preview = TransformPreview(work_tree)
 
2659
        self.addCleanup(preview.finalize)
 
2660
        new_id = preview.new_file('name', preview.root, 'contents', 'new-id',
 
2661
                                  'executable')
 
2662
        tree = preview.get_preview_tree()
 
2663
        self.assertEqual(len('old'), tree.get_file_size('old-id'))
 
2664
        self.assertEqual(len('contents'), tree.get_file_size('new-id'))
 
2665
 
2493
2666
    def test_get_file(self):
2494
2667
        preview = self.get_empty_preview()
2495
2668
        preview.new_file('file', preview.root, 'contents', 'file-id')
2907
3080
        merger.merge_type = Merge3Merger
2908
3081
        merger.do_merge()
2909
3082
 
 
3083
    def test_has_filename(self):
 
3084
        wt = self.make_branch_and_tree('tree')
 
3085
        self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
 
3086
        tt = TransformPreview(wt)
 
3087
        removed_id = tt.trans_id_tree_path('removed')
 
3088
        tt.delete_contents(removed_id)
 
3089
        tt.new_file('new', tt.root, 'contents')
 
3090
        modified_id = tt.trans_id_tree_path('modified')
 
3091
        tt.delete_contents(modified_id)
 
3092
        tt.create_file('modified-contents', modified_id)
 
3093
        self.addCleanup(tt.finalize)
 
3094
        tree = tt.get_preview_tree()
 
3095
        self.assertTrue(tree.has_filename('unmodified'))
 
3096
        self.assertFalse(tree.has_filename('not-present'))
 
3097
        self.assertFalse(tree.has_filename('removed'))
 
3098
        self.assertTrue(tree.has_filename('new'))
 
3099
        self.assertTrue(tree.has_filename('modified'))
 
3100
 
2910
3101
    def test_is_executable(self):
2911
3102
        tree = self.make_branch_and_tree('tree')
2912
3103
        preview = TransformPreview(tree)
3218
3409
        trans_id = tt.trans_id_tree_path('file')
3219
3410
        self.assertEqual((LINES_ONE,),
3220
3411
            tt._get_parents_texts(trans_id))
 
3412
 
 
3413
 
 
3414
class TestOrphan(tests.TestCaseWithTransport):
 
3415
 
 
3416
    def test_no_orphan_for_transform_preview(self):
 
3417
        tree = self.make_branch_and_tree('tree')
 
3418
        tt = transform.TransformPreview(tree)
 
3419
        self.addCleanup(tt.finalize)
 
3420
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
 
3421
 
 
3422
    def _set_orphan_policy(self, wt, policy):
 
3423
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3424
                                               policy)
 
3425
 
 
3426
    def _prepare_orphan(self, wt):
 
3427
        self.build_tree(['dir/', 'dir/file', 'dir/foo'])
 
3428
        wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
3429
        wt.commit('add dir and file ignoring foo')
 
3430
        tt = transform.TreeTransform(wt)
 
3431
        self.addCleanup(tt.finalize)
 
3432
        # dir and bar are deleted
 
3433
        dir_tid = tt.trans_id_tree_path('dir')
 
3434
        file_tid = tt.trans_id_tree_path('dir/file')
 
3435
        orphan_tid = tt.trans_id_tree_path('dir/foo')
 
3436
        tt.delete_contents(file_tid)
 
3437
        tt.unversion_file(file_tid)
 
3438
        tt.delete_contents(dir_tid)
 
3439
        tt.unversion_file(dir_tid)
 
3440
        # There should be a conflict because dir still contain foo
 
3441
        raw_conflicts = tt.find_conflicts()
 
3442
        self.assertLength(1, raw_conflicts)
 
3443
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
 
3444
        return tt, orphan_tid
 
3445
 
 
3446
    def test_new_orphan_created(self):
 
3447
        wt = self.make_branch_and_tree('.')
 
3448
        self._set_orphan_policy(wt, 'move')
 
3449
        tt, orphan_tid = self._prepare_orphan(wt)
 
3450
        warnings = []
 
3451
        def warning(*args):
 
3452
            warnings.append(args[0] % args[1:])
 
3453
        self.overrideAttr(trace, 'warning', warning)
 
3454
        remaining_conflicts = resolve_conflicts(tt)
 
3455
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3456
                          warnings)
 
3457
        # Yeah for resolved conflicts !
 
3458
        self.assertLength(0, remaining_conflicts)
 
3459
        # We have a new orphan
 
3460
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
 
3461
        self.assertEquals('bzr-orphans',
 
3462
                          tt.final_name(tt.final_parent(orphan_tid)))
 
3463
 
 
3464
    def test_never_orphan(self):
 
3465
        wt = self.make_branch_and_tree('.')
 
3466
        self._set_orphan_policy(wt, 'conflict')
 
3467
        tt, orphan_tid = self._prepare_orphan(wt)
 
3468
        remaining_conflicts = resolve_conflicts(tt)
 
3469
        self.assertLength(1, remaining_conflicts)
 
3470
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3471
                         remaining_conflicts.pop())
 
3472
 
 
3473
    def test_orphan_error(self):
 
3474
        def bogus_orphan(tt, orphan_id, parent_id):
 
3475
            raise transform.OrphaningError(tt.final_name(orphan_id),
 
3476
                                           tt.final_name(parent_id))
 
3477
        transform.orphaning_registry.register('bogus', bogus_orphan,
 
3478
                                              'Raise an error when orphaning')
 
3479
        wt = self.make_branch_and_tree('.')
 
3480
        self._set_orphan_policy(wt, 'bogus')
 
3481
        tt, orphan_tid = self._prepare_orphan(wt)
 
3482
        remaining_conflicts = resolve_conflicts(tt)
 
3483
        self.assertLength(1, remaining_conflicts)
 
3484
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3485
                         remaining_conflicts.pop())
 
3486
 
 
3487
    def test_unknown_orphan_policy(self):
 
3488
        wt = self.make_branch_and_tree('.')
 
3489
        # Set a fictional policy nobody ever implemented
 
3490
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
 
3491
        tt, orphan_tid = self._prepare_orphan(wt)
 
3492
        warnings = []
 
3493
        def warning(*args):
 
3494
            warnings.append(args[0] % args[1:])
 
3495
        self.overrideAttr(trace, 'warning', warning)
 
3496
        remaining_conflicts = resolve_conflicts(tt)
 
3497
        # We fallback to the default policy which create a conflict
 
3498
        self.assertLength(1, remaining_conflicts)
 
3499
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3500
                         remaining_conflicts.pop())
 
3501
        self.assertLength(1, warnings)
 
3502
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')