~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_win32utils.py

  • Committer: Martin Packman
  • Date: 2011-12-05 10:43:46 UTC
  • mto: This revision was merged to the branch mainline in revision 6358.
  • Revision ID: martin.packman@canonical.com-20111205104346-67hz756vsquty9mk
Add wrapper for GetEnvironmentVariableW on windows for unicode environ access

Show diffs side-by-side

added added

removed removed

Lines of Context:
328
328
        self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
329
329
        self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
330
330
            ["add", u"d/*"])
 
331
 
 
332
 
 
333
class TestGetEnvironUnicode(tests.TestCase):
 
334
    """Tests for accessing the environment via the windows wide api"""
 
335
 
 
336
    _test_needs_features = [CtypesFeature, features.win32_feature]
 
337
 
 
338
    def setUp(self):
 
339
        super(TestGetEnvironUnicode, self).setUp()
 
340
        self.overrideEnv("TEST", "1")
 
341
 
 
342
    def test_get(self):
 
343
        """In the normal case behaves the same as os.environ access"""
 
344
        self.assertEqual("1", win32utils.get_environ_unicode("TEST"))
 
345
 
 
346
    def test_unset(self):
 
347
        """A variable not present in the environment gives None by default"""
 
348
        del os.environ["TEST"]
 
349
        self.assertIs(None, win32utils.get_environ_unicode("TEST"))
 
350
 
 
351
    def test_unset_default(self):
 
352
        """A variable not present in the environment gives passed default"""
 
353
        del os.environ["TEST"]
 
354
        self.assertIs("a", win32utils.get_environ_unicode("TEST", "a"))
 
355
 
 
356
    def test_unicode(self):
 
357
        """A non-ascii variable is returned as unicode"""
 
358
        unicode_val = u"\xa7" # non-ascii character present in many encodings
 
359
        try:
 
360
            bytes_val = unicode_val.encode(osutils.get_user_encoding())
 
361
        except UnicodeEncodeError:
 
362
            self.skip("Couldn't encode non-ascii string to place in environ")
 
363
        os.environ["TEST"] = bytes_val
 
364
        self.assertEqual(unicode_val, win32utils.get_environ_unicode("TEST"))
 
365
 
 
366
    def test_long(self):
 
367
        """A variable bigger than heuristic buffer size is still accessible"""
 
368
        big_val = "x" * (2<<10)
 
369
        os.environ["TEST"] = big_val
 
370
        self.assertEqual(big_val, win32utils.get_environ_unicode("TEST"))
 
371
 
 
372
    def test_buffer_sizing(self):
 
373
        """Need buffer for the length of the variable plus null terminal"""
 
374
        self.assertRaises(ValueError,
 
375
            win32utils.get_environ_unicode, "TEST", _size=1)
 
376
        self.assertEqual("1", win32utils.get_environ_unicode("TEST", _size=2))
 
377
 
 
378
    def test_unexpected_error(self):
 
379
        """An error from the underlying platform function is propogated"""
 
380
        ERROR_INVALID_PARAMETER = 87
 
381
        SetLastError = win32utils.ctypes.windll.kernel32.SetLastError
 
382
        def failer(*args, **kwargs):
 
383
            SetLastError(ERROR_INVALID_PARAMETER)
 
384
            return 0
 
385
        self.overrideAttr(win32utils.get_environ_unicode, "_func", failer)
 
386
        e = self.assertRaises(WindowsError,
 
387
            win32utils.get_environ_unicode, "TEST")
 
388
        self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)