1
# Copyright (C) 2008 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 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()
40
class TestWin32Finder(tests.TestCaseInTempDir):
42
_test_needs_features = [WalkdirsWin32Feature]
45
super(TestWin32Finder, self).setUp()
46
from bzrlib._walkdirs_win32 import (
47
_walkdirs_utf8_win32_find_file
49
self.walkdirs_utf8 = _walkdirs_utf8_win32_find_file
51
def _remove_stat_from_dirblock(self, dirblock):
52
return [info[:3] + info[4:] for info in dirblock]
54
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)))
59
self.assertEqual(expected, result)
61
def test_empty_directory(self):
62
self.assertWalkdirs([(('', u'.'), [])], u'.')
64
def test_file_in_dir(self):
65
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)
117
def test_missing_dir(self):
118
e = self.assertRaises(WindowsError, list,
119
self.walkdirs_utf8(u'no_such_dir'))
120
self.assertEqual(errno.ENOENT, e.errno)
121
self.assertEqual(3, e.winerror)
122
self.assertEqual((3, u'no_such_dir/*'), e.args)