~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 04:25:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5472.
  • Revision ID: andrew.bennetts@canonical.com-20101008042510-sg9vdhmnggilzxsk
Fix stray TAB in source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
18
17
import os
19
18
from StringIO import StringIO
20
19
import sys
28
27
    osutils,
29
28
    revision as _mod_revision,
30
29
    rules,
31
 
    symbol_versioning,
32
30
    tests,
33
 
    trace,
34
 
    transform,
35
31
    urlutils,
36
32
    )
37
33
from bzrlib.bzrdir import BzrDir
53
49
    ImmortalPendingDeletion,
54
50
    LockError,
55
51
    MalformedTransform,
 
52
    NoSuchFile,
56
53
    ReusingTransform,
57
54
)
58
55
from bzrlib.osutils import (
61
58
)
62
59
from bzrlib.merge import Merge3Merger, Merger
63
60
from bzrlib.tests import (
64
 
    features,
65
61
    HardlinkFeature,
66
62
    SymlinkFeature,
 
63
    TestCase,
67
64
    TestCaseInTempDir,
68
65
    TestSkipped,
69
66
)
73
70
    cook_conflicts,
74
71
    _FileMover,
75
72
    FinalPaths,
 
73
    get_backup_name,
76
74
    resolve_conflicts,
77
75
    resolve_checkout,
78
76
    ROOT_PARENT,
93
91
        self.addCleanup(transform.finalize)
94
92
        return transform, transform.root
95
93
 
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
 
 
106
94
    def test_existing_limbo(self):
107
95
        transform, root = self.get_transform()
108
96
        limbo_name = transform._limbodir
171
159
        transform.finalize()
172
160
        transform.finalize()
173
161
 
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
 
 
235
162
    def test_create_files_same_timestamp(self):
236
163
        transform, root = self.get_transform()
237
164
        self.wt.lock_tree_write()
295
222
        trans_id = target_transform.create_path('file1', target_transform.root)
296
223
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
297
224
        target_transform.apply()
298
 
        self.assertPathExists('target/file1')
 
225
        self.failUnlessExists('target/file1')
299
226
        source_stat = os.stat(self.wt.abspath('file1'))
300
227
        target_stat = os.stat('target/file1')
301
228
        self.assertEqual(source_stat, target_stat)
467
394
        transform.new_file('FiLe', transform.root, 'content')
468
395
        resolve_conflicts(transform)
469
396
        transform.apply()
470
 
        self.assertPathExists('tree/file')
471
 
        self.assertPathExists('tree/FiLe.moved')
 
397
        self.failUnlessExists('tree/file')
 
398
        self.failUnlessExists('tree/FiLe.moved')
472
399
 
473
400
    def test_resolve_checkout_case_conflict(self):
474
401
        tree = self.make_branch_and_tree('tree')
483
410
        resolve_conflicts(transform,
484
411
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
485
412
        transform.apply()
486
 
        self.assertPathExists('tree/file')
487
 
        self.assertPathExists('tree/FiLe.moved')
 
413
        self.failUnlessExists('tree/file')
 
414
        self.failUnlessExists('tree/FiLe.moved')
488
415
 
489
416
    def test_apply_case_conflict(self):
490
417
        """Ensure that a transform with case conflicts can always be applied"""
498
425
        transform.new_file('dirFiLe', dir, 'content')
499
426
        resolve_conflicts(transform)
500
427
        transform.apply()
501
 
        self.assertPathExists('tree/file')
 
428
        self.failUnlessExists('tree/file')
502
429
        if not os.path.exists('tree/FiLe.moved'):
503
 
            self.assertPathExists('tree/FiLe')
504
 
        self.assertPathExists('tree/dir/dirfile')
 
430
            self.failUnlessExists('tree/FiLe')
 
431
        self.failUnlessExists('tree/dir/dirfile')
505
432
        if not os.path.exists('tree/dir/dirFiLe.moved'):
506
 
            self.assertPathExists('tree/dir/dirFiLe')
 
433
            self.failUnlessExists('tree/dir/dirFiLe')
507
434
 
508
435
    def test_case_insensitive_limbo(self):
509
436
        tree = self.make_branch_and_tree('tree')
887
814
        self.assertIs(None, self.wt.path2id('parent'))
888
815
        self.assertIs(None, self.wt.path2id('parent.new'))
889
816
 
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
 
 
905
817
    def test_moving_versioned_directories(self):
906
818
        create, root = self.get_transform()
907
819
        kansas = create.new_directory('kansas', root, 'kansas-id')
941
853
        rename.apply()
942
854
 
943
855
    def test_rename_fails(self):
944
 
        self.requireFeature(features.not_running_as_root)
945
856
        # see https://bugs.launchpad.net/bzr/+bug/491763
946
857
        create, root_id = self.get_transform()
947
858
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
969
880
        # On windows looks like:
970
881
        # "Failed to rename .../work/myfile to 
971
882
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
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")
 
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)")
980
886
 
981
887
    def test_set_executability_order(self):
982
888
        """Ensure that executability behaves the same, no matter what order.
1255
1161
        parent2 = transform.new_directory('parent2', root)
1256
1162
        transform.adjust_path('child1', parent2, child1)
1257
1163
        transform.apply()
1258
 
        self.assertPathDoesNotExist(self.wt.abspath('parent1/child1'))
1259
 
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1164
        self.failIfExists(self.wt.abspath('parent1/child1'))
 
1165
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1260
1166
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
1261
1167
        # no rename for child1 (counting only renames during apply)
1262
 
        self.assertEqual(2, transform.rename_count)
 
1168
        self.failUnlessEqual(2, transform.rename_count)
1263
1169
 
1264
1170
    def test_cancel_parent(self):
1265
1171
        """Cancelling a parent doesn't cause deletion of a non-empty directory
1288
1194
        parent2 = transform.new_directory('parent2', root)
1289
1195
        transform.adjust_path('child1', parent2, child1)
1290
1196
        transform.apply()
1291
 
        self.assertPathDoesNotExist(self.wt.abspath('parent1'))
1292
 
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1197
        self.failIfExists(self.wt.abspath('parent1'))
 
1198
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1293
1199
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
1294
 
        self.assertEqual(2, transform.rename_count)
 
1200
        self.failUnlessEqual(2, transform.rename_count)
1295
1201
 
1296
1202
    def test_adjust_and_cancel(self):
1297
1203
        """Make sure adjust_path keeps track of limbo children properly"""
1330
1236
        child = transform.new_directory('child', parent)
1331
1237
        transform.adjust_path('parent', root, parent)
1332
1238
        transform.apply()
1333
 
        self.assertPathExists(self.wt.abspath('parent/child'))
 
1239
        self.failUnlessExists(self.wt.abspath('parent/child'))
1334
1240
        self.assertEqual(1, transform.rename_count)
1335
1241
 
1336
1242
    def test_reuse_name(self):
1467
1373
        tt.create_file(["aa\n"], bar_trans_id)
1468
1374
        tt.version_file("bar-1", bar_trans_id)
1469
1375
        tt.apply()
1470
 
        self.assertPathExists("foo/bar")
 
1376
        self.failUnlessExists("foo/bar")
1471
1377
        wt.lock_read()
1472
1378
        try:
1473
1379
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1490
1396
        tt.delete_contents(foo_trans_id)
1491
1397
        tt.create_symlink("bar", foo_trans_id)
1492
1398
        tt.apply()
1493
 
        self.assertPathExists("foo")
 
1399
        self.failUnlessExists("foo")
1494
1400
        wt.lock_read()
1495
1401
        self.addCleanup(wt.unlock)
1496
1402
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1509
1415
        tt.delete_versioned(bar_trans_id)
1510
1416
        tt.create_file(["aa\n"], foo_trans_id)
1511
1417
        tt.apply()
1512
 
        self.assertPathExists("foo")
 
1418
        self.failUnlessExists("foo")
1513
1419
        wt.lock_read()
1514
1420
        self.addCleanup(wt.unlock)
1515
1421
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1530
1436
        self.build_tree(['baz'])
1531
1437
        tt.create_hardlink("baz", foo_trans_id)
1532
1438
        tt.apply()
1533
 
        self.assertPathExists("foo")
1534
 
        self.assertPathExists("baz")
 
1439
        self.failUnlessExists("foo")
 
1440
        self.failUnlessExists("baz")
1535
1441
        wt.lock_read()
1536
1442
        self.addCleanup(wt.unlock)
1537
1443
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1814
1720
        tree.add_reference(subtree)
1815
1721
        tree.commit('a revision')
1816
1722
        tree.branch.create_checkout('target')
1817
 
        self.assertPathExists('target')
1818
 
        self.assertPathExists('target/subtree')
 
1723
        self.failUnlessExists('target')
 
1724
        self.failUnlessExists('target/subtree')
1819
1725
 
1820
1726
    def test_file_conflict_handling(self):
1821
1727
        """Ensure that when building trees, conflict handling is done"""
1868
1774
        source.commit('added file')
1869
1775
        build_tree(source.basis_tree(), target)
1870
1776
        self.assertEqual([], target.conflicts())
1871
 
        self.assertPathExists('target/dir1/file')
 
1777
        self.failUnlessExists('target/dir1/file')
1872
1778
 
1873
1779
        # Ensure contents are merged
1874
1780
        target = self.make_branch_and_tree('target2')
1875
1781
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1876
1782
        build_tree(source.basis_tree(), target)
1877
1783
        self.assertEqual([], target.conflicts())
1878
 
        self.assertPathExists('target2/dir1/file2')
1879
 
        self.assertPathExists('target2/dir1/file')
 
1784
        self.failUnlessExists('target2/dir1/file2')
 
1785
        self.failUnlessExists('target2/dir1/file')
1880
1786
 
1881
1787
        # Ensure new contents are suppressed for existing branches
1882
1788
        target = self.make_branch_and_tree('target3')
1883
1789
        self.make_branch('target3/dir1')
1884
1790
        self.build_tree(['target3/dir1/file2'])
1885
1791
        build_tree(source.basis_tree(), target)
1886
 
        self.assertPathDoesNotExist('target3/dir1/file')
1887
 
        self.assertPathExists('target3/dir1/file2')
1888
 
        self.assertPathExists('target3/dir1.diverted/file')
 
1792
        self.failIfExists('target3/dir1/file')
 
1793
        self.failUnlessExists('target3/dir1/file2')
 
1794
        self.failUnlessExists('target3/dir1.diverted/file')
1889
1795
        self.assertEqual([DuplicateEntry('Diverted to',
1890
1796
            'dir1.diverted', 'dir1', 'new-dir1', None)],
1891
1797
            target.conflicts())
1894
1800
        self.build_tree(['target4/dir1/'])
1895
1801
        self.make_branch('target4/dir1/file')
1896
1802
        build_tree(source.basis_tree(), target)
1897
 
        self.assertPathExists('target4/dir1/file')
 
1803
        self.failUnlessExists('target4/dir1/file')
1898
1804
        self.assertEqual('directory', file_kind('target4/dir1/file'))
1899
 
        self.assertPathExists('target4/dir1/file.diverted')
 
1805
        self.failUnlessExists('target4/dir1/file.diverted')
1900
1806
        self.assertEqual([DuplicateEntry('Diverted to',
1901
1807
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
1902
1808
            target.conflicts())
1970
1876
        self.addCleanup(target.unlock)
1971
1877
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1972
1878
 
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
 
 
1985
1879
    def test_build_tree_accelerator_tree_missing_file(self):
1986
1880
        source = self.create_ab_tree()
1987
1881
        os.unlink('source/file1')
2145
2039
        self.assertEqual('file.moved', target.id2path('lower-id'))
2146
2040
        self.assertEqual('FILE', target.id2path('upper-id'))
2147
2041
 
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
 
 
2184
2042
 
2185
2043
class TestCommitTransform(tests.TestCaseWithTransport):
2186
2044
 
2320
2178
        self.assertEqual('tree', revision.properties['branch-nick'])
2321
2179
 
2322
2180
 
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
 
 
 
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):
2337
2201
        tt = MockTransform()
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)
 
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~')
2346
2212
 
2347
2213
 
2348
2214
class TestFileMover(tests.TestCaseWithTransport):
2351
2217
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2352
2218
        mover = _FileMover()
2353
2219
        mover.rename('a', 'q')
2354
 
        self.assertPathExists('q')
2355
 
        self.assertPathDoesNotExist('a')
2356
 
        self.assertPathExists('q/b')
2357
 
        self.assertPathExists('c')
2358
 
        self.assertPathExists('c/d')
 
2220
        self.failUnlessExists('q')
 
2221
        self.failIfExists('a')
 
2222
        self.failUnlessExists('q/b')
 
2223
        self.failUnlessExists('c')
 
2224
        self.failUnlessExists('c/d')
2359
2225
 
2360
2226
    def test_pre_delete_rollback(self):
2361
2227
        self.build_tree(['a/'])
2362
2228
        mover = _FileMover()
2363
2229
        mover.pre_delete('a', 'q')
2364
 
        self.assertPathExists('q')
2365
 
        self.assertPathDoesNotExist('a')
 
2230
        self.failUnlessExists('q')
 
2231
        self.failIfExists('a')
2366
2232
        mover.rollback()
2367
 
        self.assertPathDoesNotExist('q')
2368
 
        self.assertPathExists('a')
 
2233
        self.failIfExists('q')
 
2234
        self.failUnlessExists('a')
2369
2235
 
2370
2236
    def test_apply_deletions(self):
2371
2237
        self.build_tree(['a/', 'b/'])
2372
2238
        mover = _FileMover()
2373
2239
        mover.pre_delete('a', 'q')
2374
2240
        mover.pre_delete('b', 'r')
2375
 
        self.assertPathExists('q')
2376
 
        self.assertPathExists('r')
2377
 
        self.assertPathDoesNotExist('a')
2378
 
        self.assertPathDoesNotExist('b')
 
2241
        self.failUnlessExists('q')
 
2242
        self.failUnlessExists('r')
 
2243
        self.failIfExists('a')
 
2244
        self.failIfExists('b')
2379
2245
        mover.apply_deletions()
2380
 
        self.assertPathDoesNotExist('q')
2381
 
        self.assertPathDoesNotExist('r')
2382
 
        self.assertPathDoesNotExist('a')
2383
 
        self.assertPathDoesNotExist('b')
 
2246
        self.failIfExists('q')
 
2247
        self.failIfExists('r')
 
2248
        self.failIfExists('a')
 
2249
        self.failIfExists('b')
2384
2250
 
2385
2251
    def test_file_mover_rollback(self):
2386
2252
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2391
2257
            mover.rename('a', 'c')
2392
2258
        except errors.FileExists, e:
2393
2259
            mover.rollback()
2394
 
        self.assertPathExists('a')
2395
 
        self.assertPathExists('c/d')
 
2260
        self.failUnlessExists('a')
 
2261
        self.failUnlessExists('c/d')
2396
2262
 
2397
2263
 
2398
2264
class Bogus(Exception):
2428
2294
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2429
2295
        self.assertRaises(Bogus, tt.apply,
2430
2296
                          _mover=self.ExceptionFileMover(bad_source='a'))
2431
 
        self.assertPathExists('a')
2432
 
        self.assertPathExists('a/b')
 
2297
        self.failUnlessExists('a')
 
2298
        self.failUnlessExists('a/b')
2433
2299
        tt.apply()
2434
 
        self.assertPathExists('c')
2435
 
        self.assertPathExists('c/d')
 
2300
        self.failUnlessExists('c')
 
2301
        self.failUnlessExists('c/d')
2436
2302
 
2437
2303
    def test_rollback_rename_into_place(self):
2438
2304
        tree = self.make_branch_and_tree('.')
2444
2310
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2445
2311
        self.assertRaises(Bogus, tt.apply,
2446
2312
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
2447
 
        self.assertPathExists('a')
2448
 
        self.assertPathExists('a/b')
 
2313
        self.failUnlessExists('a')
 
2314
        self.failUnlessExists('a/b')
2449
2315
        tt.apply()
2450
 
        self.assertPathExists('c')
2451
 
        self.assertPathExists('c/d')
 
2316
        self.failUnlessExists('c')
 
2317
        self.failUnlessExists('c/d')
2452
2318
 
2453
2319
    def test_rollback_deletion(self):
2454
2320
        tree = self.make_branch_and_tree('.')
2460
2326
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2461
2327
        self.assertRaises(Bogus, tt.apply,
2462
2328
                          _mover=self.ExceptionFileMover(bad_target='d'))
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):
 
2329
        self.failUnlessExists('a')
 
2330
        self.failUnlessExists('a/b')
 
2331
 
 
2332
    def test_resolve_no_parent(self):
2470
2333
        wt = self.make_branch_and_tree('.')
2471
 
        self.build_tree(['dir/',])
2472
 
        wt.add(['dir'], ['dir-id'])
2473
 
        wt.commit('Create dir')
2474
2334
        tt = TreeTransform(wt)
2475
2335
        self.addCleanup(tt.finalize)
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())
 
2336
        parent = tt.trans_id_file_id('parent-id')
 
2337
        tt.new_file('file', parent, 'Contents')
 
2338
        resolve_conflicts(tt)
2500
2339
 
2501
2340
 
2502
2341
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2503
2342
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2504
2343
                  (False, False))
2505
2344
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2506
 
              ('', ''), ('directory', 'directory'), (False, False))
 
2345
              ('', ''), ('directory', 'directory'), (False, None))
2507
2346
 
2508
2347
 
2509
2348
class TestTransformPreview(tests.TestCaseWithTransport):
2596
2435
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2597
2436
        changes = preview_tree.iter_changes(revision_tree,
2598
2437
                                            specific_files=[''])
2599
 
        self.assertEqual([A_ENTRY], list(changes))
 
2438
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
2600
2439
 
2601
2440
    def test_want_unversioned(self):
2602
2441
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2603
2442
        changes = preview_tree.iter_changes(revision_tree,
2604
2443
                                            want_unversioned=True)
2605
 
        self.assertEqual([A_ENTRY], list(changes))
 
2444
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
2606
2445
 
2607
2446
    def test_ignore_extra_trees_no_specific_files(self):
2608
2447
        # extra_trees is harmless without specific_files, so we'll silently
3379
3218
        trans_id = tt.trans_id_tree_path('file')
3380
3219
        self.assertEqual((LINES_ONE,),
3381
3220
            tt._get_parents_texts(trans_id))
3382
 
 
3383
 
 
3384
 
class TestOrphan(tests.TestCaseWithTransport):
3385
 
 
3386
 
    def test_no_orphan_for_transform_preview(self):
3387
 
        tree = self.make_branch_and_tree('tree')
3388
 
        tt = transform.TransformPreview(tree)
3389
 
        self.addCleanup(tt.finalize)
3390
 
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3391
 
 
3392
 
    def _set_orphan_policy(self, wt, policy):
3393
 
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3394
 
                                               policy)
3395
 
 
3396
 
    def _prepare_orphan(self, wt):
3397
 
        self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3398
 
        wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3399
 
        wt.commit('add dir and file ignoring foo')
3400
 
        tt = transform.TreeTransform(wt)
3401
 
        self.addCleanup(tt.finalize)
3402
 
        # dir and bar are deleted
3403
 
        dir_tid = tt.trans_id_tree_path('dir')
3404
 
        file_tid = tt.trans_id_tree_path('dir/file')
3405
 
        orphan_tid = tt.trans_id_tree_path('dir/foo')
3406
 
        tt.delete_contents(file_tid)
3407
 
        tt.unversion_file(file_tid)
3408
 
        tt.delete_contents(dir_tid)
3409
 
        tt.unversion_file(dir_tid)
3410
 
        # There should be a conflict because dir still contain foo
3411
 
        raw_conflicts = tt.find_conflicts()
3412
 
        self.assertLength(1, raw_conflicts)
3413
 
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3414
 
        return tt, orphan_tid
3415
 
 
3416
 
    def test_new_orphan_created(self):
3417
 
        wt = self.make_branch_and_tree('.')
3418
 
        self._set_orphan_policy(wt, 'move')
3419
 
        tt, orphan_tid = self._prepare_orphan(wt)
3420
 
        warnings = []
3421
 
        def warning(*args):
3422
 
            warnings.append(args[0] % args[1:])
3423
 
        self.overrideAttr(trace, 'warning', warning)
3424
 
        remaining_conflicts = resolve_conflicts(tt)
3425
 
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3426
 
                          warnings)
3427
 
        # Yeah for resolved conflicts !
3428
 
        self.assertLength(0, remaining_conflicts)
3429
 
        # We have a new orphan
3430
 
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3431
 
        self.assertEquals('bzr-orphans',
3432
 
                          tt.final_name(tt.final_parent(orphan_tid)))
3433
 
 
3434
 
    def test_never_orphan(self):
3435
 
        wt = self.make_branch_and_tree('.')
3436
 
        self._set_orphan_policy(wt, 'conflict')
3437
 
        tt, orphan_tid = self._prepare_orphan(wt)
3438
 
        remaining_conflicts = resolve_conflicts(tt)
3439
 
        self.assertLength(1, remaining_conflicts)
3440
 
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3441
 
                         remaining_conflicts.pop())
3442
 
 
3443
 
    def test_orphan_error(self):
3444
 
        def bogus_orphan(tt, orphan_id, parent_id):
3445
 
            raise transform.OrphaningError(tt.final_name(orphan_id),
3446
 
                                           tt.final_name(parent_id))
3447
 
        transform.orphaning_registry.register('bogus', bogus_orphan,
3448
 
                                              'Raise an error when orphaning')
3449
 
        wt = self.make_branch_and_tree('.')
3450
 
        self._set_orphan_policy(wt, 'bogus')
3451
 
        tt, orphan_tid = self._prepare_orphan(wt)
3452
 
        remaining_conflicts = resolve_conflicts(tt)
3453
 
        self.assertLength(1, remaining_conflicts)
3454
 
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3455
 
                         remaining_conflicts.pop())
3456
 
 
3457
 
    def test_unknown_orphan_policy(self):
3458
 
        wt = self.make_branch_and_tree('.')
3459
 
        # Set a fictional policy nobody ever implemented
3460
 
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3461
 
        tt, orphan_tid = self._prepare_orphan(wt)
3462
 
        warnings = []
3463
 
        def warning(*args):
3464
 
            warnings.append(args[0] % args[1:])
3465
 
        self.overrideAttr(trace, 'warning', warning)
3466
 
        remaining_conflicts = resolve_conflicts(tt)
3467
 
        # We fallback to the default policy which create a conflict
3468
 
        self.assertLength(1, remaining_conflicts)
3469
 
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3470
 
                         remaining_conflicts.pop())
3471
 
        self.assertLength(1, warnings)
3472
 
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')