~bzr-pqm/bzr/bzr.dev

3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
1
# Copyright (C) 2008 Canonical Ltd
2
#
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.
7
#
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.
12
#
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
16
17
"""Tests for the win32 walkdir extension."""
18
3504.4.8 by John Arbash Meinel
Some code cleanups.
19
import errno
20
3696.3.2 by John Arbash Meinel
Fix up some of the path joining logic.
21
from bzrlib import (
22
    osutils,
23
    tests,
24
    )
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
25
26
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
27
class _Win32ReadDirFeature(tests.Feature):
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
28
29
    def _probe(self):
30
        try:
31
            import bzrlib._walkdirs_win32
32
        except ImportError:
33
            return False
34
        else:
35
            return True
36
37
    def feature_name(self):
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
38
        return 'bzrlib._Win32ReadDir'
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
39
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
40
Win32ReadDirFeature = _Win32ReadDirFeature()
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
41
42
43
class TestWin32Finder(tests.TestCaseInTempDir):
44
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
45
    _test_needs_features = [Win32ReadDirFeature]
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
46
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
47
    def setUp(self):
48
        super(TestWin32Finder, self).setUp()
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
49
        from bzrlib._walkdirs_win32 import (
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
50
            Win32ReadDir,
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
51
            )
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
52
        self.reader = Win32ReadDir()
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
53
54
    def _remove_stat_from_dirblock(self, dirblock):
55
        return [info[:3] + info[4:] for info in dirblock]
56
57
    def assertWalkdirs(self, expected, top, prefix=''):
3696.3.2 by John Arbash Meinel
Fix up some of the path joining logic.
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
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
69
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
70
    def assertReadDir(self, expected, prefix, top_unicode):
71
        result = self._remove_stat_from_dirblock(
72
            self.reader.read_dir(prefix, top_unicode))
73
        self.assertEqual(expected, result)
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'),
3696.3.2 by John Arbash Meinel
Fix up some of the path joining logic.
78
            self.reader.top_prefix_to_starting_dir(u'\x12'.encode('utf8'),
79
                                                   'prefix'))
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
80
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
81
    def test_empty_directory(self):
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
82
        self.assertReadDir([], 'prefix', u'.')
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
83
        self.assertWalkdirs([(('', u'.'), [])], u'.')
3504.4.4 by John Arbash Meinel
We have walkdirs basically working, only without timestamps
84
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
85
    def test_file(self):
3504.4.4 by John Arbash Meinel
We have walkdirs basically working, only without timestamps
86
        self.build_tree(['foo'])
3696.3.1 by Robert Collins
Refactor bzrlib.osutils._walkdirs_utf8 to aid API migration in future.
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'.')
3504.4.8 by John Arbash Meinel
Some code cleanups.
102
103
    def test_missing_dir(self):
3696.3.3 by John Arbash Meinel
read_dir raises the exception directly, it isn't an iterator/generator
104
        e = self.assertRaises(WindowsError,
105
            self.reader.read_dir, 'prefix', u'no_such_dir')
3504.4.8 by John Arbash Meinel
Some code cleanups.
106
        self.assertEqual(errno.ENOENT, e.errno)
107
        self.assertEqual(3, e.winerror)
108
        self.assertEqual((3, u'no_such_dir/*'), e.args)