~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: John Arbash Meinel
  • Date: 2010-11-05 20:54:32 UTC
  • mfrom: (5526 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5527.
  • Revision ID: john@arbash-meinel.com-20101105205432-rmyozu8sthyhmri8
Merge bzr.dev to resolve bzr-2.3.txt (aka NEWS)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    osutils,
28
28
    revision as _mod_revision,
29
29
    rules,
 
30
    symbol_versioning,
30
31
    tests,
 
32
    trace,
 
33
    transform,
31
34
    urlutils,
32
35
    )
33
36
from bzrlib.bzrdir import BzrDir
58
61
)
59
62
from bzrlib.merge import Merge3Merger, Merger
60
63
from bzrlib.tests import (
 
64
    features,
61
65
    HardlinkFeature,
62
66
    SymlinkFeature,
63
67
    TestCase,
814
818
        self.assertIs(None, self.wt.path2id('parent'))
815
819
        self.assertIs(None, self.wt.path2id('parent.new'))
816
820
 
 
821
    def test_resolve_conflicts_missing_parent(self):
 
822
        wt = self.make_branch_and_tree('.')
 
823
        tt = TreeTransform(wt)
 
824
        self.addCleanup(tt.finalize)
 
825
        parent = tt.trans_id_file_id('parent-id')
 
826
        tt.new_file('file', parent, 'Contents')
 
827
        raw_conflicts = resolve_conflicts(tt)
 
828
        # Since the directory doesn't exist it's seen as 'missing'.  So
 
829
        # 'resolve_conflicts' create a conflict asking for it to be created.
 
830
        self.assertLength(1, raw_conflicts)
 
831
        self.assertEqual(('missing parent', 'Created directory', 'new-1'),
 
832
                         raw_conflicts.pop())
 
833
        # apply fail since the missing directory doesn't exist
 
834
        self.assertRaises(errors.NoFinalPath, tt.apply)
 
835
 
817
836
    def test_moving_versioned_directories(self):
818
837
        create, root = self.get_transform()
819
838
        kansas = create.new_directory('kansas', root, 'kansas-id')
853
872
        rename.apply()
854
873
 
855
874
    def test_rename_fails(self):
 
875
        self.requireFeature(features.not_running_as_root)
856
876
        # see https://bugs.launchpad.net/bzr/+bug/491763
857
877
        create, root_id = self.get_transform()
858
878
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
2178
2198
        self.assertEqual('tree', revision.properties['branch-nick'])
2179
2199
 
2180
2200
 
2181
 
class MockTransform(object):
2182
 
 
2183
 
    def has_named_child(self, by_parent, parent_id, name):
2184
 
        for child_id in by_parent[parent_id]:
2185
 
            if child_id == '0':
2186
 
                if name == "name~":
2187
 
                    return True
2188
 
            elif name == "name.~%s~" % child_id:
2189
 
                return True
2190
 
        return False
2191
 
 
2192
 
 
2193
 
class MockEntry(object):
2194
 
    def __init__(self):
2195
 
        object.__init__(self)
2196
 
        self.name = "name"
2197
 
 
2198
 
 
2199
 
class TestGetBackupName(TestCase):
2200
 
    def test_get_backup_name(self):
 
2201
class TestBackupName(tests.TestCase):
 
2202
 
 
2203
    def test_deprecations(self):
 
2204
        class MockTransform(object):
 
2205
 
 
2206
            def has_named_child(self, by_parent, parent_id, name):
 
2207
                return name in by_parent.get(parent_id, [])
 
2208
 
 
2209
        class MockEntry(object):
 
2210
 
 
2211
            def __init__(self):
 
2212
                object.__init__(self)
 
2213
                self.name = "name"
 
2214
 
2201
2215
        tt = MockTransform()
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~')
 
2216
        name1 = self.applyDeprecated(
 
2217
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2218
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
 
2219
        self.assertEqual('name.~1~', name1)
 
2220
        name2 = self.applyDeprecated(
 
2221
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2222
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
 
2223
        self.assertEqual('name.~2~', name2)
2212
2224
 
2213
2225
 
2214
2226
class TestFileMover(tests.TestCaseWithTransport):
2329
2341
        self.failUnlessExists('a')
2330
2342
        self.failUnlessExists('a/b')
2331
2343
 
2332
 
    def test_resolve_no_parent(self):
 
2344
 
 
2345
class TestTransformMissingParent(tests.TestCaseWithTransport):
 
2346
 
 
2347
    def make_tt_with_versioned_dir(self):
2333
2348
        wt = self.make_branch_and_tree('.')
 
2349
        self.build_tree(['dir/',])
 
2350
        wt.add(['dir'], ['dir-id'])
 
2351
        wt.commit('Create dir')
2334
2352
        tt = TreeTransform(wt)
2335
2353
        self.addCleanup(tt.finalize)
2336
 
        parent = tt.trans_id_file_id('parent-id')
2337
 
        tt.new_file('file', parent, 'Contents')
2338
 
        resolve_conflicts(tt)
 
2354
        return wt, tt
 
2355
 
 
2356
    def test_resolve_create_parent_for_versioned_file(self):
 
2357
        wt, tt = self.make_tt_with_versioned_dir()
 
2358
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2359
        file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
 
2360
        tt.delete_contents(dir_tid)
 
2361
        tt.unversion_file(dir_tid)
 
2362
        conflicts = resolve_conflicts(tt)
 
2363
        # one conflict for the missing directory, one for the unversioned
 
2364
        # parent
 
2365
        self.assertLength(2, conflicts)
 
2366
 
 
2367
    def test_non_versioned_file_create_conflict(self):
 
2368
        wt, tt = self.make_tt_with_versioned_dir()
 
2369
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2370
        tt.new_file('file', dir_tid, 'Contents')
 
2371
        tt.delete_contents(dir_tid)
 
2372
        tt.unversion_file(dir_tid)
 
2373
        conflicts = resolve_conflicts(tt)
 
2374
        # no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
 
2375
        self.assertLength(1, conflicts)
 
2376
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
2377
                         conflicts.pop())
2339
2378
 
2340
2379
 
2341
2380
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
3218
3257
        trans_id = tt.trans_id_tree_path('file')
3219
3258
        self.assertEqual((LINES_ONE,),
3220
3259
            tt._get_parents_texts(trans_id))
 
3260
 
 
3261
 
 
3262
class TestOrphan(tests.TestCaseWithTransport):
 
3263
 
 
3264
    def test_no_orphan_for_transform_preview(self):
 
3265
        tree = self.make_branch_and_tree('tree')
 
3266
        tt = transform.TransformPreview(tree)
 
3267
        self.addCleanup(tt.finalize)
 
3268
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
 
3269
 
 
3270
    def _set_orphan_policy(self, wt, policy):
 
3271
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3272
                                               policy)
 
3273
 
 
3274
    def _prepare_orphan(self, wt):
 
3275
        self.build_tree(['dir/', 'dir/foo'])
 
3276
        wt.add(['dir'], ['dir-id'])
 
3277
        wt.commit('add dir')
 
3278
        tt = transform.TreeTransform(wt)
 
3279
        self.addCleanup(tt.finalize)
 
3280
        dir_tid = tt.trans_id_tree_path('dir')
 
3281
        orphan_tid = tt.trans_id_tree_path('dir/foo')
 
3282
        tt.delete_contents(dir_tid)
 
3283
        tt.unversion_file(dir_tid)
 
3284
        raw_conflicts = tt.find_conflicts()
 
3285
        self.assertLength(1, raw_conflicts)
 
3286
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
 
3287
        return tt, orphan_tid
 
3288
 
 
3289
    def test_new_orphan_created(self):
 
3290
        wt = self.make_branch_and_tree('.')
 
3291
        self._set_orphan_policy(wt, 'move')
 
3292
        tt, orphan_tid = self._prepare_orphan(wt)
 
3293
        remaining_conflicts = resolve_conflicts(tt)
 
3294
        # Yeah for resolved conflicts !
 
3295
        self.assertLength(0, remaining_conflicts)
 
3296
        # We have a new orphan
 
3297
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
 
3298
        self.assertEquals('bzr-orphans',
 
3299
                          tt.final_name(tt.final_parent(orphan_tid)))
 
3300
 
 
3301
    def test_never_orphan(self):
 
3302
        wt = self.make_branch_and_tree('.')
 
3303
        self._set_orphan_policy(wt, 'conflict')
 
3304
        tt, orphan_tid = self._prepare_orphan(wt)
 
3305
        remaining_conflicts = resolve_conflicts(tt)
 
3306
        self.assertLength(1, remaining_conflicts)
 
3307
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3308
                         remaining_conflicts.pop())
 
3309
 
 
3310
    def test_orphan_error(self):
 
3311
        def bogus_orphan(tt, orphan_id, parent_id):
 
3312
            raise transform.OrphaningError(tt.final_name(orphan_id),
 
3313
                                           tt.final_name(parent_id))
 
3314
        transform.orphaning_registry.register('bogus', bogus_orphan,
 
3315
                                              'Raise an error when orphaning')
 
3316
        wt = self.make_branch_and_tree('.')
 
3317
        self._set_orphan_policy(wt, 'bogus')
 
3318
        tt, orphan_tid = self._prepare_orphan(wt)
 
3319
        remaining_conflicts = resolve_conflicts(tt)
 
3320
        self.assertLength(1, remaining_conflicts)
 
3321
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3322
                         remaining_conflicts.pop())
 
3323
 
 
3324
    def test_unknown_orphan_policy(self):
 
3325
        wt = self.make_branch_and_tree('.')
 
3326
        # Set a fictional policy nobody ever implemented
 
3327
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
 
3328
        tt, orphan_tid = self._prepare_orphan(wt)
 
3329
        warnings = []
 
3330
        def warning(*args):
 
3331
            warnings.append(args[0] % args[1:])
 
3332
        self.overrideAttr(trace, 'warning', warning)
 
3333
        remaining_conflicts = resolve_conflicts(tt)
 
3334
        # We fallback to the default policy which create a conflict
 
3335
        self.assertLength(1, remaining_conflicts)
 
3336
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3337
                         remaining_conflicts.pop())
 
3338
        self.assertLength(1, warnings)
 
3339
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')