/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/tree_implementations/test_walkdirs.py

Merge with serialize-transform

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the generic Tree.walkdirs interface."""
 
18
 
 
19
import os
 
20
 
 
21
from bzrlib import tests
 
22
from bzrlib.osutils import has_symlinks
 
23
from bzrlib.tests.tree_implementations import TestCaseWithTree
 
24
 
 
25
 
 
26
class TestWalkdirs(TestCaseWithTree):
 
27
 
 
28
    def get_all_subdirs_expected(self, tree, symlinks):
 
29
        if symlinks:
 
30
            return [
 
31
                (('', tree.path2id('')),
 
32
                [
 
33
                 ('0file', '0file', 'file', None, '2file', 'file'),
 
34
                 ('1top-dir', '1top-dir', 'directory', None, '1top-dir', 'directory'),
 
35
                 (u'2utf\u1234file', u'2utf\u1234file', 'file', None,
 
36
                                         u'0utf\u1234file'.encode('utf8'), 'file'),
 
37
                 ('symlink', 'symlink', 'symlink', None, 'symlink', 'symlink')
 
38
                ]),
 
39
                (('1top-dir', '1top-dir'),
 
40
                [('1top-dir/0file-in-1topdir', '0file-in-1topdir', 'file', None, '1file-in-1topdir', 'file'),
 
41
                 ('1top-dir/1dir-in-1topdir', '1dir-in-1topdir', 'directory', None, '0dir-in-1topdir', 'directory'),
 
42
                ]),
 
43
                (('1top-dir/1dir-in-1topdir', '0dir-in-1topdir'),
 
44
                [
 
45
                ]),
 
46
                ]
 
47
        else:
 
48
            return [
 
49
                (('', tree.path2id('')),
 
50
                [
 
51
                 ('0file', '0file', 'file', None, '2file', 'file'),
 
52
                 ('1top-dir', '1top-dir', 'directory', None, '1top-dir', 'directory'),
 
53
                 (u'2utf\u1234file', u'2utf\u1234file', 'file', None,
 
54
                                         u'0utf\u1234file'.encode('utf8'), 'file'),
 
55
                ]),
 
56
                (('1top-dir', '1top-dir'),
 
57
                [('1top-dir/0file-in-1topdir', '0file-in-1topdir', 'file', None, '1file-in-1topdir', 'file'),
 
58
                 ('1top-dir/1dir-in-1topdir', '1dir-in-1topdir', 'directory', None, '0dir-in-1topdir', 'directory'),
 
59
                ]),
 
60
                (('1top-dir/1dir-in-1topdir', '0dir-in-1topdir'),
 
61
                [
 
62
                ]),
 
63
                ]
 
64
 
 
65
    def test_walkdir_root(self):
 
66
        tree = self.get_tree_with_subdirs_and_all_supported_content_types(has_symlinks())
 
67
        tree.lock_read()
 
68
        expected_dirblocks = self.get_all_subdirs_expected(tree, has_symlinks())
 
69
        # test that its iterable by iterating
 
70
        result = []
 
71
        for dirinfo, block in tree.walkdirs():
 
72
            newblock = []
 
73
            for row in block:
 
74
                if row[4] is not None:
 
75
                    newblock.append(row[0:3] + (None,) + row[4:])
 
76
                else:
 
77
                    newblock.append(row)
 
78
            result.append((dirinfo, newblock))
 
79
        tree.unlock()
 
80
        # check each return value for debugging ease.
 
81
        for pos, item in enumerate(expected_dirblocks):
 
82
            self.assertEqual(item, result[pos])
 
83
        self.assertEqual(len(expected_dirblocks), len(result))
 
84
            
 
85
    def test_walkdir_subtree(self):
 
86
        tree = self.get_tree_with_subdirs_and_all_supported_content_types(has_symlinks())
 
87
        # test that its iterable by iterating
 
88
        result = []
 
89
        tree.lock_read()
 
90
        expected_dirblocks = self.get_all_subdirs_expected(tree, has_symlinks())[1:]
 
91
        for dirinfo, block in tree.walkdirs('1top-dir'):
 
92
            newblock = []
 
93
            for row in block:
 
94
                if row[4] is not None:
 
95
                    newblock.append(row[0:3] + (None,) + row[4:])
 
96
                else:
 
97
                    newblock.append(row)
 
98
            result.append((dirinfo, newblock))
 
99
        tree.unlock()
 
100
        # check each return value for debugging ease.
 
101
        for pos, item in enumerate(expected_dirblocks):
 
102
            self.assertEqual(item, result[pos])
 
103
        self.assertEqual(len(expected_dirblocks), len(result))
 
104
 
 
105
    def test_walkdir_versioned_kind(self):
 
106
        work_tree = self.make_branch_and_tree('tree')
 
107
        work_tree.set_root_id('tree-root')
 
108
        self.build_tree(['tree/file', 'tree/dir/'])
 
109
        work_tree.add(['file', 'dir'], ['file-id', 'dir-id'])
 
110
        os.unlink('tree/file')
 
111
        os.rmdir('tree/dir')
 
112
        tree = self._convert_tree(work_tree)
 
113
        tree.lock_read()
 
114
        self.addCleanup(tree.unlock)
 
115
        if tree.path2id('file') is None:
 
116
            raise tests.TestNotApplicable(
 
117
                'Tree type cannot represent dangling ids.')
 
118
        expected = [(('', 'tree-root'), [
 
119
            ('dir', 'dir', 'unknown', None, 'dir-id', 'directory'),
 
120
            ('file', 'file', 'unknown', None, 'file-id', 'file')]),
 
121
            (('dir', 'dir-id'), [])]
 
122
        self.assertEqual(expected, list(tree.walkdirs()))