/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Andrew Bennetts
  • Date: 2009-11-11 06:50:40 UTC
  • mto: This revision was merged to the branch mainline in revision 4793.
  • Revision ID: andrew.bennetts@canonical.com-20091111065040-ibq6r3cnza7ekyzs
Show real branch/repo format description in 'info -v' over HPSS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import os
 
18
import sys
 
19
 
 
20
from bzrlib import (
 
21
    osutils,
 
22
    tests,
 
23
    win32utils,
 
24
    )
 
25
from bzrlib.tests import (
 
26
    Feature,
 
27
    TestCase,
 
28
    TestCaseInTempDir,
 
29
    TestSkipped,
 
30
    UnicodeFilenameFeature,
 
31
    )
 
32
from bzrlib.win32utils import glob_expand, get_app_path
 
33
 
 
34
 
 
35
# Features
 
36
# --------
 
37
 
 
38
class _NeedsGlobExpansionFeature(Feature):
 
39
 
 
40
    def _probe(self):
 
41
        return sys.platform == 'win32'
 
42
 
 
43
    def feature_name(self):
 
44
        return 'Internally performed glob expansion'
 
45
 
 
46
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
 
47
 
 
48
 
 
49
class _RequiredModuleFeature(Feature):
 
50
 
 
51
    def __init__(self, mod_name):
 
52
        self.mod_name = mod_name
 
53
        super(_RequiredModuleFeature, self).__init__()
 
54
 
 
55
    def _probe(self):
 
56
        try:
 
57
            __import__(self.mod_name)
 
58
            return True
 
59
        except ImportError:
 
60
            return False
 
61
 
 
62
    def feature_name(self):
 
63
        return self.mod_name
 
64
 
 
65
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
 
66
CtypesFeature = _RequiredModuleFeature('ctypes')
 
67
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
 
68
 
 
69
 
 
70
# Tests
 
71
# -----
 
72
 
 
73
class TestNeedsGlobExpansionFeature(TestCase):
 
74
 
 
75
    def test_available(self):
 
76
        self.assertEqual(sys.platform == 'win32',
 
77
                         NeedsGlobExpansionFeature.available())
 
78
 
 
79
    def test_str(self):
 
80
        self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
 
81
 
 
82
 
 
83
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
 
84
 
 
85
    _test_needs_features = [NeedsGlobExpansionFeature]
 
86
 
 
87
    def test_empty_tree(self):
 
88
        self.build_tree([])
 
89
        self._run_testset([
 
90
            [['a'], ['a']],
 
91
            [['?'], ['?']],
 
92
            [['*'], ['*']],
 
93
            [['a', 'a'], ['a', 'a']]])
 
94
 
 
95
    def test_tree_ascii(self):
 
96
        """Checks the glob expansion and path separation char
 
97
        normalization"""
 
98
        self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
 
99
                         'b', 'b1', 'b2', 'b3',
 
100
                         'c/', 'c/c1', 'c/c2',
 
101
                         'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
 
102
        self._run_testset([
 
103
            # no wildcards
 
104
            [[u'a'], [u'a']],
 
105
            [[u'a', u'a' ], [u'a', u'a']],
 
106
            [[u'A'], [u'A']],
 
107
 
 
108
            [[u'd'], [u'd']],
 
109
            [[u'd/'], [u'd/']],
 
110
            [[u'd\\'], [u'd/']],
 
111
 
 
112
            # wildcards
 
113
            [[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
 
114
            [[u'?'], [u'a', u'b', u'c', u'd']],
 
115
            [[u'a?'], [u'a1', u'a2']],
 
116
            [[u'a??'], [u'a11', u'a.1']],
 
117
            [[u'b[1-2]'], [u'b1', u'b2']],
 
118
            [[u'A?'], [u'a1', u'a2']],
 
119
 
 
120
            [[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
 
121
            [[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
 
122
            [[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
 
123
            [[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
 
124
            [[u'*/'], [u'c/', u'd/']],
 
125
            [[u'*\\'], [u'c/', u'd/']]])
 
126
 
 
127
    def test_tree_unicode(self):
 
128
        """Checks behaviour with non-ascii filenames"""
 
129
        self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
 
130
        self._run_testset([
 
131
            # no wildcards
 
132
            [[u'\u1234'], [u'\u1234']],
 
133
            [[u'\u1235'], [u'\u1235']],
 
134
 
 
135
            [[u'\u1235/'], [u'\u1235/']],
 
136
            [[u'\u1235/\u1235'], [u'\u1235/\u1235']],
 
137
 
 
138
            # wildcards
 
139
            [[u'?'], [u'\u1234', u'\u1235']],
 
140
            [[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
 
141
            [[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
 
142
 
 
143
            [[u'\u1235/?'], [u'\u1235/\u1235']],
 
144
            [[u'\u1235/*'], [u'\u1235/\u1235']],
 
145
            [[u'\u1235\\?'], [u'\u1235/\u1235']],
 
146
            [[u'\u1235\\*'], [u'\u1235/\u1235']],
 
147
            [[u'?/'], [u'\u1235/']],
 
148
            [[u'*/'], [u'\u1235/']],
 
149
            [[u'?\\'], [u'\u1235/']],
 
150
            [[u'*\\'], [u'\u1235/']],
 
151
            [[u'?/?'], [u'\u1235/\u1235']],
 
152
            [[u'*/*'], [u'\u1235/\u1235']],
 
153
            [[u'?\\?'], [u'\u1235/\u1235']],
 
154
            [[u'*\\*'], [u'\u1235/\u1235']]])
 
155
 
 
156
    def _run_testset(self, testset):
 
157
        for pattern, expected in testset:
 
158
            result = glob_expand(pattern)
 
159
            expected.sort()
 
160
            result.sort()
 
161
            self.assertEqual(expected, result, 'pattern %s' % pattern)
 
162
 
 
163
 
 
164
class TestAppPaths(TestCase):
 
165
 
 
166
    _test_needs_features = [Win32RegistryFeature]
 
167
 
 
168
    def test_iexplore(self):
 
169
        # typical windows users should have IE installed
 
170
        for a in ('iexplore', 'iexplore.exe'):
 
171
            p = get_app_path(a)
 
172
            d, b = os.path.split(p)
 
173
            self.assertEquals('iexplore.exe', b.lower())
 
174
            self.assertNotEquals('', d)
 
175
 
 
176
    def test_wordpad(self):
 
177
        # typical windows users should have wordpad in the system
 
178
        # but there is problem: its path has the format REG_EXPAND_SZ
 
179
        # so naive attempt to get the path is not working
 
180
        for a in ('wordpad', 'wordpad.exe'):
 
181
            p = get_app_path(a)
 
182
            d, b = os.path.split(p)
 
183
            self.assertEquals('wordpad.exe', b.lower())
 
184
            self.assertNotEquals('', d)
 
185
 
 
186
    def test_not_existing(self):
 
187
        p = get_app_path('not-existing')
 
188
        self.assertEquals('not-existing', p)
 
189
 
 
190
 
 
191
class TestLocationsCtypes(TestCase):
 
192
 
 
193
    _test_needs_features = [CtypesFeature]
 
194
 
 
195
    def assertPathsEqual(self, p1, p2):
 
196
        # TODO: The env var values in particular might return the "short"
 
197
        # version (ie, "C:\DOCUME~1\...").  Its even possible the returned
 
198
        # values will differ only by case - handle these situations as we
 
199
        # come across them.
 
200
        self.assertEquals(p1, p2)
 
201
 
 
202
    def test_appdata_not_using_environment(self):
 
203
        # Test that we aren't falling back to the environment
 
204
        first = win32utils.get_appdata_location()
 
205
        self._captureVar("APPDATA", None)
 
206
        self.assertPathsEqual(first, win32utils.get_appdata_location())
 
207
 
 
208
    def test_appdata_matches_environment(self):
 
209
        # Typically the APPDATA environment variable will match
 
210
        # get_appdata_location
 
211
        # XXX - See bug 262874, which asserts the correct encoding is 'mbcs',
 
212
        encoding = osutils.get_user_encoding()
 
213
        env_val = os.environ.get("APPDATA", None)
 
214
        if not env_val:
 
215
            raise TestSkipped("No APPDATA environment variable exists")
 
216
        self.assertPathsEqual(win32utils.get_appdata_location(),
 
217
                              env_val.decode(encoding))
 
218
 
 
219
    def test_local_appdata_not_using_environment(self):
 
220
        # Test that we aren't falling back to the environment
 
221
        first = win32utils.get_local_appdata_location()
 
222
        self._captureVar("LOCALAPPDATA", None)
 
223
        self.assertPathsEqual(first, win32utils.get_local_appdata_location())
 
224
 
 
225
    def test_local_appdata_matches_environment(self):
 
226
        # LOCALAPPDATA typically only exists on Vista, so we only attempt to
 
227
        # compare when it exists.
 
228
        lad = win32utils.get_local_appdata_location()
 
229
        env = os.environ.get("LOCALAPPDATA")
 
230
        if env:
 
231
            # XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
 
232
            encoding = osutils.get_user_encoding()
 
233
            self.assertPathsEqual(lad, env.decode(encoding))
 
234
 
 
235
 
 
236
class TestLocationsPywin32(TestLocationsCtypes):
 
237
 
 
238
    _test_needs_features = [Win32comShellFeature]
 
239
 
 
240
    def setUp(self):
 
241
        super(TestLocationsPywin32, self).setUp()
 
242
        # We perform the exact same tests after disabling the use of ctypes.
 
243
        # This causes the implementation to fall back to pywin32.
 
244
        self.old_ctypes = win32utils.has_ctypes
 
245
        win32utils.has_ctypes = False
 
246
        self.addCleanup(self.restoreCtypes)
 
247
 
 
248
    def restoreCtypes(self):
 
249
        win32utils.has_ctypes = self.old_ctypes
 
250
 
 
251
 
 
252
class TestSetHidden(TestCaseInTempDir):
 
253
 
 
254
    def test_unicode_dir(self):
 
255
        # we should handle unicode paths without errors
 
256
        self.requireFeature(UnicodeFilenameFeature)
 
257
        os.mkdir(u'\u1234')
 
258
        win32utils.set_file_attr_hidden(u'\u1234')
 
259
 
 
260
    def test_dot_bzr_in_unicode_dir(self):
 
261
        # we should not raise traceback if we try to set hidden attribute
 
262
        # on .bzr directory below unicode path
 
263
        self.requireFeature(UnicodeFilenameFeature)
 
264
        os.makedirs(u'\u1234\\.bzr')
 
265
        path = osutils.abspath(u'\u1234\\.bzr')
 
266
        win32utils.set_file_attr_hidden(path)
 
267
 
 
268
 
 
269
 
 
270
class TestUnicodeShlex(tests.TestCase):
 
271
 
 
272
    def assertAsTokens(self, expected, line):
 
273
        s = win32utils.UnicodeShlex(line)
 
274
        self.assertEqual(expected, list(s))
 
275
 
 
276
    def test_simple(self):
 
277
        self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
 
278
                            u'foo bar baz')
 
279
 
 
280
    def test_ignore_multiple_spaces(self):
 
281
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo  bar')
 
282
 
 
283
    def test_ignore_leading_space(self):
 
284
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'  foo bar')
 
285
 
 
286
    def test_ignore_trailing_space(self):
 
287
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar  ')
 
288
 
 
289
    def test_posix_quotations(self):
 
290
        self.assertAsTokens([(True, u'foo bar')], u'"foo bar"')
 
291
        self.assertAsTokens([(False, u"'fo''o"), (False, u"b''ar'")],
 
292
            u"'fo''o b''ar'")
 
293
        self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"')
 
294
        self.assertAsTokens([(True, u"fo'o"), (True, u"b'ar")],
 
295
            u'"fo"\'o b\'"ar"')
 
296
 
 
297
    def test_nested_quotations(self):
 
298
        self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
 
299
        self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
 
300
 
 
301
    def test_empty_result(self):
 
302
        self.assertAsTokens([], u'')
 
303
        self.assertAsTokens([], u'    ')
 
304
 
 
305
    def test_quoted_empty(self):
 
306
        self.assertAsTokens([(True, '')], u'""')
 
307
        self.assertAsTokens([(False, u"''")], u"''")
 
308
 
 
309
    def test_unicode_chars(self):
 
310
        self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
 
311
                             u'f\xb5\xee \u1234\u3456')
 
312
 
 
313
    def test_newline_in_quoted_section(self):
 
314
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
 
315
 
 
316
    def test_escape_chars(self):
 
317
        self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
 
318
 
 
319
    def test_escape_quote(self):
 
320
        self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
 
321
 
 
322
    def test_double_escape(self):
 
323
        self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\bar"')
 
324
        self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
 
325
 
 
326
 
 
327
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
 
328
 
 
329
    def assertCommandLine(self, expected, line):
 
330
        self.assertEqual(expected, win32utils._command_line_to_argv(line))
 
331
 
 
332
    def test_glob_paths(self):
 
333
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
 
334
        self.assertCommandLine([u'a/b.c', u'a/c.c'], 'a/*.c')
 
335
        self.build_tree(['b/', 'b/b.c', 'b/d.c', 'b/d.h'])
 
336
        self.assertCommandLine([u'a/b.c', u'b/b.c'], '*/b.c')
 
337
        self.assertCommandLine([u'a/b.c', u'a/c.c', u'b/b.c', u'b/d.c'],
 
338
                               '*/*.c')
 
339
        # Bash style, just pass through the argument if nothing matches
 
340
        self.assertCommandLine([u'*/*.qqq'], '*/*.qqq')
 
341
 
 
342
    def test_quoted_globs(self):
 
343
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
 
344
        self.assertCommandLine([u'a/*.c'], '"a/*.c"')
 
345
        self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
 
346
 
 
347
    def test_slashes_changed(self):
 
348
        self.assertCommandLine([u'a/*.c'], '"a\\*.c"')
 
349
        # Expands the glob, but nothing matches
 
350
        self.assertCommandLine([u'a/*.c'], 'a\\*.c')
 
351
        self.assertCommandLine([u'a/foo.c'], 'a\\foo.c')
 
352
 
 
353
    def test_no_single_quote_supported(self):
 
354
        self.assertCommandLine(["add", "let's-do-it.txt"],
 
355
            "add let's-do-it.txt")