~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__walkdirs_win32.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-24 01:43:25 UTC
  • mfrom: (3696.3.12 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20080924014325-ucivgbdmsbuthnqw
(robertc) Accelerate _walkdirs_utf8 on unix platforms. (Robert
        Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import errno
20
20
 
21
 
from bzrlib import tests
22
 
 
23
 
 
24
 
class _WalkdirsWin32Feature(tests.Feature):
 
21
from bzrlib import (
 
22
    osutils,
 
23
    tests,
 
24
    )
 
25
 
 
26
 
 
27
class _Win32ReadDirFeature(tests.Feature):
25
28
 
26
29
    def _probe(self):
27
30
        try:
32
35
            return True
33
36
 
34
37
    def feature_name(self):
35
 
        return 'bzrlib._walkdirs_win32'
 
38
        return 'bzrlib._Win32ReadDir'
36
39
 
37
 
WalkdirsWin32Feature = _WalkdirsWin32Feature()
 
40
Win32ReadDirFeature = _Win32ReadDirFeature()
38
41
 
39
42
 
40
43
class TestWin32Finder(tests.TestCaseInTempDir):
41
44
 
42
 
    _test_needs_features = [WalkdirsWin32Feature]
 
45
    _test_needs_features = [Win32ReadDirFeature]
43
46
 
44
47
    def setUp(self):
45
48
        super(TestWin32Finder, self).setUp()
46
49
        from bzrlib._walkdirs_win32 import (
47
 
            _walkdirs_utf8_win32_find_file
 
50
            Win32ReadDir,
48
51
            )
49
 
        self.walkdirs_utf8 = _walkdirs_utf8_win32_find_file
 
52
        self.reader = Win32ReadDir()
50
53
 
51
54
    def _remove_stat_from_dirblock(self, dirblock):
52
55
        return [info[:3] + info[4:] for info in dirblock]
53
56
 
54
57
    def assertWalkdirs(self, expected, top, prefix=''):
55
 
        finder = self.walkdirs_utf8(top, prefix=prefix)
56
 
        result = []
57
 
        for dirname, dirblock in finder:
58
 
            result.append((dirname, self._remove_stat_from_dirblock(dirblock)))
 
58
        old_selected_dir_reader = osutils._selected_dir_reader
 
59
        try:
 
60
            osutils._selected_dir_reader = self.reader
 
61
            finder = osutils._walkdirs_utf8(top, prefix=prefix)
 
62
            result = []
 
63
            for dirname, dirblock in finder:
 
64
                dirblock = self._remove_stat_from_dirblock(dirblock)
 
65
                result.append((dirname, dirblock))
 
66
            self.assertEqual(expected, result)
 
67
        finally:
 
68
            osutils._selected_dir_reader = old_selected_dir_reader
 
69
 
 
70
    def assertReadDir(self, expected, prefix, top_unicode):
 
71
        result = self._remove_stat_from_dirblock(
 
72
            self.reader.read_dir(prefix, top_unicode))
59
73
        self.assertEqual(expected, result)
60
74
 
 
75
    def test_top_prefix_to_starting_dir(self):
 
76
        # preparing an iteration should create a unicode native path.
 
77
        self.assertEqual(('prefix', None, None, None, u'\x12'),
 
78
            self.reader.top_prefix_to_starting_dir(u'\x12'.encode('utf8'),
 
79
                                                   'prefix'))
 
80
 
61
81
    def test_empty_directory(self):
 
82
        self.assertReadDir([], 'prefix', u'.')
62
83
        self.assertWalkdirs([(('', u'.'), [])], u'.')
63
84
 
64
 
    def test_file_in_dir(self):
 
85
    def test_file(self):
65
86
        self.build_tree(['foo'])
66
 
        self.assertWalkdirs([
67
 
            (('', u'.'), [('foo', 'foo', 'file', u'./foo')])
68
 
            ], u'.')
69
 
 
70
 
    def test_subdir(self):
71
 
        self.build_tree(['foo', 'bar/', 'bar/baz'])
72
 
        self.assertWalkdirs([
73
 
            (('', u'.'), [('bar', 'bar', 'directory', u'./bar'),
74
 
                          ('foo', 'foo', 'file', u'./foo'),
75
 
                         ]),
76
 
            (('bar', u'./bar'), [('bar/baz', 'baz', 'file', u'./bar/baz')]),
77
 
            ], '.')
78
 
        self.assertWalkdirs([
79
 
            (('xxx', u'.'), [('xxx/bar', 'bar', 'directory', u'./bar'),
80
 
                             ('xxx/foo', 'foo', 'file', u'./foo'),
81
 
                            ]),
82
 
            (('xxx/bar', u'./bar'), [('xxx/bar/baz', 'baz', 'file', u'./bar/baz')]),
83
 
            ], '.', prefix='xxx')
84
 
        self.assertWalkdirs([
85
 
            (('', u'bar'), [('baz', 'baz', 'file', u'bar/baz')]),
86
 
            ], 'bar')
87
 
 
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'),
93
 
                         ]
94
 
        self.assertWalkdirs([
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')]),
99
 
            ], '.')
100
 
 
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)
 
87
        self.assertReadDir([('foo', 'foo', 'file', u'./foo')],
 
88
            '', u'.')
 
89
 
 
90
    def test_dir(self):
 
91
        self.build_tree(['bar/'])
 
92
        self.assertReadDir([('bar', 'bar', 'directory', u'./bar')],
 
93
            '', u'.')
 
94
 
 
95
    def test_prefix(self):
 
96
        self.build_tree(['bar/', 'baf'])
 
97
        self.assertReadDir([
 
98
            ('xxx/baf', 'baf', 'file', u'./baf'),
 
99
            ('xxx/bar', 'bar', 'directory', u'./bar'),
 
100
            ],
 
101
            'xxx', u'.')
116
102
 
117
103
    def test_missing_dir(self):
118
 
        e = self.assertRaises(WindowsError, list,
119
 
                                self.walkdirs_utf8(u'no_such_dir'))
 
104
        e = self.assertRaises(WindowsError,
 
105
            self.reader.read_dir, 'prefix', u'no_such_dir')
120
106
        self.assertEqual(errno.ENOENT, e.errno)
121
107
        self.assertEqual(3, e.winerror)
122
108
        self.assertEqual((3, u'no_such_dir/*'), e.args)