/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: John Arbash Meinel
  • Date: 2011-04-20 09:46:28 UTC
  • mfrom: (5609.33.4 2.3)
  • mto: (5609.33.5 2.3)
  • mto: This revision was merged to the branch mainline in revision 5811.
  • Revision ID: john@arbash-meinel.com-20110420094628-l0bafq1lwb6ib1v2
Merge lp:bzr/2.3 @ 5640 so we can update the release notes (aka NEWS)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007-2010 Canonical Ltd
 
1
# Copyright (C) 2007-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
 
17
"""Tests for win32utils."""
 
18
 
17
19
import os
18
 
import sys
19
20
 
20
21
from bzrlib import (
21
22
    osutils,
29
30
    TestSkipped,
30
31
    UnicodeFilenameFeature,
31
32
    )
 
33
from bzrlib.tests.features import backslashdir_feature
32
34
from bzrlib.win32utils import glob_expand, get_app_path
33
35
 
34
36
 
35
 
class _BackslashDirSeparatorFeature(tests.Feature):
36
 
 
37
 
    def _probe(self):
38
 
        try:
39
 
            os.lstat(os.getcwd() + '\\')
40
 
        except OSError:
41
 
            return False
42
 
        else:
43
 
            return True
44
 
 
45
 
    def feature_name(self):
46
 
        return "Filesystem treats '\\' as a directory separator."
47
 
 
48
 
BackslashDirSeparatorFeature = _BackslashDirSeparatorFeature()
49
 
 
50
 
 
51
37
class _RequiredModuleFeature(Feature):
52
38
 
53
39
    def __init__(self, mod_name):
67
53
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
68
54
CtypesFeature = _RequiredModuleFeature('ctypes')
69
55
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
 
56
Win32ApiFeature = _RequiredModuleFeature('win32api') 
70
57
 
71
58
 
72
59
# Tests
121
108
            ])
122
109
 
123
110
    def test_backslash_globbing(self):
124
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
111
        self.requireFeature(backslashdir_feature)
125
112
        self.build_ascii_tree()
126
113
        self._run_testset([
127
114
            [[u'd\\'], [u'd/']],
132
119
            ])
133
120
 
134
121
    def test_case_insensitive_globbing(self):
135
 
        self.requireFeature(tests.CaseInsCasePresFilenameFeature)
 
122
        if os.path.normcase("AbC") == "AbC":
 
123
            self.skip("Test requires case insensitive globbing function")
136
124
        self.build_ascii_tree()
137
125
        self._run_testset([
138
126
            [[u'A'], [u'A']],
164
152
            ])
165
153
 
166
154
    def test_unicode_backslashes(self):
167
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
155
        self.requireFeature(backslashdir_feature)
168
156
        self.build_unicode_tree()
169
157
        self._run_testset([
170
158
            # no wildcards
202
190
        # typical windows users should have wordpad in the system
203
191
        # but there is problem: its path has the format REG_EXPAND_SZ
204
192
        # so naive attempt to get the path is not working
 
193
        self.requireFeature(Win32ApiFeature)
205
194
        for a in ('wordpad', 'wordpad.exe'):
206
195
            p = get_app_path(a)
207
196
            d, b = os.path.split(p)
227
216
    def test_appdata_not_using_environment(self):
228
217
        # Test that we aren't falling back to the environment
229
218
        first = win32utils.get_appdata_location()
230
 
        self._captureVar("APPDATA", None)
 
219
        self.overrideEnv("APPDATA", None)
231
220
        self.assertPathsEqual(first, win32utils.get_appdata_location())
232
221
 
233
222
    def test_appdata_matches_environment(self):
244
233
    def test_local_appdata_not_using_environment(self):
245
234
        # Test that we aren't falling back to the environment
246
235
        first = win32utils.get_local_appdata_location()
247
 
        self._captureVar("LOCALAPPDATA", None)
 
236
        self.overrideEnv("LOCALAPPDATA", None)
248
237
        self.assertPathsEqual(first, win32utils.get_local_appdata_location())
249
238
 
250
239
    def test_local_appdata_matches_environment(self):
266
255
        super(TestLocationsPywin32, self).setUp()
267
256
        # We perform the exact same tests after disabling the use of ctypes.
268
257
        # This causes the implementation to fall back to pywin32.
269
 
        self.old_ctypes = win32utils.has_ctypes
270
 
        win32utils.has_ctypes = False
271
 
        self.addCleanup(self.restoreCtypes)
272
 
 
273
 
    def restoreCtypes(self):
274
 
        win32utils.has_ctypes = self.old_ctypes
 
258
        self.overrideAttr(win32utils, 'has_ctypes', False)
 
259
        # FIXME: this should be done by parametrization -- vila 100123
275
260
 
276
261
 
277
262
class TestSetHidden(TestCaseInTempDir):
291
276
        win32utils.set_file_attr_hidden(path)
292
277
 
293
278
 
294
 
 
295
 
class TestUnicodeShlex(tests.TestCase):
296
 
 
297
 
    def assertAsTokens(self, expected, line):
298
 
        s = win32utils.UnicodeShlex(line)
299
 
        self.assertEqual(expected, list(s))
300
 
 
301
 
    def test_simple(self):
302
 
        self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
303
 
                            u'foo bar baz')
304
 
 
305
 
    def test_ignore_multiple_spaces(self):
306
 
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo  bar')
307
 
 
308
 
    def test_ignore_leading_space(self):
309
 
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'  foo bar')
310
 
 
311
 
    def test_ignore_trailing_space(self):
312
 
        self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar  ')
313
 
 
314
 
    def test_posix_quotations(self):
315
 
        self.assertAsTokens([(True, u'foo bar')], u'"foo bar"')
316
 
        self.assertAsTokens([(False, u"'fo''o"), (False, u"b''ar'")],
317
 
            u"'fo''o b''ar'")
318
 
        self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"')
319
 
        self.assertAsTokens([(True, u"fo'o"), (True, u"b'ar")],
320
 
            u'"fo"\'o b\'"ar"')
321
 
 
322
 
    def test_nested_quotations(self):
323
 
        self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
324
 
        self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
325
 
 
326
 
    def test_empty_result(self):
327
 
        self.assertAsTokens([], u'')
328
 
        self.assertAsTokens([], u'    ')
329
 
 
330
 
    def test_quoted_empty(self):
331
 
        self.assertAsTokens([(True, '')], u'""')
332
 
        self.assertAsTokens([(False, u"''")], u"''")
333
 
 
334
 
    def test_unicode_chars(self):
335
 
        self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
336
 
                             u'f\xb5\xee \u1234\u3456')
337
 
 
338
 
    def test_newline_in_quoted_section(self):
339
 
        self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
340
 
 
341
 
    def test_escape_chars(self):
342
 
        self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
343
 
 
344
 
    def test_escape_quote(self):
345
 
        self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
346
 
 
347
 
    def test_double_escape(self):
348
 
        self.assertAsTokens([(True, u'foo\\\\bar')], u'"foo\\\\bar"')
349
 
        self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
350
 
 
351
 
    def test_n_backslashes_handling(self):
352
 
        # https://bugs.launchpad.net/bzr/+bug/528944
353
 
        # actually we care about the doubled backslashes when they're
354
 
        # represents UNC paths.
355
 
        # But in fact there is too much weird corner cases
356
 
        # (see https://bugs.launchpad.net/tortoisebzr/+bug/569050)
357
 
        # so to reproduce every bit of windows command-line handling
358
 
        # could be not worth of efforts?
359
 
        self.requireFeature(BackslashDirSeparatorFeature)
360
 
        self.assertAsTokens([(True, r'\\host\path')], r'"\\host\path"')
361
 
        self.assertAsTokens([(False, r'\\host\path')], r'\\host\path')
362
 
        # handling of " after the 2n and 2n+1 backslashes
363
 
        # inside and outside the quoted string
364
 
        self.assertAsTokens([(True, r'\\'), (False, r'*.py')], r'"\\\\" *.py')
365
 
        self.assertAsTokens([(True, r'\\" *.py')], r'"\\\\\" *.py"')
366
 
        self.assertAsTokens([(True, r'\\ *.py')], r'\\\\" *.py"')
367
 
        self.assertAsTokens([(False, r'\\"'), (False, r'*.py')],
368
 
                            r'\\\\\" *.py')
369
 
        self.assertAsTokens([(True, u'\\\\')], u'"\\\\')
370
 
 
371
 
 
372
279
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
373
280
 
374
 
    def assertCommandLine(self, expected, line):
 
281
    def assertCommandLine(self, expected, line, argv=None,
 
282
            single_quotes_allowed=False):
375
283
        # Strictly speaking we should respect parameter order versus glob
376
284
        # expansions, but it's not really worth the effort here
377
 
        self.assertEqual(expected,
378
 
                         sorted(win32utils._command_line_to_argv(line)))
 
285
        if argv is None:
 
286
            argv = [line]
 
287
        argv = win32utils._command_line_to_argv(line, argv,
 
288
                single_quotes_allowed=single_quotes_allowed)
 
289
        self.assertEqual(expected, sorted(argv))
379
290
 
380
291
    def test_glob_paths(self):
381
292
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
391
302
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
392
303
        self.assertCommandLine([u'a/*.c'], '"a/*.c"')
393
304
        self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
 
305
        self.assertCommandLine([u'a/*.c'], "'a/*.c'",
 
306
            single_quotes_allowed=True)
394
307
 
395
308
    def test_slashes_changed(self):
396
309
        # Quoting doesn't change the supplied args
397
310
        self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
 
311
        self.assertCommandLine([u'a\\*.c'], "'a\\*.c'",
 
312
            single_quotes_allowed=True)
398
313
        # Expands the glob, but nothing matches, swaps slashes
399
314
        self.assertCommandLine([u'a/*.c'], 'a\\*.c')
400
315
        self.assertCommandLine([u'a/?.c'], 'a\\?.c')
401
316
        # No glob, doesn't touch slashes
402
317
        self.assertCommandLine([u'a\\foo.c'], 'a\\foo.c')
403
318
 
404
 
    def test_no_single_quote_supported(self):
 
319
    def test_single_quote_support(self):
405
320
        self.assertCommandLine(["add", "let's-do-it.txt"],
406
 
            "add let's-do-it.txt")
 
321
            "add let's-do-it.txt",
 
322
            ["add", "let's-do-it.txt"])
 
323
        self.expectFailure("Using single quotes breaks trimming from argv",
 
324
            self.assertCommandLine, ["add", "lets do it.txt"],
 
325
            "add 'lets do it.txt'", ["add", "'lets", "do", "it.txt'"],
 
326
            single_quotes_allowed=True)
407
327
 
408
328
    def test_case_insensitive_globs(self):
409
 
        self.requireFeature(tests.CaseInsCasePresFilenameFeature)
 
329
        if os.path.normcase("AbC") == "AbC":
 
330
            self.skip("Test requires case insensitive globbing function")
410
331
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
411
332
        self.assertCommandLine([u'A/b.c'], 'A/B*')
412
333
 
413
334
    def test_backslashes(self):
414
 
        self.requireFeature(BackslashDirSeparatorFeature)
 
335
        self.requireFeature(backslashdir_feature)
415
336
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
416
337
        self.assertCommandLine([u'a/b.c'], 'a\\b*')
 
338
 
 
339
    def test_with_pdb(self):
 
340
        """Check stripping Python arguments before bzr script per lp:587868"""
 
341
        self.assertCommandLine([u"rocks"], "-m pdb rocks", ["rocks"])
 
342
        self.build_tree(['d/', 'd/f1', 'd/f2'])
 
343
        self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
 
344
        self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
 
345
            ["add", u"d/*"])