/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
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1852.15.9 by Robert Collins
Add missing test script.
16
17
"""Tests for the extra cases that WorkingTree.walkdirs can encounter."""
18
19
import os
20
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
21
from breezy import transform
22
from breezy.tests.features import SymlinkFeature
23
from breezy.tests.per_workingtree import TestCaseWithWorkingTree
1852.15.9 by Robert Collins
Add missing test script.
24
25
# tests to write:
26
# type mismatches - file to link, dir, dir to file, link, link to file, dir
27
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
28
class DirBlock:
2457.2.6 by Marius Kruger
add DirBlock comment
29
    """Object representation of the tuples returned by dirstate."""
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
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)
3376.2.11 by Martin Pool
Compare to None using is/is not not ==
36
        if file_name is None:
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
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 """
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
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,
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
63
        self.file_name, self.id, self.inventory_kind, self.stat,
64
        self.disk_kind)
65
66
67
class TestWalkdirs(TestCaseWithWorkingTree):
68
69
    added='added'
70
    missing='missing'
71
    unknown='unknown'
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
72
73
    def get_tree(self, file_status, prefix=None):
1852.15.9 by Robert Collins
Add missing test script.
74
        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.
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):
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
85
            dirblock = DirBlock(tree, path)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
86
            if file_status != self.unknown:
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:
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
99
            tree.add(paths)
100
            for dirblock in dirblocks:
101
                if file_status != self.unknown:
102
                    dirblock.id = tree.path2id(dirblock.relpath)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
103
104
        if file_status == self.missing:
105
            # now make the files be missing
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
106
            tree.controldir.root_transport.delete(dirblocks[0].relpath)
107
            tree.controldir.root_transport.delete_tree(dirblocks[1].relpath)
108
            tree.controldir.root_transport.delete_tree(dirblocks[3].relpath)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
109
1852.15.9 by Robert Collins
Add missing test script.
110
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
111
            (('', tree.path2id('')),
6883.22.15 by Jelmer Vernooij
Fix remaining walkdirs test.
112
             [dirblocks[1].as_tuple()] +
6883.22.20 by Jelmer Vernooij
Fix test.
113
             ([dirblocks[3].as_tuple()]
6883.22.15 by Jelmer Vernooij
Fix remaining walkdirs test.
114
                 if (tree.has_versioned_directories() or file_status == self.unknown) else []) +
6883.18.1 by Jelmer Vernooij
Support walkdirs when used for trees without versioned directories.
115
             [dirblocks[0].as_tuple()]
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
116
            ),
117
            (dirblocks[1].as_dir_tuple(),
118
             [dirblocks[2].as_tuple()]
119
            ),
6862.5.1 by Jelmer Vernooij
Fix walkdirs tests for formats without versioned directories.
120
            ]
6883.18.1 by Jelmer Vernooij
Support walkdirs when used for trees without versioned directories.
121
        if (tree.has_versioned_directories() or
122
            file_status != self.missing):
123
            expected_dirblocks.append(
124
                (dirblocks[3].as_dir_tuple(),
125
                 []
126
                ))
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
127
        if prefix:
128
            expected_dirblocks = [e for e in expected_dirblocks
129
                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.
130
        return tree, expected_dirblocks
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
131
2457.2.7 by Marius Kruger
extract method as per review request
132
    def _test_walkdir(self, file_status, prefix=""):
133
        result = []
134
        tree, expected_dirblocks = self.get_tree(file_status, prefix)
135
        tree.lock_read()
136
        for dirinfo, dirblock in tree.walkdirs(prefix):
137
            result.append((dirinfo, list(dirblock)))
138
        tree.unlock()
139
140
        # check each return value for debugging ease.
141
        for pos, item in enumerate(expected_dirblocks):
142
            result_pos = []
143
            if len(result) > pos:
144
                result_pos = result[pos]
145
            self.assertEqual(item, result_pos)
146
        self.assertEqual(expected_dirblocks, result)
147
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
148
    def test_walkdir_unknowns(self):
149
        """unknown files and directories should be reported by walkdirs."""
2457.2.7 by Marius Kruger
extract method as per review request
150
        self._test_walkdir(self.unknown)
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
151
152
    def test_walkdir_from_unknown_dir(self):
153
        """Doing a walkdir when the requested prefix is unknown but on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
154
        self._test_walkdir(self.unknown, 'unknown dir')
155
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
156
    def test_walkdir_missings(self):
157
        """missing files and directories should be reported by walkdirs."""
2457.2.7 by Marius Kruger
extract method as per review request
158
        self._test_walkdir(self.missing)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
159
160
    def test_walkdir_from_dir(self):
161
        """Doing a walkdir when the requested prefix is known and on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
162
        self._test_walkdir(self.added, 'added dir')
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
163
164
    def test_walkdir_from_empty_dir(self):
165
        """Doing a walkdir when the requested prefix is empty dir."""
2457.2.7 by Marius Kruger
extract method as per review request
166
        self._test_walkdir(self.added, 'added empty dir')
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
167
168
    def test_walkdir_from_missing_dir(self):
169
        """Doing a walkdir when the requested prefix is missing but on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
170
        self._test_walkdir(self.missing, 'missing dir')
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
171
172
    def test_walkdirs_type_changes(self):
173
        """Walkdir shows the actual kinds on disk and the recorded kinds."""
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
174
        self.requireFeature(SymlinkFeature)
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
175
        tree = self.make_branch_and_tree('.')
176
        paths = ['file1', 'file2', 'dir1/', 'dir2/']
177
        self.build_tree(paths)
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
178
        tree.add(paths)
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
179
        tt = transform.TreeTransform(tree)
180
        root_transaction_id = tt.trans_id_tree_path('')
181
        tt.new_symlink('link1',
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
182
            root_transaction_id, 'link-target', b'link1')
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
183
        tt.new_symlink('link2',
7045.1.1 by Jelmer Vernooij
Fix another 300 tests.
184
            root_transaction_id, 'link-target', b'link2')
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
185
        tt.apply()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
186
        tree.controldir.root_transport.delete_tree('dir1')
187
        tree.controldir.root_transport.delete_tree('dir2')
188
        tree.controldir.root_transport.delete('file1')
189
        tree.controldir.root_transport.delete('file2')
190
        tree.controldir.root_transport.delete('link1')
191
        tree.controldir.root_transport.delete('link2')
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
192
        changed_paths = ['dir1', 'file1/', 'link1', 'link2/']
193
        self.build_tree(changed_paths)
194
        os.symlink('target', 'dir2')
195
        os.symlink('target', 'file2')
196
        dir1_stat = os.lstat('dir1')
197
        dir2_stat = os.lstat('dir2')
198
        file1_stat = os.lstat('file1')
199
        file2_stat = os.lstat('file2')
200
        link1_stat = os.lstat('link1')
201
        link2_stat = os.lstat('link2')
202
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
203
             (('', tree.path2id('')),
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
204
              [('dir1', 'dir1', 'file', dir1_stat, tree.path2id('dir1'),
205
                  'directory' if tree.has_versioned_directories() else None),
206
               ('dir2', 'dir2', 'symlink', dir2_stat, tree.path2id('dir2'),
207
                   'directory' if tree.has_versioned_directories() else None),
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
208
               ('file1', 'file1', 'directory', file1_stat, tree.path2id('file1'), 'file'),
209
               ('file2', 'file2', 'symlink', file2_stat, tree.path2id('file2'), 'file'),
210
               ('link1', 'link1', 'file', link1_stat, tree.path2id('link1'), 'symlink'),
211
               ('link2', 'link2', 'directory', link2_stat, tree.path2id('link2'), 'symlink'),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
212
              ]
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
213
             )]
214
        if tree.has_versioned_directories():
215
            expected_dirblocks.extend([
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
216
             (('dir1', tree.path2id('dir1')),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
217
              [
218
              ]
219
             ),
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
220
             (('dir2', tree.path2id('dir2')),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
221
              [
222
              ]
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
223
             )])
224
        expected_dirblocks.extend([
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
225
             (('file1', None),
226
              [
227
              ]
228
             ),
229
             (('link2', None),
230
              [
231
              ]
232
             ),
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
233
            ])
234
        with tree.lock_read():
235
            result = list(tree.walkdirs())
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
236
        # check each return value for debugging ease.
237
        for pos, item in enumerate(expected_dirblocks):
238
            self.assertEqual(item, result[pos])
239
        self.assertEqual(len(expected_dirblocks), len(result))
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
240
241
    def test_walkdirs_type_changes_wo_symlinks(self):
242
        # similar to test_walkdirs_type_changes
243
        # but don't use symlinks for safe testing on win32
244
        tree = self.make_branch_and_tree('.')
245
        paths = ['file1', 'dir1/']
246
        self.build_tree(paths)
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
247
        tree.add(paths)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
248
        tree.controldir.root_transport.delete_tree('dir1')
249
        tree.controldir.root_transport.delete('file1')
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
250
        changed_paths = ['dir1', 'file1/']
251
        self.build_tree(changed_paths)
252
        dir1_stat = os.lstat('dir1')
253
        file1_stat = os.lstat('file1')
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
254
        if tree.has_versioned_directories():
255
            expected_dirblocks = [
256
                 (('', tree.path2id('')),
257
                  [('dir1', 'dir1', 'file', dir1_stat, tree.path2id('dir1'), 'directory'),
258
                   ('file1', 'file1', 'directory', file1_stat, tree.path2id('file1'), 'file'),
259
                  ]
260
                 ),
261
                 (('dir1', tree.path2id('dir1')),
262
                  [
263
                  ]
264
                 ),
265
                 (('file1', None),
266
                  [
267
                  ]
268
                 ),
269
                ]
270
        else:
271
            expected_dirblocks = [
272
                 (('', tree.path2id('')),
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
273
                  [('dir1', 'dir1', 'file', dir1_stat, tree.path2id('dir1'), None),
274
                   ('file1', 'file1', 'directory', file1_stat, tree.path2id('file1'), 'file'),
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
275
                  ]
276
                 ),
277
                 (('file1', None),
278
                  [
279
                  ]
280
                 ),
281
                ]
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
282
        with tree.lock_read():
283
            result = list(tree.walkdirs())
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
284
        # check each return value for debugging ease.
285
        for pos, item in enumerate(expected_dirblocks):
286
            self.assertEqual(item, result[pos])
287
        self.assertEqual(len(expected_dirblocks), len(result))