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

merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for the extra cases that WorkingTree.walkdirs can encounter."""
18
18
 
19
19
import os
20
20
 
21
21
from bzrlib import transform
22
 
from bzrlib.osutils import has_symlinks
23
 
from bzrlib.tests import TestSkipped
 
22
from bzrlib.tests import SymlinkFeature
24
23
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
25
24
 
26
25
# tests to write:
27
26
# type mismatches - file to link, dir, dir to file, link, link to file, dir
28
27
 
 
28
class DirBlock:
 
29
    """Object representation of the tuples returned by dirstate."""
 
30
 
 
31
    def __init__(self, tree, file_path, file_name=None, id=None,
 
32
                 inventory_kind=None, stat=None, disk_kind='unknown'):
 
33
        self.file_path = file_path
 
34
        self.abspath = tree.abspath(file_path)
 
35
        self.relpath = tree.relpath(file_path)
 
36
        if file_name is None:
 
37
           file_name = os.path.split(file_path)[-1]
 
38
           if len(file_name) == 0:
 
39
               file_name = os.path.split(file_path)[-2]
 
40
        self.file_name = file_name
 
41
        self.id = id
 
42
        self.inventory_kind = inventory_kind
 
43
        self.stat = stat
 
44
        self.disk_kind = disk_kind
 
45
 
 
46
    def as_tuple(self):
 
47
         return (self.relpath, self.file_name, self.disk_kind,
 
48
                 self.stat, self.id, self.inventory_kind)
 
49
 
 
50
    def as_dir_tuple(self):
 
51
         return (self.relpath, self.id)
 
52
 
 
53
    def __str__(self):
 
54
        return """
 
55
file_path      = %r
 
56
abspath        = %r
 
57
relpath        = %r
 
58
file_name      = %r
 
59
id             = %r
 
60
inventory_kind = %r
 
61
stat           = %r
 
62
disk_kind      = %r""" % (self.file_path, self.abspath, self.relpath,
 
63
        self.file_name, self.id, self.inventory_kind, self.stat,
 
64
        self.disk_kind)
 
65
 
 
66
 
29
67
class TestWalkdirs(TestCaseWithWorkingTree):
30
68
 
31
 
    def get_tree_with_unknowns(self):
 
69
    added='added'
 
70
    missing='missing'
 
71
    unknown='unknown'
 
72
 
 
73
    def get_tree(self, file_status, prefix=None):
32
74
        tree = self.make_branch_and_tree('.')
33
 
        self.build_tree([
34
 
            'unknown file',
35
 
            'unknown dir/',
36
 
            'unknown dir/a file',
37
 
            ])
38
 
        u_f_stat = os.lstat('unknown file')
39
 
        u_d_stat = os.lstat('unknown dir')
40
 
        u_d_f_stat = os.lstat('unknown dir/a file')
 
75
        dirblocks = []
 
76
        paths = [
 
77
            file_status + ' file',
 
78
            file_status + ' dir/',
 
79
            file_status + ' dir/a file',
 
80
            file_status + ' empty dir/',
 
81
            ]
 
82
        self.build_tree(paths)
 
83
 
 
84
        def add_dirblock(path, kind):
 
85
            dirblock = DirBlock(tree, path)
 
86
            if file_status != self.unknown:
 
87
                dirblock.id = 'a ' + str(path).replace('/','-') + '-id'
 
88
                dirblock.inventory_kind = kind
 
89
            if file_status != self.missing:
 
90
                dirblock.disk_kind = kind
 
91
                dirblock.stat = os.lstat(dirblock.relpath)
 
92
            dirblocks.append(dirblock)
 
93
 
 
94
        add_dirblock(paths[0], 'file')
 
95
        add_dirblock(paths[1], 'directory')
 
96
        add_dirblock(paths[2], 'file')
 
97
        add_dirblock(paths[3], 'directory')
 
98
 
 
99
        if file_status != self.unknown:
 
100
            tree.add(paths, [db.id for db in dirblocks])
 
101
 
 
102
        if file_status == self.missing:
 
103
            # now make the files be missing
 
104
            tree.bzrdir.root_transport.delete(dirblocks[0].relpath)
 
105
            tree.bzrdir.root_transport.delete_tree(dirblocks[1].relpath)
 
106
            tree.bzrdir.root_transport.delete_tree(dirblocks[3].relpath)
 
107
 
41
108
        expected_dirblocks = [
42
109
            (('', tree.path2id('')),
43
 
             [
44
 
              ('unknown dir', 'unknown dir', 'directory', u_d_stat, None, None),
45
 
              ('unknown file', 'unknown file', 'file', u_f_stat, None, None),
46
 
             ]
47
 
            ),
48
 
            (('unknown dir', None),
49
 
             [('unknown dir/a file', 'a file', 'file', u_d_f_stat, None, None),
50
 
             ]
 
110
             [dirblocks[1].as_tuple(), dirblocks[3].as_tuple(),
 
111
              dirblocks[0].as_tuple()]
 
112
            ),
 
113
            (dirblocks[1].as_dir_tuple(),
 
114
             [dirblocks[2].as_tuple()]
 
115
            ),
 
116
            (dirblocks[3].as_dir_tuple(),
 
117
             []
51
118
            ),
52
119
            ]
 
120
        if prefix:
 
121
            expected_dirblocks = [e for e in expected_dirblocks
 
122
                if len(e) > 0 and len(e[0]) > 0 and e[0][0] == prefix]
53
123
        return tree, expected_dirblocks
54
 
    
 
124
 
 
125
    def _test_walkdir(self, file_status, prefix=""):
 
126
        result = []
 
127
        tree, expected_dirblocks = self.get_tree(file_status, prefix)
 
128
        tree.lock_read()
 
129
        for dirinfo, dirblock in tree.walkdirs(prefix):
 
130
            result.append((dirinfo, list(dirblock)))
 
131
        tree.unlock()
 
132
 
 
133
        # check each return value for debugging ease.
 
134
        for pos, item in enumerate(expected_dirblocks):
 
135
            result_pos = []
 
136
            if len(result) > pos:
 
137
                result_pos = result[pos]
 
138
            self.assertEqual(item, result_pos)
 
139
        self.assertEqual(expected_dirblocks, result)
 
140
 
55
141
    def test_walkdir_unknowns(self):
56
142
        """unknown files and directories should be reported by walkdirs."""
57
 
        # test that its iterable by iterating:
58
 
        result = []
59
 
        tree, expected_dirblocks = self.get_tree_with_unknowns()
60
 
        tree.lock_read()
61
 
        for dirinfo, dirblock in tree.walkdirs():
62
 
            result.append((dirinfo, list(dirblock)))
63
 
        tree.unlock()
64
 
        # check each return value for debugging ease.
65
 
        for pos, item in enumerate(expected_dirblocks):
66
 
            self.assertEqual(item, result[pos])
67
 
        self.assertEqual(len(expected_dirblocks), len(result))
 
143
        self._test_walkdir(self.unknown)
68
144
 
69
145
    def test_walkdir_from_unknown_dir(self):
70
146
        """Doing a walkdir when the requested prefix is unknown but on disk."""
71
 
        result = []
72
 
        tree, expected_dirblocks = self.get_tree_with_unknowns()
73
 
        tree.lock_read()
74
 
        for dirinfo, dirblock in tree.walkdirs('unknown dir'):
75
 
            result.append((dirinfo, list(dirblock)))
76
 
        tree.unlock()
77
 
        # check each return value for debugging ease.
78
 
        for pos, item in enumerate(expected_dirblocks[1:]):
79
 
            self.assertEqual(item, result[pos])
80
 
        self.assertEqual(len(expected_dirblocks) - 1, len(result))
 
147
        self._test_walkdir(self.unknown, 'unknown dir')
81
148
 
82
 
    def get_tree_with_missings(self):
83
 
        tree = self.make_branch_and_tree('.')
84
 
        paths = [
85
 
            'missing file',
86
 
            'missing dir/',
87
 
            'missing dir/a file',
88
 
            ]
89
 
        ids = [
90
 
            'a file',
91
 
            'a dir',
92
 
            'a dir-a file',
93
 
            ]
94
 
        self.build_tree(paths)
95
 
        tree.add(paths, ids)
96
 
        # now make the files be missing
97
 
        tree.bzrdir.root_transport.delete_tree('missing dir')
98
 
        tree.bzrdir.root_transport.delete('missing file')
99
 
        expected_dirblocks = [
100
 
            (('', tree.path2id('')),
101
 
             [
102
 
              ('missing dir', 'missing dir', 'unknown', None, 'a dir', 'directory'),
103
 
              ('missing file', 'missing file', 'unknown', None, 'a file', 'file'),
104
 
             ]
105
 
            ),
106
 
            (('missing dir', 'a dir'),
107
 
             [('missing dir/a file', 'a file', 'unknown', None, 'a dir-a file', 'file'),
108
 
             ]
109
 
            ),
110
 
            ]
111
 
        return tree, expected_dirblocks
112
 
    
113
149
    def test_walkdir_missings(self):
114
150
        """missing files and directories should be reported by walkdirs."""
115
 
        # test that its iterable by iterating:
116
 
        result = []
117
 
        tree, expected_dirblocks = self.get_tree_with_missings()
118
 
        tree.lock_read()
119
 
        for dirinfo, dirblock in tree.walkdirs():
120
 
            result.append((dirinfo, list(dirblock)))
121
 
        tree.unlock()
122
 
        # check each return value for debugging ease.
123
 
        for pos, item in enumerate(expected_dirblocks):
124
 
            self.assertEqual(item, result[pos])
125
 
        self.assertEqual(len(expected_dirblocks), len(result))
 
151
        self._test_walkdir(self.missing)
 
152
 
 
153
    def test_walkdir_from_dir(self):
 
154
        """Doing a walkdir when the requested prefix is known and on disk."""
 
155
        self._test_walkdir(self.added, 'added dir')
 
156
 
 
157
    def test_walkdir_from_empty_dir(self):
 
158
        """Doing a walkdir when the requested prefix is empty dir."""
 
159
        self._test_walkdir(self.added, 'added empty dir')
126
160
 
127
161
    def test_walkdir_from_missing_dir(self):
128
162
        """Doing a walkdir when the requested prefix is missing but on disk."""
129
 
        result = []
130
 
        tree, expected_dirblocks = self.get_tree_with_missings()
131
 
        tree.lock_read()
132
 
        for dirinfo, dirblock in tree.walkdirs('missing dir'):
133
 
            result.append((dirinfo, list(dirblock)))
134
 
        tree.unlock()
135
 
        # check each return value for debugging ease.
136
 
        for pos, item in enumerate(expected_dirblocks[1:]):
137
 
            self.assertEqual(item, result[pos])
138
 
        self.assertEqual(len(expected_dirblocks[1:]), len(result))
 
163
        self._test_walkdir(self.missing, 'missing dir')
139
164
 
140
165
    def test_walkdirs_type_changes(self):
141
166
        """Walkdir shows the actual kinds on disk and the recorded kinds."""
142
 
        if not has_symlinks():
143
 
            raise TestSkipped('No symlink support')
 
167
        self.requireFeature(SymlinkFeature)
144
168
        tree = self.make_branch_and_tree('.')
145
169
        paths = ['file1', 'file2', 'dir1/', 'dir2/']
146
170
        ids = ['file1', 'file2', 'dir1', 'dir2']