~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

(gz) Backslash escape selftest output when printing to non-unicode consoles
 (Martin [gz])

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
    filters,
26
26
    generate_ids,
27
27
    osutils,
28
 
    progress,
29
28
    revision as _mod_revision,
30
29
    rules,
 
30
    symbol_versioning,
31
31
    tests,
 
32
    trace,
 
33
    transform,
32
34
    urlutils,
33
35
    )
34
36
from bzrlib.bzrdir import BzrDir
35
 
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
36
 
                              UnversionedParent, ParentLoop, DeletingParent,
37
 
                              NonDirectoryParent)
 
37
from bzrlib.conflicts import (
 
38
    DeletingParent,
 
39
    DuplicateEntry,
 
40
    DuplicateID,
 
41
    MissingParent,
 
42
    NonDirectoryParent,
 
43
    ParentLoop,
 
44
    UnversionedParent,
 
45
)
38
46
from bzrlib.diff import show_diff_trees
39
 
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
40
 
                           ReusingTransform, CantMoveRoot,
41
 
                           PathsNotVersionedError, ExistingLimbo,
42
 
                           ExistingPendingDeletion, ImmortalLimbo,
43
 
                           ImmortalPendingDeletion, LockError)
44
 
from bzrlib.osutils import file_kind, pathjoin
 
47
from bzrlib.errors import (
 
48
    DuplicateKey,
 
49
    ExistingLimbo,
 
50
    ExistingPendingDeletion,
 
51
    ImmortalLimbo,
 
52
    ImmortalPendingDeletion,
 
53
    LockError,
 
54
    MalformedTransform,
 
55
    NoSuchFile,
 
56
    ReusingTransform,
 
57
)
 
58
from bzrlib.osutils import (
 
59
    file_kind,
 
60
    pathjoin,
 
61
)
45
62
from bzrlib.merge import Merge3Merger, Merger
46
63
from bzrlib.tests import (
 
64
    features,
47
65
    HardlinkFeature,
48
66
    SymlinkFeature,
49
67
    TestCase,
50
68
    TestCaseInTempDir,
51
69
    TestSkipped,
52
 
    )
53
 
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths,
54
 
                              resolve_conflicts, cook_conflicts,
55
 
                              build_tree, get_backup_name,
56
 
                              _FileMover, resolve_checkout,
57
 
                              TransformPreview, create_from_tree)
 
70
)
 
71
from bzrlib.transform import (
 
72
    build_tree,
 
73
    create_from_tree,
 
74
    cook_conflicts,
 
75
    _FileMover,
 
76
    FinalPaths,
 
77
    get_backup_name,
 
78
    resolve_conflicts,
 
79
    resolve_checkout,
 
80
    ROOT_PARENT,
 
81
    TransformPreview,
 
82
    TreeTransform,
 
83
)
58
84
 
59
85
 
60
86
class TestTreeTransform(tests.TestCaseWithTransport):
101
127
        imaginary_id = transform.trans_id_tree_path('imaginary')
102
128
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
103
129
        self.assertEqual(imaginary_id, imaginary_id2)
104
 
        self.assertEqual(transform.get_tree_parent(imaginary_id), root)
105
 
        self.assertEqual(transform.final_kind(root), 'directory')
106
 
        self.assertEqual(transform.final_file_id(root), self.wt.get_root_id())
 
130
        self.assertEqual(root, transform.get_tree_parent(imaginary_id))
 
131
        self.assertEqual('directory', transform.final_kind(root))
 
132
        self.assertEqual(self.wt.get_root_id(), transform.final_file_id(root))
107
133
        trans_id = transform.create_path('name', root)
108
134
        self.assertIs(transform.final_file_id(trans_id), None)
109
 
        self.assertRaises(NoSuchFile, transform.final_kind, trans_id)
 
135
        self.assertIs(None, transform.final_kind(trans_id))
110
136
        transform.create_file('contents', trans_id)
111
137
        transform.set_executability(True, trans_id)
112
138
        transform.version_file('my_pretties', trans_id)
735
761
                                         ' versioned, but has versioned'
736
762
                                         ' children.  Versioned directory.')
737
763
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
738
 
                                         ' oz/emeraldcity.  Cancelled move.')
 
764
                                         ' oz/emeraldcity. Cancelled move.')
739
765
 
740
766
    def prepare_wrong_parent_kind(self):
741
767
        tt, root = self.get_transform()
792
818
        self.assertIs(None, self.wt.path2id('parent'))
793
819
        self.assertIs(None, self.wt.path2id('parent.new'))
794
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
 
795
836
    def test_moving_versioned_directories(self):
796
837
        create, root = self.get_transform()
797
838
        kansas = create.new_directory('kansas', root, 'kansas-id')
830
871
        rename.set_executability(True, myfile)
831
872
        rename.apply()
832
873
 
 
874
    def test_rename_fails(self):
 
875
        self.requireFeature(features.not_running_as_root)
 
876
        # see https://bugs.launchpad.net/bzr/+bug/491763
 
877
        create, root_id = self.get_transform()
 
878
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
 
879
        myfile = create.new_file('myfile', root_id, 'myfile-text',
 
880
                                 'myfile-id')
 
881
        create.apply()
 
882
        if os.name == "posix" and sys.platform != "cygwin":
 
883
            # posix filesystems fail on renaming if the readonly bit is set
 
884
            osutils.make_readonly(self.wt.abspath('first-dir'))
 
885
        elif os.name == "nt":
 
886
            # windows filesystems fail on renaming open files
 
887
            self.addCleanup(file(self.wt.abspath('myfile')).close)
 
888
        else:
 
889
            self.skip("Don't know how to force a permissions error on rename")
 
890
        # now transform to rename
 
891
        rename_transform, root_id = self.get_transform()
 
892
        file_trans_id = rename_transform.trans_id_file_id('myfile-id')
 
893
        dir_id = rename_transform.trans_id_file_id('first-id')
 
894
        rename_transform.adjust_path('newname', dir_id, file_trans_id)
 
895
        e = self.assertRaises(errors.TransformRenameFailed,
 
896
            rename_transform.apply)
 
897
        # On nix looks like: 
 
898
        # "Failed to rename .../work/.bzr/checkout/limbo/new-1
 
899
        # to .../first-dir/newname: [Errno 13] Permission denied"
 
900
        # On windows looks like:
 
901
        # "Failed to rename .../work/myfile to 
 
902
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
 
903
        # The strerror will vary per OS and language so it's not checked here
 
904
        self.assertContainsRe(str(e),
 
905
            "Failed to rename .*(first-dir.newname:|myfile)")
 
906
 
833
907
    def test_set_executability_order(self):
834
908
        """Ensure that executability behaves the same, no matter what order.
835
909
 
2091
2165
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2092
2166
                          'message')
2093
2167
 
2094
 
 
2095
 
class MockTransform(object):
2096
 
 
2097
 
    def has_named_child(self, by_parent, parent_id, name):
2098
 
        for child_id in by_parent[parent_id]:
2099
 
            if child_id == '0':
2100
 
                if name == "name~":
2101
 
                    return True
2102
 
            elif name == "name.~%s~" % child_id:
2103
 
                return True
2104
 
        return False
2105
 
 
2106
 
 
2107
 
class MockEntry(object):
2108
 
    def __init__(self):
2109
 
        object.__init__(self)
2110
 
        self.name = "name"
2111
 
 
2112
 
 
2113
 
class TestGetBackupName(TestCase):
2114
 
    def test_get_backup_name(self):
 
2168
    def test_commit_rich_revision_data(self):
 
2169
        branch, tt = self.get_branch_and_transform()
 
2170
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
 
2171
                           committer='me <me@example.com>',
 
2172
                           revprops={'foo': 'bar'}, revision_id='revid-1',
 
2173
                           authors=['Author1 <author1@example.com>',
 
2174
                              'Author2 <author2@example.com>',
 
2175
                               ])
 
2176
        self.assertEqual('revid-1', rev_id)
 
2177
        revision = branch.repository.get_revision(rev_id)
 
2178
        self.assertEqual(1, revision.timestamp)
 
2179
        self.assertEqual(43201, revision.timezone)
 
2180
        self.assertEqual('me <me@example.com>', revision.committer)
 
2181
        self.assertEqual(['Author1 <author1@example.com>',
 
2182
                          'Author2 <author2@example.com>'],
 
2183
                         revision.get_apparent_authors())
 
2184
        del revision.properties['authors']
 
2185
        self.assertEqual({'foo': 'bar',
 
2186
                          'branch-nick': 'tree'},
 
2187
                         revision.properties)
 
2188
 
 
2189
    def test_no_explicit_revprops(self):
 
2190
        branch, tt = self.get_branch_and_transform()
 
2191
        rev_id = tt.commit(branch, 'message', authors=[
 
2192
            'Author1 <author1@example.com>',
 
2193
            'Author2 <author2@example.com>', ])
 
2194
        revision = branch.repository.get_revision(rev_id)
 
2195
        self.assertEqual(['Author1 <author1@example.com>',
 
2196
                          'Author2 <author2@example.com>'],
 
2197
                         revision.get_apparent_authors())
 
2198
        self.assertEqual('tree', revision.properties['branch-nick'])
 
2199
 
 
2200
 
 
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
 
2115
2215
        tt = MockTransform()
2116
 
        name = get_backup_name(MockEntry(), {'a':[]}, 'a', tt)
2117
 
        self.assertEqual(name, 'name.~1~')
2118
 
        name = get_backup_name(MockEntry(), {'a':['1']}, 'a', tt)
2119
 
        self.assertEqual(name, 'name.~2~')
2120
 
        name = get_backup_name(MockEntry(), {'a':['2']}, 'a', tt)
2121
 
        self.assertEqual(name, 'name.~1~')
2122
 
        name = get_backup_name(MockEntry(), {'a':['2'], 'b':[]}, 'b', tt)
2123
 
        self.assertEqual(name, 'name.~1~')
2124
 
        name = get_backup_name(MockEntry(), {'a':['1', '2', '3']}, 'a', tt)
2125
 
        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)
2126
2224
 
2127
2225
 
2128
2226
class TestFileMover(tests.TestCaseWithTransport):
2243
2341
        self.failUnlessExists('a')
2244
2342
        self.failUnlessExists('a/b')
2245
2343
 
2246
 
    def test_resolve_no_parent(self):
 
2344
 
 
2345
class TestTransformMissingParent(tests.TestCaseWithTransport):
 
2346
 
 
2347
    def make_tt_with_versioned_dir(self):
2247
2348
        wt = self.make_branch_and_tree('.')
 
2349
        self.build_tree(['dir/',])
 
2350
        wt.add(['dir'], ['dir-id'])
 
2351
        wt.commit('Create dir')
2248
2352
        tt = TreeTransform(wt)
2249
2353
        self.addCleanup(tt.finalize)
2250
 
        parent = tt.trans_id_file_id('parent-id')
2251
 
        tt.new_file('file', parent, 'Contents')
2252
 
        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())
2253
2378
 
2254
2379
 
2255
2380
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2371
2496
    def test_ignore_pb(self):
2372
2497
        # pb could be supported, but TT.iter_changes doesn't support it.
2373
2498
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2374
 
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
 
2499
        preview_tree.iter_changes(revision_tree)
2375
2500
 
2376
2501
    def test_kind(self):
2377
2502
        revision_tree = self.create_tree()
2779
2904
        file_trans_id = preview.trans_id_file_id('file-id')
2780
2905
        preview.delete_contents(file_trans_id)
2781
2906
        preview.create_file('a\nb\n', file_trans_id)
2782
 
        pb = progress.DummyProgress()
2783
2907
        preview_tree = preview.get_preview_tree()
2784
 
        merger = Merger.from_revision_ids(pb, preview_tree,
 
2908
        merger = Merger.from_revision_ids(None, preview_tree,
2785
2909
                                          child_tree.branch.last_revision(),
2786
2910
                                          other_branch=child_tree.branch,
2787
2911
                                          tree_branch=work_tree.branch)
2799
2923
        tt.new_file('name', tt.root, 'content', 'file-id')
2800
2924
        tree2 = self.make_branch_and_tree('tree2')
2801
2925
        tree2.set_root_id('TREE_ROOT')
2802
 
        pb = progress.DummyProgress()
2803
2926
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2804
 
                                         pb, tree.basis_tree())
 
2927
                                         None, tree.basis_tree())
2805
2928
        merger.merge_type = Merge3Merger
2806
2929
        merger.do_merge()
2807
2930
 
2817
2940
        tt.create_file('baz', trans_id)
2818
2941
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2819
2942
        self.build_tree_contents([('tree2/foo', 'qux')])
2820
 
        pb = progress.DummyProgress()
 
2943
        pb = None
2821
2944
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2822
2945
                                         pb, tree.basis_tree())
2823
2946
        merger.merge_type = Merge3Merger
3134
3257
        trans_id = tt.trans_id_tree_path('file')
3135
3258
        self.assertEqual((LINES_ONE,),
3136
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/file', 'dir/foo'])
 
3276
        wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
3277
        wt.commit('add dir and file ignoring foo')
 
3278
        tt = transform.TreeTransform(wt)
 
3279
        self.addCleanup(tt.finalize)
 
3280
        # dir and bar are deleted
 
3281
        dir_tid = tt.trans_id_tree_path('dir')
 
3282
        file_tid = tt.trans_id_tree_path('dir/file')
 
3283
        orphan_tid = tt.trans_id_tree_path('dir/foo')
 
3284
        tt.delete_contents(file_tid)
 
3285
        tt.unversion_file(file_tid)
 
3286
        tt.delete_contents(dir_tid)
 
3287
        tt.unversion_file(dir_tid)
 
3288
        # There should be a conflict because dir still contain foo
 
3289
        raw_conflicts = tt.find_conflicts()
 
3290
        self.assertLength(1, raw_conflicts)
 
3291
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
 
3292
        return tt, orphan_tid
 
3293
 
 
3294
    def test_new_orphan_created(self):
 
3295
        wt = self.make_branch_and_tree('.')
 
3296
        self._set_orphan_policy(wt, 'move')
 
3297
        tt, orphan_tid = self._prepare_orphan(wt)
 
3298
        warnings = []
 
3299
        def warning(*args):
 
3300
            warnings.append(args[0] % args[1:])
 
3301
        self.overrideAttr(trace, 'warning', warning)
 
3302
        remaining_conflicts = resolve_conflicts(tt)
 
3303
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3304
                          warnings)
 
3305
        # Yeah for resolved conflicts !
 
3306
        self.assertLength(0, remaining_conflicts)
 
3307
        # We have a new orphan
 
3308
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
 
3309
        self.assertEquals('bzr-orphans',
 
3310
                          tt.final_name(tt.final_parent(orphan_tid)))
 
3311
 
 
3312
    def test_never_orphan(self):
 
3313
        wt = self.make_branch_and_tree('.')
 
3314
        self._set_orphan_policy(wt, 'conflict')
 
3315
        tt, orphan_tid = self._prepare_orphan(wt)
 
3316
        remaining_conflicts = resolve_conflicts(tt)
 
3317
        self.assertLength(1, remaining_conflicts)
 
3318
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3319
                         remaining_conflicts.pop())
 
3320
 
 
3321
    def test_orphan_error(self):
 
3322
        def bogus_orphan(tt, orphan_id, parent_id):
 
3323
            raise transform.OrphaningError(tt.final_name(orphan_id),
 
3324
                                           tt.final_name(parent_id))
 
3325
        transform.orphaning_registry.register('bogus', bogus_orphan,
 
3326
                                              'Raise an error when orphaning')
 
3327
        wt = self.make_branch_and_tree('.')
 
3328
        self._set_orphan_policy(wt, 'bogus')
 
3329
        tt, orphan_tid = self._prepare_orphan(wt)
 
3330
        remaining_conflicts = resolve_conflicts(tt)
 
3331
        self.assertLength(1, remaining_conflicts)
 
3332
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3333
                         remaining_conflicts.pop())
 
3334
 
 
3335
    def test_unknown_orphan_policy(self):
 
3336
        wt = self.make_branch_and_tree('.')
 
3337
        # Set a fictional policy nobody ever implemented
 
3338
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
 
3339
        tt, orphan_tid = self._prepare_orphan(wt)
 
3340
        warnings = []
 
3341
        def warning(*args):
 
3342
            warnings.append(args[0] % args[1:])
 
3343
        self.overrideAttr(trace, 'warning', warning)
 
3344
        remaining_conflicts = resolve_conflicts(tt)
 
3345
        # We fallback to the default policy which create a conflict
 
3346
        self.assertLength(1, remaining_conflicts)
 
3347
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3348
                         remaining_conflicts.pop())
 
3349
        self.assertLength(1, warnings)
 
3350
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')