1
# Copyright (C) 2006, 2007 Canonical Ltd
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.
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.
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
17
"""Tests for the extra cases that WorkingTree.walkdirs can encounter."""
21
from bzrlib import transform
22
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
25
# type mismatches - file to link, dir, dir to file, link, link to file, dir
27
class TestWalkdirs(TestCaseWithWorkingTree):
29
def get_tree_with_unknowns(self):
30
tree = self.make_branch_and_tree('.')
36
u_f_stat = os.lstat('unknown file')
37
u_d_stat = os.lstat('unknown dir')
38
u_d_f_stat = os.lstat('unknown dir/a file')
39
expected_dirblocks = [
40
(('', tree.path2id('')),
42
('unknown dir', 'unknown dir', 'directory', u_d_stat, None, None),
43
('unknown file', 'unknown file', 'file', u_f_stat, None, None),
46
(('unknown dir', None),
47
[('unknown dir/a file', 'a file', 'file', u_d_f_stat, None, None),
51
return tree, expected_dirblocks
53
def test_walkdir_unknowns(self):
54
"""unknown files and directories should be reported by walkdirs."""
55
# test that its iterable by iterating:
57
tree, expected_dirblocks = self.get_tree_with_unknowns()
59
for dirinfo, dirblock in tree.walkdirs():
60
result.append((dirinfo, list(dirblock)))
62
# check each return value for debugging ease.
63
for pos, item in enumerate(expected_dirblocks):
64
self.assertEqual(item, result[pos])
65
self.assertEqual(len(expected_dirblocks), len(result))
67
def test_walkdir_from_unknown_dir(self):
68
"""Doing a walkdir when the requested prefix is unknown but on disk."""
70
tree, expected_dirblocks = self.get_tree_with_unknowns()
72
for dirinfo, dirblock in tree.walkdirs('unknown dir'):
73
result.append((dirinfo, list(dirblock)))
75
# check each return value for debugging ease.
76
for pos, item in enumerate(expected_dirblocks[1:]):
77
self.assertEqual(item, result[pos])
78
self.assertEqual(len(expected_dirblocks) - 1, len(result))
80
def get_tree_with_missings(self):
81
tree = self.make_branch_and_tree('.')
92
self.build_tree(paths)
94
# now make the files be missing
95
tree.bzrdir.root_transport.delete_tree('missing dir')
96
tree.bzrdir.root_transport.delete('missing file')
97
expected_dirblocks = [
98
(('', tree.path2id('')),
100
('missing dir', 'missing dir', 'unknown', None, 'a dir', 'directory'),
101
('missing file', 'missing file', 'unknown', None, 'a file', 'file'),
104
(('missing dir', 'a dir'),
105
[('missing dir/a file', 'a file', 'unknown', None, 'a dir-a file', 'file'),
109
return tree, expected_dirblocks
111
def test_walkdir_missings(self):
112
"""missing files and directories should be reported by walkdirs."""
113
# test that its iterable by iterating:
115
tree, expected_dirblocks = self.get_tree_with_missings()
117
for dirinfo, dirblock in tree.walkdirs():
118
result.append((dirinfo, list(dirblock)))
120
# check each return value for debugging ease.
121
for pos, item in enumerate(expected_dirblocks):
122
self.assertEqual(item, result[pos])
123
self.assertEqual(len(expected_dirblocks), len(result))
125
def test_walkdir_from_missing_dir(self):
126
"""Doing a walkdir when the requested prefix is missing but on disk."""
128
tree, expected_dirblocks = self.get_tree_with_missings()
130
for dirinfo, dirblock in tree.walkdirs('missing dir'):
131
result.append((dirinfo, list(dirblock)))
133
# check each return value for debugging ease.
134
for pos, item in enumerate(expected_dirblocks[1:]):
135
self.assertEqual(item, result[pos])
136
self.assertEqual(len(expected_dirblocks[1:]), len(result))
138
def test_walkdirs_type_changes(self):
139
"""Walkdir shows the actual kinds on disk and the recorded kinds."""
140
tree = self.make_branch_and_tree('.')
141
paths = ['file1', 'file2', 'dir1/', 'dir2/']
142
ids = ['file1', 'file2', 'dir1', 'dir2']
143
self.build_tree(paths)
145
tt = transform.TreeTransform(tree)
146
root_transaction_id = tt.trans_id_tree_path('')
147
tt.new_symlink('link1',
148
root_transaction_id, 'link-target', 'link1')
149
tt.new_symlink('link2',
150
root_transaction_id, 'link-target', 'link2')
152
tree.bzrdir.root_transport.delete_tree('dir1')
153
tree.bzrdir.root_transport.delete_tree('dir2')
154
tree.bzrdir.root_transport.delete('file1')
155
tree.bzrdir.root_transport.delete('file2')
156
tree.bzrdir.root_transport.delete('link1')
157
tree.bzrdir.root_transport.delete('link2')
158
changed_paths = ['dir1', 'file1/', 'link1', 'link2/']
159
self.build_tree(changed_paths)
160
os.symlink('target', 'dir2')
161
os.symlink('target', 'file2')
162
dir1_stat = os.lstat('dir1')
163
dir2_stat = os.lstat('dir2')
164
file1_stat = os.lstat('file1')
165
file2_stat = os.lstat('file2')
166
link1_stat = os.lstat('link1')
167
link2_stat = os.lstat('link2')
168
expected_dirblocks = [
169
(('', tree.path2id('')),
170
[('dir1', 'dir1', 'file', dir1_stat, 'dir1', 'directory'),
171
('dir2', 'dir2', 'symlink', dir2_stat, 'dir2', 'directory'),
172
('file1', 'file1', 'directory', file1_stat, 'file1', 'file'),
173
('file2', 'file2', 'symlink', file2_stat, 'file2', 'file'),
174
('link1', 'link1', 'file', link1_stat, 'link1', 'symlink'),
175
('link2', 'link2', 'directory', link2_stat, 'link2', 'symlink'),
196
result = list(tree.walkdirs())
198
# check each return value for debugging ease.
199
for pos, item in enumerate(expected_dirblocks):
200
self.assertEqual(item, result[pos])
201
self.assertEqual(len(expected_dirblocks), len(result))