171
136
transform.finalize()
172
137
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
def test_create_files_same_timestamp(self):
236
transform, root = self.get_transform()
237
self.wt.lock_tree_write()
238
self.addCleanup(self.wt.unlock)
239
# Roll back the clock, so that we know everything is being set to the
241
transform._creation_mtime = creation_mtime = time.time() - 20.0
242
transform.create_file('content-one',
243
transform.create_path('one', root))
244
time.sleep(1) # *ugly*
245
transform.create_file('content-two',
246
transform.create_path('two', root))
248
fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
250
fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
252
# We only guarantee 2s resolution
253
self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
254
"%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
255
# But if we have more than that, all files should get the same result
256
self.assertEqual(st1.st_mtime, st2.st_mtime)
258
def test_change_root_id(self):
259
transform, root = self.get_transform()
260
self.assertNotEqual('new-root-id', self.wt.get_root_id())
261
transform.new_directory('', ROOT_PARENT, 'new-root-id')
262
transform.delete_contents(root)
263
transform.unversion_file(root)
264
transform.fixup_new_roots()
266
self.assertEqual('new-root-id', self.wt.get_root_id())
268
def test_change_root_id_add_files(self):
269
transform, root = self.get_transform()
270
self.assertNotEqual('new-root-id', self.wt.get_root_id())
271
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
272
transform.new_file('file', new_trans_id, ['new-contents\n'],
274
transform.delete_contents(root)
275
transform.unversion_file(root)
276
transform.fixup_new_roots()
278
self.assertEqual('new-root-id', self.wt.get_root_id())
279
self.assertEqual('new-file-id', self.wt.path2id('file'))
280
self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
282
def test_add_two_roots(self):
283
transform, root = self.get_transform()
284
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
285
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
286
self.assertRaises(ValueError, transform.fixup_new_roots)
288
def test_add_unversioned_root(self):
289
transform, root = self.get_transform()
290
new_trans_id = transform.new_directory('', ROOT_PARENT, None)
291
transform.fixup_new_roots()
292
self.assertNotIn(transform.root, transform._new_id)
294
def test_remove_root_fixup(self):
295
transform, root = self.get_transform()
296
old_root_id = self.wt.get_root_id()
297
self.assertNotEqual('new-root-id', old_root_id)
298
transform.delete_contents(root)
299
transform.unversion_file(root)
300
transform.fixup_new_roots()
302
self.assertEqual(old_root_id, self.wt.get_root_id())
304
transform, root = self.get_transform()
305
new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
306
new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
307
self.assertRaises(ValueError, transform.fixup_new_roots)
309
def test_apply_retains_root_directory(self):
310
# Do not attempt to delete the physical root directory, because that
312
transform, root = self.get_transform()
314
transform.delete_contents(root)
315
e = self.assertRaises(AssertionError, self.assertRaises,
316
errors.TransformRenameFailed,
318
self.assertContainsRe('TransformRenameFailed not raised', str(e))
320
139
def test_hardlink(self):
321
140
self.requireFeature(HardlinkFeature)
322
141
transform, root = self.get_transform()
718
525
resolve_conflicts(replace)
721
def _test_symlinks(self, link_name1,link_target1,
722
link_name2, link_target2):
724
def ozpath(p): return 'oz/' + p
528
def test_symlinks(self):
726
529
self.requireFeature(SymlinkFeature)
727
transform, root = self.get_transform()
530
transform,root = self.get_transform()
728
531
oz_id = transform.new_directory('oz', root, 'oz-id')
729
wizard = transform.new_symlink(link_name1, oz_id, link_target1,
532
wizard = transform.new_symlink('wizard', oz_id, 'wizard-target',
731
wiz_id = transform.create_path(link_name2, oz_id)
732
transform.create_symlink(link_target2, wiz_id)
534
wiz_id = transform.create_path('wizard2', oz_id)
535
transform.create_symlink('behind_curtain', wiz_id)
733
536
transform.version_file('wiz-id2', wiz_id)
734
537
transform.set_executability(True, wiz_id)
735
538
self.assertEqual(transform.find_conflicts(),
736
539
[('non-file executability', wiz_id)])
737
540
transform.set_executability(None, wiz_id)
738
541
transform.apply()
739
self.assertEqual(self.wt.path2id(ozpath(link_name1)), 'wizard-id')
740
self.assertEqual('symlink',
741
file_kind(self.wt.abspath(ozpath(link_name1))))
742
self.assertEqual(link_target2,
743
osutils.readlink(self.wt.abspath(ozpath(link_name2))))
744
self.assertEqual(link_target1,
745
osutils.readlink(self.wt.abspath(ozpath(link_name1))))
747
def test_symlinks(self):
748
self._test_symlinks('wizard', 'wizard-target',
749
'wizard2', 'behind_curtain')
751
def test_symlinks_unicode(self):
752
self.requireFeature(tests.UnicodeFilenameFeature)
753
self._test_symlinks(u'\N{Euro Sign}wizard',
754
u'wizard-targ\N{Euro Sign}t',
755
u'\N{Euro Sign}wizard2',
756
u'b\N{Euro Sign}hind_curtain')
542
self.assertEqual(self.wt.path2id('oz/wizard'), 'wizard-id')
543
self.assertEqual(file_kind(self.wt.abspath('oz/wizard')), 'symlink')
544
self.assertEqual(os.readlink(self.wt.abspath('oz/wizard2')),
546
self.assertEqual(os.readlink(self.wt.abspath('oz/wizard')),
758
549
def test_unable_create_symlink(self):
2146
1840
self.assertEqual([], list(target.iter_changes(revision_tree)))
2147
1841
self.assertTrue(source.is_executable('file1-id'))
2149
def install_rot13_content_filter(self, pattern):
2151
# self.addCleanup(filters._reset_registry, filters._reset_registry())
2152
# below, but that looks a bit... hard to read even if it's exactly
2154
original_registry = filters._reset_registry()
2155
def restore_registry():
2156
filters._reset_registry(original_registry)
2157
self.addCleanup(restore_registry)
2158
def rot13(chunks, context=None):
2159
return [''.join(chunks).encode('rot13')]
2160
rot13filter = filters.ContentFilter(rot13, rot13)
2161
filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
2162
os.mkdir(self.test_home_dir + '/.bazaar')
2163
rules_filename = self.test_home_dir + '/.bazaar/rules'
2164
f = open(rules_filename, 'wb')
2165
f.write('[name %s]\nrot13=yes\n' % (pattern,))
2167
def uninstall_rules():
2168
os.remove(rules_filename)
2170
self.addCleanup(uninstall_rules)
2173
def test_build_tree_content_filtered_files_are_not_hardlinked(self):
2174
"""build_tree will not hardlink files that have content filtering rules
2175
applied to them (but will still hardlink other files from the same tree
2178
self.requireFeature(HardlinkFeature)
2179
self.install_rot13_content_filter('file1')
2180
source = self.create_ab_tree()
2181
target = self.make_branch_and_tree('target')
2182
revision_tree = source.basis_tree()
2183
revision_tree.lock_read()
2184
self.addCleanup(revision_tree.unlock)
2185
build_tree(revision_tree, target, source, hardlink=True)
2187
self.addCleanup(target.unlock)
2188
self.assertEqual([], list(target.iter_changes(revision_tree)))
2189
source_stat = os.stat('source/file1')
2190
target_stat = os.stat('target/file1')
2191
self.assertNotEqual(source_stat, target_stat)
2192
source_stat = os.stat('source/file2')
2193
target_stat = os.stat('target/file2')
2194
self.assertEqualStat(source_stat, target_stat)
2196
1843
def test_case_insensitive_build_tree_inventory(self):
2197
if (tests.CaseInsensitiveFilesystemFeature.available()
2198
or tests.CaseInsCasePresFilenameFeature.available()):
2199
raise tests.UnavailableFeature('Fully case sensitive filesystem')
2200
1844
source = self.make_branch_and_tree('source')
2201
1845
self.build_tree(['source/file', 'source/FILE'])
2202
1846
source.add(['file', 'FILE'], ['lower-id', 'upper-id'])
2209
1853
self.assertEqual('file.moved', target.id2path('lower-id'))
2210
1854
self.assertEqual('FILE', target.id2path('upper-id'))
2212
def test_build_tree_observes_sha(self):
2213
source = self.make_branch_and_tree('source')
2214
self.build_tree(['source/file1', 'source/dir/', 'source/dir/file2'])
2215
source.add(['file1', 'dir', 'dir/file2'],
2216
['file1-id', 'dir-id', 'file2-id'])
2217
source.commit('new files')
2218
target = self.make_branch_and_tree('target')
2220
self.addCleanup(target.unlock)
2221
# We make use of the fact that DirState caches its cutoff time. So we
2222
# set the 'safe' time to one minute in the future.
2223
state = target.current_dirstate()
2224
state._cutoff_time = time.time() + 60
2225
build_tree(source.basis_tree(), target)
2226
entry1_sha = osutils.sha_file_by_name('source/file1')
2227
entry2_sha = osutils.sha_file_by_name('source/dir/file2')
2228
# entry[1] is the state information, entry[1][0] is the state of the
2229
# working tree, entry[1][0][1] is the sha value for the current working
2231
entry1 = state._get_entry(0, path_utf8='file1')
2232
self.assertEqual(entry1_sha, entry1[1][0][1])
2233
# The 'size' field must also be set.
2234
self.assertEqual(25, entry1[1][0][2])
2235
entry1_state = entry1[1][0]
2236
entry2 = state._get_entry(0, path_utf8='dir/file2')
2237
self.assertEqual(entry2_sha, entry2[1][0][1])
2238
self.assertEqual(29, entry2[1][0][2])
2239
entry2_state = entry2[1][0]
2240
# Now, make sure that we don't have to re-read the content. The
2241
# packed_stat should match exactly.
2242
self.assertEqual(entry1_sha, target.get_file_sha1('file1-id', 'file1'))
2243
self.assertEqual(entry2_sha,
2244
target.get_file_sha1('file2-id', 'dir/file2'))
2245
self.assertEqual(entry1_state, entry1[1][0])
2246
self.assertEqual(entry2_state, entry2[1][0])
2249
class TestCommitTransform(tests.TestCaseWithTransport):
2251
def get_branch(self):
2252
tree = self.make_branch_and_tree('tree')
2254
self.addCleanup(tree.unlock)
2255
tree.commit('empty commit')
2258
def get_branch_and_transform(self):
2259
branch = self.get_branch()
2260
tt = TransformPreview(branch.basis_tree())
2261
self.addCleanup(tt.finalize)
2264
def test_commit_wrong_basis(self):
2265
branch = self.get_branch()
2266
basis = branch.repository.revision_tree(
2267
_mod_revision.NULL_REVISION)
2268
tt = TransformPreview(basis)
2269
self.addCleanup(tt.finalize)
2270
e = self.assertRaises(ValueError, tt.commit, branch, '')
2271
self.assertEqual('TreeTransform not based on branch basis: null:',
2274
def test_empy_commit(self):
2275
branch, tt = self.get_branch_and_transform()
2276
rev = tt.commit(branch, 'my message')
2277
self.assertEqual(2, branch.revno())
2278
repo = branch.repository
2279
self.assertEqual('my message', repo.get_revision(rev).message)
2281
def test_merge_parents(self):
2282
branch, tt = self.get_branch_and_transform()
2283
rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
2284
self.assertEqual(['rev1b', 'rev1c'],
2285
branch.basis_tree().get_parent_ids()[1:])
2287
def test_first_commit(self):
2288
branch = self.make_branch('branch')
2290
self.addCleanup(branch.unlock)
2291
tt = TransformPreview(branch.basis_tree())
2292
self.addCleanup(tt.finalize)
2293
tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
2294
rev = tt.commit(branch, 'my message')
2295
self.assertEqual([], branch.basis_tree().get_parent_ids())
2296
self.assertNotEqual(_mod_revision.NULL_REVISION,
2297
branch.last_revision())
2299
def test_first_commit_with_merge_parents(self):
2300
branch = self.make_branch('branch')
2302
self.addCleanup(branch.unlock)
2303
tt = TransformPreview(branch.basis_tree())
2304
self.addCleanup(tt.finalize)
2305
e = self.assertRaises(ValueError, tt.commit, branch,
2306
'my message', ['rev1b-id'])
2307
self.assertEqual('Cannot supply merge parents for first commit.',
2309
self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
2311
def test_add_files(self):
2312
branch, tt = self.get_branch_and_transform()
2313
tt.new_file('file', tt.root, 'contents', 'file-id')
2314
trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2315
if SymlinkFeature.available():
2316
tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2317
rev = tt.commit(branch, 'message')
2318
tree = branch.basis_tree()
2319
self.assertEqual('file', tree.id2path('file-id'))
2320
self.assertEqual('contents', tree.get_file_text('file-id'))
2321
self.assertEqual('dir', tree.id2path('dir-id'))
2322
if SymlinkFeature.available():
2323
self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2324
self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2326
def test_add_unversioned(self):
2327
branch, tt = self.get_branch_and_transform()
2328
tt.new_file('file', tt.root, 'contents')
2329
self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
2330
'message', strict=True)
2332
def test_modify_strict(self):
2333
branch, tt = self.get_branch_and_transform()
2334
tt.new_file('file', tt.root, 'contents', 'file-id')
2335
tt.commit(branch, 'message', strict=True)
2336
tt = TransformPreview(branch.basis_tree())
2337
self.addCleanup(tt.finalize)
2338
trans_id = tt.trans_id_file_id('file-id')
2339
tt.delete_contents(trans_id)
2340
tt.create_file('contents', trans_id)
2341
tt.commit(branch, 'message', strict=True)
2343
def test_commit_malformed(self):
2344
"""Committing a malformed transform should raise an exception.
2346
In this case, we are adding a file without adding its parent.
2348
branch, tt = self.get_branch_and_transform()
2349
parent_id = tt.trans_id_file_id('parent-id')
2350
tt.new_file('file', parent_id, 'contents', 'file-id')
2351
self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2354
def test_commit_rich_revision_data(self):
2355
branch, tt = self.get_branch_and_transform()
2356
rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2357
committer='me <me@example.com>',
2358
revprops={'foo': 'bar'}, revision_id='revid-1',
2359
authors=['Author1 <author1@example.com>',
2360
'Author2 <author2@example.com>',
2362
self.assertEqual('revid-1', rev_id)
2363
revision = branch.repository.get_revision(rev_id)
2364
self.assertEqual(1, revision.timestamp)
2365
self.assertEqual(43201, revision.timezone)
2366
self.assertEqual('me <me@example.com>', revision.committer)
2367
self.assertEqual(['Author1 <author1@example.com>',
2368
'Author2 <author2@example.com>'],
2369
revision.get_apparent_authors())
2370
del revision.properties['authors']
2371
self.assertEqual({'foo': 'bar',
2372
'branch-nick': 'tree'},
2373
revision.properties)
2375
def test_no_explicit_revprops(self):
2376
branch, tt = self.get_branch_and_transform()
2377
rev_id = tt.commit(branch, 'message', authors=[
2378
'Author1 <author1@example.com>',
2379
'Author2 <author2@example.com>', ])
2380
revision = branch.repository.get_revision(rev_id)
2381
self.assertEqual(['Author1 <author1@example.com>',
2382
'Author2 <author2@example.com>'],
2383
revision.get_apparent_authors())
2384
self.assertEqual('tree', revision.properties['branch-nick'])
2387
class TestBackupName(tests.TestCase):
2389
def test_deprecations(self):
2390
class MockTransform(object):
2392
def has_named_child(self, by_parent, parent_id, name):
2393
return name in by_parent.get(parent_id, [])
2395
class MockEntry(object):
2398
object.__init__(self)
1857
class MockTransform(object):
1859
def has_named_child(self, by_parent, parent_id, name):
1860
for child_id in by_parent[parent_id]:
1864
elif name == "name.~%s~" % child_id:
1869
class MockEntry(object):
1871
object.__init__(self)
1875
class TestGetBackupName(TestCase):
1876
def test_get_backup_name(self):
2401
1877
tt = MockTransform()
2402
name1 = self.applyDeprecated(
2403
symbol_versioning.deprecated_in((2, 3, 0)),
2404
transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
2405
self.assertEqual('name.~1~', name1)
2406
name2 = self.applyDeprecated(
2407
symbol_versioning.deprecated_in((2, 3, 0)),
2408
transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
2409
self.assertEqual('name.~2~', name2)
1878
name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
1879
self.assertEqual(name, 'name.~1~')
1880
name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
1881
self.assertEqual(name, 'name.~2~')
1882
name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
1883
self.assertEqual(name, 'name.~1~')
1884
name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
1885
self.assertEqual(name, 'name.~1~')
1886
name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
1887
self.assertEqual(name, 'name.~4~')
2412
1890
class TestFileMover(tests.TestCaseWithTransport):
2524
2002
tt.adjust_path('d', tt.root, tt.trans_id_tree_path('a/b'))
2525
2003
self.assertRaises(Bogus, tt.apply,
2526
2004
_mover=self.ExceptionFileMover(bad_target='d'))
2527
self.assertPathExists('a')
2528
self.assertPathExists('a/b')
2531
class TestFinalizeRobustness(tests.TestCaseWithTransport):
2532
"""Ensure treetransform creation errors can be safely cleaned up after"""
2534
def _override_globals_in_method(self, instance, method_name, globals):
2535
"""Replace method on instance with one with updated globals"""
2537
func = getattr(instance, method_name).im_func
2538
new_globals = dict(func.func_globals)
2539
new_globals.update(globals)
2540
new_func = types.FunctionType(func.func_code, new_globals,
2541
func.func_name, func.func_defaults)
2542
setattr(instance, method_name,
2543
types.MethodType(new_func, instance, instance.__class__))
2544
self.addCleanup(delattr, instance, method_name)
2547
def _fake_open_raises_before(name, mode):
2548
"""Like open() but raises before doing anything"""
2552
def _fake_open_raises_after(name, mode):
2553
"""Like open() but raises after creating file without returning"""
2554
open(name, mode).close()
2557
def create_transform_and_root_trans_id(self):
2558
"""Setup a transform creating a file in limbo"""
2559
tree = self.make_branch_and_tree('.')
2560
tt = TreeTransform(tree)
2561
return tt, tt.create_path("a", tt.root)
2563
def create_transform_and_subdir_trans_id(self):
2564
"""Setup a transform creating a directory containing a file in limbo"""
2565
tree = self.make_branch_and_tree('.')
2566
tt = TreeTransform(tree)
2567
d_trans_id = tt.create_path("d", tt.root)
2568
tt.create_directory(d_trans_id)
2569
f_trans_id = tt.create_path("a", d_trans_id)
2570
tt.adjust_path("a", d_trans_id, f_trans_id)
2571
return tt, f_trans_id
2573
def test_root_create_file_open_raises_before_creation(self):
2574
tt, trans_id = self.create_transform_and_root_trans_id()
2575
self._override_globals_in_method(tt, "create_file",
2576
{"open": self._fake_open_raises_before})
2577
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2578
path = tt._limbo_name(trans_id)
2579
self.assertPathDoesNotExist(path)
2581
self.assertPathDoesNotExist(tt._limbodir)
2583
def test_root_create_file_open_raises_after_creation(self):
2584
tt, trans_id = self.create_transform_and_root_trans_id()
2585
self._override_globals_in_method(tt, "create_file",
2586
{"open": self._fake_open_raises_after})
2587
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2588
path = tt._limbo_name(trans_id)
2589
self.assertPathExists(path)
2591
self.assertPathDoesNotExist(path)
2592
self.assertPathDoesNotExist(tt._limbodir)
2594
def test_subdir_create_file_open_raises_before_creation(self):
2595
tt, trans_id = self.create_transform_and_subdir_trans_id()
2596
self._override_globals_in_method(tt, "create_file",
2597
{"open": self._fake_open_raises_before})
2598
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2599
path = tt._limbo_name(trans_id)
2600
self.assertPathDoesNotExist(path)
2602
self.assertPathDoesNotExist(tt._limbodir)
2604
def test_subdir_create_file_open_raises_after_creation(self):
2605
tt, trans_id = self.create_transform_and_subdir_trans_id()
2606
self._override_globals_in_method(tt, "create_file",
2607
{"open": self._fake_open_raises_after})
2608
self.assertRaises(RuntimeError, tt.create_file, ["contents"], trans_id)
2609
path = tt._limbo_name(trans_id)
2610
self.assertPathExists(path)
2612
self.assertPathDoesNotExist(path)
2613
self.assertPathDoesNotExist(tt._limbodir)
2615
def test_rename_in_limbo_rename_raises_after_rename(self):
2616
tt, trans_id = self.create_transform_and_root_trans_id()
2617
parent1 = tt.new_directory('parent1', tt.root)
2618
child1 = tt.new_file('child1', parent1, 'contents')
2619
parent2 = tt.new_directory('parent2', tt.root)
2621
class FakeOSModule(object):
2622
def rename(self, old, new):
2625
self._override_globals_in_method(tt, "_rename_in_limbo",
2626
{"os": FakeOSModule()})
2628
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2629
path = osutils.pathjoin(tt._limbo_name(parent2), "child1")
2630
self.assertPathExists(path)
2632
self.assertPathDoesNotExist(path)
2633
self.assertPathDoesNotExist(tt._limbodir)
2635
def test_rename_in_limbo_rename_raises_before_rename(self):
2636
tt, trans_id = self.create_transform_and_root_trans_id()
2637
parent1 = tt.new_directory('parent1', tt.root)
2638
child1 = tt.new_file('child1', parent1, 'contents')
2639
parent2 = tt.new_directory('parent2', tt.root)
2641
class FakeOSModule(object):
2642
def rename(self, old, new):
2644
self._override_globals_in_method(tt, "_rename_in_limbo",
2645
{"os": FakeOSModule()})
2647
RuntimeError, tt.adjust_path, "child1", parent2, child1)
2648
path = osutils.pathjoin(tt._limbo_name(parent1), "child1")
2649
self.assertPathExists(path)
2651
self.assertPathDoesNotExist(path)
2652
self.assertPathDoesNotExist(tt._limbodir)
2655
class TestTransformMissingParent(tests.TestCaseWithTransport):
2657
def make_tt_with_versioned_dir(self):
2005
self.failUnlessExists('a')
2006
self.failUnlessExists('a/b')
2008
def test_resolve_no_parent(self):
2658
2009
wt = self.make_branch_and_tree('.')
2659
self.build_tree(['dir/',])
2660
wt.add(['dir'], ['dir-id'])
2661
wt.commit('Create dir')
2662
2010
tt = TreeTransform(wt)
2663
2011
self.addCleanup(tt.finalize)
2666
def test_resolve_create_parent_for_versioned_file(self):
2667
wt, tt = self.make_tt_with_versioned_dir()
2668
dir_tid = tt.trans_id_tree_file_id('dir-id')
2669
file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
2670
tt.delete_contents(dir_tid)
2671
tt.unversion_file(dir_tid)
2672
conflicts = resolve_conflicts(tt)
2673
# one conflict for the missing directory, one for the unversioned
2675
self.assertLength(2, conflicts)
2677
def test_non_versioned_file_create_conflict(self):
2678
wt, tt = self.make_tt_with_versioned_dir()
2679
dir_tid = tt.trans_id_tree_file_id('dir-id')
2680
tt.new_file('file', dir_tid, 'Contents')
2681
tt.delete_contents(dir_tid)
2682
tt.unversion_file(dir_tid)
2683
conflicts = resolve_conflicts(tt)
2684
# no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
2685
self.assertLength(1, conflicts)
2686
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
2012
parent = tt.trans_id_file_id('parent-id')
2013
tt.new_file('file', parent, 'Contents')
2014
resolve_conflicts(tt)
2690
2017
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2691
2018
('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2692
2019
(False, False))
2693
2020
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2694
('', ''), ('directory', 'directory'), (False, False))
2021
('', ''), ('directory', 'directory'), (False, None))
2697
2024
class TestTransformPreview(tests.TestCaseWithTransport):
3597
2857
trans_id = tt.trans_id_tree_path('file')
3598
2858
self.assertEqual((LINES_ONE,),
3599
2859
tt._get_parents_texts(trans_id))
3602
class TestOrphan(tests.TestCaseWithTransport):
3604
def test_no_orphan_for_transform_preview(self):
3605
tree = self.make_branch_and_tree('tree')
3606
tt = transform.TransformPreview(tree)
3607
self.addCleanup(tt.finalize)
3608
self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
3610
def _set_orphan_policy(self, wt, policy):
3611
wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
3614
def _prepare_orphan(self, wt):
3615
self.build_tree(['dir/', 'dir/file', 'dir/foo'])
3616
wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
3617
wt.commit('add dir and file ignoring foo')
3618
tt = transform.TreeTransform(wt)
3619
self.addCleanup(tt.finalize)
3620
# dir and bar are deleted
3621
dir_tid = tt.trans_id_tree_path('dir')
3622
file_tid = tt.trans_id_tree_path('dir/file')
3623
orphan_tid = tt.trans_id_tree_path('dir/foo')
3624
tt.delete_contents(file_tid)
3625
tt.unversion_file(file_tid)
3626
tt.delete_contents(dir_tid)
3627
tt.unversion_file(dir_tid)
3628
# There should be a conflict because dir still contain foo
3629
raw_conflicts = tt.find_conflicts()
3630
self.assertLength(1, raw_conflicts)
3631
self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
3632
return tt, orphan_tid
3634
def test_new_orphan_created(self):
3635
wt = self.make_branch_and_tree('.')
3636
self._set_orphan_policy(wt, 'move')
3637
tt, orphan_tid = self._prepare_orphan(wt)
3640
warnings.append(args[0] % args[1:])
3641
self.overrideAttr(trace, 'warning', warning)
3642
remaining_conflicts = resolve_conflicts(tt)
3643
self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
3645
# Yeah for resolved conflicts !
3646
self.assertLength(0, remaining_conflicts)
3647
# We have a new orphan
3648
self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
3649
self.assertEquals('bzr-orphans',
3650
tt.final_name(tt.final_parent(orphan_tid)))
3652
def test_never_orphan(self):
3653
wt = self.make_branch_and_tree('.')
3654
self._set_orphan_policy(wt, 'conflict')
3655
tt, orphan_tid = self._prepare_orphan(wt)
3656
remaining_conflicts = resolve_conflicts(tt)
3657
self.assertLength(1, remaining_conflicts)
3658
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3659
remaining_conflicts.pop())
3661
def test_orphan_error(self):
3662
def bogus_orphan(tt, orphan_id, parent_id):
3663
raise transform.OrphaningError(tt.final_name(orphan_id),
3664
tt.final_name(parent_id))
3665
transform.orphaning_registry.register('bogus', bogus_orphan,
3666
'Raise an error when orphaning')
3667
wt = self.make_branch_and_tree('.')
3668
self._set_orphan_policy(wt, 'bogus')
3669
tt, orphan_tid = self._prepare_orphan(wt)
3670
remaining_conflicts = resolve_conflicts(tt)
3671
self.assertLength(1, remaining_conflicts)
3672
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3673
remaining_conflicts.pop())
3675
def test_unknown_orphan_policy(self):
3676
wt = self.make_branch_and_tree('.')
3677
# Set a fictional policy nobody ever implemented
3678
self._set_orphan_policy(wt, 'donttouchmypreciouuus')
3679
tt, orphan_tid = self._prepare_orphan(wt)
3682
warnings.append(args[0] % args[1:])
3683
self.overrideAttr(trace, 'warning', warning)
3684
remaining_conflicts = resolve_conflicts(tt)
3685
# We fallback to the default policy which create a conflict
3686
self.assertLength(1, remaining_conflicts)
3687
self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
3688
remaining_conflicts.pop())
3689
self.assertLength(1, warnings)
3690
self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')