13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
17
"""Tests for the extra cases that WorkingTree.walkdirs can encounter."""
21
21
from bzrlib import transform
22
from bzrlib.osutils import has_symlinks
23
from bzrlib.tests import TestSkipped
22
from bzrlib.tests import SymlinkFeature
24
23
from bzrlib.tests.workingtree_implementations import TestCaseWithWorkingTree
27
26
# type mismatches - file to link, dir, dir to file, link, link to file, dir
29
"""Object representation of the tuples returned by dirstate."""
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)
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
42
self.inventory_kind = inventory_kind
44
self.disk_kind = disk_kind
47
return (self.relpath, self.file_name, self.disk_kind,
48
self.stat, self.id, self.inventory_kind)
50
def as_dir_tuple(self):
51
return (self.relpath, self.id)
62
disk_kind = %r""" % (self.file_path, self.abspath, self.relpath,
63
self.file_name, self.id, self.inventory_kind, self.stat,
29
67
class TestWalkdirs(TestCaseWithWorkingTree):
31
def get_tree_with_unknowns(self):
73
def get_tree(self, file_status, prefix=None):
32
74
tree = self.make_branch_and_tree('.')
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')
77
file_status + ' file',
78
file_status + ' dir/',
79
file_status + ' dir/a file',
80
file_status + ' empty dir/',
82
self.build_tree(paths)
84
def add_dirblock(path, kind):
85
dirblock = DirBlock(tree, path)
86
if file_status != self.unknown:
87
dirblock.id = 'a ' + str(path).replace('/','-') + '-id'
88
dirblock.inventory_kind = kind
89
if file_status != self.missing:
90
dirblock.disk_kind = kind
91
dirblock.stat = os.lstat(dirblock.relpath)
92
dirblocks.append(dirblock)
94
add_dirblock(paths[0], 'file')
95
add_dirblock(paths[1], 'directory')
96
add_dirblock(paths[2], 'file')
97
add_dirblock(paths[3], 'directory')
99
if file_status != self.unknown:
100
tree.add(paths, [db.id for db in dirblocks])
102
if file_status == self.missing:
103
# now make the files be missing
104
tree.bzrdir.root_transport.delete(dirblocks[0].relpath)
105
tree.bzrdir.root_transport.delete_tree(dirblocks[1].relpath)
106
tree.bzrdir.root_transport.delete_tree(dirblocks[3].relpath)
41
108
expected_dirblocks = [
42
109
(('', tree.path2id('')),
44
('unknown dir', 'unknown dir', 'directory', u_d_stat, None, None),
45
('unknown file', 'unknown file', 'file', u_f_stat, None, None),
48
(('unknown dir', None),
49
[('unknown dir/a file', 'a file', 'file', u_d_f_stat, None, None),
110
[dirblocks[1].as_tuple(), dirblocks[3].as_tuple(),
111
dirblocks[0].as_tuple()]
113
(dirblocks[1].as_dir_tuple(),
114
[dirblocks[2].as_tuple()]
116
(dirblocks[3].as_dir_tuple(),
121
expected_dirblocks = [e for e in expected_dirblocks
122
if len(e) > 0 and len(e[0]) > 0 and e[0][0] == prefix]
53
123
return tree, expected_dirblocks
125
def _test_walkdir(self, file_status, prefix=""):
127
tree, expected_dirblocks = self.get_tree(file_status, prefix)
129
for dirinfo, dirblock in tree.walkdirs(prefix):
130
result.append((dirinfo, list(dirblock)))
133
# check each return value for debugging ease.
134
for pos, item in enumerate(expected_dirblocks):
136
if len(result) > pos:
137
result_pos = result[pos]
138
self.assertEqual(item, result_pos)
139
self.assertEqual(expected_dirblocks, result)
55
141
def test_walkdir_unknowns(self):
56
142
"""unknown files and directories should be reported by walkdirs."""
57
# test that its iterable by iterating:
59
tree, expected_dirblocks = self.get_tree_with_unknowns()
61
for dirinfo, dirblock in tree.walkdirs():
62
result.append((dirinfo, list(dirblock)))
64
# check each return value for debugging ease.
65
for pos, item in enumerate(expected_dirblocks):
66
self.assertEqual(item, result[pos])
67
self.assertEqual(len(expected_dirblocks), len(result))
143
self._test_walkdir(self.unknown)
69
145
def test_walkdir_from_unknown_dir(self):
70
146
"""Doing a walkdir when the requested prefix is unknown but on disk."""
72
tree, expected_dirblocks = self.get_tree_with_unknowns()
74
for dirinfo, dirblock in tree.walkdirs('unknown dir'):
75
result.append((dirinfo, list(dirblock)))
77
# check each return value for debugging ease.
78
for pos, item in enumerate(expected_dirblocks[1:]):
79
self.assertEqual(item, result[pos])
80
self.assertEqual(len(expected_dirblocks) - 1, len(result))
147
self._test_walkdir(self.unknown, 'unknown dir')
82
def get_tree_with_missings(self):
83
tree = self.make_branch_and_tree('.')
94
self.build_tree(paths)
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 = [
100
(('', tree.path2id('')),
102
('missing dir', 'missing dir', 'unknown', None, 'a dir', 'directory'),
103
('missing file', 'missing file', 'unknown', None, 'a file', 'file'),
106
(('missing dir', 'a dir'),
107
[('missing dir/a file', 'a file', 'unknown', None, 'a dir-a file', 'file'),
111
return tree, expected_dirblocks
113
149
def test_walkdir_missings(self):
114
150
"""missing files and directories should be reported by walkdirs."""
115
# test that its iterable by iterating:
117
tree, expected_dirblocks = self.get_tree_with_missings()
119
for dirinfo, dirblock in tree.walkdirs():
120
result.append((dirinfo, list(dirblock)))
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))
151
self._test_walkdir(self.missing)
153
def test_walkdir_from_dir(self):
154
"""Doing a walkdir when the requested prefix is known and on disk."""
155
self._test_walkdir(self.added, 'added dir')
157
def test_walkdir_from_empty_dir(self):
158
"""Doing a walkdir when the requested prefix is empty dir."""
159
self._test_walkdir(self.added, 'added empty dir')
127
161
def test_walkdir_from_missing_dir(self):
128
162
"""Doing a walkdir when the requested prefix is missing but on disk."""
130
tree, expected_dirblocks = self.get_tree_with_missings()
132
for dirinfo, dirblock in tree.walkdirs('missing dir'):
133
result.append((dirinfo, list(dirblock)))
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))
163
self._test_walkdir(self.missing, 'missing dir')
140
165
def test_walkdirs_type_changes(self):
141
166
"""Walkdir shows the actual kinds on disk and the recorded kinds."""
142
if not has_symlinks():
143
raise TestSkipped('No symlink support')
167
self.requireFeature(SymlinkFeature)
144
168
tree = self.make_branch_and_tree('.')
145
169
paths = ['file1', 'file2', 'dir1/', 'dir2/']
146
170
ids = ['file1', 'file2', 'dir1', 'dir2']