/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: Jonathan Lange
  • Date: 2009-12-09 09:20:42 UTC
  • mfrom: (4881 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4907.
  • Revision ID: jml@canonical.com-20091209092042-s2zgqcf8f39yzxpj
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
21
20
 
22
21
from bzrlib import (
23
22
    bencode,
24
23
    errors,
 
24
    filters,
25
25
    generate_ids,
26
26
    osutils,
27
27
    progress,
28
28
    revision as _mod_revision,
29
 
    symbol_versioning,
 
29
    rules,
30
30
    tests,
31
31
    urlutils,
32
32
    )
369
369
        self.assertContainsRe(transform._limbo_name(first), 'new-1/file')
370
370
        self.assertNotContainsRe(transform._limbo_name(second), 'new-1/FiLe')
371
371
 
 
372
    def test_adjust_path_updates_child_limbo_names(self):
 
373
        tree = self.make_branch_and_tree('tree')
 
374
        transform = TreeTransform(tree)
 
375
        self.addCleanup(transform.finalize)
 
376
        foo_id = transform.new_directory('foo', transform.root)
 
377
        bar_id = transform.new_directory('bar', foo_id)
 
378
        baz_id = transform.new_directory('baz', bar_id)
 
379
        qux_id = transform.new_directory('qux', baz_id)
 
380
        transform.adjust_path('quxx', foo_id, bar_id)
 
381
        self.assertStartsWith(transform._limbo_name(qux_id),
 
382
                              transform._limbo_name(bar_id))
 
383
 
372
384
    def test_add_del(self):
373
385
        start, root = self.get_transform()
374
386
        start.new_directory('a', root, 'a')
1856
1868
        self.assertEqual([], list(target.iter_changes(revision_tree)))
1857
1869
        self.assertTrue(source.is_executable('file1-id'))
1858
1870
 
 
1871
    def install_rot13_content_filter(self, pattern):
 
1872
        original_registry = filters._reset_registry()
 
1873
        def restore_registry():
 
1874
            filters._reset_registry(original_registry)
 
1875
        self.addCleanup(restore_registry)
 
1876
        def rot13(chunks, context=None):
 
1877
            return [''.join(chunks).encode('rot13')]
 
1878
        rot13filter = filters.ContentFilter(rot13, rot13)
 
1879
        filters.register_filter_stack_map('rot13', {'yes': [rot13filter]}.get)
 
1880
        os.mkdir(self.test_home_dir + '/.bazaar')
 
1881
        rules_filename = self.test_home_dir + '/.bazaar/rules'
 
1882
        f = open(rules_filename, 'wb')
 
1883
        f.write('[name %s]\nrot13=yes\n' % (pattern,))
 
1884
        f.close()
 
1885
        def uninstall_rules():
 
1886
            os.remove(rules_filename)
 
1887
            rules.reset_rules()
 
1888
        self.addCleanup(uninstall_rules)
 
1889
        rules.reset_rules()
 
1890
 
 
1891
    def test_build_tree_content_filtered_files_are_not_hardlinked(self):
 
1892
        """build_tree will not hardlink files that have content filtering rules
 
1893
        applied to them (but will still hardlink other files from the same tree
 
1894
        if it can).
 
1895
        """
 
1896
        self.requireFeature(HardlinkFeature)
 
1897
        self.install_rot13_content_filter('file1')
 
1898
        source = self.create_ab_tree()
 
1899
        target = self.make_branch_and_tree('target')
 
1900
        revision_tree = source.basis_tree()
 
1901
        revision_tree.lock_read()
 
1902
        self.addCleanup(revision_tree.unlock)
 
1903
        build_tree(revision_tree, target, source, hardlink=True)
 
1904
        target.lock_read()
 
1905
        self.addCleanup(target.unlock)
 
1906
        self.assertEqual([], list(target.iter_changes(revision_tree)))
 
1907
        source_stat = os.stat('source/file1')
 
1908
        target_stat = os.stat('target/file1')
 
1909
        self.assertNotEqual(source_stat, target_stat)
 
1910
        source_stat = os.stat('source/file2')
 
1911
        target_stat = os.stat('target/file2')
 
1912
        self.assertEqualStat(source_stat, target_stat)
 
1913
 
1859
1914
    def test_case_insensitive_build_tree_inventory(self):
1860
1915
        if (tests.CaseInsensitiveFilesystemFeature.available()
1861
1916
            or tests.CaseInsCasePresFilenameFeature.available()):
1873
1928
        self.assertEqual('FILE', target.id2path('upper-id'))
1874
1929
 
1875
1930
 
 
1931
class TestCommitTransform(tests.TestCaseWithTransport):
 
1932
 
 
1933
    def get_branch(self):
 
1934
        tree = self.make_branch_and_tree('tree')
 
1935
        tree.lock_write()
 
1936
        self.addCleanup(tree.unlock)
 
1937
        tree.commit('empty commit')
 
1938
        return tree.branch
 
1939
 
 
1940
    def get_branch_and_transform(self):
 
1941
        branch = self.get_branch()
 
1942
        tt = TransformPreview(branch.basis_tree())
 
1943
        self.addCleanup(tt.finalize)
 
1944
        return branch, tt
 
1945
 
 
1946
    def test_commit_wrong_basis(self):
 
1947
        branch = self.get_branch()
 
1948
        basis = branch.repository.revision_tree(
 
1949
            _mod_revision.NULL_REVISION)
 
1950
        tt = TransformPreview(basis)
 
1951
        self.addCleanup(tt.finalize)
 
1952
        e = self.assertRaises(ValueError, tt.commit, branch, '')
 
1953
        self.assertEqual('TreeTransform not based on branch basis: null:',
 
1954
                         str(e))
 
1955
 
 
1956
    def test_empy_commit(self):
 
1957
        branch, tt = self.get_branch_and_transform()
 
1958
        rev = tt.commit(branch, 'my message')
 
1959
        self.assertEqual(2, branch.revno())
 
1960
        repo = branch.repository
 
1961
        self.assertEqual('my message', repo.get_revision(rev).message)
 
1962
 
 
1963
    def test_merge_parents(self):
 
1964
        branch, tt = self.get_branch_and_transform()
 
1965
        rev = tt.commit(branch, 'my message', ['rev1b', 'rev1c'])
 
1966
        self.assertEqual(['rev1b', 'rev1c'],
 
1967
                         branch.basis_tree().get_parent_ids()[1:])
 
1968
 
 
1969
    def test_first_commit(self):
 
1970
        branch = self.make_branch('branch')
 
1971
        branch.lock_write()
 
1972
        self.addCleanup(branch.unlock)
 
1973
        tt = TransformPreview(branch.basis_tree())
 
1974
        self.addCleanup(tt.finalize)
 
1975
        tt.new_directory('', ROOT_PARENT, 'TREE_ROOT')
 
1976
        rev = tt.commit(branch, 'my message')
 
1977
        self.assertEqual([], branch.basis_tree().get_parent_ids())
 
1978
        self.assertNotEqual(_mod_revision.NULL_REVISION,
 
1979
                            branch.last_revision())
 
1980
 
 
1981
    def test_first_commit_with_merge_parents(self):
 
1982
        branch = self.make_branch('branch')
 
1983
        branch.lock_write()
 
1984
        self.addCleanup(branch.unlock)
 
1985
        tt = TransformPreview(branch.basis_tree())
 
1986
        self.addCleanup(tt.finalize)
 
1987
        e = self.assertRaises(ValueError, tt.commit, branch,
 
1988
                          'my message', ['rev1b-id'])
 
1989
        self.assertEqual('Cannot supply merge parents for first commit.',
 
1990
                         str(e))
 
1991
        self.assertEqual(_mod_revision.NULL_REVISION, branch.last_revision())
 
1992
 
 
1993
    def test_add_files(self):
 
1994
        branch, tt = self.get_branch_and_transform()
 
1995
        tt.new_file('file', tt.root, 'contents', 'file-id')
 
1996
        trans_id = tt.new_directory('dir', tt.root, 'dir-id')
 
1997
        if SymlinkFeature.available():
 
1998
            tt.new_symlink('symlink', trans_id, 'target', 'symlink-id')
 
1999
        rev = tt.commit(branch, 'message')
 
2000
        tree = branch.basis_tree()
 
2001
        self.assertEqual('file', tree.id2path('file-id'))
 
2002
        self.assertEqual('contents', tree.get_file_text('file-id'))
 
2003
        self.assertEqual('dir', tree.id2path('dir-id'))
 
2004
        if SymlinkFeature.available():
 
2005
            self.assertEqual('dir/symlink', tree.id2path('symlink-id'))
 
2006
            self.assertEqual('target', tree.get_symlink_target('symlink-id'))
 
2007
 
 
2008
    def test_add_unversioned(self):
 
2009
        branch, tt = self.get_branch_and_transform()
 
2010
        tt.new_file('file', tt.root, 'contents')
 
2011
        self.assertRaises(errors.StrictCommitFailed, tt.commit, branch,
 
2012
                          'message', strict=True)
 
2013
 
 
2014
    def test_modify_strict(self):
 
2015
        branch, tt = self.get_branch_and_transform()
 
2016
        tt.new_file('file', tt.root, 'contents', 'file-id')
 
2017
        tt.commit(branch, 'message', strict=True)
 
2018
        tt = TransformPreview(branch.basis_tree())
 
2019
        self.addCleanup(tt.finalize)
 
2020
        trans_id = tt.trans_id_file_id('file-id')
 
2021
        tt.delete_contents(trans_id)
 
2022
        tt.create_file('contents', trans_id)
 
2023
        tt.commit(branch, 'message', strict=True)
 
2024
 
 
2025
    def test_commit_malformed(self):
 
2026
        """Committing a malformed transform should raise an exception.
 
2027
 
 
2028
        In this case, we are adding a file without adding its parent.
 
2029
        """
 
2030
        branch, tt = self.get_branch_and_transform()
 
2031
        parent_id = tt.trans_id_file_id('parent-id')
 
2032
        tt.new_file('file', parent_id, 'contents', 'file-id')
 
2033
        self.assertRaises(errors.MalformedTransform, tt.commit, branch,
 
2034
                          'message')
 
2035
 
 
2036
 
1876
2037
class MockTransform(object):
1877
2038
 
1878
2039
    def has_named_child(self, by_parent, parent_id, name):
2045
2206
    def create_tree(self):
2046
2207
        tree = self.make_branch_and_tree('.')
2047
2208
        self.build_tree_contents([('a', 'content 1')])
 
2209
        tree.set_root_id('TREE_ROOT')
2048
2210
        tree.add('a', 'a-id')
2049
2211
        tree.commit('rev1', rev_id='rev1')
2050
2212
        return tree.branch.repository.revision_tree('rev1')
2172
2334
        self.assertEqual(os.stat(limbo_path).st_mtime,
2173
2335
                         preview_tree.get_file_mtime('file-id'))
2174
2336
 
 
2337
    def test_get_file_mtime_renamed(self):
 
2338
        work_tree = self.make_branch_and_tree('tree')
 
2339
        self.build_tree(['tree/file'])
 
2340
        work_tree.add('file', 'file-id')
 
2341
        preview = TransformPreview(work_tree)
 
2342
        self.addCleanup(preview.finalize)
 
2343
        file_trans_id = preview.trans_id_tree_file_id('file-id')
 
2344
        preview.adjust_path('renamed', preview.root, file_trans_id)
 
2345
        preview_tree = preview.get_preview_tree()
 
2346
        preview_mtime = preview_tree.get_file_mtime('file-id', 'renamed')
 
2347
        work_mtime = work_tree.get_file_mtime('file-id', 'file')
 
2348
 
2175
2349
    def test_get_file(self):
2176
2350
        preview = self.get_empty_preview()
2177
2351
        preview.new_file('file', preview.root, 'contents', 'file-id')
2325
2499
        self.assertEqual(('missing', None, None, None), summary)
2326
2500
 
2327
2501
    def test_file_content_summary_executable(self):
2328
 
        if not osutils.supports_executable():
2329
 
            raise TestNotApplicable()
2330
2502
        preview = self.get_empty_preview()
2331
2503
        path_id = preview.new_file('path', preview.root, 'contents', 'path-id')
2332
2504
        preview.set_executability(True, path_id)
2341
2513
        self.assertIs(None, summary[3])
2342
2514
 
2343
2515
    def test_change_executability(self):
2344
 
        if not osutils.supports_executable():
2345
 
            raise TestNotApplicable()
2346
2516
        tree = self.make_branch_and_tree('tree')
2347
2517
        self.build_tree(['tree/path'])
2348
2518
        tree.add('path')
2362
2532
        # size must be known
2363
2533
        self.assertEqual(len('contents'), summary[1])
2364
2534
        # not executable
2365
 
        if osutils.supports_executable():
2366
 
            self.assertEqual(False, summary[2])
2367
 
        else:
2368
 
            self.assertEqual(None, summary[2])
 
2535
        self.assertEqual(False, summary[2])
2369
2536
        # will not have hash (not cheap to determine)
2370
2537
        self.assertIs(None, summary[3])
2371
2538
 
2512
2679
 
2513
2680
    def test_walkdirs(self):
2514
2681
        preview = self.get_empty_preview()
2515
 
        preview.version_file('tree-root', preview.root)
 
2682
        root = preview.new_directory('', ROOT_PARENT, 'tree-root')
 
2683
        # FIXME: new_directory should mark root.
 
2684
        preview.adjust_path('', ROOT_PARENT, root)
2516
2685
        preview_tree = preview.get_preview_tree()
2517
2686
        file_trans_id = preview.new_file('a', preview.root, 'contents',
2518
2687
                                         'a-id')
2549
2718
        self.addCleanup(work_tree.unlock)
2550
2719
        preview = TransformPreview(work_tree)
2551
2720
        self.addCleanup(preview.finalize)
2552
 
        preview_tree = preview.get_preview_tree()
2553
2721
        file_trans_id = preview.trans_id_file_id('file-id')
2554
2722
        preview.delete_contents(file_trans_id)
2555
2723
        preview.create_file('a\nb\n', file_trans_id)
2556
2724
        pb = progress.DummyProgress()
 
2725
        preview_tree = preview.get_preview_tree()
2557
2726
        merger = Merger.from_revision_ids(pb, preview_tree,
2558
2727
                                          child_tree.branch.last_revision(),
2559
2728
                                          other_branch=child_tree.branch,
2566
2735
 
2567
2736
    def test_merge_preview_into_workingtree(self):
2568
2737
        tree = self.make_branch_and_tree('tree')
 
2738
        tree.set_root_id('TREE_ROOT')
2569
2739
        tt = TransformPreview(tree)
2570
2740
        self.addCleanup(tt.finalize)
2571
2741
        tt.new_file('name', tt.root, 'content', 'file-id')
2572
2742
        tree2 = self.make_branch_and_tree('tree2')
 
2743
        tree2.set_root_id('TREE_ROOT')
2573
2744
        pb = progress.DummyProgress()
2574
2745
        merger = Merger.from_uncommitted(tree2, tt.get_preview_tree(),
2575
2746
                                         pb, tree.basis_tree())
2621
2792
        rev2_tree = tree.branch.repository.revision_tree(rev2_id)
2622
2793
        self.assertEqual('contents', rev2_tree.get_file_text('file_id'))
2623
2794
 
 
2795
    def test_ascii_limbo_paths(self):
 
2796
        self.requireFeature(tests.UnicodeFilenameFeature)
 
2797
        branch = self.make_branch('any')
 
2798
        tree = branch.repository.revision_tree(_mod_revision.NULL_REVISION)
 
2799
        tt = TransformPreview(tree)
 
2800
        self.addCleanup(tt.finalize)
 
2801
        foo_id = tt.new_directory('', ROOT_PARENT)
 
2802
        bar_id = tt.new_file(u'\u1234bar', foo_id, 'contents')
 
2803
        limbo_path = tt._limbo_name(bar_id)
 
2804
        self.assertEqual(limbo_path.encode('ascii', 'replace'), limbo_path)
 
2805
 
2624
2806
 
2625
2807
class FakeSerializer(object):
2626
2808
    """Serializer implementation that simply returns the input.