/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.tests.features import SymlinkFeature
22
from breezy.tests.per_workingtree import TestCaseWithWorkingTree
1852.15.9 by Robert Collins
Add missing test script.
23
24
# tests to write:
25
# type mismatches - file to link, dir, dir to file, link, link to file, dir
26
7143.15.2 by Jelmer Vernooij
Run autopep8.
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
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
31
    def __init__(self, tree, file_path, file_name=None,
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
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:
7143.15.2 by Jelmer Vernooij
Run autopep8.
37
            file_name = os.path.split(file_path)[-1]
38
            if len(file_name) == 0:
39
                file_name = os.path.split(file_path)[-2]
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
40
        self.file_name = file_name
41
        self.inventory_kind = inventory_kind
42
        self.stat = stat
43
        self.disk_kind = disk_kind
44
45
    def as_tuple(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
46
        return (self.relpath, self.file_name, self.disk_kind,
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
47
                self.stat, self.inventory_kind)
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
48
49
    def as_dir_tuple(self):
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
50
        return self.relpath
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
51
52
    def __str__(self):
53
        return """
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
54
file_path      = %r
55
abspath        = %r
56
relpath        = %r
57
file_name      = %r
58
inventory_kind = %r
59
stat           = %r
60
disk_kind      = %r""" % (self.file_path, self.abspath, self.relpath,
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
61
                          self.file_name, self.inventory_kind, self.stat,
7143.15.2 by Jelmer Vernooij
Run autopep8.
62
                          self.disk_kind)
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
63
64
65
class TestWalkdirs(TestCaseWithWorkingTree):
66
7143.15.2 by Jelmer Vernooij
Run autopep8.
67
    added = 'added'
68
    missing = 'missing'
69
    unknown = 'unknown'
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
70
71
    def get_tree(self, file_status, prefix=None):
1852.15.9 by Robert Collins
Add missing test script.
72
        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.
73
        dirblocks = []
74
        paths = [
75
            file_status + ' file',
76
            file_status + ' dir/',
77
            file_status + ' dir/a file',
78
            file_status + ' empty dir/',
79
            ]
80
        self.build_tree(paths)
81
82
        def add_dirblock(path, kind):
2457.2.5 by Marius Kruger
move DirBlock out of TestWalkdirs as per review comment
83
            dirblock = DirBlock(tree, path)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
84
            if file_status != self.unknown:
85
                dirblock.inventory_kind = kind
86
            if file_status != self.missing:
87
                dirblock.disk_kind = kind
88
                dirblock.stat = os.lstat(dirblock.relpath)
89
            dirblocks.append(dirblock)
90
91
        add_dirblock(paths[0], 'file')
92
        add_dirblock(paths[1], 'directory')
93
        add_dirblock(paths[2], 'file')
94
        add_dirblock(paths[3], 'directory')
95
96
        if file_status != self.unknown:
6829.2.1 by Jelmer Vernooij
Avoid passing in file_ids/revision_ids in a few more places.
97
            tree.add(paths)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
98
99
        if file_status == self.missing:
100
            # now make the files be missing
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
101
            tree.controldir.root_transport.delete(dirblocks[0].relpath)
102
            tree.controldir.root_transport.delete_tree(dirblocks[1].relpath)
103
            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.
104
1852.15.9 by Robert Collins
Add missing test script.
105
        expected_dirblocks = [
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
106
            ('',
6883.22.15 by Jelmer Vernooij
Fix remaining walkdirs test.
107
             [dirblocks[1].as_tuple()] +
6883.22.20 by Jelmer Vernooij
Fix test.
108
             ([dirblocks[3].as_tuple()]
6883.22.15 by Jelmer Vernooij
Fix remaining walkdirs test.
109
                 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.
110
             [dirblocks[0].as_tuple()]
7143.15.2 by Jelmer Vernooij
Run autopep8.
111
             ),
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
112
            (dirblocks[1].as_dir_tuple(),
113
             [dirblocks[2].as_tuple()]
7143.15.2 by Jelmer Vernooij
Run autopep8.
114
             ),
6862.5.1 by Jelmer Vernooij
Fix walkdirs tests for formats without versioned directories.
115
            ]
6883.18.1 by Jelmer Vernooij
Support walkdirs when used for trees without versioned directories.
116
        if (tree.has_versioned_directories() or
7143.15.2 by Jelmer Vernooij
Run autopep8.
117
                file_status != self.missing):
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
118
            expected_dirblocks.append((dirblocks[3].as_dir_tuple(), []))
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
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
121
                                  if len(e) > 0 and len(e[0]) > 0 and e[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
2457.2.7 by Marius Kruger
extract method as per review request
124
    def _test_walkdir(self, file_status, prefix=""):
125
        result = []
126
        tree, expected_dirblocks = self.get_tree(file_status, prefix)
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
127
        with tree.lock_read():
128
            for dirpath, dirblock in tree.walkdirs(prefix):
129
                result.append((dirpath, list(dirblock)))
2457.2.7 by Marius Kruger
extract method as per review request
130
131
        # check each return value for debugging ease.
132
        for pos, item in enumerate(expected_dirblocks):
133
            result_pos = []
134
            if len(result) > pos:
135
                result_pos = result[pos]
136
            self.assertEqual(item, result_pos)
137
        self.assertEqual(expected_dirblocks, result)
138
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
139
    def test_walkdir_unknowns(self):
140
        """unknown files and directories should be reported by walkdirs."""
2457.2.7 by Marius Kruger
extract method as per review request
141
        self._test_walkdir(self.unknown)
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
142
143
    def test_walkdir_from_unknown_dir(self):
144
        """Doing a walkdir when the requested prefix is unknown but on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
145
        self._test_walkdir(self.unknown, 'unknown dir')
146
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
147
    def test_walkdir_missings(self):
148
        """missing files and directories should be reported by walkdirs."""
2457.2.7 by Marius Kruger
extract method as per review request
149
        self._test_walkdir(self.missing)
2457.2.1 by Marius Kruger
* Fix workingtree.walkdirs to support getting a prefix which specifies an empty directory.
150
151
    def test_walkdir_from_dir(self):
152
        """Doing a walkdir when the requested prefix is known and on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
153
        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.
154
155
    def test_walkdir_from_empty_dir(self):
156
        """Doing a walkdir when the requested prefix is empty dir."""
2457.2.7 by Marius Kruger
extract method as per review request
157
        self._test_walkdir(self.added, 'added empty dir')
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
158
159
    def test_walkdir_from_missing_dir(self):
160
        """Doing a walkdir when the requested prefix is missing but on disk."""
2457.2.7 by Marius Kruger
extract method as per review request
161
        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.
162
163
    def test_walkdirs_type_changes(self):
164
        """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
165
        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.
166
        tree = self.make_branch_and_tree('.')
167
        paths = ['file1', 'file2', 'dir1/', 'dir2/']
168
        self.build_tree(paths)
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
169
        tree.add(paths)
7490.77.2 by Jelmer Vernooij
Split out git and bzr-specific transforms.
170
        tt = tree.transform()
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
171
        root_transaction_id = tt.trans_id_tree_path('')
172
        tt.new_symlink('link1',
7143.15.2 by Jelmer Vernooij
Run autopep8.
173
                       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.
174
        tt.new_symlink('link2',
7143.15.2 by Jelmer Vernooij
Run autopep8.
175
                       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.
176
        tt.apply()
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
177
        tree.controldir.root_transport.delete_tree('dir1')
178
        tree.controldir.root_transport.delete_tree('dir2')
179
        tree.controldir.root_transport.delete('file1')
180
        tree.controldir.root_transport.delete('file2')
181
        tree.controldir.root_transport.delete('link1')
182
        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.
183
        changed_paths = ['dir1', 'file1/', 'link1', 'link2/']
184
        self.build_tree(changed_paths)
185
        os.symlink('target', 'dir2')
186
        os.symlink('target', 'file2')
187
        dir1_stat = os.lstat('dir1')
188
        dir2_stat = os.lstat('dir2')
189
        file1_stat = os.lstat('file1')
190
        file2_stat = os.lstat('file2')
191
        link1_stat = os.lstat('link1')
192
        link2_stat = os.lstat('link2')
193
        expected_dirblocks = [
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
194
            ('',
195
             [('dir1', 'dir1', 'file', dir1_stat,
7143.15.2 by Jelmer Vernooij
Run autopep8.
196
               'directory' if tree.has_versioned_directories() else None),
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
197
              ('dir2', 'dir2', 'symlink', dir2_stat,
7143.15.2 by Jelmer Vernooij
Run autopep8.
198
               'directory' if tree.has_versioned_directories() else None),
199
              ('file1', 'file1', 'directory', file1_stat,
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
200
               'file'),
201
              ('file2', 'file2', 'symlink', file2_stat, 'file'),
202
              ('link1', 'link1', 'file', link1_stat, 'symlink'),
203
              ('link2', 'link2', 'directory', link2_stat, 'symlink'),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
204
              ]
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
205
             )]
206
        if tree.has_versioned_directories():
207
            expected_dirblocks.extend([
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
208
                ('dir1', []),
209
                ('dir2', [])])
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
210
        expected_dirblocks.extend([
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
211
            ('file1', []),
212
            ('link2', []),
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
213
            ])
214
        with tree.lock_read():
215
            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.
216
        # check each return value for debugging ease.
217
        for pos, item in enumerate(expected_dirblocks):
218
            self.assertEqual(item, result[pos])
219
        self.assertEqual(len(expected_dirblocks), len(result))
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
220
221
    def test_walkdirs_type_changes_wo_symlinks(self):
222
        # similar to test_walkdirs_type_changes
223
        # but don't use symlinks for safe testing on win32
224
        tree = self.make_branch_and_tree('.')
225
        paths = ['file1', 'dir1/']
226
        self.build_tree(paths)
6816.1.1 by Jelmer Vernooij
Avoid passing in file ids.
227
        tree.add(paths)
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
228
        tree.controldir.root_transport.delete_tree('dir1')
229
        tree.controldir.root_transport.delete('file1')
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
230
        changed_paths = ['dir1', 'file1/']
231
        self.build_tree(changed_paths)
232
        dir1_stat = os.lstat('dir1')
233
        file1_stat = os.lstat('file1')
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
234
        if tree.has_versioned_directories():
235
            expected_dirblocks = [
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
236
                ('',
237
                 [('dir1', 'dir1', 'file', dir1_stat, 'directory'),
238
                  ('file1', 'file1', 'directory', file1_stat, 'file'),
239
                 ]),
240
                ('dir1', []),
241
                ('file1', []),
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
242
                ]
243
        else:
244
            expected_dirblocks = [
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
245
                ('',
246
                 [('dir1', 'dir1', 'file', dir1_stat, None),
247
                  ('file1', 'file1', 'directory', file1_stat, 'file'),
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
248
                  ]
249
                 ),
7490.121.1 by Jelmer Vernooij
Drop file ids from the Tree.walkdirs API.
250
                ('file1', []),
6861.1.1 by Jelmer Vernooij
More foreign branch test fixes.
251
                ]
6876.3.2 by Jelmer Vernooij
Fix walkdirs for trees without versioned directories.
252
        with tree.lock_read():
253
            result = list(tree.walkdirs())
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
254
        # check each return value for debugging ease.
255
        for pos, item in enumerate(expected_dirblocks):
256
            self.assertEqual(item, result[pos])
257
        self.assertEqual(len(expected_dirblocks), len(result))