123
135
imaginary_id = transform.trans_id_tree_path('imaginary')
124
136
imaginary_id2 = transform.trans_id_tree_path('imaginary/')
125
137
self.assertEqual(imaginary_id, imaginary_id2)
126
self.assertEqual(transform.get_tree_parent(imaginary_id), root)
127
self.assertEqual(transform.final_kind(root), 'directory')
128
self.assertEqual(transform.final_file_id(root), self.wt.get_root_id())
138
self.assertEqual(root, transform.get_tree_parent(imaginary_id))
139
self.assertEqual('directory', transform.final_kind(root))
140
self.assertEqual(self.wt.get_root_id(), transform.final_file_id(root))
129
141
trans_id = transform.create_path('name', root)
130
142
self.assertIs(transform.final_file_id(trans_id), None)
131
self.assertRaises(NoSuchFile, transform.final_kind, trans_id)
143
self.assertIs(None, transform.final_kind(trans_id))
132
144
transform.create_file('contents', trans_id)
133
145
transform.set_executability(True, trans_id)
134
146
transform.version_file('my_pretties', trans_id)
159
171
transform.finalize()
160
172
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)
162
235
def test_create_files_same_timestamp(self):
163
236
transform, root = self.get_transform()
164
237
self.wt.lock_tree_write()
814
887
self.assertIs(None, self.wt.path2id('parent'))
815
888
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)
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')
852
940
rename.set_executability(True, myfile)
943
def test_rename_fails(self):
944
self.requireFeature(features.not_running_as_root)
945
# see https://bugs.launchpad.net/bzr/+bug/491763
946
create, root_id = self.get_transform()
947
first_dir = create.new_directory('first-dir', root_id, 'first-id')
948
myfile = create.new_file('myfile', root_id, 'myfile-text',
951
if os.name == "posix" and sys.platform != "cygwin":
952
# posix filesystems fail on renaming if the readonly bit is set
953
osutils.make_readonly(self.wt.abspath('first-dir'))
954
elif os.name == "nt":
955
# windows filesystems fail on renaming open files
956
self.addCleanup(file(self.wt.abspath('myfile')).close)
958
self.skip("Don't know how to force a permissions error on rename")
959
# now transform to rename
960
rename_transform, root_id = self.get_transform()
961
file_trans_id = rename_transform.trans_id_file_id('myfile-id')
962
dir_id = rename_transform.trans_id_file_id('first-id')
963
rename_transform.adjust_path('newname', dir_id, file_trans_id)
964
e = self.assertRaises(errors.TransformRenameFailed,
965
rename_transform.apply)
967
# "Failed to rename .../work/.bzr/checkout/limbo/new-1
968
# to .../first-dir/newname: [Errno 13] Permission denied"
969
# On windows looks like:
970
# "Failed to rename .../work/myfile to
971
# .../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")
855
981
def test_set_executability_order(self):
856
982
"""Ensure that executability behaves the same, no matter what order.
2146
2272
self.assertEqual('tree', revision.properties['branch-nick'])
2149
class MockTransform(object):
2151
def has_named_child(self, by_parent, parent_id, name):
2152
for child_id in by_parent[parent_id]:
2156
elif name == "name.~%s~" % child_id:
2161
class MockEntry(object):
2163
object.__init__(self)
2167
class TestGetBackupName(TestCase):
2168
def test_get_backup_name(self):
2275
class TestBackupName(tests.TestCase):
2277
def test_deprecations(self):
2278
class MockTransform(object):
2280
def has_named_child(self, by_parent, parent_id, name):
2281
return name in by_parent.get(parent_id, [])
2283
class MockEntry(object):
2286
object.__init__(self)
2169
2289
tt = MockTransform()
2170
name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
2171
self.assertEqual(name, 'name.~1~')
2172
name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2173
self.assertEqual(name, 'name.~2~')
2174
name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2175
self.assertEqual(name, 'name.~1~')
2176
name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2177
self.assertEqual(name, 'name.~1~')
2178
name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2179
self.assertEqual(name, 'name.~4~')
2290
name1 = self.applyDeprecated(
2291
symbol_versioning.deprecated_in((2, 3, 0)),
2292
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2293
self.assertEqual('name.~1~', name1)
2294
name2 = self.applyDeprecated(
2295
symbol_versioning.deprecated_in((2, 3, 0)),
2296
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2297
self.assertEqual('name.~2~', name2)
2182
2300
class TestFileMover(tests.TestCaseWithTransport):
2297
2415
self.failUnlessExists('a')
2298
2416
self.failUnlessExists('a/b')
2300
def test_resolve_no_parent(self):
2419
class TestTransformMissingParent(tests.TestCaseWithTransport):
2421
def make_tt_with_versioned_dir(self):
2301
2422
wt = self.make_branch_and_tree('.')
2423
self.build_tree(['dir/',])
2424
wt.add(['dir'], ['dir-id'])
2425
wt.commit('Create dir')
2302
2426
tt = TreeTransform(wt)
2303
2427
self.addCleanup(tt.finalize)
2304
parent = tt.trans_id_file_id('parent-id')
2305
tt.new_file('file', parent, 'Contents')
2306
resolve_conflicts(tt)
2430
def test_resolve_create_parent_for_versioned_file(self):
2431
wt, tt = self.make_tt_with_versioned_dir()
2432
dir_tid = tt.trans_id_tree_file_id('dir-id')
2433
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2434
tt.delete_contents(dir_tid)
2435
tt.unversion_file(dir_tid)
2436
conflicts = resolve_conflicts(tt)
2437
# one conflict for the missing directory, one for the unversioned
2439
self.assertLength(2, conflicts)
2441
def test_non_versioned_file_create_conflict(self):
2442
wt, tt = self.make_tt_with_versioned_dir()
2443
dir_tid = tt.trans_id_tree_file_id('dir-id')
2444
tt.new_file('file', dir_tid, 'Contents')
2445
tt.delete_contents(dir_tid)
2446
tt.unversion_file(dir_tid)
2447
conflicts = resolve_conflicts(tt)
2448
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2449
self.assertLength(1, conflicts)
2450
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2309
2454
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2310
2455
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2311
2456
(False, False))
2312
2457
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2313
('', ''), ('directory', 'directory'), (False, None))
2458
('', ''), ('directory', 'directory'), (False, False))
2316
2461
class TestTransformPreview(tests.TestCaseWithTransport):
3186
3331
trans_id = tt.trans_id_tree_path('file')
3187
3332
self.assertEqual((LINES_ONE,),
3188
3333
tt._get_parents_texts(trans_id))
3336
class TestOrphan(tests.TestCaseWithTransport):
3338
def test_no_orphan_for_transform_preview(self):
3339
tree = self.make_branch_and_tree('tree')
3340
tt = transform.TransformPreview(tree)
3341
self.addCleanup(tt.finalize)
3342
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3344
def _set_orphan_policy(self, wt, policy):
3345
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3348
def _prepare_orphan(self, wt):
3349
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3350
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3351
wt.commit('add dir and file ignoring foo')
3352
tt = transform.TreeTransform(wt)
3353
self.addCleanup(tt.finalize)
3354
# dir and bar are deleted
3355
dir_tid = tt.trans_id_tree_path('dir')
3356
file_tid = tt.trans_id_tree_path('dir/file')
3357
orphan_tid = tt.trans_id_tree_path('dir/foo')
3358
tt.delete_contents(file_tid)
3359
tt.unversion_file(file_tid)
3360
tt.delete_contents(dir_tid)
3361
tt.unversion_file(dir_tid)
3362
# There should be a conflict because dir still contain foo
3363
raw_conflicts = tt.find_conflicts()
3364
self.assertLength(1, raw_conflicts)
3365
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3366
return tt, orphan_tid
3368
def test_new_orphan_created(self):
3369
wt = self.make_branch_and_tree('.')
3370
self._set_orphan_policy(wt, 'move')
3371
tt, orphan_tid = self._prepare_orphan(wt)
3374
warnings.append(args[0] % args[1:])
3375
self.overrideAttr(trace, 'warning', warning)
3376
remaining_conflicts = resolve_conflicts(tt)
3377
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3379
# Yeah for resolved conflicts !
3380
self.assertLength(0, remaining_conflicts)
3381
# We have a new orphan
3382
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3383
self.assertEquals('bzr-orphans',
3384
tt.final_name(tt.final_parent(orphan_tid)))
3386
def test_never_orphan(self):
3387
wt = self.make_branch_and_tree('.')
3388
self._set_orphan_policy(wt, 'conflict')
3389
tt, orphan_tid = self._prepare_orphan(wt)
3390
remaining_conflicts = resolve_conflicts(tt)
3391
self.assertLength(1, remaining_conflicts)
3392
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3393
remaining_conflicts.pop())
3395
def test_orphan_error(self):
3396
def bogus_orphan(tt, orphan_id, parent_id):
3397
raise transform.OrphaningError(tt.final_name(orphan_id),
3398
tt.final_name(parent_id))
3399
transform.orphaning_registry.register('bogus', bogus_orphan,
3400
'Raise an error when orphaning')
3401
wt = self.make_branch_and_tree('.')
3402
self._set_orphan_policy(wt, 'bogus')
3403
tt, orphan_tid = self._prepare_orphan(wt)
3404
remaining_conflicts = resolve_conflicts(tt)
3405
self.assertLength(1, remaining_conflicts)
3406
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3407
remaining_conflicts.pop())
3409
def test_unknown_orphan_policy(self):
3410
wt = self.make_branch_and_tree('.')
3411
# Set a fictional policy nobody ever implemented
3412
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3413
tt, orphan_tid = self._prepare_orphan(wt)
3416
warnings.append(args[0] % args[1:])
3417
self.overrideAttr(trace, 'warning', warning)
3418
remaining_conflicts = resolve_conflicts(tt)
3419
# We fallback to the default policy which create a conflict
3420
self.assertLength(1, remaining_conflicts)
3421
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3422
remaining_conflicts.pop())
3423
self.assertLength(1, warnings)
3424
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')