~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-08-27 02:27:19 UTC
  • mfrom: (4634.3.19 gc-batching)
  • Revision ID: pqm@pqm.ubuntu.com-20090827022719-bl2yoqhpj3fcfczu
(andrew) Fix #402657: 2a fetch over dumb transport reads one group at
        a time.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
import os
18
18
import sys
19
19
 
20
20
from bzrlib import osutils
21
 
from bzrlib.tests import TestCase, TestCaseInTempDir, Feature
 
21
from bzrlib.tests import (
 
22
    Feature,
 
23
    TestCase,
 
24
    TestCaseInTempDir,
 
25
    TestSkipped,
 
26
    UnicodeFilenameFeature,
 
27
    )
22
28
from bzrlib.win32utils import glob_expand, get_app_path
 
29
from bzrlib import win32utils
23
30
 
24
31
 
25
32
# Features
36
43
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
37
44
 
38
45
 
39
 
class _Win32RegistryFeature(Feature):
 
46
class _RequiredModuleFeature(Feature):
 
47
 
 
48
    def __init__(self, mod_name):
 
49
        self.mod_name = mod_name
 
50
        super(_RequiredModuleFeature, self).__init__()
40
51
 
41
52
    def _probe(self):
42
53
        try:
43
 
            import _winreg
 
54
            __import__(self.mod_name)
44
55
            return True
45
56
        except ImportError:
46
57
            return False
47
58
 
48
59
    def feature_name(self):
49
 
        return '_winreg'
 
60
        return self.mod_name
50
61
 
51
 
Win32RegistryFeature = _Win32RegistryFeature()
 
62
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
 
63
CtypesFeature = _RequiredModuleFeature('ctypes')
 
64
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
52
65
 
53
66
 
54
67
# Tests
55
68
# -----
56
69
 
57
70
class TestNeedsGlobExpansionFeature(TestCase):
58
 
    
 
71
 
59
72
    def test_available(self):
60
 
        self.assertEqual(sys.platform == 'win32', 
 
73
        self.assertEqual(sys.platform == 'win32',
61
74
                         NeedsGlobExpansionFeature.available())
62
 
        
 
75
 
63
76
    def test_str(self):
64
77
        self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
65
78
 
67
80
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
68
81
 
69
82
    _test_needs_features = [NeedsGlobExpansionFeature]
70
 
   
 
83
 
71
84
    def test_empty_tree(self):
72
85
        self.build_tree([])
73
86
        self._run_testset([
75
88
            [['?'], ['?']],
76
89
            [['*'], ['*']],
77
90
            [['a', 'a'], ['a', 'a']]])
78
 
        
 
91
 
79
92
    def test_tree_ascii(self):
80
93
        """Checks the glob expansion and path separation char
81
94
        normalization"""
82
95
        self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
83
96
                         'b', 'b1', 'b2', 'b3',
84
 
                         'c/', 'c/c1', 'c/c2', 
 
97
                         'c/', 'c/c1', 'c/c2',
85
98
                         'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
86
99
        self._run_testset([
87
100
            # no wildcards
88
101
            [[u'a'], [u'a']],
89
102
            [[u'a', u'a' ], [u'a', u'a']],
90
103
            [[u'A'], [u'A']],
91
 
                
 
104
 
92
105
            [[u'd'], [u'd']],
93
106
            [[u'd/'], [u'd/']],
94
107
            [[u'd\\'], [u'd/']],
95
 
            
 
108
 
96
109
            # wildcards
97
110
            [[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
98
111
            [[u'?'], [u'a', u'b', u'c', u'd']],
100
113
            [[u'a??'], [u'a11', u'a.1']],
101
114
            [[u'b[1-2]'], [u'b1', u'b2']],
102
115
            [[u'A?'], [u'a1', u'a2']],
103
 
               
 
116
 
104
117
            [[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
105
118
            [[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
106
119
            [[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
107
120
            [[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
108
121
            [[u'*/'], [u'c/', u'd/']],
109
122
            [[u'*\\'], [u'c/', u'd/']]])
110
 
        
 
123
 
111
124
    def test_tree_unicode(self):
112
125
        """Checks behaviour with non-ascii filenames"""
113
126
        self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
115
128
            # no wildcards
116
129
            [[u'\u1234'], [u'\u1234']],
117
130
            [[u'\u1235'], [u'\u1235']],
118
 
         
 
131
 
119
132
            [[u'\u1235/'], [u'\u1235/']],
120
133
            [[u'\u1235/\u1235'], [u'\u1235/\u1235']],
121
 
            
 
134
 
122
135
            # wildcards
123
136
            [[u'?'], [u'\u1234', u'\u1235']],
124
137
            [[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
125
138
            [[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
126
 
            
 
139
 
127
140
            [[u'\u1235/?'], [u'\u1235/\u1235']],
128
141
            [[u'\u1235/*'], [u'\u1235/\u1235']],
129
142
            [[u'\u1235\\?'], [u'\u1235/\u1235']],
157
170
            self.assertEquals('iexplore.exe', b.lower())
158
171
            self.assertNotEquals('', d)
159
172
 
 
173
    def test_wordpad(self):
 
174
        # typical windows users should have wordpad in the system
 
175
        # but there is problem: its path has the format REG_EXPAND_SZ
 
176
        # so naive attempt to get the path is not working
 
177
        for a in ('wordpad', 'wordpad.exe'):
 
178
            p = get_app_path(a)
 
179
            d, b = os.path.split(p)
 
180
            self.assertEquals('wordpad.exe', b.lower())
 
181
            self.assertNotEquals('', d)
 
182
 
160
183
    def test_not_existing(self):
161
184
        p = get_app_path('not-existing')
162
185
        self.assertEquals('not-existing', p)
 
186
 
 
187
 
 
188
class TestLocationsCtypes(TestCase):
 
189
 
 
190
    _test_needs_features = [CtypesFeature]
 
191
 
 
192
    def assertPathsEqual(self, p1, p2):
 
193
        # TODO: The env var values in particular might return the "short"
 
194
        # version (ie, "C:\DOCUME~1\...").  Its even possible the returned
 
195
        # values will differ only by case - handle these situations as we
 
196
        # come across them.
 
197
        self.assertEquals(p1, p2)
 
198
 
 
199
    def test_appdata_not_using_environment(self):
 
200
        # Test that we aren't falling back to the environment
 
201
        first = win32utils.get_appdata_location()
 
202
        self._captureVar("APPDATA", None)
 
203
        self.assertPathsEqual(first, win32utils.get_appdata_location())
 
204
 
 
205
    def test_appdata_matches_environment(self):
 
206
        # Typically the APPDATA environment variable will match
 
207
        # get_appdata_location
 
208
        # XXX - See bug 262874, which asserts the correct encoding is 'mbcs',
 
209
        encoding = osutils.get_user_encoding()
 
210
        env_val = os.environ.get("APPDATA", None)
 
211
        if not env_val:
 
212
            raise TestSkipped("No APPDATA environment variable exists")
 
213
        self.assertPathsEqual(win32utils.get_appdata_location(),
 
214
                              env_val.decode(encoding))
 
215
 
 
216
    def test_local_appdata_not_using_environment(self):
 
217
        # Test that we aren't falling back to the environment
 
218
        first = win32utils.get_local_appdata_location()
 
219
        self._captureVar("LOCALAPPDATA", None)
 
220
        self.assertPathsEqual(first, win32utils.get_local_appdata_location())
 
221
 
 
222
    def test_local_appdata_matches_environment(self):
 
223
        # LOCALAPPDATA typically only exists on Vista, so we only attempt to
 
224
        # compare when it exists.
 
225
        lad = win32utils.get_local_appdata_location()
 
226
        env = os.environ.get("LOCALAPPDATA")
 
227
        if env:
 
228
            # XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
 
229
            encoding = osutils.get_user_encoding()
 
230
            self.assertPathsEqual(lad, env.decode(encoding))
 
231
 
 
232
 
 
233
class TestLocationsPywin32(TestLocationsCtypes):
 
234
 
 
235
    _test_needs_features = [Win32comShellFeature]
 
236
 
 
237
    def setUp(self):
 
238
        super(TestLocationsPywin32, self).setUp()
 
239
        # We perform the exact same tests after disabling the use of ctypes.
 
240
        # This causes the implementation to fall back to pywin32.
 
241
        self.old_ctypes = win32utils.has_ctypes
 
242
        win32utils.has_ctypes = False
 
243
        self.addCleanup(self.restoreCtypes)
 
244
 
 
245
    def restoreCtypes(self):
 
246
        win32utils.has_ctypes = self.old_ctypes
 
247
 
 
248
 
 
249
class TestSetHidden(TestCaseInTempDir):
 
250
 
 
251
    def test_unicode_dir(self):
 
252
        # we should handle unicode paths without errors
 
253
        self.requireFeature(UnicodeFilenameFeature)
 
254
        os.mkdir(u'\u1234')
 
255
        win32utils.set_file_attr_hidden(u'\u1234')
 
256
 
 
257
    def test_dot_bzr_in_unicode_dir(self):
 
258
        # we should not raise traceback if we try to set hidden attribute
 
259
        # on .bzr directory below unicode path
 
260
        self.requireFeature(UnicodeFilenameFeature)
 
261
        os.makedirs(u'\u1234\\.bzr')
 
262
        path = osutils.abspath(u'\u1234\\.bzr')
 
263
        win32utils.set_file_attr_hidden(path)