/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 brzlib/tests/test_win32utils.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
import os
20
20
 
21
 
from .. import (
 
21
from brzlib import (
22
22
    osutils,
 
23
    symbol_versioning,
23
24
    tests,
24
25
    win32utils,
25
26
    )
26
 
from . import (
 
27
from brzlib.tests import (
27
28
    TestCase,
28
29
    TestCaseInTempDir,
29
30
    TestSkipped,
30
31
    )
31
 
from .features import backslashdir_feature
32
 
from ..win32utils import glob_expand, get_app_path
33
 
from . import (
 
32
from brzlib.tests.features import backslashdir_feature
 
33
from brzlib.win32utils import glob_expand, get_app_path
 
34
from brzlib.tests import (
34
35
    features,
35
36
    )
36
37
 
37
38
 
38
39
Win32RegistryFeature = features.ModuleAvailableFeature('_winreg')
39
 
 
 
40
CtypesFeature = features.ModuleAvailableFeature('ctypes')
 
41
Win32comShellFeature = features.ModuleAvailableFeature('win32com.shell')
 
42
Win32ApiFeature = features.ModuleAvailableFeature('win32api') 
 
43
 
 
44
 
 
45
# Tests
 
46
# -----
40
47
 
41
48
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
42
49
 
68
75
        self._run_testset([
69
76
            # no wildcards
70
77
            [[u'a'], [u'a']],
71
 
            [[u'a', u'a'], [u'a', u'a']],
 
78
            [[u'a', u'a' ], [u'a', u'a']],
72
79
 
73
80
            [[u'd'], [u'd']],
74
81
            [[u'd/'], [u'd/']],
99
106
 
100
107
    def test_case_insensitive_globbing(self):
101
108
        if os.path.normcase("AbC") == "AbC":
102
 
            self.skipTest("Test requires case insensitive globbing function")
 
109
            self.skip("Test requires case insensitive globbing function")
103
110
        self.build_ascii_tree()
104
111
        self._run_testset([
105
112
            [[u'A'], [u'A']],
182
189
 
183
190
 
184
191
class TestLocations(TestCase):
185
 
 
186
 
    _test_needs_features = [features.win32_feature]
 
192
    """Tests for windows specific path and name retrieving functions"""
 
193
 
 
194
    def test__ensure_unicode_deprecated(self):
 
195
        s = "text"
 
196
        u1 = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
197
            win32utils._ensure_unicode, s)
 
198
        self.assertEqual(s, u1)
 
199
        self.assertIsInstance(u1, unicode)
 
200
        u2 = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
201
            win32utils._ensure_unicode, u1)
 
202
        self.assertIs(u1, u2)
 
203
    
 
204
    def test_appdata_unicode_deprecated(self):
 
205
        self.overrideEnv("APPDATA", "fakepath")
 
206
        s = win32utils.get_appdata_location()
 
207
        u = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
208
            win32utils.get_appdata_location_unicode)
 
209
        self.assertEqual(s, u)
 
210
        self.assertIsInstance(s, unicode)
 
211
 
 
212
    def test_home_unicode_deprecated(self):
 
213
        s = win32utils.get_home_location()
 
214
        u = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
215
            win32utils.get_home_location_unicode)
 
216
        self.assertEqual(s, u)
 
217
        self.assertIsInstance(s, unicode)
 
218
 
 
219
    def test_user_unicode_deprecated(self):
 
220
        self.overrideEnv("USERNAME", "alien")
 
221
        s = win32utils.get_user_name()
 
222
        u = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
223
            win32utils.get_user_name_unicode)
 
224
        self.assertEqual(s, u)
 
225
        self.assertIsInstance(s, unicode)
 
226
 
 
227
    def test_host_unicode_deprecated(self):
 
228
        self.overrideEnv("COMPUTERNAME", "alienbox")
 
229
        s = win32utils.get_host_name()
 
230
        u = self.applyDeprecated(symbol_versioning.deprecated_in((2, 5, 0)),
 
231
            win32utils.get_host_name_unicode)
 
232
        self.assertEqual(s, u)
 
233
        self.assertIsInstance(s, unicode)
 
234
 
 
235
 
 
236
class TestLocationsCtypes(TestCase):
 
237
 
 
238
    _test_needs_features = [CtypesFeature]
187
239
 
188
240
    def assertPathsEqual(self, p1, p2):
189
241
        # TODO: The env var values in particular might return the "short"
221
273
        lad = win32utils.get_local_appdata_location()
222
274
        env = os.environ.get("LOCALAPPDATA")
223
275
        if env:
224
 
            # XXX - See bug 262874, which asserts the correct encoding is
225
 
            # 'mbcs'
 
276
            # XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
226
277
            encoding = osutils.get_user_encoding()
227
278
            self.assertPathsEqual(lad, env.decode(encoding))
228
279
 
229
280
 
 
281
class TestLocationsPywin32(TestLocationsCtypes):
 
282
 
 
283
    _test_needs_features = [Win32comShellFeature]
 
284
 
 
285
    def setUp(self):
 
286
        super(TestLocationsPywin32, self).setUp()
 
287
        # We perform the exact same tests after disabling the use of ctypes.
 
288
        # This causes the implementation to fall back to pywin32.
 
289
        self.overrideAttr(win32utils, 'has_ctypes', False)
 
290
        # FIXME: this should be done by parametrization -- vila 100123
 
291
 
 
292
 
230
293
class TestSetHidden(TestCaseInTempDir):
231
294
 
232
 
    _test_needs_features = [features.win32_feature]
233
 
 
234
295
    def test_unicode_dir(self):
235
296
        # we should handle unicode paths without errors
236
297
        self.requireFeature(features.UnicodeFilenameFeature)
249
310
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
250
311
 
251
312
    def assertCommandLine(self, expected, line, argv=None,
252
 
                          single_quotes_allowed=False):
 
313
            single_quotes_allowed=False):
253
314
        # Strictly speaking we should respect parameter order versus glob
254
315
        # expansions, but it's not really worth the effort here
255
316
        if argv is None:
256
317
            argv = [line]
257
 
        argv = win32utils._command_line_to_argv(
258
 
            line, argv, single_quotes_allowed=single_quotes_allowed)
 
318
        argv = win32utils._command_line_to_argv(line, argv,
 
319
                single_quotes_allowed=single_quotes_allowed)
259
320
        self.assertEqual(expected, sorted(argv))
260
321
 
261
322
    def test_glob_paths(self):
273
334
        self.assertCommandLine([u'a/*.c'], '"a/*.c"')
274
335
        self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
275
336
        self.assertCommandLine([u'a/*.c'], "'a/*.c'",
276
 
                               single_quotes_allowed=True)
 
337
            single_quotes_allowed=True)
277
338
 
278
339
    def test_slashes_changed(self):
279
340
        # Quoting doesn't change the supplied args
280
341
        self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
281
342
        self.assertCommandLine([u'a\\*.c'], "'a\\*.c'",
282
 
                               single_quotes_allowed=True)
 
343
            single_quotes_allowed=True)
283
344
        # Expands the glob, but nothing matches, swaps slashes
284
345
        self.assertCommandLine([u'a/*.c'], 'a\\*.c')
285
346
        self.assertCommandLine([u'a/?.c'], 'a\\?.c')
288
349
 
289
350
    def test_single_quote_support(self):
290
351
        self.assertCommandLine(["add", "let's-do-it.txt"],
291
 
                               "add let's-do-it.txt",
292
 
                               ["add", "let's-do-it.txt"])
 
352
            "add let's-do-it.txt",
 
353
            ["add", "let's-do-it.txt"])
293
354
        self.expectFailure("Using single quotes breaks trimming from argv",
294
 
                           self.assertCommandLine, ["add", "lets do it.txt"],
295
 
                           "add 'lets do it.txt'", [
296
 
                               "add", "'lets", "do", "it.txt'"],
297
 
                           single_quotes_allowed=True)
 
355
            self.assertCommandLine, ["add", "lets do it.txt"],
 
356
            "add 'lets do it.txt'", ["add", "'lets", "do", "it.txt'"],
 
357
            single_quotes_allowed=True)
298
358
 
299
359
    def test_case_insensitive_globs(self):
300
360
        if os.path.normcase("AbC") == "AbC":
301
 
            self.skipTest("Test requires case insensitive globbing function")
 
361
            self.skip("Test requires case insensitive globbing function")
302
362
        self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
303
363
        self.assertCommandLine([u'A/b.c'], 'A/B*')
304
364
 
313
373
        self.build_tree(['d/', 'd/f1', 'd/f2'])
314
374
        self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
315
375
        self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
316
 
                               ["add", u"d/*"])
 
376
            ["add", u"d/*"])
317
377
 
318
378
 
319
379
class TestGetEnvironUnicode(tests.TestCase):
320
380
    """Tests for accessing the environment via the windows wide api"""
321
381
 
322
 
    _test_needs_features = [features.win32_feature]
 
382
    _test_needs_features = [CtypesFeature, features.win32_feature]
323
383
 
324
384
    def setUp(self):
325
385
        super(TestGetEnvironUnicode, self).setUp()
341
401
 
342
402
    def test_unicode(self):
343
403
        """A non-ascii variable is returned as unicode"""
344
 
        unicode_val = u"\xa7"  # non-ascii character present in many encodings
 
404
        unicode_val = u"\xa7" # non-ascii character present in many encodings
345
405
        try:
346
406
            bytes_val = unicode_val.encode(osutils.get_user_encoding())
347
407
        except UnicodeEncodeError:
348
 
            self.skipTest("Couldn't encode non-ascii string for environ")
 
408
            self.skip("Couldn't encode non-ascii string to place in environ")
349
409
        os.environ["TEST"] = bytes_val
350
410
        self.assertEqual(unicode_val, win32utils.get_environ_unicode("TEST"))
351
411
 
352
412
    def test_long(self):
353
413
        """A variable bigger than heuristic buffer size is still accessible"""
354
 
        big_val = "x" * (2 << 10)
 
414
        big_val = "x" * (2<<10)
355
415
        os.environ["TEST"] = big_val
356
416
        self.assertEqual(big_val, win32utils.get_environ_unicode("TEST"))
357
417
 
359
419
        """An error from the underlying platform function is propogated"""
360
420
        ERROR_INVALID_PARAMETER = 87
361
421
        SetLastError = win32utils.ctypes.windll.kernel32.SetLastError
362
 
 
363
422
        def failer(*args, **kwargs):
364
423
            SetLastError(ERROR_INVALID_PARAMETER)
365
424
            return 0
366
425
        self.overrideAttr(win32utils.get_environ_unicode, "_c_function",
367
 
                          failer)
 
426
            failer)
368
427
        e = self.assertRaises(WindowsError,
369
 
                              win32utils.get_environ_unicode, "TEST")
 
428
            win32utils.get_environ_unicode, "TEST")
370
429
        self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)