/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1852.16.5 by John Arbash Meinel
[merge] bzr.dev 2255, resolve conflicts, update copyrights
1
# Copyright (C) 2006, 2007 Canonical Ltd
1852.15.9 by Robert Collins
Add missing test script.
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 extra cases that WorkingTree.walkdirs can encounter."""
18
19
import os
20
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
21
from bzrlib import transform
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
22
from bzrlib.osutils import has_symlinks
23
from bzrlib.tests import TestSkipped
1852.15.9 by Robert Collins
Add missing test script.
24
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
25
26
# tests to write:
27
# type mismatches - file to link, dir, dir to file, link, link to file, dir
28
29
class TestWalkdirs(TestCaseWithWorkingTree):
30
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
31
    added='added'
32
    missing='missing'
33
    unknown='unknown'
34
35
    class DirBlock:
36
37
        def __init__(self, tree, file_path, file_name=None, id=None,
38
                     inventory_kind=None, stat=None, disk_kind='unknown'):
39
            self.file_path = file_path
40
            self.abspath = tree.abspath(file_path)
41
            self.relpath = tree.relpath(file_path)
42
            if file_name == None:
43
               file_name = os.path.split(file_path)[-1]
44
               if len(file_name) == 0:
45
                   file_name = os.path.split(file_path)[-2]
46
            self.file_name = file_name
47
            self.id = id
48
            self.inventory_kind = inventory_kind
49
            self.stat = stat
50
            self.disk_kind = disk_kind
51
52
        def as_tuple(self):
53
             return (self.relpath, self.file_name, self.disk_kind,
54
                     self.stat, self.id, self.inventory_kind)
55
56
        def as_dir_tuple(self):
57
             return (self.relpath, self.id)
58
59
        def __str__(self):
60
            return """
61
file_path      = %r
62
abspath        = %r
63
relpath        = %r
64
file_name      = %r
65
id             = %r
66
inventory_kind = %r
67
stat           = %r
68
disk_kind      = %r""" % (self.file_path, self.abspath, self.relpath,
69
            self.file_name, self.id, self.inventory_kind, self.stat,
70
            self.disk_kind)
71
72
    def get_tree(self, file_status, prefix=None):
1852.15.9 by Robert Collins
Add missing test script.
73
        tree = self.make_branch_and_tree('.')
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
74
        dirblocks = []
75
        paths = [
76
            file_status + ' file',
77
            file_status + ' dir/',
78
            file_status + ' dir/a file',
79
            file_status + ' empty dir/',
80
            ]
81
        self.build_tree(paths)
82
83
        def add_dirblock(path, kind):
84
            dirblock = TestWalkdirs.DirBlock(tree, path)
85
            if file_status != self.unknown:
86
                dirblock.id = 'a ' + str(path).replace('/','-') + '-id'
87
                dirblock.inventory_kind = kind
88
            if file_status != self.missing:
89
                dirblock.disk_kind = kind
90
                dirblock.stat = os.lstat(dirblock.relpath)
91
            dirblocks.append(dirblock)
92
93
        add_dirblock(paths[0], 'file')
94
        add_dirblock(paths[1], 'directory')
95
        add_dirblock(paths[2], 'file')
96
        add_dirblock(paths[3], 'directory')
97
98
        if file_status != self.unknown:
99
            tree.add(paths, [db.id for db in dirblocks])
100
101
        if file_status == self.missing:
102
            # now make the files be missing
103
            tree.bzrdir.root_transport.delete(dirblocks[0].relpath)
104
            tree.bzrdir.root_transport.delete_tree(dirblocks[1].relpath)
105
            tree.bzrdir.root_transport.delete_tree(dirblocks[3].relpath)
106
1852.15.9 by Robert Collins
Add missing test script.
107
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
108
            (('', tree.path2id('')),
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
109
             [dirblocks[1].as_tuple(), dirblocks[3].as_tuple(),
110
              dirblocks[0].as_tuple()]
111
            ),
112
            (dirblocks[1].as_dir_tuple(),
113
             [dirblocks[2].as_tuple()]
114
            ),
115
            (dirblocks[3].as_dir_tuple(),
116
             []
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
117
            ),
118
            ]
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
119
        if prefix:
120
            expected_dirblocks = [e for e in expected_dirblocks
121
                if len(e) > 0 and len(e[0]) > 0 and e[0][0] == prefix]
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
122
        return tree, expected_dirblocks
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
123
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
124
    def test_walkdir_unknowns(self):
125
        """unknown files and directories should be reported by walkdirs."""
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
126
        # test that its iterable by iterating:
1852.15.9 by Robert Collins
Add missing test script.
127
        result = []
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
128
        tree, expected_dirblocks = self.get_tree(self.unknown)
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
129
        tree.lock_read()
1852.15.9 by Robert Collins
Add missing test script.
130
        for dirinfo, dirblock in tree.walkdirs():
131
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
132
        tree.unlock()
1852.15.9 by Robert Collins
Add missing test script.
133
        # check each return value for debugging ease.
134
        for pos, item in enumerate(expected_dirblocks):
135
            self.assertEqual(item, result[pos])
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
136
        self.assertEqual(len(expected_dirblocks), len(result))
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
137
138
    def test_walkdir_from_unknown_dir(self):
139
        """Doing a walkdir when the requested prefix is unknown but on disk."""
140
        result = []
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
141
        prefix = 'unknown dir'
142
        tree, expected_dirblocks = self.get_tree(self.unknown, prefix)
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
143
        tree.lock_read()
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
144
        for dirinfo, dirblock in tree.walkdirs(prefix):
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
145
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
146
        tree.unlock()
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
147
        # check each return value for debugging ease.
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
148
        for pos, item in enumerate(expected_dirblocks):
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
149
            self.assertEqual(item, result[pos])
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
150
        self.assertEqual(len(expected_dirblocks), len(result))
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
151
    
152
    def test_walkdir_missings(self):
153
        """missing files and directories should be reported by walkdirs."""
154
        # test that its iterable by iterating:
155
        result = []
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
156
        tree, expected_dirblocks = self.get_tree(self.missing)
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
157
        tree.lock_read()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
158
        for dirinfo, dirblock in tree.walkdirs():
159
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
160
        tree.unlock()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
161
        # check each return value for debugging ease.
162
        for pos, item in enumerate(expected_dirblocks):
163
            self.assertEqual(item, result[pos])
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
164
        self.assertEqual(expected_dirblocks, result)
165
        self.assertEqual(len(expected_dirblocks), len(result))
166
167
    def test_walkdir_from_dir(self):
168
        """Doing a walkdir when the requested prefix is known and on disk."""
169
        result = []
170
        prefix = 'added dir'
171
        tree, expected_dirblocks = self.get_tree(self.added, prefix)
172
        tree.lock_read()
173
        for dirinfo, dirblock in tree.walkdirs(prefix):
174
            result.append((dirinfo, list(dirblock)))
175
        tree.unlock()
176
177
        # check each return value for debugging ease.
178
        for pos, item in enumerate(expected_dirblocks):
179
            result_pos = []
180
            if len(result) > pos:
181
                result_pos = result[pos]
182
            self.assertEqual(item, result_pos)
183
184
        self.assertEqual(len(expected_dirblocks), len(result))
185
186
    def test_walkdir_from_empty_dir(self):
187
        """Doing a walkdir when the requested prefix is empty dir."""
188
        result = []
189
        prefix = 'added empty dir'
190
        tree, expected_dirblocks = self.get_tree(self.added, prefix)
191
        tree.lock_read()
192
        for dirinfo, dirblock in tree.walkdirs(prefix):
193
            result.append((dirinfo, list(dirblock)))
194
        tree.unlock()
195
196
        # check each return value for debugging ease.
197
        for pos, item in enumerate(expected_dirblocks):
198
            result_pos = []
199
            if len(result) > pos:
200
                result_pos = result[pos]
201
            self.assertEqual(item, result_pos)
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
202
        self.assertEqual(len(expected_dirblocks), len(result))
203
204
    def test_walkdir_from_missing_dir(self):
205
        """Doing a walkdir when the requested prefix is missing but on disk."""
206
        result = []
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
207
        prefix = 'missing dir'
208
        tree, expected_dirblocks = self.get_tree(self.missing, prefix)
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
209
        tree.lock_read()
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
210
        for dirinfo, dirblock in tree.walkdirs(prefix):
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
211
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
212
        tree.unlock()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
213
        # check each return value for debugging ease.
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
214
        for pos, item in enumerate(expected_dirblocks):
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
215
            self.assertEqual(item, result[pos])
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
216
        self.assertEqual(len(expected_dirblocks), len(result))
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
217
218
    def test_walkdirs_type_changes(self):
219
        """Walkdir shows the actual kinds on disk and the recorded kinds."""
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
220
        if not has_symlinks():
221
            raise TestSkipped('No symlink support')
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
222
        tree = self.make_branch_and_tree('.')
223
        paths = ['file1', 'file2', 'dir1/', 'dir2/']
224
        ids = ['file1', 'file2', 'dir1', 'dir2']
225
        self.build_tree(paths)
226
        tree.add(paths, ids)
227
        tt = transform.TreeTransform(tree)
228
        root_transaction_id = tt.trans_id_tree_path('')
229
        tt.new_symlink('link1',
230
            root_transaction_id, 'link-target', 'link1')
231
        tt.new_symlink('link2',
232
            root_transaction_id, 'link-target', 'link2')
233
        tt.apply()
234
        tree.bzrdir.root_transport.delete_tree('dir1')
235
        tree.bzrdir.root_transport.delete_tree('dir2')
236
        tree.bzrdir.root_transport.delete('file1')
237
        tree.bzrdir.root_transport.delete('file2')
238
        tree.bzrdir.root_transport.delete('link1')
239
        tree.bzrdir.root_transport.delete('link2')
240
        changed_paths = ['dir1', 'file1/', 'link1', 'link2/']
241
        self.build_tree(changed_paths)
242
        os.symlink('target', 'dir2')
243
        os.symlink('target', 'file2')
244
        dir1_stat = os.lstat('dir1')
245
        dir2_stat = os.lstat('dir2')
246
        file1_stat = os.lstat('file1')
247
        file2_stat = os.lstat('file2')
248
        link1_stat = os.lstat('link1')
249
        link2_stat = os.lstat('link2')
250
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
251
             (('', tree.path2id('')),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
252
              [('dir1', 'dir1', 'file', dir1_stat, 'dir1', 'directory'),
253
               ('dir2', 'dir2', 'symlink', dir2_stat, 'dir2', 'directory'),
254
               ('file1', 'file1', 'directory', file1_stat, 'file1', 'file'),
255
               ('file2', 'file2', 'symlink', file2_stat, 'file2', 'file'),
256
               ('link1', 'link1', 'file', link1_stat, 'link1', 'symlink'),
257
               ('link2', 'link2', 'directory', link2_stat, 'link2', 'symlink'),
258
              ]
259
             ),
260
             (('dir1', 'dir1'),
261
              [
262
              ]
263
             ),
264
             (('dir2', 'dir2'),
265
              [
266
              ]
267
             ),
268
             (('file1', None),
269
              [
270
              ]
271
             ),
272
             (('link2', None),
273
              [
274
              ]
275
             ),
276
            ]
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
277
        tree.lock_read()
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
278
        result = list(tree.walkdirs())
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
279
        tree.unlock()
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
280
        # check each return value for debugging ease.
281
        for pos, item in enumerate(expected_dirblocks):
282
            self.assertEqual(item, result[pos])
283
        self.assertEqual(len(expected_dirblocks), len(result))
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
284
285
    def test_walkdirs_type_changes_wo_symlinks(self):
286
        # similar to test_walkdirs_type_changes
287
        # but don't use symlinks for safe testing on win32
288
        tree = self.make_branch_and_tree('.')
289
        paths = ['file1', 'dir1/']
290
        ids = ['file1', 'dir1']
291
        self.build_tree(paths)
292
        tree.add(paths, ids)
293
        tree.bzrdir.root_transport.delete_tree('dir1')
294
        tree.bzrdir.root_transport.delete('file1')
295
        changed_paths = ['dir1', 'file1/']
296
        self.build_tree(changed_paths)
297
        dir1_stat = os.lstat('dir1')
298
        file1_stat = os.lstat('file1')
299
        expected_dirblocks = [
300
             (('', tree.path2id('')),
301
              [('dir1', 'dir1', 'file', dir1_stat, 'dir1', 'directory'),
302
               ('file1', 'file1', 'directory', file1_stat, 'file1', 'file'),
303
              ]
304
             ),
305
             (('dir1', 'dir1'),
306
              [
307
              ]
308
             ),
309
             (('file1', None),
310
              [
311
              ]
312
             ),
313
            ]
314
        tree.lock_read()
315
        result = list(tree.walkdirs())
316
        tree.unlock()
317
        # check each return value for debugging ease.
318
        for pos, item in enumerate(expected_dirblocks):
319
            self.assertEqual(item, result[pos])
320
        self.assertEqual(len(expected_dirblocks), len(result))