~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

(gz) Fix test failure on alpha by correcting format string for
 gc_chk_sha1_record (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
from bzrlib.merge import Merge3Merger, Merger
63
63
from bzrlib.tests import (
64
64
    features,
 
65
    HardlinkFeature,
 
66
    SymlinkFeature,
65
67
    TestCaseInTempDir,
66
68
    TestSkipped,
67
 
    )
68
 
from bzrlib.tests.features import (
69
 
    HardlinkFeature,
70
 
    SymlinkFeature,
71
 
    )
 
69
)
72
70
from bzrlib.transform import (
73
71
    build_tree,
74
72
    create_from_tree,
95
93
        self.addCleanup(transform.finalize)
96
94
        return transform, transform.root
97
95
 
98
 
    def get_transform_for_sha1_test(self):
99
 
        trans, root = self.get_transform()
100
 
        self.wt.lock_tree_write()
101
 
        self.addCleanup(self.wt.unlock)
102
 
        contents = ['just some content\n']
103
 
        sha1 = osutils.sha_strings(contents)
104
 
        # Roll back the clock
105
 
        trans._creation_mtime = time.time() - 20.0
106
 
        return trans, root, contents, sha1
107
 
 
108
96
    def test_existing_limbo(self):
109
97
        transform, root = self.get_transform()
110
98
        limbo_name = transform._limbodir
173
161
        transform.finalize()
174
162
        transform.finalize()
175
163
 
176
 
    def test_apply_informs_tree_of_observed_sha1(self):
177
 
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
178
 
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
179
 
                                  sha1=sha1)
180
 
        calls = []
181
 
        orig = self.wt._observed_sha1
182
 
        def _observed_sha1(*args):
183
 
            calls.append(args)
184
 
            orig(*args)
185
 
        self.wt._observed_sha1 = _observed_sha1
186
 
        trans.apply()
187
 
        self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
188
 
                         calls)
189
 
 
190
 
    def test_create_file_caches_sha1(self):
191
 
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
192
 
        trans_id = trans.create_path('file1', root)
193
 
        trans.create_file(contents, trans_id, sha1=sha1)
194
 
        st_val = osutils.lstat(trans._limbo_name(trans_id))
195
 
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
196
 
        self.assertEqual(o_sha1, sha1)
197
 
        self.assertEqualStat(o_st_val, st_val)
198
 
 
199
 
    def test__apply_insertions_updates_sha1(self):
200
 
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
201
 
        trans_id = trans.create_path('file1', root)
202
 
        trans.create_file(contents, trans_id, sha1=sha1)
203
 
        st_val = osutils.lstat(trans._limbo_name(trans_id))
204
 
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
205
 
        self.assertEqual(o_sha1, sha1)
206
 
        self.assertEqualStat(o_st_val, st_val)
207
 
        creation_mtime = trans._creation_mtime + 10.0
208
 
        # We fake a time difference from when the file was created until now it
209
 
        # is being renamed by using os.utime. Note that the change we actually
210
 
        # want to see is the real ctime change from 'os.rename()', but as long
211
 
        # as we observe a new stat value, we should be fine.
212
 
        os.utime(trans._limbo_name(trans_id), (creation_mtime, creation_mtime))
213
 
        trans.apply()
214
 
        new_st_val = osutils.lstat(self.wt.abspath('file1'))
215
 
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
216
 
        self.assertEqual(o_sha1, sha1)
217
 
        self.assertEqualStat(o_st_val, new_st_val)
218
 
        self.assertNotEqual(st_val.st_mtime, new_st_val.st_mtime)
219
 
 
220
 
    def test_new_file_caches_sha1(self):
221
 
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
222
 
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
223
 
                                  sha1=sha1)
224
 
        st_val = osutils.lstat(trans._limbo_name(trans_id))
225
 
        o_sha1, o_st_val = trans._observed_sha1s[trans_id]
226
 
        self.assertEqual(o_sha1, sha1)
227
 
        self.assertEqualStat(o_st_val, st_val)
228
 
 
229
 
    def test_cancel_creation_removes_observed_sha1(self):
230
 
        trans, root, contents, sha1 = self.get_transform_for_sha1_test()
231
 
        trans_id = trans.new_file('file1', root, contents, file_id='file1-id',
232
 
                                  sha1=sha1)
233
 
        self.assertTrue(trans_id in trans._observed_sha1s)
234
 
        trans.cancel_creation(trans_id)
235
 
        self.assertFalse(trans_id in trans._observed_sha1s)
236
 
 
237
164
    def test_create_files_same_timestamp(self):
238
165
        transform, root = self.get_transform()
239
166
        self.wt.lock_tree_write()
287
214
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
288
215
        self.assertRaises(ValueError, transform.fixup_new_roots)
289
216
 
290
 
    def test_retain_existing_root(self):
291
 
        tt, root = self.get_transform()
292
 
        with tt:
293
 
            tt.new_directory('', ROOT_PARENT, 'new-root-id')
294
 
            tt.fixup_new_roots()
295
 
            self.assertNotEqual('new-root-id', tt.final_file_id(tt.root))
296
 
 
297
 
    def test_retain_existing_root_added_file(self):
298
 
        tt, root = self.get_transform()
299
 
        new_trans_id = tt.new_directory('', ROOT_PARENT, 'new-root-id')
300
 
        child = tt.new_directory('child', new_trans_id, 'child-id')
301
 
        tt.fixup_new_roots()
302
 
        self.assertEqual(tt.root, tt.final_parent(child))
303
 
 
304
 
    def test_add_unversioned_root(self):
305
 
        transform, root = self.get_transform()
306
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, None)
307
 
        transform.delete_contents(transform.root)
308
 
        transform.fixup_new_roots()
309
 
        self.assertNotIn(transform.root, transform._new_id)
310
 
 
311
 
    def test_remove_root_fixup(self):
312
 
        transform, root = self.get_transform()
313
 
        old_root_id = self.wt.get_root_id()
314
 
        self.assertNotEqual('new-root-id', old_root_id)
315
 
        transform.delete_contents(root)
316
 
        transform.unversion_file(root)
317
 
        transform.fixup_new_roots()
318
 
        transform.apply()
319
 
        self.assertEqual(old_root_id, self.wt.get_root_id())
320
 
 
321
 
        transform, root = self.get_transform()
322
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
323
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
324
 
        self.assertRaises(ValueError, transform.fixup_new_roots)
325
 
 
326
 
    def test_fixup_new_roots_permits_empty_tree(self):
327
 
        transform, root = self.get_transform()
328
 
        transform.delete_contents(root)
329
 
        transform.unversion_file(root)
330
 
        transform.fixup_new_roots()
331
 
        self.assertIs(None, transform.final_kind(root))
332
 
        self.assertIs(None, transform.final_file_id(root))
333
 
 
334
 
    def test_apply_retains_root_directory(self):
335
 
        # Do not attempt to delete the physical root directory, because that
336
 
        # is impossible.
337
 
        transform, root = self.get_transform()
338
 
        with transform:
339
 
            transform.delete_contents(root)
340
 
            e = self.assertRaises(AssertionError, self.assertRaises,
341
 
                                  errors.TransformRenameFailed,
342
 
                                  transform.apply)
343
 
        self.assertContainsRe('TransformRenameFailed not raised', str(e))
344
 
 
345
 
    def test_apply_retains_file_id(self):
346
 
        transform, root = self.get_transform()
347
 
        old_root_id = transform.tree_file_id(root)
348
 
        transform.unversion_file(root)
349
 
        transform.apply()
350
 
        self.assertEqual(old_root_id, self.wt.get_root_id())
351
 
 
352
217
    def test_hardlink(self):
353
218
        self.requireFeature(HardlinkFeature)
354
219
        transform, root = self.get_transform()
359
224
        trans_id = target_transform.create_path('file1', target_transform.root)
360
225
        target_transform.create_hardlink(self.wt.abspath('file1'), trans_id)
361
226
        target_transform.apply()
362
 
        self.assertPathExists('target/file1')
 
227
        self.failUnlessExists('target/file1')
363
228
        source_stat = os.stat(self.wt.abspath('file1'))
364
229
        target_stat = os.stat('target/file1')
365
230
        self.assertEqual(source_stat, target_stat)
531
396
        transform.new_file('FiLe', transform.root, 'content')
532
397
        resolve_conflicts(transform)
533
398
        transform.apply()
534
 
        self.assertPathExists('tree/file')
535
 
        self.assertPathExists('tree/FiLe.moved')
 
399
        self.failUnlessExists('tree/file')
 
400
        self.failUnlessExists('tree/FiLe.moved')
536
401
 
537
402
    def test_resolve_checkout_case_conflict(self):
538
403
        tree = self.make_branch_and_tree('tree')
547
412
        resolve_conflicts(transform,
548
413
                          pass_func=lambda t, c: resolve_checkout(t, c, []))
549
414
        transform.apply()
550
 
        self.assertPathExists('tree/file')
551
 
        self.assertPathExists('tree/FiLe.moved')
 
415
        self.failUnlessExists('tree/file')
 
416
        self.failUnlessExists('tree/FiLe.moved')
552
417
 
553
418
    def test_apply_case_conflict(self):
554
419
        """Ensure that a transform with case conflicts can always be applied"""
562
427
        transform.new_file('dirFiLe', dir, 'content')
563
428
        resolve_conflicts(transform)
564
429
        transform.apply()
565
 
        self.assertPathExists('tree/file')
 
430
        self.failUnlessExists('tree/file')
566
431
        if not os.path.exists('tree/FiLe.moved'):
567
 
            self.assertPathExists('tree/FiLe')
568
 
        self.assertPathExists('tree/dir/dirfile')
 
432
            self.failUnlessExists('tree/FiLe')
 
433
        self.failUnlessExists('tree/dir/dirfile')
569
434
        if not os.path.exists('tree/dir/dirFiLe.moved'):
570
 
            self.assertPathExists('tree/dir/dirFiLe')
 
435
            self.failUnlessExists('tree/dir/dirFiLe')
571
436
 
572
437
    def test_case_insensitive_limbo(self):
573
438
        tree = self.make_branch_and_tree('tree')
781
646
                            'wizard2', 'behind_curtain')
782
647
 
783
648
    def test_symlinks_unicode(self):
784
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
649
        self.requireFeature(tests.UnicodeFilenameFeature)
785
650
        self._test_symlinks(u'\N{Euro Sign}wizard',
786
651
                            u'wizard-targ\N{Euro Sign}t',
787
652
                            u'\N{Euro Sign}wizard2',
875
740
        raw_conflicts = resolve_conflicts(tt)
876
741
        cooked_conflicts = cook_conflicts(raw_conflicts, tt)
877
742
        tt.finalize()
878
 
        conflicts_s = [unicode(c) for c in cooked_conflicts]
 
743
        conflicts_s = [str(c) for c in cooked_conflicts]
879
744
        self.assertEqual(len(cooked_conflicts), len(conflicts_s))
880
745
        self.assertEqual(conflicts_s[0], 'Conflict adding file dorothy.  '
881
746
                                         'Moved existing file to '
1319
1184
        parent2 = transform.new_directory('parent2', root)
1320
1185
        transform.adjust_path('child1', parent2, child1)
1321
1186
        transform.apply()
1322
 
        self.assertPathDoesNotExist(self.wt.abspath('parent1/child1'))
1323
 
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1187
        self.failIfExists(self.wt.abspath('parent1/child1'))
 
1188
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1324
1189
        # rename limbo/new-1 => parent1, rename limbo/new-3 => parent2
1325
1190
        # no rename for child1 (counting only renames during apply)
1326
 
        self.assertEqual(2, transform.rename_count)
 
1191
        self.failUnlessEqual(2, transform.rename_count)
1327
1192
 
1328
1193
    def test_cancel_parent(self):
1329
1194
        """Cancelling a parent doesn't cause deletion of a non-empty directory
1352
1217
        parent2 = transform.new_directory('parent2', root)
1353
1218
        transform.adjust_path('child1', parent2, child1)
1354
1219
        transform.apply()
1355
 
        self.assertPathDoesNotExist(self.wt.abspath('parent1'))
1356
 
        self.assertPathExists(self.wt.abspath('parent2/child1'))
 
1220
        self.failIfExists(self.wt.abspath('parent1'))
 
1221
        self.failUnlessExists(self.wt.abspath('parent2/child1'))
1357
1222
        # rename limbo/new-3 => parent2, rename limbo/new-2 => child1
1358
 
        self.assertEqual(2, transform.rename_count)
 
1223
        self.failUnlessEqual(2, transform.rename_count)
1359
1224
 
1360
1225
    def test_adjust_and_cancel(self):
1361
1226
        """Make sure adjust_path keeps track of limbo children properly"""
1394
1259
        child = transform.new_directory('child', parent)
1395
1260
        transform.adjust_path('parent', root, parent)
1396
1261
        transform.apply()
1397
 
        self.assertPathExists(self.wt.abspath('parent/child'))
 
1262
        self.failUnlessExists(self.wt.abspath('parent/child'))
1398
1263
        self.assertEqual(1, transform.rename_count)
1399
1264
 
1400
1265
    def test_reuse_name(self):
1531
1396
        tt.create_file(["aa\n"], bar_trans_id)
1532
1397
        tt.version_file("bar-1", bar_trans_id)
1533
1398
        tt.apply()
1534
 
        self.assertPathExists("foo/bar")
 
1399
        self.failUnlessExists("foo/bar")
1535
1400
        wt.lock_read()
1536
1401
        try:
1537
1402
            self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1554
1419
        tt.delete_contents(foo_trans_id)
1555
1420
        tt.create_symlink("bar", foo_trans_id)
1556
1421
        tt.apply()
1557
 
        self.assertPathExists("foo")
 
1422
        self.failUnlessExists("foo")
1558
1423
        wt.lock_read()
1559
1424
        self.addCleanup(wt.unlock)
1560
1425
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1573
1438
        tt.delete_versioned(bar_trans_id)
1574
1439
        tt.create_file(["aa\n"], foo_trans_id)
1575
1440
        tt.apply()
1576
 
        self.assertPathExists("foo")
 
1441
        self.failUnlessExists("foo")
1577
1442
        wt.lock_read()
1578
1443
        self.addCleanup(wt.unlock)
1579
1444
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1594
1459
        self.build_tree(['baz'])
1595
1460
        tt.create_hardlink("baz", foo_trans_id)
1596
1461
        tt.apply()
1597
 
        self.assertPathExists("foo")
1598
 
        self.assertPathExists("baz")
 
1462
        self.failUnlessExists("foo")
 
1463
        self.failUnlessExists("baz")
1599
1464
        wt.lock_read()
1600
1465
        self.addCleanup(wt.unlock)
1601
1466
        self.assertEqual(wt.inventory.get_file_kind(wt.path2id("foo")),
1663
1528
    return template % ('<' * 7, tree, '=' * 7, merge, '>' * 7)
1664
1529
 
1665
1530
 
1666
 
class TestInventoryAltered(tests.TestCaseWithTransport):
1667
 
 
1668
 
    def test_inventory_altered_unchanged(self):
1669
 
        tree = self.make_branch_and_tree('tree')
1670
 
        self.build_tree(['tree/foo'])
1671
 
        tree.add('foo', 'foo-id')
1672
 
        with TransformPreview(tree) as tt:
1673
 
            self.assertEqual([], tt._inventory_altered())
1674
 
 
1675
 
    def test_inventory_altered_changed_parent_id(self):
1676
 
        tree = self.make_branch_and_tree('tree')
1677
 
        self.build_tree(['tree/foo'])
1678
 
        tree.add('foo', 'foo-id')
1679
 
        with TransformPreview(tree) as tt:
1680
 
            tt.unversion_file(tt.root)
1681
 
            tt.version_file('new-id', tt.root)
1682
 
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1683
 
            foo_tuple = ('foo', foo_trans_id)
1684
 
            root_tuple = ('', tt.root)
1685
 
            self.assertEqual([root_tuple, foo_tuple], tt._inventory_altered())
1686
 
 
1687
 
    def test_inventory_altered_noop_changed_parent_id(self):
1688
 
        tree = self.make_branch_and_tree('tree')
1689
 
        self.build_tree(['tree/foo'])
1690
 
        tree.add('foo', 'foo-id')
1691
 
        with TransformPreview(tree) as tt:
1692
 
            tt.unversion_file(tt.root)
1693
 
            tt.version_file(tree.get_root_id(), tt.root)
1694
 
            foo_trans_id = tt.trans_id_tree_file_id('foo-id')
1695
 
            self.assertEqual([], tt._inventory_altered())
1696
 
 
1697
 
 
1698
1531
class TestTransformMerge(TestCaseInTempDir):
1699
1532
 
1700
1533
    def test_text_merge(self):
1910
1743
        tree.add_reference(subtree)
1911
1744
        tree.commit('a revision')
1912
1745
        tree.branch.create_checkout('target')
1913
 
        self.assertPathExists('target')
1914
 
        self.assertPathExists('target/subtree')
 
1746
        self.failUnlessExists('target')
 
1747
        self.failUnlessExists('target/subtree')
1915
1748
 
1916
1749
    def test_file_conflict_handling(self):
1917
1750
        """Ensure that when building trees, conflict handling is done"""
1964
1797
        source.commit('added file')
1965
1798
        build_tree(source.basis_tree(), target)
1966
1799
        self.assertEqual([], target.conflicts())
1967
 
        self.assertPathExists('target/dir1/file')
 
1800
        self.failUnlessExists('target/dir1/file')
1968
1801
 
1969
1802
        # Ensure contents are merged
1970
1803
        target = self.make_branch_and_tree('target2')
1971
1804
        self.build_tree(['target2/dir1/', 'target2/dir1/file2'])
1972
1805
        build_tree(source.basis_tree(), target)
1973
1806
        self.assertEqual([], target.conflicts())
1974
 
        self.assertPathExists('target2/dir1/file2')
1975
 
        self.assertPathExists('target2/dir1/file')
 
1807
        self.failUnlessExists('target2/dir1/file2')
 
1808
        self.failUnlessExists('target2/dir1/file')
1976
1809
 
1977
1810
        # Ensure new contents are suppressed for existing branches
1978
1811
        target = self.make_branch_and_tree('target3')
1979
1812
        self.make_branch('target3/dir1')
1980
1813
        self.build_tree(['target3/dir1/file2'])
1981
1814
        build_tree(source.basis_tree(), target)
1982
 
        self.assertPathDoesNotExist('target3/dir1/file')
1983
 
        self.assertPathExists('target3/dir1/file2')
1984
 
        self.assertPathExists('target3/dir1.diverted/file')
 
1815
        self.failIfExists('target3/dir1/file')
 
1816
        self.failUnlessExists('target3/dir1/file2')
 
1817
        self.failUnlessExists('target3/dir1.diverted/file')
1985
1818
        self.assertEqual([DuplicateEntry('Diverted to',
1986
1819
            'dir1.diverted', 'dir1', 'new-dir1', None)],
1987
1820
            target.conflicts())
1990
1823
        self.build_tree(['target4/dir1/'])
1991
1824
        self.make_branch('target4/dir1/file')
1992
1825
        build_tree(source.basis_tree(), target)
1993
 
        self.assertPathExists('target4/dir1/file')
 
1826
        self.failUnlessExists('target4/dir1/file')
1994
1827
        self.assertEqual('directory', file_kind('target4/dir1/file'))
1995
 
        self.assertPathExists('target4/dir1/file.diverted')
 
1828
        self.failUnlessExists('target4/dir1/file.diverted')
1996
1829
        self.assertEqual([DuplicateEntry('Diverted to',
1997
1830
            'dir1/file.diverted', 'dir1/file', 'new-file', None)],
1998
1831
            target.conflicts())
2066
1899
        self.addCleanup(target.unlock)
2067
1900
        self.assertEqual([], list(target.iter_changes(revision_tree)))
2068
1901
 
2069
 
    def test_build_tree_accelerator_tree_observes_sha1(self):
2070
 
        source = self.create_ab_tree()
2071
 
        sha1 = osutils.sha_string('A')
2072
 
        target = self.make_branch_and_tree('target')
2073
 
        target.lock_write()
2074
 
        self.addCleanup(target.unlock)
2075
 
        state = target.current_dirstate()
2076
 
        state._cutoff_time = time.time() + 60
2077
 
        build_tree(source.basis_tree(), target, source)
2078
 
        entry = state._get_entry(0, path_utf8='file1')
2079
 
        self.assertEqual(sha1, entry[1][0][1])
2080
 
 
2081
1902
    def test_build_tree_accelerator_tree_missing_file(self):
2082
1903
        source = self.create_ab_tree()
2083
1904
        os.unlink('source/file1')
2226
2047
        self.assertEqualStat(source_stat, target_stat)
2227
2048
 
2228
2049
    def test_case_insensitive_build_tree_inventory(self):
2229
 
        if (features.CaseInsensitiveFilesystemFeature.available()
2230
 
            or features.CaseInsCasePresFilenameFeature.available()):
 
2050
        if (tests.CaseInsensitiveFilesystemFeature.available()
 
2051
            or tests.CaseInsCasePresFilenameFeature.available()):
2231
2052
            raise tests.UnavailableFeature('Fully case sensitive filesystem')
2232
2053
        source = self.make_branch_and_tree('source')
2233
2054
        self.build_tree(['source/file', 'source/FILE'])
2241
2062
        self.assertEqual('file.moved', target.id2path('lower-id'))
2242
2063
        self.assertEqual('FILE', target.id2path('upper-id'))
2243
2064
 
2244
 
    def test_build_tree_observes_sha(self):
2245
 
        source = self.make_branch_and_tree('source')
2246
 
        self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2247
 
        source.add(['file1', 'dir', 'dir/file2'],
2248
 
                   ['file1-id', 'dir-id', 'file2-id'])
2249
 
        source.commit('new files')
2250
 
        target = self.make_branch_and_tree('target')
2251
 
        target.lock_write()
2252
 
        self.addCleanup(target.unlock)
2253
 
        # We make use of the fact that DirState caches its cutoff time. So we
2254
 
        # set the 'safe' time to one minute in the future.
2255
 
        state = target.current_dirstate()
2256
 
        state._cutoff_time = time.time() + 60
2257
 
        build_tree(source.basis_tree(), target)
2258
 
        entry1_sha = osutils.sha_file_by_name('source/file1')
2259
 
        entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2260
 
        # entry[1] is the state information, entry[1][0] is the state of the
2261
 
        # working tree, entry[1][0][1] is the sha value for the current working
2262
 
        # tree
2263
 
        entry1 = state._get_entry(0, path_utf8='file1')
2264
 
        self.assertEqual(entry1_sha, entry1[1][0][1])
2265
 
        # The 'size' field must also be set.
2266
 
        self.assertEqual(25, entry1[1][0][2])
2267
 
        entry1_state = entry1[1][0]
2268
 
        entry2 = state._get_entry(0, path_utf8='dir/file2')
2269
 
        self.assertEqual(entry2_sha, entry2[1][0][1])
2270
 
        self.assertEqual(29, entry2[1][0][2])
2271
 
        entry2_state = entry2[1][0]
2272
 
        # Now, make sure that we don't have to re-read the content. The
2273
 
        # packed_stat should match exactly.
2274
 
        self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2275
 
        self.assertEqual(entry2_sha,
2276
 
                         target.get_file_sha1('file2-id', 'dir/file2'))
2277
 
        self.assertEqual(entry1_state, entry1[1][0])
2278
 
        self.assertEqual(entry2_state, entry2[1][0])
2279
 
 
2280
2065
 
2281
2066
class TestCommitTransform(tests.TestCaseWithTransport):
2282
2067
 
2447
2232
        self.build_tree(['a/', 'a/b', 'c/', 'c/d'])
2448
2233
        mover = _FileMover()
2449
2234
        mover.rename('a', 'q')
2450
 
        self.assertPathExists('q')
2451
 
        self.assertPathDoesNotExist('a')
2452
 
        self.assertPathExists('q/b')
2453
 
        self.assertPathExists('c')
2454
 
        self.assertPathExists('c/d')
 
2235
        self.failUnlessExists('q')
 
2236
        self.failIfExists('a')
 
2237
        self.failUnlessExists('q/b')
 
2238
        self.failUnlessExists('c')
 
2239
        self.failUnlessExists('c/d')
2455
2240
 
2456
2241
    def test_pre_delete_rollback(self):
2457
2242
        self.build_tree(['a/'])
2458
2243
        mover = _FileMover()
2459
2244
        mover.pre_delete('a', 'q')
2460
 
        self.assertPathExists('q')
2461
 
        self.assertPathDoesNotExist('a')
 
2245
        self.failUnlessExists('q')
 
2246
        self.failIfExists('a')
2462
2247
        mover.rollback()
2463
 
        self.assertPathDoesNotExist('q')
2464
 
        self.assertPathExists('a')
 
2248
        self.failIfExists('q')
 
2249
        self.failUnlessExists('a')
2465
2250
 
2466
2251
    def test_apply_deletions(self):
2467
2252
        self.build_tree(['a/', 'b/'])
2468
2253
        mover = _FileMover()
2469
2254
        mover.pre_delete('a', 'q')
2470
2255
        mover.pre_delete('b', 'r')
2471
 
        self.assertPathExists('q')
2472
 
        self.assertPathExists('r')
2473
 
        self.assertPathDoesNotExist('a')
2474
 
        self.assertPathDoesNotExist('b')
 
2256
        self.failUnlessExists('q')
 
2257
        self.failUnlessExists('r')
 
2258
        self.failIfExists('a')
 
2259
        self.failIfExists('b')
2475
2260
        mover.apply_deletions()
2476
 
        self.assertPathDoesNotExist('q')
2477
 
        self.assertPathDoesNotExist('r')
2478
 
        self.assertPathDoesNotExist('a')
2479
 
        self.assertPathDoesNotExist('b')
 
2261
        self.failIfExists('q')
 
2262
        self.failIfExists('r')
 
2263
        self.failIfExists('a')
 
2264
        self.failIfExists('b')
2480
2265
 
2481
2266
    def test_file_mover_rollback(self):
2482
2267
        self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
2487
2272
            mover.rename('a', 'c')
2488
2273
        except errors.FileExists, e:
2489
2274
            mover.rollback()
2490
 
        self.assertPathExists('a')
2491
 
        self.assertPathExists('c/d')
 
2275
        self.failUnlessExists('a')
 
2276
        self.failUnlessExists('c/d')
2492
2277
 
2493
2278
 
2494
2279
class Bogus(Exception):
2524
2309
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2525
2310
        self.assertRaises(Bogus, tt.apply,
2526
2311
                          _mover=self.ExceptionFileMover(bad_source='a'))
2527
 
        self.assertPathExists('a')
2528
 
        self.assertPathExists('a/b')
 
2312
        self.failUnlessExists('a')
 
2313
        self.failUnlessExists('a/b')
2529
2314
        tt.apply()
2530
 
        self.assertPathExists('c')
2531
 
        self.assertPathExists('c/d')
 
2315
        self.failUnlessExists('c')
 
2316
        self.failUnlessExists('c/d')
2532
2317
 
2533
2318
    def test_rollback_rename_into_place(self):
2534
2319
        tree = self.make_branch_and_tree('.')
2540
2325
        tt.adjust_path('d', a_id, tt.trans_id_tree_path('a/b'))
2541
2326
        self.assertRaises(Bogus, tt.apply,
2542
2327
                          _mover=self.ExceptionFileMover(bad_target='c/d'))
2543
 
        self.assertPathExists('a')
2544
 
        self.assertPathExists('a/b')
 
2328
        self.failUnlessExists('a')
 
2329
        self.failUnlessExists('a/b')
2545
2330
        tt.apply()
2546
 
        self.assertPathExists('c')
2547
 
        self.assertPathExists('c/d')
 
2331
        self.failUnlessExists('c')
 
2332
        self.failUnlessExists('c/d')
2548
2333
 
2549
2334
    def test_rollback_deletion(self):
2550
2335
        tree = self.make_branch_and_tree('.')
2556
2341
        tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2557
2342
        self.assertRaises(Bogus, tt.apply,
2558
2343
                          _mover=self.ExceptionFileMover(bad_target='d'))
2559
 
        self.assertPathExists('a')
2560
 
        self.assertPathExists('a/b')
2561
 
 
2562
 
 
2563
 
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2564
 
    """Ensure treetransform creation errors can be safely cleaned up after"""
2565
 
 
2566
 
    def _override_globals_in_method(self, instance, method_name, globals):
2567
 
        """Replace method on instance with one with updated globals"""
2568
 
        import types
2569
 
        func = getattr(instance, method_name).im_func
2570
 
        new_globals = dict(func.func_globals)
2571
 
        new_globals.update(globals)
2572
 
        new_func = types.FunctionType(func.func_code, new_globals,
2573
 
            func.func_name, func.func_defaults)
2574
 
        setattr(instance, method_name,
2575
 
            types.MethodType(new_func, instance, instance.__class__))
2576
 
        self.addCleanup(delattr, instance, method_name)
2577
 
 
2578
 
    @staticmethod
2579
 
    def _fake_open_raises_before(name, mode):
2580
 
        """Like open() but raises before doing anything"""
2581
 
        raise RuntimeError
2582
 
 
2583
 
    @staticmethod
2584
 
    def _fake_open_raises_after(name, mode):
2585
 
        """Like open() but raises after creating file without returning"""
2586
 
        open(name, mode).close()
2587
 
        raise RuntimeError
2588
 
 
2589
 
    def create_transform_and_root_trans_id(self):
2590
 
        """Setup a transform creating a file in limbo"""
2591
 
        tree = self.make_branch_and_tree('.')
2592
 
        tt = TreeTransform(tree)
2593
 
        return tt, tt.create_path("a", tt.root)
2594
 
 
2595
 
    def create_transform_and_subdir_trans_id(self):
2596
 
        """Setup a transform creating a directory containing a file in limbo"""
2597
 
        tree = self.make_branch_and_tree('.')
2598
 
        tt = TreeTransform(tree)
2599
 
        d_trans_id = tt.create_path("d", tt.root)
2600
 
        tt.create_directory(d_trans_id)
2601
 
        f_trans_id = tt.create_path("a", d_trans_id)
2602
 
        tt.adjust_path("a", d_trans_id, f_trans_id)
2603
 
        return tt, f_trans_id
2604
 
 
2605
 
    def test_root_create_file_open_raises_before_creation(self):
2606
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2607
 
        self._override_globals_in_method(tt, "create_file",
2608
 
            {"open": self._fake_open_raises_before})
2609
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2610
 
        path = tt._limbo_name(trans_id)
2611
 
        self.assertPathDoesNotExist(path)
2612
 
        tt.finalize()
2613
 
        self.assertPathDoesNotExist(tt._limbodir)
2614
 
 
2615
 
    def test_root_create_file_open_raises_after_creation(self):
2616
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2617
 
        self._override_globals_in_method(tt, "create_file",
2618
 
            {"open": self._fake_open_raises_after})
2619
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2620
 
        path = tt._limbo_name(trans_id)
2621
 
        self.assertPathExists(path)
2622
 
        tt.finalize()
2623
 
        self.assertPathDoesNotExist(path)
2624
 
        self.assertPathDoesNotExist(tt._limbodir)
2625
 
 
2626
 
    def test_subdir_create_file_open_raises_before_creation(self):
2627
 
        tt, trans_id = self.create_transform_and_subdir_trans_id()
2628
 
        self._override_globals_in_method(tt, "create_file",
2629
 
            {"open": self._fake_open_raises_before})
2630
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2631
 
        path = tt._limbo_name(trans_id)
2632
 
        self.assertPathDoesNotExist(path)
2633
 
        tt.finalize()
2634
 
        self.assertPathDoesNotExist(tt._limbodir)
2635
 
 
2636
 
    def test_subdir_create_file_open_raises_after_creation(self):
2637
 
        tt, trans_id = self.create_transform_and_subdir_trans_id()
2638
 
        self._override_globals_in_method(tt, "create_file",
2639
 
            {"open": self._fake_open_raises_after})
2640
 
        self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2641
 
        path = tt._limbo_name(trans_id)
2642
 
        self.assertPathExists(path)
2643
 
        tt.finalize()
2644
 
        self.assertPathDoesNotExist(path)
2645
 
        self.assertPathDoesNotExist(tt._limbodir)
2646
 
 
2647
 
    def test_rename_in_limbo_rename_raises_after_rename(self):
2648
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2649
 
        parent1 = tt.new_directory('parent1', tt.root)
2650
 
        child1 = tt.new_file('child1', parent1, 'contents')
2651
 
        parent2 = tt.new_directory('parent2', tt.root)
2652
 
 
2653
 
        class FakeOSModule(object):
2654
 
            def rename(self, old, new):
2655
 
                os.rename(old, new)
2656
 
                raise RuntimeError
2657
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2658
 
            {"os": FakeOSModule()})
2659
 
        self.assertRaises(
2660
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2661
 
        path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2662
 
        self.assertPathExists(path)
2663
 
        tt.finalize()
2664
 
        self.assertPathDoesNotExist(path)
2665
 
        self.assertPathDoesNotExist(tt._limbodir)
2666
 
 
2667
 
    def test_rename_in_limbo_rename_raises_before_rename(self):
2668
 
        tt, trans_id = self.create_transform_and_root_trans_id()
2669
 
        parent1 = tt.new_directory('parent1', tt.root)
2670
 
        child1 = tt.new_file('child1', parent1, 'contents')
2671
 
        parent2 = tt.new_directory('parent2', tt.root)
2672
 
 
2673
 
        class FakeOSModule(object):
2674
 
            def rename(self, old, new):
2675
 
                raise RuntimeError
2676
 
        self._override_globals_in_method(tt, "_rename_in_limbo",
2677
 
            {"os": FakeOSModule()})
2678
 
        self.assertRaises(
2679
 
            RuntimeError, tt.adjust_path, "child1", parent2, child1)
2680
 
        path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2681
 
        self.assertPathExists(path)
2682
 
        tt.finalize()
2683
 
        self.assertPathDoesNotExist(path)
2684
 
        self.assertPathDoesNotExist(tt._limbodir)
 
2344
        self.failUnlessExists('a')
 
2345
        self.failUnlessExists('a/b')
2685
2346
 
2686
2347
 
2687
2348
class TestTransformMissingParent(tests.TestCaseWithTransport):
2871
2532
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2872
2533
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
2873
2534
 
2874
 
    def test_get_file_size(self):
2875
 
        work_tree = self.make_branch_and_tree('tree')
2876
 
        self.build_tree_contents([('tree/old', 'old')])
2877
 
        work_tree.add('old', 'old-id')
2878
 
        preview = TransformPreview(work_tree)
2879
 
        self.addCleanup(preview.finalize)
2880
 
        new_id = preview.new_file('name', preview.root, 'contents', 'new-id',
2881
 
                                  'executable')
2882
 
        tree = preview.get_preview_tree()
2883
 
        self.assertEqual(len('old'), tree.get_file_size('old-id'))
2884
 
        self.assertEqual(len('contents'), tree.get_file_size('new-id'))
2885
 
 
2886
2535
    def test_get_file(self):
2887
2536
        preview = self.get_empty_preview()
2888
2537
        preview.new_file('file', preview.root, 'contents', 'file-id')
3300
2949
        merger.merge_type = Merge3Merger
3301
2950
        merger.do_merge()
3302
2951
 
3303
 
    def test_has_filename(self):
3304
 
        wt = self.make_branch_and_tree('tree')
3305
 
        self.build_tree(['tree/unmodified', 'tree/removed', 'tree/modified'])
3306
 
        tt = TransformPreview(wt)
3307
 
        removed_id = tt.trans_id_tree_path('removed')
3308
 
        tt.delete_contents(removed_id)
3309
 
        tt.new_file('new', tt.root, 'contents')
3310
 
        modified_id = tt.trans_id_tree_path('modified')
3311
 
        tt.delete_contents(modified_id)
3312
 
        tt.create_file('modified-contents', modified_id)
3313
 
        self.addCleanup(tt.finalize)
3314
 
        tree = tt.get_preview_tree()
3315
 
        self.assertTrue(tree.has_filename('unmodified'))
3316
 
        self.assertFalse(tree.has_filename('not-present'))
3317
 
        self.assertFalse(tree.has_filename('removed'))
3318
 
        self.assertTrue(tree.has_filename('new'))
3319
 
        self.assertTrue(tree.has_filename('modified'))
3320
 
 
3321
2952
    def test_is_executable(self):
3322
2953
        tree = self.make_branch_and_tree('tree')
3323
2954
        preview = TransformPreview(tree)
3346
2977
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
3347
2978
 
3348
2979
    def test_ascii_limbo_paths(self):
3349
 
        self.requireFeature(features.UnicodeFilenameFeature)
 
2980
        self.requireFeature(tests.UnicodeFilenameFeature)
3350
2981
        branch = self.make_branch('any')
3351
2982
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
3352
2983
        tt = TransformPreview(tree)
3369
3000
 
3370
3001
class TestSerializeTransform(tests.TestCaseWithTransport):
3371
3002
 
3372
 
    _test_needs_features = [features.UnicodeFilenameFeature]
 
3003
    _test_needs_features = [tests.UnicodeFilenameFeature]
3373
3004
 
3374
3005
    def get_preview(self, tree=None):
3375
3006
        if tree is None:
3450
3081
        return self.make_records(attribs, contents)
3451
3082
 
3452
3083
    def test_serialize_symlink_creation(self):
3453
 
        self.requireFeature(features.SymlinkFeature)
 
3084
        self.requireFeature(tests.SymlinkFeature)
3454
3085
        tt = self.get_preview()
3455
3086
        tt.new_symlink(u'foo\u1234', tt.root, u'bar\u1234')
3456
3087
        self.assertSerializesTo(self.symlink_creation_records(), tt)
3457
3088
 
3458
3089
    def test_deserialize_symlink_creation(self):
3459
 
        self.requireFeature(features.SymlinkFeature)
 
3090
        self.requireFeature(tests.SymlinkFeature)
3460
3091
        tt = self.get_preview()
3461
3092
        tt.deserialize(iter(self.symlink_creation_records()))
3462
3093
        abspath = tt._limbo_name('new-1')