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/*",
333
class TestGetEnvironUnicode(tests.TestCase):
334
"""Tests for accessing the environment via the windows wide api"""
336
_test_needs_features = [CtypesFeature, features.win32_feature]
339
super(TestGetEnvironUnicode, self).setUp()
340
self.overrideEnv("TEST", "1")
343
"""In the normal case behaves the same as os.environ access"""
344
self.assertEqual("1", win32utils.get_environ_unicode("TEST"))
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"))
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"))
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
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"))
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"))
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))
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)
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)