~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Andrew Bennetts
  • Date: 2010-02-12 04:33:05 UTC
  • mfrom: (5031 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5032.
  • Revision ID: andrew.bennetts@canonical.com-20100212043305-ujdbsdoviql2t7i3
MergeĀ lp:bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
19
18
from StringIO import StringIO
20
19
import sys
 
20
import time
21
21
 
22
22
from bzrlib import (
23
23
    bencode,
24
24
    errors,
 
25
    filters,
25
26
    generate_ids,
26
27
    osutils,
27
28
    progress,
28
29
    revision as _mod_revision,
29
 
    symbol_versioning,
 
30
    rules,
30
31
    tests,
31
32
    urlutils,
32
33
    )
136
137
        transform.finalize()
137
138
        transform.finalize()
138
139
 
 
140
    def test_create_files_same_timestamp(self):
 
141
        transform, root = self.get_transform()
 
142
        self.wt.lock_tree_write()
 
143
        self.addCleanup(self.wt.unlock)
 
144
        # Roll back the clock, so that we know everything is being set to the
 
145
        # exact time
 
146
        transform._creation_mtime = creation_mtime = time.time() - 20.0
 
147
        transform.create_file('content-one',
 
148
                              transform.create_path('one', root))
 
149
        time.sleep(1) # *ugly*
 
150
        transform.create_file('content-two',
 
151
                              transform.create_path('two', root))
 
152
        transform.apply()
 
153
        fo, st1 = self.wt.get_file_with_stat(None, path='one', filtered=False)
 
154
        fo.close()
 
155
        fo, st2 = self.wt.get_file_with_stat(None, path='two', filtered=False)
 
156
        fo.close()
 
157
        # We only guarantee 2s resolution
 
158
        self.assertTrue(abs(creation_mtime - st1.st_mtime) < 2.0,
 
159
            "%s != %s within 2 seconds" % (creation_mtime, st1.st_mtime))
 
160
        # But if we have more than that, all files should get the same result
 
161
        self.assertEqual(st1.st_mtime, st2.st_mtime)
 
162
 
 
163
    def test_change_root_id(self):
 
164
        transform, root = self.get_transform()
 
165
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
 
166
        transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
167
        transform.delete_contents(root)
 
168
        transform.unversion_file(root)
 
169
        transform.fixup_new_roots()
 
170
        transform.apply()
 
171
        self.assertEqual('new-root-id', self.wt.get_root_id())
 
172
 
 
173
    def test_change_root_id_add_files(self):
 
174
        transform, root = self.get_transform()
 
175
        self.assertNotEqual('new-root-id', self.wt.get_root_id())
 
176
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
177
        transform.new_file('file', new_trans_id, ['new-contents\n'],
 
178
                           'new-file-id')
 
179
        transform.delete_contents(root)
 
180
        transform.unversion_file(root)
 
181
        transform.fixup_new_roots()
 
182
        transform.apply()
 
183
        self.assertEqual('new-root-id', self.wt.get_root_id())
 
184
        self.assertEqual('new-file-id', self.wt.path2id('file'))
 
185
        self.assertFileEqual('new-contents\n', self.wt.abspath('file'))
 
186
 
 
187
    def test_add_two_roots(self):
 
188
        transform, root = self.get_transform()
 
189
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'new-root-id')
 
190
        new_trans_id = transform.new_directory('', ROOT_PARENT, 'alt-root-id')
 
191
        self.assertRaises(ValueError, transform.fixup_new_roots)
 
192
 
139
193
    def test_hardlink(self):
140
194
        self.requireFeature(HardlinkFeature)
141
195
        transform, root = self.get_transform()
758
812
        create.apply()
759
813
        transform, root = self.get_transform()
760
814
        transform.adjust_root_path('oldroot', fun)
761
 
        new_root=transform.trans_id_tree_path('')
 
815
        new_root = transform.trans_id_tree_path('')
762
816
        transform.version_file('new-root', new_root)
763
817
        transform.apply()
764
818
 
1868
1922
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1869
1923
        self.assertTrue(source.is_executable('file1-id'))
1870
1924
 
 
1925
    def install_rot13_content_filter(self, pattern):
 
1926
        # We could use
 
1927
        # self.addCleanup(filters._reset_registry, filters._reset_registry())
 
1928
        # below, but that looks a bit... hard to read even if it's exactly
 
1929
        # the same thing.
 
1930
        original_registry = filters._reset_registry()
 
1931
        def restore_registry():
 
1932
            filters._reset_registry(original_registry)
 
1933
        self.addCleanup(restore_registry)
 
1934
        def rot13(chunks, context=None):
 
1935
            return [''.join(chunks).encode('rot13')]
 
1936
        rot13filter = filters.ContentFilter(rot13, rot13)
 
1937
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
 
1938
        os.mkdir(self.test_home_dir + '/.bazaar')
 
1939
        rules_filename = self.test_home_dir + '/.bazaar/rules'
 
1940
        f = open(rules_filename, 'wb')
 
1941
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
 
1942
        f.close()
 
1943
        def uninstall_rules():
 
1944
            os.remove(rules_filename)
 
1945
            rules.reset_rules()
 
1946
        self.addCleanup(uninstall_rules)
 
1947
        rules.reset_rules()
 
1948
 
 
1949
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
 
1950
        """build_tree will not hardlink files that have content filtering rules
 
1951
        applied to them (but will still hardlink other files from the same tree
 
1952
        if it can).
 
1953
        """
 
1954
        self.requireFeature(HardlinkFeature)
 
1955
        self.install_rot13_content_filter('file1')
 
1956
        source = self.create_ab_tree()
 
1957
        target = self.make_branch_and_tree('target')
 
1958
        revision_tree = source.basis_tree()
 
1959
        revision_tree.lock_read()
 
1960
        self.addCleanup(revision_tree.unlock)
 
1961
        build_tree(revision_tree, target, source, hardlink=True)
 
1962
        target.lock_read()
 
1963
        self.addCleanup(target.unlock)
 
1964
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1965
        source_stat = os.stat('source/file1')
 
1966
        target_stat = os.stat('target/file1')
 
1967
        self.assertNotEqual(source_stat, target_stat)
 
1968
        source_stat = os.stat('source/file2')
 
1969
        target_stat = os.stat('target/file2')
 
1970
        self.assertEqualStat(source_stat, target_stat)
 
1971
 
1871
1972
    def test_case_insensitive_build_tree_inventory(self):
1872
1973
        if (tests.CaseInsensitiveFilesystemFeature.available()
1873
1974
            or tests.CaseInsCasePresFilenameFeature.available()):
1951
2052
        branch, tt = self.get_branch_and_transform()
1952
2053
        tt.new_file('file', tt.root, 'contents', 'file-id')
1953
2054
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
1954
 
        tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
 
2055
        if SymlinkFeature.available():
 
2056
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
1955
2057
        rev = tt.commit(branch, 'message')
1956
2058
        tree = branch.basis_tree()
1957
2059
        self.assertEqual('file', tree.id2path('file-id'))
1958
2060
        self.assertEqual('contents', tree.get_file_text('file-id'))
1959
2061
        self.assertEqual('dir', tree.id2path('dir-id'))
1960
 
        self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
1961
 
        self.assertEqual('target', tree.get_symlink_target('symlink-id'))
 
2062
        if SymlinkFeature.available():
 
2063
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
 
2064
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
1962
2065
 
1963
2066
    def test_add_unversioned(self):
1964
2067
        branch, tt = self.get_branch_and_transform()
2268
2371
    def test_ignore_pb(self):
2269
2372
        # pb could be supported, but TT.iter_changes doesn't support it.
2270
2373
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
2271
 
        preview_tree.iter_changes(revision_tree, pb=progress.DummyProgress())
 
2374
        preview_tree.iter_changes(revision_tree)
2272
2375
 
2273
2376
    def test_kind(self):
2274
2377
        revision_tree = self.create_tree()
2454
2557
        self.assertEqual(('missing', None, None, None), summary)
2455
2558
 
2456
2559
    def test_file_content_summary_executable(self):
2457
 
        if not osutils.supports_executable():
2458
 
            raise TestNotApplicable()
2459
2560
        preview = self.get_empty_preview()
2460
2561
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2461
2562
        preview.set_executability(True, path_id)
2470
2571
        self.assertIs(None, summary[3])
2471
2572
 
2472
2573
    def test_change_executability(self):
2473
 
        if not osutils.supports_executable():
2474
 
            raise TestNotApplicable()
2475
2574
        tree = self.make_branch_and_tree('tree')
2476
2575
        self.build_tree(['tree/path'])
2477
2576
        tree.add('path')
2491
2590
        # size must be known
2492
2591
        self.assertEqual(len('contents'), summary[1])
2493
2592
        # not executable
2494
 
        if osutils.supports_executable():
2495
 
            self.assertEqual(False, summary[2])
2496
 
        else:
2497
 
            self.assertEqual(None, summary[2])
 
2593
        self.assertEqual(False, summary[2])
2498
2594
        # will not have hash (not cheap to determine)
2499
2595
        self.assertIs(None, summary[3])
2500
2596
 
2643
2739
        preview = self.get_empty_preview()
2644
2740
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2645
2741
        # FIXME: new_directory should mark root.
2646
 
        preview.adjust_path('', ROOT_PARENT, root)
 
2742
        preview.fixup_new_roots()
2647
2743
        preview_tree = preview.get_preview_tree()
2648
2744
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2649
2745
                                         'a-id')
2683
2779
        file_trans_id = preview.trans_id_file_id('file-id')
2684
2780
        preview.delete_contents(file_trans_id)
2685
2781
        preview.create_file('a\nb\n', file_trans_id)
2686
 
        pb = progress.DummyProgress()
2687
2782
        preview_tree = preview.get_preview_tree()
2688
 
        merger = Merger.from_revision_ids(pb, preview_tree,
 
2783
        merger = Merger.from_revision_ids(None, preview_tree,
2689
2784
                                          child_tree.branch.last_revision(),
2690
2785
                                          other_branch=child_tree.branch,
2691
2786
                                          tree_branch=work_tree.branch)
2703
2798
        tt.new_file('name', tt.root, 'content', 'file-id')
2704
2799
        tree2 = self.make_branch_and_tree('tree2')
2705
2800
        tree2.set_root_id('TREE_ROOT')
2706
 
        pb = progress.DummyProgress()
2707
2801
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2708
 
                                         pb, tree.basis_tree())
 
2802
                                         None, tree.basis_tree())
2709
2803
        merger.merge_type = Merge3Merger
2710
2804
        merger.do_merge()
2711
2805
 
2721
2815
        tt.create_file('baz', trans_id)
2722
2816
        tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
2723
2817
        self.build_tree_contents([('tree2/foo', 'qux')])
2724
 
        pb = progress.DummyProgress()
 
2818
        pb = None
2725
2819
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2726
2820
                                         pb, tree.basis_tree())
2727
2821
        merger.merge_type = Merge3Merger
2759
2853
        branch = self.make_branch('any')
2760
2854
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2761
2855
        tt = TransformPreview(tree)
 
2856
        self.addCleanup(tt.finalize)
2762
2857
        foo_id = tt.new_directory('', ROOT_PARENT)
2763
2858
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2764
2859
        limbo_path = tt._limbo_name(bar_id)