/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
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
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
21
from bzrlib import tests
22
23
24
class _WalkdirsWin32Feature(tests.Feature):
25
26
    def _probe(self):
27
        try:
28
            import bzrlib._walkdirs_win32
29
        except ImportError:
30
            return False
31
        else:
32
            return True
33
34
    def feature_name(self):
35
        return 'bzrlib._walkdirs_win32'
36
37
WalkdirsWin32Feature = _WalkdirsWin32Feature()
38
39
40
class TestWin32Finder(tests.TestCaseInTempDir):
41
42
    _test_needs_features = [WalkdirsWin32Feature]
43
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
44
    def setUp(self):
45
        super(TestWin32Finder, self).setUp()
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
46
        from bzrlib._walkdirs_win32 import (
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
47
            _walkdirs_utf8_win32_find_file
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
48
            )
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
49
        self.walkdirs_utf8 = _walkdirs_utf8_win32_find_file
50
51
    def _remove_stat_from_dirblock(self, dirblock):
52
        return [info[:3] + info[4:] for info in dirblock]
53
54
    def assertWalkdirs(self, expected, top, prefix=''):
55
        finder = self.walkdirs_utf8(top, prefix=prefix)
3504.4.4 by John Arbash Meinel
We have walkdirs basically working, only without timestamps
56
        result = []
57
        for dirname, dirblock in finder:
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
58
            result.append((dirname, self._remove_stat_from_dirblock(dirblock)))
3504.4.3 by John Arbash Meinel
Start working on an extension specifically for win32,
59
        self.assertEqual(expected, result)
60
61
    def test_empty_directory(self):
62
        self.assertWalkdirs([(('', u'.'), [])], u'.')
3504.4.4 by John Arbash Meinel
We have walkdirs basically working, only without timestamps
63
64
    def test_file_in_dir(self):
65
        self.build_tree(['foo'])
66
        self.assertWalkdirs([
67
            (('', u'.'), [('foo', 'foo', 'file', u'./foo')])
68
            ], u'.')
3504.4.5 by John Arbash Meinel
Add tests to ensure that you can skip subdirs, start exposing the function.
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)
3504.4.8 by John Arbash Meinel
Some code cleanups.
116
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)