~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Martin Pool
  • Date: 2011-02-07 01:39:42 UTC
  • mto: This revision was merged to the branch mainline in revision 5650.
  • Revision ID: mbp@canonical.com-20110207013942-roj88kez6jir13tr
Add brief user documentation of command line splitting

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
import errno
17
18
import os
18
19
from StringIO import StringIO
19
20
import sys
25
26
    filters,
26
27
    generate_ids,
27
28
    osutils,
28
 
    progress,
29
29
    revision as _mod_revision,
30
30
    rules,
 
31
    symbol_versioning,
31
32
    tests,
 
33
    trace,
 
34
    transform,
32
35
    urlutils,
33
36
    )
34
37
from bzrlib.bzrdir import BzrDir
35
 
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
36
 
                              UnversionedParent, ParentLoop, DeletingParent,
37
 
                              NonDirectoryParent)
 
38
from bzrlib.conflicts import (
 
39
    DeletingParent,
 
40
    DuplicateEntry,
 
41
    DuplicateID,
 
42
    MissingParent,
 
43
    NonDirectoryParent,
 
44
    ParentLoop,
 
45
    UnversionedParent,
 
46
)
38
47
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
 
48
from bzrlib.errors import (
 
49
    DuplicateKey,
 
50
    ExistingLimbo,
 
51
    ExistingPendingDeletion,
 
52
    ImmortalLimbo,
 
53
    ImmortalPendingDeletion,
 
54
    LockError,
 
55
    MalformedTransform,
 
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
 
    TestCase,
50
67
    TestCaseInTempDir,
51
68
    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)
 
69
)
 
70
from bzrlib.transform import (
 
71
    build_tree,
 
72
    create_from_tree,
 
73
    cook_conflicts,
 
74
    _FileMover,
 
75
    FinalPaths,
 
76
    resolve_conflicts,
 
77
    resolve_checkout,
 
78
    ROOT_PARENT,
 
79
    TransformPreview,
 
80
    TreeTransform,
 
81
)
58
82
 
59
83
 
60
84
class TestTreeTransform(tests.TestCaseWithTransport):
101
125
        imaginary_id = transform.trans_id_tree_path('imaginary')
102
126
        imaginary_id2 = transform.trans_id_tree_path('imaginary/')
103
127
        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())
 
128
        self.assertEqual(root, transform.get_tree_parent(imaginary_id))
 
129
        self.assertEqual('directory', transform.final_kind(root))
 
130
        self.assertEqual(self.wt.get_root_id(), transform.final_file_id(root))
107
131
        trans_id = transform.create_path('name', root)
108
132
        self.assertIs(transform.final_file_id(trans_id), None)
109
 
        self.assertRaises(NoSuchFile, transform.final_kind, trans_id)
 
133
        self.assertIs(None, transform.final_kind(trans_id))
110
134
        transform.create_file('contents', trans_id)
111
135
        transform.set_executability(True, trans_id)
112
136
        transform.version_file('my_pretties', trans_id)
792
816
        self.assertIs(None, self.wt.path2id('parent'))
793
817
        self.assertIs(None, self.wt.path2id('parent.new'))
794
818
 
 
819
    def test_resolve_conflicts_missing_parent(self):
 
820
        wt = self.make_branch_and_tree('.')
 
821
        tt = TreeTransform(wt)
 
822
        self.addCleanup(tt.finalize)
 
823
        parent = tt.trans_id_file_id('parent-id')
 
824
        tt.new_file('file', parent, 'Contents')
 
825
        raw_conflicts = resolve_conflicts(tt)
 
826
        # Since the directory doesn't exist it's seen as 'missing'.  So
 
827
        # 'resolve_conflicts' create a conflict asking for it to be created.
 
828
        self.assertLength(1, raw_conflicts)
 
829
        self.assertEqual(('missing parent', 'Created directory', 'new-1'),
 
830
                         raw_conflicts.pop())
 
831
        # apply fail since the missing directory doesn't exist
 
832
        self.assertRaises(errors.NoFinalPath, tt.apply)
 
833
 
795
834
    def test_moving_versioned_directories(self):
796
835
        create, root = self.get_transform()
797
836
        kansas = create.new_directory('kansas', root, 'kansas-id')
830
869
        rename.set_executability(True, myfile)
831
870
        rename.apply()
832
871
 
 
872
    def test_rename_fails(self):
 
873
        self.requireFeature(features.not_running_as_root)
 
874
        # see https://bugs.launchpad.net/bzr/+bug/491763
 
875
        create, root_id = self.get_transform()
 
876
        first_dir = create.new_directory('first-dir', root_id, 'first-id')
 
877
        myfile = create.new_file('myfile', root_id, 'myfile-text',
 
878
                                 'myfile-id')
 
879
        create.apply()
 
880
        if os.name == "posix" and sys.platform != "cygwin":
 
881
            # posix filesystems fail on renaming if the readonly bit is set
 
882
            osutils.make_readonly(self.wt.abspath('first-dir'))
 
883
        elif os.name == "nt":
 
884
            # windows filesystems fail on renaming open files
 
885
            self.addCleanup(file(self.wt.abspath('myfile')).close)
 
886
        else:
 
887
            self.skip("Don't know how to force a permissions error on rename")
 
888
        # now transform to rename
 
889
        rename_transform, root_id = self.get_transform()
 
890
        file_trans_id = rename_transform.trans_id_file_id('myfile-id')
 
891
        dir_id = rename_transform.trans_id_file_id('first-id')
 
892
        rename_transform.adjust_path('newname', dir_id, file_trans_id)
 
893
        e = self.assertRaises(errors.TransformRenameFailed,
 
894
            rename_transform.apply)
 
895
        # On nix looks like: 
 
896
        # "Failed to rename .../work/.bzr/checkout/limbo/new-1
 
897
        # to .../first-dir/newname: [Errno 13] Permission denied"
 
898
        # On windows looks like:
 
899
        # "Failed to rename .../work/myfile to 
 
900
        # .../work/.bzr/checkout/limbo/new-1: [Errno 13] Permission denied"
 
901
        # This test isn't concerned with exactly what the error looks like,
 
902
        # and the strerror will vary across OS and locales, but the assert
 
903
        # that the exeception attributes are what we expect
 
904
        self.assertEqual(e.errno, errno.EACCES)
 
905
        if os.name == "posix":
 
906
            self.assertEndsWith(e.to_path, "/first-dir/newname")
 
907
        else:
 
908
            self.assertEqual(os.path.basename(e.from_path), "myfile")
 
909
 
833
910
    def test_set_executability_order(self):
834
911
        """Ensure that executability behaves the same, no matter what order.
835
912
 
2091
2168
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2092
2169
                          'message')
2093
2170
 
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):
 
2171
    def test_commit_rich_revision_data(self):
 
2172
        branch, tt = self.get_branch_and_transform()
 
2173
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
 
2174
                           committer='me <me@example.com>',
 
2175
                           revprops={'foo': 'bar'}, revision_id='revid-1',
 
2176
                           authors=['Author1 <author1@example.com>',
 
2177
                              'Author2 <author2@example.com>',
 
2178
                               ])
 
2179
        self.assertEqual('revid-1', rev_id)
 
2180
        revision = branch.repository.get_revision(rev_id)
 
2181
        self.assertEqual(1, revision.timestamp)
 
2182
        self.assertEqual(43201, revision.timezone)
 
2183
        self.assertEqual('me <me@example.com>', revision.committer)
 
2184
        self.assertEqual(['Author1 <author1@example.com>',
 
2185
                          'Author2 <author2@example.com>'],
 
2186
                         revision.get_apparent_authors())
 
2187
        del revision.properties['authors']
 
2188
        self.assertEqual({'foo': 'bar',
 
2189
                          'branch-nick': 'tree'},
 
2190
                         revision.properties)
 
2191
 
 
2192
    def test_no_explicit_revprops(self):
 
2193
        branch, tt = self.get_branch_and_transform()
 
2194
        rev_id = tt.commit(branch, 'message', authors=[
 
2195
            'Author1 <author1@example.com>',
 
2196
            'Author2 <author2@example.com>', ])
 
2197
        revision = branch.repository.get_revision(rev_id)
 
2198
        self.assertEqual(['Author1 <author1@example.com>',
 
2199
                          'Author2 <author2@example.com>'],
 
2200
                         revision.get_apparent_authors())
 
2201
        self.assertEqual('tree', revision.properties['branch-nick'])
 
2202
 
 
2203
 
 
2204
class TestBackupName(tests.TestCase):
 
2205
 
 
2206
    def test_deprecations(self):
 
2207
        class MockTransform(object):
 
2208
 
 
2209
            def has_named_child(self, by_parent, parent_id, name):
 
2210
                return name in by_parent.get(parent_id, [])
 
2211
 
 
2212
        class MockEntry(object):
 
2213
 
 
2214
            def __init__(self):
 
2215
                object.__init__(self)
 
2216
                self.name = "name"
 
2217
 
2115
2218
        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~')
 
2219
        name1 = self.applyDeprecated(
 
2220
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2221
            transform.get_backup_name, MockEntry(), {'a':[]}, 'a', tt)
 
2222
        self.assertEqual('name.~1~', name1)
 
2223
        name2 = self.applyDeprecated(
 
2224
            symbol_versioning.deprecated_in((2, 3, 0)),
 
2225
            transform._get_backup_name, 'name', {'a':['name.~1~']}, 'a', tt)
 
2226
        self.assertEqual('name.~2~', name2)
2126
2227
 
2127
2228
 
2128
2229
class TestFileMover(tests.TestCaseWithTransport):
2243
2344
        self.failUnlessExists('a')
2244
2345
        self.failUnlessExists('a/b')
2245
2346
 
2246
 
    def test_resolve_no_parent(self):
 
2347
 
 
2348
class TestTransformMissingParent(tests.TestCaseWithTransport):
 
2349
 
 
2350
    def make_tt_with_versioned_dir(self):
2247
2351
        wt = self.make_branch_and_tree('.')
 
2352
        self.build_tree(['dir/',])
 
2353
        wt.add(['dir'], ['dir-id'])
 
2354
        wt.commit('Create dir')
2248
2355
        tt = TreeTransform(wt)
2249
2356
        self.addCleanup(tt.finalize)
2250
 
        parent = tt.trans_id_file_id('parent-id')
2251
 
        tt.new_file('file', parent, 'Contents')
2252
 
        resolve_conflicts(tt)
 
2357
        return wt, tt
 
2358
 
 
2359
    def test_resolve_create_parent_for_versioned_file(self):
 
2360
        wt, tt = self.make_tt_with_versioned_dir()
 
2361
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2362
        file_tid = tt.new_file('file', dir_tid, 'Contents', file_id='file-id')
 
2363
        tt.delete_contents(dir_tid)
 
2364
        tt.unversion_file(dir_tid)
 
2365
        conflicts = resolve_conflicts(tt)
 
2366
        # one conflict for the missing directory, one for the unversioned
 
2367
        # parent
 
2368
        self.assertLength(2, conflicts)
 
2369
 
 
2370
    def test_non_versioned_file_create_conflict(self):
 
2371
        wt, tt = self.make_tt_with_versioned_dir()
 
2372
        dir_tid = tt.trans_id_tree_file_id('dir-id')
 
2373
        tt.new_file('file', dir_tid, 'Contents')
 
2374
        tt.delete_contents(dir_tid)
 
2375
        tt.unversion_file(dir_tid)
 
2376
        conflicts = resolve_conflicts(tt)
 
2377
        # no conflicts or rather: orphaning 'file' resolve the 'dir' conflict
 
2378
        self.assertLength(1, conflicts)
 
2379
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
2380
                         conflicts.pop())
2253
2381
 
2254
2382
 
2255
2383
A_ENTRY = ('a-id', ('a', 'a'), True, (True, True),
2256
2384
                  ('TREE_ROOT', 'TREE_ROOT'), ('a', 'a'), ('file', 'file'),
2257
2385
                  (False, False))
2258
2386
ROOT_ENTRY = ('TREE_ROOT', ('', ''), False, (True, True), (None, None),
2259
 
              ('', ''), ('directory', 'directory'), (False, None))
 
2387
              ('', ''), ('directory', 'directory'), (False, False))
2260
2388
 
2261
2389
 
2262
2390
class TestTransformPreview(tests.TestCaseWithTransport):
2349
2477
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2350
2478
        changes = preview_tree.iter_changes(revision_tree,
2351
2479
                                            specific_files=[''])
2352
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
2480
        self.assertEqual([A_ENTRY], list(changes))
2353
2481
 
2354
2482
    def test_want_unversioned(self):
2355
2483
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2356
2484
        changes = preview_tree.iter_changes(revision_tree,
2357
2485
                                            want_unversioned=True)
2358
 
        self.assertEqual([ROOT_ENTRY, A_ENTRY], list(changes))
 
2486
        self.assertEqual([A_ENTRY], list(changes))
2359
2487
 
2360
2488
    def test_ignore_extra_trees_no_specific_files(self):
2361
2489
        # extra_trees is harmless without specific_files, so we'll silently
3132
3260
        trans_id = tt.trans_id_tree_path('file')
3133
3261
        self.assertEqual((LINES_ONE,),
3134
3262
            tt._get_parents_texts(trans_id))
 
3263
 
 
3264
 
 
3265
class TestOrphan(tests.TestCaseWithTransport):
 
3266
 
 
3267
    def test_no_orphan_for_transform_preview(self):
 
3268
        tree = self.make_branch_and_tree('tree')
 
3269
        tt = transform.TransformPreview(tree)
 
3270
        self.addCleanup(tt.finalize)
 
3271
        self.assertRaises(NotImplementedError, tt.new_orphan, 'foo', 'bar')
 
3272
 
 
3273
    def _set_orphan_policy(self, wt, policy):
 
3274
        wt.branch.get_config().set_user_option('bzr.transform.orphan_policy',
 
3275
                                               policy)
 
3276
 
 
3277
    def _prepare_orphan(self, wt):
 
3278
        self.build_tree(['dir/', 'dir/file', 'dir/foo'])
 
3279
        wt.add(['dir', 'dir/file'], ['dir-id', 'file-id'])
 
3280
        wt.commit('add dir and file ignoring foo')
 
3281
        tt = transform.TreeTransform(wt)
 
3282
        self.addCleanup(tt.finalize)
 
3283
        # dir and bar are deleted
 
3284
        dir_tid = tt.trans_id_tree_path('dir')
 
3285
        file_tid = tt.trans_id_tree_path('dir/file')
 
3286
        orphan_tid = tt.trans_id_tree_path('dir/foo')
 
3287
        tt.delete_contents(file_tid)
 
3288
        tt.unversion_file(file_tid)
 
3289
        tt.delete_contents(dir_tid)
 
3290
        tt.unversion_file(dir_tid)
 
3291
        # There should be a conflict because dir still contain foo
 
3292
        raw_conflicts = tt.find_conflicts()
 
3293
        self.assertLength(1, raw_conflicts)
 
3294
        self.assertEqual(('missing parent', 'new-1'), raw_conflicts[0])
 
3295
        return tt, orphan_tid
 
3296
 
 
3297
    def test_new_orphan_created(self):
 
3298
        wt = self.make_branch_and_tree('.')
 
3299
        self._set_orphan_policy(wt, 'move')
 
3300
        tt, orphan_tid = self._prepare_orphan(wt)
 
3301
        warnings = []
 
3302
        def warning(*args):
 
3303
            warnings.append(args[0] % args[1:])
 
3304
        self.overrideAttr(trace, 'warning', warning)
 
3305
        remaining_conflicts = resolve_conflicts(tt)
 
3306
        self.assertEquals(['dir/foo has been orphaned in bzr-orphans'],
 
3307
                          warnings)
 
3308
        # Yeah for resolved conflicts !
 
3309
        self.assertLength(0, remaining_conflicts)
 
3310
        # We have a new orphan
 
3311
        self.assertEquals('foo.~1~', tt.final_name(orphan_tid))
 
3312
        self.assertEquals('bzr-orphans',
 
3313
                          tt.final_name(tt.final_parent(orphan_tid)))
 
3314
 
 
3315
    def test_never_orphan(self):
 
3316
        wt = self.make_branch_and_tree('.')
 
3317
        self._set_orphan_policy(wt, 'conflict')
 
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_orphan_error(self):
 
3325
        def bogus_orphan(tt, orphan_id, parent_id):
 
3326
            raise transform.OrphaningError(tt.final_name(orphan_id),
 
3327
                                           tt.final_name(parent_id))
 
3328
        transform.orphaning_registry.register('bogus', bogus_orphan,
 
3329
                                              'Raise an error when orphaning')
 
3330
        wt = self.make_branch_and_tree('.')
 
3331
        self._set_orphan_policy(wt, 'bogus')
 
3332
        tt, orphan_tid = self._prepare_orphan(wt)
 
3333
        remaining_conflicts = resolve_conflicts(tt)
 
3334
        self.assertLength(1, remaining_conflicts)
 
3335
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3336
                         remaining_conflicts.pop())
 
3337
 
 
3338
    def test_unknown_orphan_policy(self):
 
3339
        wt = self.make_branch_and_tree('.')
 
3340
        # Set a fictional policy nobody ever implemented
 
3341
        self._set_orphan_policy(wt, 'donttouchmypreciouuus')
 
3342
        tt, orphan_tid = self._prepare_orphan(wt)
 
3343
        warnings = []
 
3344
        def warning(*args):
 
3345
            warnings.append(args[0] % args[1:])
 
3346
        self.overrideAttr(trace, 'warning', warning)
 
3347
        remaining_conflicts = resolve_conflicts(tt)
 
3348
        # We fallback to the default policy which create a conflict
 
3349
        self.assertLength(1, remaining_conflicts)
 
3350
        self.assertEqual(('deleting parent', 'Not deleting', 'new-1'),
 
3351
                         remaining_conflicts.pop())
 
3352
        self.assertLength(1, warnings)
 
3353
        self.assertStartsWith(warnings[0], 'donttouchmypreciouuus')