26
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,
28
67
class TestWalkdirs(TestCaseWithWorkingTree):
30
def get_tree_with_unknowns(self):
73
def get_tree(self, file_status, prefix=None):
31
74
tree = self.make_branch_and_tree('.')
37
u_f_stat = os.lstat('unknown file')
38
u_d_stat = os.lstat('unknown dir')
39
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)
40
108
expected_dirblocks = [
41
109
(('', tree.path2id('')),
43
('unknown dir', 'unknown dir', 'directory', u_d_stat, None, None),
44
('unknown file', 'unknown file', 'file', u_f_stat, None, None),
47
(('unknown dir', None),
48
[('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]
52
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)
54
141
def test_walkdir_unknowns(self):
55
142
"""unknown files and directories should be reported by walkdirs."""
56
# test that its iterable by iterating:
58
tree, expected_dirblocks = self.get_tree_with_unknowns()
60
for dirinfo, dirblock in tree.walkdirs():
61
result.append((dirinfo, list(dirblock)))
63
# check each return value for debugging ease.
64
for pos, item in enumerate(expected_dirblocks):
65
self.assertEqual(item, result[pos])
66
self.assertEqual(len(expected_dirblocks), len(result))
143
self._test_walkdir(self.unknown)
68
145
def test_walkdir_from_unknown_dir(self):
69
146
"""Doing a walkdir when the requested prefix is unknown but on disk."""
71
tree, expected_dirblocks = self.get_tree_with_unknowns()
73
for dirinfo, dirblock in tree.walkdirs('unknown dir'):
74
result.append((dirinfo, list(dirblock)))
76
# check each return value for debugging ease.
77
for pos, item in enumerate(expected_dirblocks[1:]):
78
self.assertEqual(item, result[pos])
79
self.assertEqual(len(expected_dirblocks) - 1, len(result))
147
self._test_walkdir(self.unknown, 'unknown dir')
81
def get_tree_with_missings(self):
82
tree = self.make_branch_and_tree('.')
93
self.build_tree(paths)
95
# now make the files be missing
96
tree.bzrdir.root_transport.delete_tree('missing dir')
97
tree.bzrdir.root_transport.delete('missing file')
98
expected_dirblocks = [
99
(('', tree.path2id('')),
101
('missing dir', 'missing dir', 'unknown', None, 'a dir', 'directory'),
102
('missing file', 'missing file', 'unknown', None, 'a file', 'file'),
105
(('missing dir', 'a dir'),
106
[('missing dir/a file', 'a file', 'unknown', None, 'a dir-a file', 'file'),
110
return tree, expected_dirblocks
112
149
def test_walkdir_missings(self):
113
150
"""missing files and directories should be reported by walkdirs."""
114
# test that its iterable by iterating:
116
tree, expected_dirblocks = self.get_tree_with_missings()
118
for dirinfo, dirblock in tree.walkdirs():
119
result.append((dirinfo, list(dirblock)))
121
# check each return value for debugging ease.
122
for pos, item in enumerate(expected_dirblocks):
123
self.assertEqual(item, result[pos])
124
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')
126
161
def test_walkdir_from_missing_dir(self):
127
162
"""Doing a walkdir when the requested prefix is missing but on disk."""
129
tree, expected_dirblocks = self.get_tree_with_missings()
131
for dirinfo, dirblock in tree.walkdirs('missing dir'):
132
result.append((dirinfo, list(dirblock)))
134
# check each return value for debugging ease.
135
for pos, item in enumerate(expected_dirblocks[1:]):
136
self.assertEqual(item, result[pos])
137
self.assertEqual(len(expected_dirblocks[1:]), len(result))
163
self._test_walkdir(self.missing, 'missing dir')
139
165
def test_walkdirs_type_changes(self):
140
166
"""Walkdir shows the actual kinds on disk and the recorded kinds."""