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 win32 walkdir extension."""
21
from bzrlib import tests
24
class _WalkdirsWin32Feature(tests.Feature):
28
import bzrlib._walkdirs_win32
34
def feature_name(self):
35
return 'bzrlib._walkdirs_win32'
37
WalkdirsWin32Feature = _WalkdirsWin32Feature()
27
win32_readdir_feature = tests.ModuleAvailableFeature('bzrlib._walkdirs_win32')
40
30
class TestWin32Finder(tests.TestCaseInTempDir):
42
_test_needs_features = [WalkdirsWin32Feature]
32
_test_needs_features = [win32_readdir_feature]
45
35
super(TestWin32Finder, self).setUp()
46
36
from bzrlib._walkdirs_win32 import (
47
_walkdirs_utf8_win32_find_file
49
self.walkdirs_utf8 = _walkdirs_utf8_win32_find_file
39
self.reader = Win32ReadDir()
51
41
def _remove_stat_from_dirblock(self, dirblock):
52
42
return [info[:3] + info[4:] for info in dirblock]
54
44
def assertWalkdirs(self, expected, top, prefix=''):
55
finder = self.walkdirs_utf8(top, prefix=prefix)
57
for dirname, dirblock in finder:
58
result.append((dirname, self._remove_stat_from_dirblock(dirblock)))
45
old_selected_dir_reader = osutils._selected_dir_reader
47
osutils._selected_dir_reader = self.reader
48
finder = osutils._walkdirs_utf8(top, prefix=prefix)
50
for dirname, dirblock in finder:
51
dirblock = self._remove_stat_from_dirblock(dirblock)
52
result.append((dirname, dirblock))
53
self.assertEqual(expected, result)
55
osutils._selected_dir_reader = old_selected_dir_reader
57
def assertReadDir(self, expected, prefix, top_unicode):
58
result = self._remove_stat_from_dirblock(
59
self.reader.read_dir(prefix, top_unicode))
59
60
self.assertEqual(expected, result)
62
def test_top_prefix_to_starting_dir(self):
63
# preparing an iteration should create a unicode native path.
64
self.assertEqual(('prefix', None, None, None, u'\x12'),
65
self.reader.top_prefix_to_starting_dir(u'\x12'.encode('utf8'),
61
68
def test_empty_directory(self):
69
self.assertReadDir([], 'prefix', u'.')
62
70
self.assertWalkdirs([(('', u'.'), [])], u'.')
64
def test_file_in_dir(self):
65
73
self.build_tree(['foo'])
67
(('', u'.'), [('foo', 'foo', 'file', u'./foo')])
70
def test_subdir(self):
71
self.build_tree(['foo', 'bar/', 'bar/baz'])
73
(('', u'.'), [('bar', 'bar', 'directory', u'./bar'),
74
('foo', 'foo', 'file', u'./foo'),
76
(('bar', u'./bar'), [('bar/baz', 'baz', 'file', u'./bar/baz')]),
79
(('xxx', u'.'), [('xxx/bar', 'bar', 'directory', u'./bar'),
80
('xxx/foo', 'foo', 'file', u'./foo'),
82
(('xxx/bar', u'./bar'), [('xxx/bar/baz', 'baz', 'file', u'./bar/baz')]),
85
(('', u'bar'), [('baz', 'baz', 'file', u'bar/baz')]),
88
def test_skip_subdir(self):
89
self.build_tree(['a/', 'b/', 'c/', 'a/aa', 'b/bb', 'c/cc'])
90
base_dirblock = [('a', 'a', 'directory', u'./a'),
91
('b', 'b', 'directory', u'./b'),
92
('c', 'c', 'directory', u'./c'),
95
(('', u'.'), base_dirblock),
96
(('a', u'./a'), [('a/aa', 'aa', 'file', u'./a/aa')]),
97
(('b', u'./b'), [('b/bb', 'bb', 'file', u'./b/bb')]),
98
(('c', u'./c'), [('c/cc', 'cc', 'file', u'./c/cc')]),
101
walker = self.walkdirs_utf8('.')
102
dir_info, first_dirblock = walker.next()
103
self.assertEqual(('', u'.'), dir_info)
104
self.assertEqual(base_dirblock,
105
self._remove_stat_from_dirblock(first_dirblock))
106
# Now, remove 'b' and it should be skipped on the next round
107
del first_dirblock[1]
108
dir_info, second_dirblock = walker.next()
109
second_dirblock = self._remove_stat_from_dirblock(second_dirblock)
110
self.assertEqual(('a', u'./a'), dir_info)
111
self.assertEqual([('a/aa', 'aa', 'file', u'./a/aa')], second_dirblock)
112
dir_info, third_dirblock = walker.next()
113
third_dirblock = self._remove_stat_from_dirblock(third_dirblock)
114
self.assertEqual(('c', u'./c'), dir_info)
115
self.assertEqual([('c/cc', 'cc', 'file', u'./c/cc')], third_dirblock)
74
self.assertReadDir([('foo', 'foo', 'file', u'./foo')],
78
self.build_tree(['bar/'])
79
self.assertReadDir([('bar', 'bar', 'directory', u'./bar')],
82
def test_prefix(self):
83
self.build_tree(['bar/', 'baf'])
85
('xxx/baf', 'baf', 'file', u'./baf'),
86
('xxx/bar', 'bar', 'directory', u'./bar'),
117
90
def test_missing_dir(self):
118
e = self.assertRaises(WindowsError, list,
119
self.walkdirs_utf8(u'no_such_dir'))
91
e = self.assertRaises(WindowsError,
92
self.reader.read_dir, 'prefix', u'no_such_dir')
120
93
self.assertEqual(errno.ENOENT, e.errno)
121
94
self.assertEqual(3, e.winerror)
122
95
self.assertEqual((3, u'no_such_dir/*'), e.args)