171
159
transform.finalize()
172
160
transform.finalize()
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',
179
orig = self.wt._observed_sha1
180
def _observed_sha1(*args):
183
self.wt._observed_sha1 = _observed_sha1
185
self.assertEqual([(None, 'file1', trans._observed_sha1s[trans_id])],
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)
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))
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)
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',
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)
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',
231
self.assertTrue(trans_id in trans._observed_sha1s)
232
trans.cancel_creation(trans_id)
233
self.assertFalse(trans_id in trans._observed_sha1s)
235
162
def test_create_files_same_timestamp(self):
236
163
transform, root = self.get_transform()
237
164
self.wt.lock_tree_write()
887
814
self.assertIs(None, self.wt.path2id('parent'))
888
815
self.assertIs(None, self.wt.path2id('parent.new'))
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'),
902
# apply fail since the missing directory doesn't exist
903
self.assertRaises(errors.NoFinalPath, tt.apply)
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')
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")
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)")
981
887
def test_set_executability_order(self):
982
888
"""Ensure that executability behaves the same, no matter what order.
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')
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')
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())
1970
1876
self.addCleanup(target.unlock)
1971
1877
self.assertEqual([], list(target.iter_changes(revision_tree)))
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')
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])
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'))
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')
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
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])
2185
2043
class TestCommitTransform(tests.TestCaseWithTransport):
2320
2178
self.assertEqual('tree', revision.properties['branch-nick'])
2323
class TestBackupName(tests.TestCase):
2325
def test_deprecations(self):
2326
class MockTransform(object):
2328
def has_named_child(self, by_parent, parent_id, name):
2329
return name in by_parent.get(parent_id, [])
2331
class MockEntry(object):
2334
object.__init__(self)
2181
class MockTransform(object):
2183
def has_named_child(self, by_parent, parent_id, name):
2184
for child_id in by_parent[parent_id]:
2188
elif name == "name.~%s~" % child_id:
2193
class MockEntry(object):
2195
object.__init__(self)
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~')
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')
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')
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')
2385
2251
def test_file_mover_rollback(self):
2386
2252
self.build_tree(['a/', 'a/b', 'c/', 'c/d/', 'c/e/'])
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')
2467
class TestTransformMissingParent(tests.TestCaseWithTransport):
2469
def make_tt_with_versioned_dir(self):
2329
self.failUnlessExists('a')
2330
self.failUnlessExists('a/b')
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)
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
2487
self.assertLength(2, conflicts)
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'),
2336
parent = tt.trans_id_file_id('parent-id')
2337
tt.new_file('file', parent, 'Contents')
2338
resolve_conflicts(tt)
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))
2509
2348
class TestTransformPreview(tests.TestCaseWithTransport):
3379
3218
trans_id = tt.trans_id_tree_path('file')
3380
3219
self.assertEqual((LINES_ONE,),
3381
3220
tt._get_parents_texts(trans_id))
3384
class TestOrphan(tests.TestCaseWithTransport):
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')
3392
def _set_orphan_policy(self, wt, policy):
3393
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
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
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)
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'],
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)))
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())
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())
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)
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')