~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-09-19 00:32:14 UTC
  • mfrom: (4685.2.1 bzr.dev)
  • Revision ID: pqm@pqm.ubuntu.com-20090919003214-2dli9jc4y5xhjj3n
(mbp for garyvdm) Revert rename of
        test_merge_uncommitted_otherbasis_ancestor_of_thisbasis.

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,
28
27
    progress,
29
28
    revision as _mod_revision,
30
 
    rules,
 
29
    symbol_versioning,
31
30
    tests,
32
31
    urlutils,
33
32
    )
137
136
        transform.finalize()
138
137
        transform.finalize()
139
138
 
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
 
 
193
139
    def test_hardlink(self):
194
140
        self.requireFeature(HardlinkFeature)
195
141
        transform, root = self.get_transform()
423
369
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
424
370
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
425
371
 
426
 
    def test_adjust_path_updates_child_limbo_names(self):
427
 
        tree = self.make_branch_and_tree('tree')
428
 
        transform = TreeTransform(tree)
429
 
        self.addCleanup(transform.finalize)
430
 
        foo_id = transform.new_directory('foo', transform.root)
431
 
        bar_id = transform.new_directory('bar', foo_id)
432
 
        baz_id = transform.new_directory('baz', bar_id)
433
 
        qux_id = transform.new_directory('qux', baz_id)
434
 
        transform.adjust_path('quxx', foo_id, bar_id)
435
 
        self.assertStartsWith(transform._limbo_name(qux_id),
436
 
                              transform._limbo_name(bar_id))
437
 
 
438
372
    def test_add_del(self):
439
373
        start, root = self.get_transform()
440
374
        start.new_directory('a', root, 'a')
812
746
        create.apply()
813
747
        transform, root = self.get_transform()
814
748
        transform.adjust_root_path('oldroot', fun)
815
 
        new_root = transform.trans_id_tree_path('')
 
749
        new_root=transform.trans_id_tree_path('')
816
750
        transform.version_file('new-root', new_root)
817
751
        transform.apply()
818
752
 
1922
1856
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1923
1857
        self.assertTrue(source.is_executable('file1-id'))
1924
1858
 
1925
 
    def install_rot13_content_filter(self, pattern):
1926
 
        original_registry = filters._reset_registry()
1927
 
        def restore_registry():
1928
 
            filters._reset_registry(original_registry)
1929
 
        self.addCleanup(restore_registry)
1930
 
        def rot13(chunks, context=None):
1931
 
            return [''.join(chunks).encode('rot13')]
1932
 
        rot13filter = filters.ContentFilter(rot13, rot13)
1933
 
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
1934
 
        os.mkdir(self.test_home_dir + '/.bazaar')
1935
 
        rules_filename = self.test_home_dir + '/.bazaar/rules'
1936
 
        f = open(rules_filename, 'wb')
1937
 
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
1938
 
        f.close()
1939
 
        def uninstall_rules():
1940
 
            os.remove(rules_filename)
1941
 
            rules.reset_rules()
1942
 
        self.addCleanup(uninstall_rules)
1943
 
        rules.reset_rules()
1944
 
 
1945
 
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
1946
 
        """build_tree will not hardlink files that have content filtering rules
1947
 
        applied to them (but will still hardlink other files from the same tree
1948
 
        if it can).
1949
 
        """
1950
 
        self.requireFeature(HardlinkFeature)
1951
 
        self.install_rot13_content_filter('file1')
1952
 
        source = self.create_ab_tree()
1953
 
        target = self.make_branch_and_tree('target')
1954
 
        revision_tree = source.basis_tree()
1955
 
        revision_tree.lock_read()
1956
 
        self.addCleanup(revision_tree.unlock)
1957
 
        build_tree(revision_tree, target, source, hardlink=True)
1958
 
        target.lock_read()
1959
 
        self.addCleanup(target.unlock)
1960
 
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1961
 
        source_stat = os.stat('source/file1')
1962
 
        target_stat = os.stat('target/file1')
1963
 
        self.assertNotEqual(source_stat, target_stat)
1964
 
        source_stat = os.stat('source/file2')
1965
 
        target_stat = os.stat('target/file2')
1966
 
        self.assertEqualStat(source_stat, target_stat)
1967
 
 
1968
1859
    def test_case_insensitive_build_tree_inventory(self):
1969
1860
        if (tests.CaseInsensitiveFilesystemFeature.available()
1970
1861
            or tests.CaseInsCasePresFilenameFeature.available()):
2048
1939
        branch, tt = self.get_branch_and_transform()
2049
1940
        tt.new_file('file', tt.root, 'contents', 'file-id')
2050
1941
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
2051
 
        if SymlinkFeature.available():
2052
 
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
 
1942
        tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
2053
1943
        rev = tt.commit(branch, 'message')
2054
1944
        tree = branch.basis_tree()
2055
1945
        self.assertEqual('file', tree.id2path('file-id'))
2056
1946
        self.assertEqual('contents', tree.get_file_text('file-id'))
2057
1947
        self.assertEqual('dir', tree.id2path('dir-id'))
2058
 
        if SymlinkFeature.available():
2059
 
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
2060
 
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
 
1948
        self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
 
1949
        self.assertEqual('target', tree.get_symlink_target('symlink-id'))
2061
1950
 
2062
1951
    def test_add_unversioned(self):
2063
1952
        branch, tt = self.get_branch_and_transform()
2388
2277
        self.assertEqual(os.stat(limbo_path).st_mtime,
2389
2278
                         preview_tree.get_file_mtime('file-id'))
2390
2279
 
2391
 
    def test_get_file_mtime_renamed(self):
2392
 
        work_tree = self.make_branch_and_tree('tree')
2393
 
        self.build_tree(['tree/file'])
2394
 
        work_tree.add('file', 'file-id')
2395
 
        preview = TransformPreview(work_tree)
2396
 
        self.addCleanup(preview.finalize)
2397
 
        file_trans_id = preview.trans_id_tree_file_id('file-id')
2398
 
        preview.adjust_path('renamed', preview.root, file_trans_id)
2399
 
        preview_tree = preview.get_preview_tree()
2400
 
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
2401
 
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
2402
 
 
2403
2280
    def test_get_file(self):
2404
2281
        preview = self.get_empty_preview()
2405
2282
        preview.new_file('file', preview.root, 'contents', 'file-id')
2553
2430
        self.assertEqual(('missing', None, None, None), summary)
2554
2431
 
2555
2432
    def test_file_content_summary_executable(self):
 
2433
        if not osutils.supports_executable():
 
2434
            raise TestNotApplicable()
2556
2435
        preview = self.get_empty_preview()
2557
2436
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2558
2437
        preview.set_executability(True, path_id)
2567
2446
        self.assertIs(None, summary[3])
2568
2447
 
2569
2448
    def test_change_executability(self):
 
2449
        if not osutils.supports_executable():
 
2450
            raise TestNotApplicable()
2570
2451
        tree = self.make_branch_and_tree('tree')
2571
2452
        self.build_tree(['tree/path'])
2572
2453
        tree.add('path')
2586
2467
        # size must be known
2587
2468
        self.assertEqual(len('contents'), summary[1])
2588
2469
        # not executable
2589
 
        self.assertEqual(False, summary[2])
 
2470
        if osutils.supports_executable():
 
2471
            self.assertEqual(False, summary[2])
 
2472
        else:
 
2473
            self.assertEqual(None, summary[2])
2590
2474
        # will not have hash (not cheap to determine)
2591
2475
        self.assertIs(None, summary[3])
2592
2476
 
2733
2617
 
2734
2618
    def test_walkdirs(self):
2735
2619
        preview = self.get_empty_preview()
2736
 
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
2737
 
        # FIXME: new_directory should mark root.
2738
 
        preview.fixup_new_roots()
 
2620
        preview.version_file('tree-root', preview.root)
2739
2621
        preview_tree = preview.get_preview_tree()
2740
2622
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2741
2623
                                         'a-id')
2772
2654
        self.addCleanup(work_tree.unlock)
2773
2655
        preview = TransformPreview(work_tree)
2774
2656
        self.addCleanup(preview.finalize)
 
2657
        preview_tree = preview.get_preview_tree()
2775
2658
        file_trans_id = preview.trans_id_file_id('file-id')
2776
2659
        preview.delete_contents(file_trans_id)
2777
2660
        preview.create_file('a\nb\n', file_trans_id)
2778
2661
        pb = progress.DummyProgress()
2779
 
        preview_tree = preview.get_preview_tree()
2780
2662
        merger = Merger.from_revision_ids(pb, preview_tree,
2781
2663
                                          child_tree.branch.last_revision(),
2782
2664
                                          other_branch=child_tree.branch,
2846
2728
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2847
2729
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2848
2730
 
2849
 
    def test_ascii_limbo_paths(self):
2850
 
        self.requireFeature(tests.UnicodeFilenameFeature)
2851
 
        branch = self.make_branch('any')
2852
 
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
2853
 
        tt = TransformPreview(tree)
2854
 
        self.addCleanup(tt.finalize)
2855
 
        foo_id = tt.new_directory('', ROOT_PARENT)
2856
 
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
2857
 
        limbo_path = tt._limbo_name(bar_id)
2858
 
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
2859
 
 
2860
2731
 
2861
2732
class FakeSerializer(object):
2862
2733
    """Serializer implementation that simply returns the input.