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

  • Committer: Jelmer Vernooij
  • Date: 2017-07-23 22:06:41 UTC
  • mfrom: (6738 trunk)
  • mto: This revision was merged to the branch mainline in revision 6739.
  • Revision ID: jelmer@jelmer.uk-20170723220641-69eczax9bmv8d6kk
Merge trunk, address review comments.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
from .. import (
22
22
    osutils,
 
23
    symbol_versioning,
23
24
    tests,
24
25
    win32utils,
25
26
    )
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/']],
181
188
        self.assertEqual('not-existing', p)
182
189
 
183
190
 
184
 
class TestLocations(TestCase):
 
191
class TestLocationsCtypes(TestCase):
185
192
 
186
 
    _test_needs_features = [features.win32_feature]
 
193
    _test_needs_features = [CtypesFeature, features.win32_feature]
187
194
 
188
195
    def assertPathsEqual(self, p1, p2):
189
196
        # TODO: The env var values in particular might return the "short"
221
228
        lad = win32utils.get_local_appdata_location()
222
229
        env = os.environ.get("LOCALAPPDATA")
223
230
        if env:
224
 
            # XXX - See bug 262874, which asserts the correct encoding is
225
 
            # 'mbcs'
 
231
            # XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
226
232
            encoding = osutils.get_user_encoding()
227
233
            self.assertPathsEqual(lad, env.decode(encoding))
228
234
 
229
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.overrideAttr(win32utils, 'has_ctypes', False)
 
245
        # FIXME: this should be done by parametrization -- vila 100123
 
246
 
 
247
 
230
248
class TestSetHidden(TestCaseInTempDir):
231
249
 
232
 
    _test_needs_features = [features.win32_feature]
233
 
 
234
250
    def test_unicode_dir(self):
235
251
        # we should handle unicode paths without errors
236
252
        self.requireFeature(features.UnicodeFilenameFeature)
249
265
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
250
266
 
251
267
    def assertCommandLine(self, expected, line, argv=None,
252
 
                          single_quotes_allowed=False):
 
268
            single_quotes_allowed=False):
253
269
        # Strictly speaking we should respect parameter order versus glob
254
270
        # expansions, but it's not really worth the effort here
255
271
        if argv is None:
256
272
            argv = [line]
257
 
        argv = win32utils._command_line_to_argv(
258
 
            line, argv, single_quotes_allowed=single_quotes_allowed)
 
273
        argv = win32utils._command_line_to_argv(line, argv,
 
274
                single_quotes_allowed=single_quotes_allowed)
259
275
        self.assertEqual(expected, sorted(argv))
260
276
 
261
277
    def test_glob_paths(self):
273
289
        self.assertCommandLine([u'a/*.c'], '"a/*.c"')
274
290
        self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
275
291
        self.assertCommandLine([u'a/*.c'], "'a/*.c'",
276
 
                               single_quotes_allowed=True)
 
292
            single_quotes_allowed=True)
277
293
 
278
294
    def test_slashes_changed(self):
279
295
        # Quoting doesn't change the supplied args
280
296
        self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
281
297
        self.assertCommandLine([u'a\\*.c'], "'a\\*.c'",
282
 
                               single_quotes_allowed=True)
 
298
            single_quotes_allowed=True)
283
299
        # Expands the glob, but nothing matches, swaps slashes
284
300
        self.assertCommandLine([u'a/*.c'], 'a\\*.c')
285
301
        self.assertCommandLine([u'a/?.c'], 'a\\?.c')
288
304
 
289
305
    def test_single_quote_support(self):
290
306
        self.assertCommandLine(["add", "let's-do-it.txt"],
291
 
                               "add let's-do-it.txt",
292
 
                               ["add", "let's-do-it.txt"])
 
307
            "add let's-do-it.txt",
 
308
            ["add", "let's-do-it.txt"])
293
309
        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)
 
310
            self.assertCommandLine, ["add", "lets do it.txt"],
 
311
            "add 'lets do it.txt'", ["add", "'lets", "do", "it.txt'"],
 
312
            single_quotes_allowed=True)
298
313
 
299
314
    def test_case_insensitive_globs(self):
300
315
        if os.path.normcase("AbC") == "AbC":
313
328
        self.build_tree(['d/', 'd/f1', 'd/f2'])
314
329
        self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
315
330
        self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
316
 
                               ["add", u"d/*"])
317
 
 
 
331
            ["add", u"d/*"])
 
332
 
 
333
 
 
334
class TestGetEnvironUnicode(tests.TestCase):
 
335
    """Tests for accessing the environment via the windows wide api"""
 
336
 
 
337
    _test_needs_features = [CtypesFeature, features.win32_feature]
 
338
 
 
339
    def setUp(self):
 
340
        super(TestGetEnvironUnicode, self).setUp()
 
341
        self.overrideEnv("TEST", "1")
 
342
 
 
343
    def test_get(self):
 
344
        """In the normal case behaves the same as os.environ access"""
 
345
        self.assertEqual("1", win32utils.get_environ_unicode("TEST"))
 
346
 
 
347
    def test_unset(self):
 
348
        """A variable not present in the environment gives None by default"""
 
349
        del os.environ["TEST"]
 
350
        self.assertIs(None, win32utils.get_environ_unicode("TEST"))
 
351
 
 
352
    def test_unset_default(self):
 
353
        """A variable not present in the environment gives passed default"""
 
354
        del os.environ["TEST"]
 
355
        self.assertIs("a", win32utils.get_environ_unicode("TEST", "a"))
 
356
 
 
357
    def test_unicode(self):
 
358
        """A non-ascii variable is returned as unicode"""
 
359
        unicode_val = u"\xa7" # non-ascii character present in many encodings
 
360
        try:
 
361
            bytes_val = unicode_val.encode(osutils.get_user_encoding())
 
362
        except UnicodeEncodeError:
 
363
            self.skipTest("Couldn't encode non-ascii string for environ")
 
364
        os.environ["TEST"] = bytes_val
 
365
        self.assertEqual(unicode_val, win32utils.get_environ_unicode("TEST"))
 
366
 
 
367
    def test_long(self):
 
368
        """A variable bigger than heuristic buffer size is still accessible"""
 
369
        big_val = "x" * (2<<10)
 
370
        os.environ["TEST"] = big_val
 
371
        self.assertEqual(big_val, win32utils.get_environ_unicode("TEST"))
 
372
 
 
373
    def test_unexpected_error(self):
 
374
        """An error from the underlying platform function is propogated"""
 
375
        ERROR_INVALID_PARAMETER = 87
 
376
        SetLastError = win32utils.ctypes.windll.kernel32.SetLastError
 
377
        def failer(*args, **kwargs):
 
378
            SetLastError(ERROR_INVALID_PARAMETER)
 
379
            return 0
 
380
        self.overrideAttr(win32utils.get_environ_unicode, "_c_function",
 
381
            failer)
 
382
        e = self.assertRaises(WindowsError,
 
383
            win32utils.get_environ_unicode, "TEST")
 
384
        self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)