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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the win32 walkdir extension."""
25
from bzrlib.tests import (
30
win32_readdir_feature = features.ModuleAvailableFeature('bzrlib._walkdirs_win32')
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()
33
40
class TestWin32Finder(tests.TestCaseInTempDir):
35
_test_needs_features = [win32_readdir_feature]
42
_test_needs_features = [WalkdirsWin32Feature]
38
45
super(TestWin32Finder, self).setUp()
39
46
from bzrlib._walkdirs_win32 import (
47
_walkdirs_utf8_win32_find_file
42
self.reader = Win32ReadDir()
49
self.walkdirs_utf8 = _walkdirs_utf8_win32_find_file
44
51
def _remove_stat_from_dirblock(self, dirblock):
45
52
return [info[:3] + info[4:] for info in dirblock]
47
54
def assertWalkdirs(self, expected, top, prefix=''):
48
old_selected_dir_reader = osutils._selected_dir_reader
50
osutils._selected_dir_reader = self.reader
51
finder = osutils._walkdirs_utf8(top, prefix=prefix)
53
for dirname, dirblock in finder:
54
dirblock = self._remove_stat_from_dirblock(dirblock)
55
result.append((dirname, dirblock))
56
self.assertEqual(expected, result)
58
osutils._selected_dir_reader = old_selected_dir_reader
60
def assertReadDir(self, expected, prefix, top_unicode):
61
result = self._remove_stat_from_dirblock(
62
self.reader.read_dir(prefix, top_unicode))
55
finder = self.walkdirs_utf8(top, prefix=prefix)
57
for dirname, dirblock in finder:
58
result.append((dirname, self._remove_stat_from_dirblock(dirblock)))
63
59
self.assertEqual(expected, result)
65
def test_top_prefix_to_starting_dir(self):
66
# preparing an iteration should create a unicode native path.
67
self.assertEqual(('prefix', None, None, None, u'\x12'),
68
self.reader.top_prefix_to_starting_dir(u'\x12'.encode('utf8'),
71
61
def test_empty_directory(self):
72
self.assertReadDir([], 'prefix', u'.')
73
62
self.assertWalkdirs([(('', u'.'), [])], u'.')
64
def test_file_in_dir(self):
76
65
self.build_tree(['foo'])
77
self.assertReadDir([('foo', 'foo', 'file', u'./foo')],
80
def test_directory(self):
81
self.build_tree(['bar/'])
82
self.assertReadDir([('bar', 'bar', 'directory', u'./bar')],
85
def test_prefix(self):
86
self.build_tree(['bar/', 'baf'])
88
('xxx/baf', 'baf', 'file', u'./baf'),
89
('xxx/bar', 'bar', 'directory', u'./bar'),
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)
93
117
def test_missing_dir(self):
94
e = self.assertRaises(WindowsError,
95
self.reader.read_dir, 'prefix', u'no_such_dir')
118
e = self.assertRaises(WindowsError, list,
119
self.walkdirs_utf8(u'no_such_dir'))
96
120
self.assertEqual(errno.ENOENT, e.errno)
97
121
self.assertEqual(3, e.winerror)
98
122
self.assertEqual((3, u'no_such_dir/*'), e.args)
101
class Test_Win32Stat(tests.TestCaseInTempDir):
103
_test_needs_features = [win32_readdir_feature]
106
super(Test_Win32Stat, self).setUp()
107
from bzrlib._walkdirs_win32 import lstat
108
self.win32_lstat = lstat
110
def test_zero_members_present(self):
111
self.build_tree(['foo'])
112
st = self.win32_lstat('foo')
113
# we only want to ensure that some members are present
114
self.assertEqual(0, st.st_dev)
115
self.assertEqual(0, st.st_ino)
116
self.assertEqual(0, st.st_uid)
117
self.assertEqual(0, st.st_gid)