~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_transform.py

  • Committer: Andrew Bennetts
  • Date: 2008-01-21 23:04:54 UTC
  • mfrom: (3193 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3195.
  • Revision ID: andrew.bennetts@canonical.com-20080121230454-0mdqybl4b2tte6d4
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import os
18
18
import stat
 
19
from StringIO import StringIO
19
20
import sys
20
21
 
21
22
from bzrlib import (
22
23
    errors,
23
24
    generate_ids,
 
25
    progress,
 
26
    revision as _mod_revision,
24
27
    symbol_versioning,
25
28
    tests,
26
29
    urlutils,
29
32
from bzrlib.conflicts import (DuplicateEntry, DuplicateID, MissingParent,
30
33
                              UnversionedParent, ParentLoop, DeletingParent,
31
34
                              NonDirectoryParent)
 
35
from bzrlib.diff import show_diff_trees
32
36
from bzrlib.errors import (DuplicateKey, MalformedTransform, NoSuchFile,
33
37
                           ReusingTransform, CantMoveRoot, 
34
38
                           PathsNotVersionedError, ExistingLimbo,
46
50
from bzrlib.transform import (TreeTransform, ROOT_PARENT, FinalPaths, 
47
51
                              resolve_conflicts, cook_conflicts, 
48
52
                              find_interesting, build_tree, get_backup_name,
49
 
                              change_entry, _FileMover, resolve_checkout)
50
 
 
 
53
                              change_entry, _FileMover, resolve_checkout,
 
54
                              TransformPreview)
51
55
 
52
56
class TestTreeTransform(tests.TestCaseWithTransport):
53
57
 
251
255
        transform.new_file('FiLe', transform.root, 'content')
252
256
        result = transform.find_conflicts()
253
257
        self.assertEqual([], result)
 
258
        transform.finalize()
254
259
        # Force the tree to report that it is case insensitive, for conflict
255
260
        # generation tests
256
261
        tree.case_sensitive = False
 
262
        transform = TreeTransform(tree)
 
263
        self.addCleanup(transform.finalize)
 
264
        transform.new_file('file', transform.root, 'content')
 
265
        transform.new_file('FiLe', transform.root, 'content')
257
266
        result = transform.find_conflicts()
258
267
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
259
268
 
269
278
        transform.new_file('file', transform.root, 'content')
270
279
        result = transform.find_conflicts()
271
280
        self.assertEqual([], result)
 
281
        transform.finalize()
272
282
        # Force the tree to report that it is case insensitive, for conflict
273
283
        # generation tests
274
284
        tree.case_sensitive = False
 
285
        transform = TreeTransform(tree)
 
286
        self.addCleanup(transform.finalize)
 
287
        transform.new_file('file', transform.root, 'content')
275
288
        result = transform.find_conflicts()
276
289
        self.assertEqual([('duplicate', 'new-1', 'new-2', 'file')], result)
277
290
 
1254
1267
        this.tt.new_file('i', this.root, '1\n2\n3\n4\n', 'i')
1255
1268
        this.tt.apply()
1256
1269
        Merge3Merger(this.wt, this.wt, base.wt, other.wt)
 
1270
 
1257
1271
        # textual merge
1258
1272
        self.assertEqual(this.wt.get_file('a').read(), 'y\nb\nc\nd\bz\n')
1259
1273
        # three-way text conflict
1804
1818
        parent = tt.trans_id_file_id('parent-id')
1805
1819
        tt.new_file('file', parent, 'Contents')
1806
1820
        resolve_conflicts(tt)
 
1821
 
 
1822
 
 
1823
class TestTransformPreview(tests.TestCaseWithTransport):
 
1824
 
 
1825
    def create_tree(self):
 
1826
        tree = self.make_branch_and_tree('.')
 
1827
        self.build_tree_contents([('a', 'content 1')])
 
1828
        tree.add('a', 'a-id')
 
1829
        tree.commit('rev1', rev_id='rev1')
 
1830
        return tree.branch.repository.revision_tree('rev1')
 
1831
 
 
1832
    def get_empty_preview(self):
 
1833
        repository = self.make_repository('repo')
 
1834
        tree = repository.revision_tree(_mod_revision.NULL_REVISION)
 
1835
        return TransformPreview(tree)
 
1836
 
 
1837
    def test_transform_preview(self):
 
1838
        revision_tree = self.create_tree()
 
1839
        preview = TransformPreview(revision_tree)
 
1840
 
 
1841
    def test_transform_preview_tree(self):
 
1842
        revision_tree = self.create_tree()
 
1843
        preview = TransformPreview(revision_tree)
 
1844
        preview.get_preview_tree()
 
1845
 
 
1846
    def test_transform_new_file(self):
 
1847
        revision_tree = self.create_tree()
 
1848
        preview = TransformPreview(revision_tree)
 
1849
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
1850
        preview_tree = preview.get_preview_tree()
 
1851
        self.assertEqual(preview_tree.kind('file2-id'), 'file')
 
1852
        self.assertEqual(
 
1853
            preview_tree.get_file('file2-id').read(), 'content B\n')
 
1854
 
 
1855
    def test_diff_preview_tree(self):
 
1856
        revision_tree = self.create_tree()
 
1857
        preview = TransformPreview(revision_tree)
 
1858
        preview.new_file('file2', preview.root, 'content B\n', 'file2-id')
 
1859
        preview_tree = preview.get_preview_tree()
 
1860
        out = StringIO()
 
1861
        show_diff_trees(revision_tree, preview_tree, out)
 
1862
        lines = out.getvalue().splitlines()
 
1863
        self.assertEqual(lines[0], "=== added file 'file2'")
 
1864
        # 3 lines of diff administrivia
 
1865
        self.assertEqual(lines[4], "+content B")
 
1866
 
 
1867
    def test_transform_conflicts(self):
 
1868
        revision_tree = self.create_tree()
 
1869
        preview = TransformPreview(revision_tree)
 
1870
        preview.new_file('a', preview.root, 'content 2')
 
1871
        resolve_conflicts(preview)
 
1872
        trans_id = preview.trans_id_file_id('a-id')
 
1873
        self.assertEqual('a.moved', preview.final_name(trans_id))
 
1874
 
 
1875
    def get_tree_and_preview_tree(self):
 
1876
        revision_tree = self.create_tree()
 
1877
        preview = TransformPreview(revision_tree)
 
1878
        a_trans_id = preview.trans_id_file_id('a-id')
 
1879
        preview.delete_contents(a_trans_id)
 
1880
        preview.create_file('b content', a_trans_id)
 
1881
        preview_tree = preview.get_preview_tree()
 
1882
        return revision_tree, preview_tree
 
1883
 
 
1884
    def test_iter_changes(self):
 
1885
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1886
        root = revision_tree.inventory.root.file_id
 
1887
        self.assertEqual([('a-id', ('a', 'a'), True, (True, True),
 
1888
                          (root, root), ('a', 'a'), ('file', 'file'),
 
1889
                          (False, False))],
 
1890
                          list(preview_tree._iter_changes(revision_tree)))
 
1891
 
 
1892
    def test_wrong_tree_value_error(self):
 
1893
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1894
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1895
                              preview_tree)
 
1896
        self.assertEqual('from_tree must be transform source tree.', str(e))
 
1897
 
 
1898
    def test_include_unchanged_value_error(self):
 
1899
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1900
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1901
                              revision_tree, include_unchanged=True)
 
1902
        self.assertEqual('include_unchanged is not supported', str(e))
 
1903
 
 
1904
    def test_specific_files(self):
 
1905
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1906
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1907
                              revision_tree, specific_files=['pete'])
 
1908
        self.assertEqual('specific_files is not supported', str(e))
 
1909
 
 
1910
    def test_want_unversioned_value_error(self):
 
1911
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1912
        e = self.assertRaises(ValueError, preview_tree._iter_changes,
 
1913
                              revision_tree, want_unversioned=True)
 
1914
        self.assertEqual('want_unversioned is not supported', str(e))
 
1915
 
 
1916
    def test_ignore_extra_trees_no_specific_files(self):
 
1917
        # extra_trees is harmless without specific_files, so we'll silently
 
1918
        # accept it, even though we won't use it.
 
1919
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1920
        preview_tree._iter_changes(revision_tree, extra_trees=[preview_tree])
 
1921
 
 
1922
    def test_ignore_require_versioned_no_specific_files(self):
 
1923
        # require_versioned is meaningless without specific_files.
 
1924
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1925
        preview_tree._iter_changes(revision_tree, require_versioned=False)
 
1926
 
 
1927
    def test_ignore_pb(self):
 
1928
        # pb could be supported, but TT.iter_changes doesn't support it.
 
1929
        revision_tree, preview_tree = self.get_tree_and_preview_tree()
 
1930
        preview_tree._iter_changes(revision_tree, pb=progress.DummyProgress())
 
1931
 
 
1932
    def test_kind(self):
 
1933
        revision_tree = self.create_tree()
 
1934
        preview = TransformPreview(revision_tree)
 
1935
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
1936
        preview.new_directory('directory', preview.root, 'dir-id')
 
1937
        preview_tree = preview.get_preview_tree()
 
1938
        self.assertEqual('file', preview_tree.kind('file-id'))
 
1939
        self.assertEqual('directory', preview_tree.kind('dir-id'))
 
1940
 
 
1941
    def test_get_file_mtime(self):
 
1942
        preview = self.get_empty_preview()
 
1943
        file_trans_id = preview.new_file('file', preview.root, 'contents',
 
1944
                                         'file-id')
 
1945
        limbo_path = preview._limbo_name(file_trans_id)
 
1946
        preview_tree = preview.get_preview_tree()
 
1947
        self.assertEqual(os.stat(limbo_path).st_mtime,
 
1948
                         preview_tree.get_file_mtime('file-id'))
 
1949
 
 
1950
    def test_get_file(self):
 
1951
        preview = self.get_empty_preview()
 
1952
        preview.new_file('file', preview.root, 'contents', 'file-id')
 
1953
        preview_tree = preview.get_preview_tree()
 
1954
        tree_file = preview_tree.get_file('file-id')
 
1955
        try:
 
1956
            self.assertEqual('contents', tree_file.read())
 
1957
        finally:
 
1958
            tree_file.close()