~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (C) 2008, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

"""Tests for the win32 walkdir extension."""

import errno

from bzrlib import (
    osutils,
    tests,
    )


win32_readdir_feature = tests.ModuleAvailableFeature('bzrlib._walkdirs_win32')


class TestWin32Finder(tests.TestCaseInTempDir):

    _test_needs_features = [win32_readdir_feature]

    def setUp(self):
        super(TestWin32Finder, self).setUp()
        from bzrlib._walkdirs_win32 import (
            Win32ReadDir,
            )
        self.reader = Win32ReadDir()

    def _remove_stat_from_dirblock(self, dirblock):
        return [info[:3] + info[4:] for info in dirblock]

    def assertWalkdirs(self, expected, top, prefix=''):
        old_selected_dir_reader = osutils._selected_dir_reader
        try:
            osutils._selected_dir_reader = self.reader
            finder = osutils._walkdirs_utf8(top, prefix=prefix)
            result = []
            for dirname, dirblock in finder:
                dirblock = self._remove_stat_from_dirblock(dirblock)
                result.append((dirname, dirblock))
            self.assertEqual(expected, result)
        finally:
            osutils._selected_dir_reader = old_selected_dir_reader

    def assertReadDir(self, expected, prefix, top_unicode):
        result = self._remove_stat_from_dirblock(
            self.reader.read_dir(prefix, top_unicode))
        self.assertEqual(expected, result)

    def test_top_prefix_to_starting_dir(self):
        # preparing an iteration should create a unicode native path.
        self.assertEqual(('prefix', None, None, None, u'\x12'),
            self.reader.top_prefix_to_starting_dir(u'\x12'.encode('utf8'),
                                                   'prefix'))

    def test_empty_directory(self):
        self.assertReadDir([], 'prefix', u'.')
        self.assertWalkdirs([(('', u'.'), [])], u'.')

    def test_file(self):
        self.build_tree(['foo'])
        self.assertReadDir([('foo', 'foo', 'file', u'./foo')],
            '', u'.')

    def test_directory(self):
        self.build_tree(['bar/'])
        self.assertReadDir([('bar', 'bar', 'directory', u'./bar')],
            '', u'.')

    def test_prefix(self):
        self.build_tree(['bar/', 'baf'])
        self.assertReadDir([
            ('xxx/baf', 'baf', 'file', u'./baf'),
            ('xxx/bar', 'bar', 'directory', u'./bar'),
            ],
            'xxx', u'.')

    def test_missing_dir(self):
        e = self.assertRaises(WindowsError,
            self.reader.read_dir, 'prefix', u'no_such_dir')
        self.assertEqual(errno.ENOENT, e.errno)
        self.assertEqual(3, e.winerror)
        self.assertEqual((3, u'no_such_dir/*'), e.args)