/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-10-19 10:59:16 UTC
  • mfrom: (4585.1.10 foreign-tests)
  • Revision ID: pqm@pqm.ubuntu.com-20091019105916-6z2jo34eqr6s0008
(Jelmer) Add infrastructure for testing foreign branch
        implementations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 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
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
 
18
import stat
18
19
from StringIO import StringIO
19
20
import sys
20
 
import time
21
21
 
22
22
from bzrlib import (
23
23
    bencode,
24
24
    errors,
25
 
    filters,
26
25
    generate_ids,
27
26
    osutils,
 
27
    progress,
28
28
    revision as _mod_revision,
29
 
    rules,
 
29
    symbol_versioning,
30
30
    tests,
31
31
    urlutils,
32
32
    )
33
33
from bzrlib.bzrdir import BzrDir
34
 
from bzrlib.conflicts import (
35
 
    DeletingParent,
36
 
    DuplicateEntry,
37
 
    DuplicateID,
38
 
    MissingParent,
39
 
    NonDirectoryParent,
40
 
    ParentLoop,
41
 
    UnversionedParent,
42
 
)
 
34
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
 
35
                              UnversionedParent, ParentLoop, DeletingParent,
 
36
                              NonDirectoryParent)
43
37
from bzrlib.diff import show_diff_trees
44
 
from bzrlib.errors import (
45
 
    DuplicateKey,
46
 
    ExistingLimbo,
47
 
    ExistingPendingDeletion,
48
 
    ImmortalLimbo,
49
 
    ImmortalPendingDeletion,
50
 
    LockError,
51
 
    MalformedTransform,
52
 
    NoSuchFile,
53
 
    ReusingTransform,
54
 
)
55
 
from bzrlib.osutils import (
56
 
    file_kind,
57
 
    pathjoin,
58
 
)
 
38
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
 
39
                           ReusingTransform, CantMoveRoot,
 
40
                           PathsNotVersionedError, ExistingLimbo,
 
41
                           ExistingPendingDeletion, ImmortalLimbo,
 
42
                           ImmortalPendingDeletion, LockError)
 
43
from bzrlib.osutils import file_kind, pathjoin
59
44
from bzrlib.merge import Merge3Merger, Merger
60
45
from bzrlib.tests import (
61
46
    HardlinkFeature,
63
48
    TestCase,
64
49
    TestCaseInTempDir,
65
50
    TestSkipped,
66
 
)
67
 
from bzrlib.transform import (
68
 
    build_tree,
69
 
    create_from_tree,
70
 
    cook_conflicts,
71
 
    _FileMover,
72
 
    FinalPaths,
73
 
    get_backup_name,
74
 
    resolve_conflicts,
75
 
    resolve_checkout,
76
 
    ROOT_PARENT,
77
 
    TransformPreview,
78
 
    TreeTransform,
79
 
)
 
51
    )
 
52
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths,
 
53
                              resolve_conflicts, cook_conflicts,
 
54
                              build_tree, get_backup_name,
 
55
                              _FileMover, resolve_checkout,
 
56
                              TransformPreview, create_from_tree)
80
57
 
81
58
 
82
59
class TestTreeTransform(tests.TestCaseWithTransport):
159
136
        transform.finalize()
160
137
        transform.finalize()
161
138
 
162
 
    def test_create_files_same_timestamp(self):
163
 
        transform, root = self.get_transform()
164
 
        self.wt.lock_tree_write()
165
 
        self.addCleanup(self.wt.unlock)
166
 
        # Roll back the clock, so that we know everything is being set to the
167
 
        # exact time
168
 
        transform._creation_mtime = creation_mtime = time.time() - 20.0
169
 
        transform.create_file('content-one',
170
 
                              transform.create_path('one', root))
171
 
        time.sleep(1) # *ugly*
172
 
        transform.create_file('content-two',
173
 
                              transform.create_path('two', root))
174
 
        transform.apply()
175
 
        fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
176
 
        fo.close()
177
 
        fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
178
 
        fo.close()
179
 
        # We only guarantee 2s resolution
180
 
        self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
181
 
            "%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
182
 
        # But if we have more than that, all files should get the same result
183
 
        self.assertEqual(st1.st_mtime, st2.st_mtime)
184
 
 
185
 
    def test_change_root_id(self):
186
 
        transform, root = self.get_transform()
187
 
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
188
 
        transform.new_directory('', ROOT_PARENT, 'new-root-id')
189
 
        transform.delete_contents(root)
190
 
        transform.unversion_file(root)
191
 
        transform.fixup_new_roots()
192
 
        transform.apply()
193
 
        self.assertEqual('new-root-id', self.wt.get_root_id())
194
 
 
195
 
    def test_change_root_id_add_files(self):
196
 
        transform, root = self.get_transform()
197
 
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
198
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
199
 
        transform.new_file('file', new_trans_id, ['new-contents\n'],
200
 
                           'new-file-id')
201
 
        transform.delete_contents(root)
202
 
        transform.unversion_file(root)
203
 
        transform.fixup_new_roots()
204
 
        transform.apply()
205
 
        self.assertEqual('new-root-id', self.wt.get_root_id())
206
 
        self.assertEqual('new-file-id', self.wt.path2id('file'))
207
 
        self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
208
 
 
209
 
    def test_add_two_roots(self):
210
 
        transform, root = self.get_transform()
211
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
212
 
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
213
 
        self.assertRaises(ValueError, transform.fixup_new_roots)
214
 
 
215
139
    def test_hardlink(self):
216
140
        self.requireFeature(HardlinkFeature)
217
141
        transform, root = self.get_transform()
757
681
                                         ' versioned, but has versioned'
758
682
                                         ' children.  Versioned directory.')
759
683
        self.assertEqual(conflicts_s[6], 'Conflict moving oz/emeraldcity into'
760
 
                                         ' oz/emeraldcity. Cancelled move.')
 
684
                                         ' oz/emeraldcity.  Cancelled move.')
761
685
 
762
686
    def prepare_wrong_parent_kind(self):
763
687
        tt, root = self.get_transform()
834
758
        create.apply()
835
759
        transform, root = self.get_transform()
836
760
        transform.adjust_root_path('oldroot', fun)
837
 
        new_root = transform.trans_id_tree_path('')
 
761
        new_root=transform.trans_id_tree_path('')
838
762
        transform.version_file('new-root', new_root)
839
763
        transform.apply()
840
764
 
1944
1868
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1945
1869
        self.assertTrue(source.is_executable('file1-id'))
1946
1870
 
1947
 
    def install_rot13_content_filter(self, pattern):
1948
 
        # We could use
1949
 
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
1950
 
        # below, but that looks a bit... hard to read even if it's exactly
1951
 
        # the same thing.
1952
 
        original_registry = filters._reset_registry()
1953
 
        def restore_registry():
1954
 
            filters._reset_registry(original_registry)
1955
 
        self.addCleanup(restore_registry)
1956
 
        def rot13(chunks, context=None):
1957
 
            return [''.join(chunks).encode('rot13')]
1958
 
        rot13filter = filters.ContentFilter(rot13, rot13)
1959
 
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1960
 
        os.mkdir(self.test_home_dir + '/.bazaar')
1961
 
        rules_filename = self.test_home_dir + '/.bazaar/rules'
1962
 
        f = open(rules_filename, 'wb')
1963
 
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
1964
 
        f.close()
1965
 
        def uninstall_rules():
1966
 
            os.remove(rules_filename)
1967
 
            rules.reset_rules()
1968
 
        self.addCleanup(uninstall_rules)
1969
 
        rules.reset_rules()
1970
 
 
1971
 
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
1972
 
        """build_tree will not hardlink files that have content filtering rules
1973
 
        applied to them (but will still hardlink other files from the same tree
1974
 
        if it can).
1975
 
        """
1976
 
        self.requireFeature(HardlinkFeature)
1977
 
        self.install_rot13_content_filter('file1')
1978
 
        source = self.create_ab_tree()
1979
 
        target = self.make_branch_and_tree('target')
1980
 
        revision_tree = source.basis_tree()
1981
 
        revision_tree.lock_read()
1982
 
        self.addCleanup(revision_tree.unlock)
1983
 
        build_tree(revision_tree, target, source, hardlink=True)
1984
 
        target.lock_read()
1985
 
        self.addCleanup(target.unlock)
1986
 
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1987
 
        source_stat = os.stat('source/file1')
1988
 
        target_stat = os.stat('target/file1')
1989
 
        self.assertNotEqual(source_stat, target_stat)
1990
 
        source_stat = os.stat('source/file2')
1991
 
        target_stat = os.stat('target/file2')
1992
 
        self.assertEqualStat(source_stat, target_stat)
1993
 
 
1994
1871
    def test_case_insensitive_build_tree_inventory(self):
1995
1872
        if (tests.CaseInsensitiveFilesystemFeature.available()
1996
1873
            or tests.CaseInsCasePresFilenameFeature.available()):
2074
1951
        branch, tt = self.get_branch_and_transform()
2075
1952
        tt.new_file('file', tt.root, 'contents', 'file-id')
2076
1953
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2077
 
        if SymlinkFeature.available():
2078
 
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
 
1954
        tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2079
1955
        rev = tt.commit(branch, 'message')
2080
1956
        tree = branch.basis_tree()
2081
1957
        self.assertEqual('file', tree.id2path('file-id'))
2082
1958
        self.assertEqual('contents', tree.get_file_text('file-id'))
2083
1959
        self.assertEqual('dir', tree.id2path('dir-id'))
2084
 
        if SymlinkFeature.available():
2085
 
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2086
 
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
 
1960
        self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
 
1961
        self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2087
1962
 
2088
1963
    def test_add_unversioned(self):
2089
1964
        branch, tt = self.get_branch_and_transform()
2113
1988
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
2114
1989
                          'message')
2115
1990
 
2116
 
    def test_commit_rich_revision_data(self):
2117
 
        branch, tt = self.get_branch_and_transform()
2118
 
        rev_id = tt.commit(branch, 'message', timestamp=1, timezone=43201,
2119
 
                           committer='me <me@example.com>',
2120
 
                           revprops={'foo': 'bar'}, revision_id='revid-1',
2121
 
                           authors=['Author1 <author1@example.com>',
2122
 
                              'Author2 <author2@example.com>',
2123
 
                               ])
2124
 
        self.assertEqual('revid-1', rev_id)
2125
 
        revision = branch.repository.get_revision(rev_id)
2126
 
        self.assertEqual(1, revision.timestamp)
2127
 
        self.assertEqual(43201, revision.timezone)
2128
 
        self.assertEqual('me <me@example.com>', revision.committer)
2129
 
        self.assertEqual(['Author1 <author1@example.com>',
2130
 
                          'Author2 <author2@example.com>'],
2131
 
                         revision.get_apparent_authors())
2132
 
        del revision.properties['authors']
2133
 
        self.assertEqual({'foo': 'bar',
2134
 
                          'branch-nick': 'tree'},
2135
 
                         revision.properties)
2136
 
 
2137
 
    def test_no_explicit_revprops(self):
2138
 
        branch, tt = self.get_branch_and_transform()
2139
 
        rev_id = tt.commit(branch, 'message', authors=[
2140
 
            'Author1 <author1@example.com>',
2141
 
            'Author2 <author2@example.com>', ])
2142
 
        revision = branch.repository.get_revision(rev_id)
2143
 
        self.assertEqual(['Author1 <author1@example.com>',
2144
 
                          'Author2 <author2@example.com>'],
2145
 
                         revision.get_apparent_authors())
2146
 
        self.assertEqual('tree', revision.properties['branch-nick'])
2147
 
 
2148
1991
 
2149
1992
class MockTransform(object):
2150
1993
 
2425
2268
    def test_ignore_pb(self):
2426
2269
        # pb could be supported, but TT.iter_changes doesn't support it.
2427
2270
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2428
 
        preview_tree.iter_changes(revision_tree)
 
2271
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
2429
2272
 
2430
2273
    def test_kind(self):
2431
2274
        revision_tree = self.create_tree()
2611
2454
        self.assertEqual(('missing', None, None, None), summary)
2612
2455
 
2613
2456
    def test_file_content_summary_executable(self):
 
2457
        if not osutils.supports_executable():
 
2458
            raise TestNotApplicable()
2614
2459
        preview = self.get_empty_preview()
2615
2460
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2616
2461
        preview.set_executability(True, path_id)
2625
2470
        self.assertIs(None, summary[3])
2626
2471
 
2627
2472
    def test_change_executability(self):
 
2473
        if not osutils.supports_executable():
 
2474
            raise TestNotApplicable()
2628
2475
        tree = self.make_branch_and_tree('tree')
2629
2476
        self.build_tree(['tree/path'])
2630
2477
        tree.add('path')
2644
2491
        # size must be known
2645
2492
        self.assertEqual(len('contents'), summary[1])
2646
2493
        # not executable
2647
 
        self.assertEqual(False, summary[2])
 
2494
        if osutils.supports_executable():
 
2495
            self.assertEqual(False, summary[2])
 
2496
        else:
 
2497
            self.assertEqual(None, summary[2])
2648
2498
        # will not have hash (not cheap to determine)
2649
2499
        self.assertIs(None, summary[3])
2650
2500
 
2793
2643
        preview = self.get_empty_preview()
2794
2644
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2795
2645
        # FIXME: new_directory should mark root.
2796
 
        preview.fixup_new_roots()
 
2646
        preview.adjust_path('', ROOT_PARENT, root)
2797
2647
        preview_tree = preview.get_preview_tree()
2798
2648
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2799
2649
                                         'a-id')
2833
2683
        file_trans_id = preview.trans_id_file_id('file-id')
2834
2684
        preview.delete_contents(file_trans_id)
2835
2685
        preview.create_file('a\nb\n', file_trans_id)
 
2686
        pb = progress.DummyProgress()
2836
2687
        preview_tree = preview.get_preview_tree()
2837
 
        merger = Merger.from_revision_ids(None, preview_tree,
 
2688
        merger = Merger.from_revision_ids(pb, preview_tree,
2838
2689
                                          child_tree.branch.last_revision(),
2839
2690
                                          other_branch=child_tree.branch,
2840
2691
                                          tree_branch=work_tree.branch)
2852
2703
        tt.new_file('name', tt.root, 'content', 'file-id')
2853
2704
        tree2 = self.make_branch_and_tree('tree2')
2854
2705
        tree2.set_root_id('TREE_ROOT')
 
2706
        pb = progress.DummyProgress()
2855
2707
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2856
 
                                         None, tree.basis_tree())
 
2708
                                         pb, tree.basis_tree())
2857
2709
        merger.merge_type = Merge3Merger
2858
2710
        merger.do_merge()
2859
2711
 
2869
2721
        tt.create_file('baz', trans_id)
2870
2722
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2871
2723
        self.build_tree_contents([('tree2/foo', 'qux')])
2872
 
        pb = None
 
2724
        pb = progress.DummyProgress()
2873
2725
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2874
2726
                                         pb, tree.basis_tree())
2875
2727
        merger.merge_type = Merge3Merger
2902
2754
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2903
2755
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2904
2756
 
2905
 
    def test_ascii_limbo_paths(self):
2906
 
        self.requireFeature(tests.UnicodeFilenameFeature)
2907
 
        branch = self.make_branch('any')
2908
 
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2909
 
        tt = TransformPreview(tree)
2910
 
        self.addCleanup(tt.finalize)
2911
 
        foo_id = tt.new_directory('', ROOT_PARENT)
2912
 
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2913
 
        limbo_path = tt._limbo_name(bar_id)
2914
 
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2915
 
 
2916
2757
 
2917
2758
class FakeSerializer(object):
2918
2759
    """Serializer implementation that simply returns the input.