~bzr-pqm/bzr/bzr.dev

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
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
31
    def get_tree_with_unknowns(self):
1852.15.9 by Robert Collins
Add missing test script.
32
        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')
41
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
42
            (('', tree.path2id('')),
1852.15.9 by Robert Collins
Add missing test script.
43
             [
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
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
             ]
51
            ),
52
            ]
53
        return tree, expected_dirblocks
54
    
55
    def test_walkdir_unknowns(self):
56
        """unknown files and directories should be reported by walkdirs."""
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
57
        # test that its iterable by iterating:
1852.15.9 by Robert Collins
Add missing test script.
58
        result = []
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
59
        tree, expected_dirblocks = self.get_tree_with_unknowns()
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
60
        tree.lock_read()
1852.15.9 by Robert Collins
Add missing test script.
61
        for dirinfo, dirblock in tree.walkdirs():
62
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
63
        tree.unlock()
1852.15.9 by Robert Collins
Add missing test script.
64
        # check each return value for debugging ease.
65
        for pos, item in enumerate(expected_dirblocks):
66
            self.assertEqual(item, result[pos])
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
67
        self.assertEqual(len(expected_dirblocks), len(result))
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
68
69
    def test_walkdir_from_unknown_dir(self):
70
        """Doing a walkdir when the requested prefix is unknown but on disk."""
71
        result = []
72
        tree, expected_dirblocks = self.get_tree_with_unknowns()
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
73
        tree.lock_read()
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
74
        for dirinfo, dirblock in tree.walkdirs('unknown dir'):
75
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
76
        tree.unlock()
1852.15.10 by Robert Collins
Tweak the Tree.walkdirs interface more to be more useful.
77
        # check each return value for debugging ease.
78
        for pos, item in enumerate(expected_dirblocks[1:]):
79
            self.assertEqual(item, result[pos])
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
80
        self.assertEqual(len(expected_dirblocks) - 1, len(result))
81
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 = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
100
            (('', tree.path2id('')),
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
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
    def test_walkdir_missings(self):
114
        """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()
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
118
        tree.lock_read()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
119
        for dirinfo, dirblock in tree.walkdirs():
120
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
121
        tree.unlock()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
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))
126
127
    def test_walkdir_from_missing_dir(self):
128
        """Doing a walkdir when the requested prefix is missing but on disk."""
129
        result = []
130
        tree, expected_dirblocks = self.get_tree_with_missings()
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
131
        tree.lock_read()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
132
        for dirinfo, dirblock in tree.walkdirs('missing dir'):
133
            result.append((dirinfo, list(dirblock)))
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
134
        tree.unlock()
1852.15.11 by Robert Collins
Tree.walkdirs handles missing contents in workingtrees.
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))
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
139
140
    def test_walkdirs_type_changes(self):
141
        """Walkdir shows the actual kinds on disk and the recorded kinds."""
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
142
        if not has_symlinks():
143
            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.
144
        tree = self.make_branch_and_tree('.')
145
        paths = ['file1', 'file2', 'dir1/', 'dir2/']
146
        ids = ['file1', 'file2', 'dir1', 'dir2']
147
        self.build_tree(paths)
148
        tree.add(paths, ids)
149
        tt = transform.TreeTransform(tree)
150
        root_transaction_id = tt.trans_id_tree_path('')
151
        tt.new_symlink('link1',
152
            root_transaction_id, 'link-target', 'link1')
153
        tt.new_symlink('link2',
154
            root_transaction_id, 'link-target', 'link2')
155
        tt.apply()
156
        tree.bzrdir.root_transport.delete_tree('dir1')
157
        tree.bzrdir.root_transport.delete_tree('dir2')
158
        tree.bzrdir.root_transport.delete('file1')
159
        tree.bzrdir.root_transport.delete('file2')
160
        tree.bzrdir.root_transport.delete('link1')
161
        tree.bzrdir.root_transport.delete('link2')
162
        changed_paths = ['dir1', 'file1/', 'link1', 'link2/']
163
        self.build_tree(changed_paths)
164
        os.symlink('target', 'dir2')
165
        os.symlink('target', 'file2')
166
        dir1_stat = os.lstat('dir1')
167
        dir2_stat = os.lstat('dir2')
168
        file1_stat = os.lstat('file1')
169
        file2_stat = os.lstat('file2')
170
        link1_stat = os.lstat('link1')
171
        link2_stat = os.lstat('link2')
172
        expected_dirblocks = [
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
173
             (('', tree.path2id('')),
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
174
              [('dir1', 'dir1', 'file', dir1_stat, 'dir1', 'directory'),
175
               ('dir2', 'dir2', 'symlink', dir2_stat, 'dir2', 'directory'),
176
               ('file1', 'file1', 'directory', file1_stat, 'file1', 'file'),
177
               ('file2', 'file2', 'symlink', file2_stat, 'file2', 'file'),
178
               ('link1', 'link1', 'file', link1_stat, 'link1', 'symlink'),
179
               ('link2', 'link2', 'directory', link2_stat, 'link2', 'symlink'),
180
              ]
181
             ),
182
             (('dir1', 'dir1'),
183
              [
184
              ]
185
             ),
186
             (('dir2', 'dir2'),
187
              [
188
              ]
189
             ),
190
             (('file1', None),
191
              [
192
              ]
193
             ),
194
             (('link2', None),
195
              [
196
              ]
197
             ),
198
            ]
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
199
        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.
200
        result = list(tree.walkdirs())
2255.2.47 by Robert Collins
Adjust walkdirs tests to talk a lock around walk_dirs.
201
        tree.unlock()
1852.15.12 by Robert Collins
WorkingTree.walkdirs handling of changing file kinds, and multi-directory with missing and unknown ordering bugfix.
202
        # check each return value for debugging ease.
203
        for pos, item in enumerate(expected_dirblocks):
204
            self.assertEqual(item, result[pos])
205
        self.assertEqual(len(expected_dirblocks), len(result))
2408.1.4 by Alexander Belchenko
workingtree_implementations: make usage of symlinks optional
206
207
    def test_walkdirs_type_changes_wo_symlinks(self):
208
        # similar to test_walkdirs_type_changes
209
        # but don't use symlinks for safe testing on win32
210
        tree = self.make_branch_and_tree('.')
211
        paths = ['file1', 'dir1/']
212
        ids = ['file1', 'dir1']
213
        self.build_tree(paths)
214
        tree.add(paths, ids)
215
        tree.bzrdir.root_transport.delete_tree('dir1')
216
        tree.bzrdir.root_transport.delete('file1')
217
        changed_paths = ['dir1', 'file1/']
218
        self.build_tree(changed_paths)
219
        dir1_stat = os.lstat('dir1')
220
        file1_stat = os.lstat('file1')
221
        expected_dirblocks = [
222
             (('', tree.path2id('')),
223
              [('dir1', 'dir1', 'file', dir1_stat, 'dir1', 'directory'),
224
               ('file1', 'file1', 'directory', file1_stat, 'file1', 'file'),
225
              ]
226
             ),
227
             (('dir1', 'dir1'),
228
              [
229
              ]
230
             ),
231
             (('file1', None),
232
              [
233
              ]
234
             ),
235
            ]
236
        tree.lock_read()
237
        result = list(tree.walkdirs())
238
        tree.unlock()
239
        # check each return value for debugging ease.
240
        for pos, item in enumerate(expected_dirblocks):
241
            self.assertEqual(item, result[pos])
242
        self.assertEqual(len(expected_dirblocks), len(result))