27
from brzlib.tests import (
31
from .features import backslashdir_feature
32
from ..win32utils import glob_expand, get_app_path
32
from brzlib.tests.features import backslashdir_feature
33
from brzlib.win32utils import glob_expand, get_app_path
34
from brzlib.tests import (
38
39
Win32RegistryFeature = features.ModuleAvailableFeature('_winreg')
40
CtypesFeature = features.ModuleAvailableFeature('ctypes')
41
Win32comShellFeature = features.ModuleAvailableFeature('win32com.shell')
42
Win32ApiFeature = features.ModuleAvailableFeature('win32api')
41
48
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
184
191
class TestLocations(TestCase):
186
_test_needs_features = [features.win32_feature]
192
"""Tests for windows specific path and name retrieving functions"""
194
def test__ensure_unicode_deprecated(self):
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)
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)
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)
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)
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)
236
class TestLocationsCtypes(TestCase):
238
_test_needs_features = [CtypesFeature]
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")
224
# XXX - See bug 262874, which asserts the correct encoding is
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))
281
class TestLocationsPywin32(TestLocationsCtypes):
283
_test_needs_features = [Win32comShellFeature]
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
230
293
class TestSetHidden(TestCaseInTempDir):
232
_test_needs_features = [features.win32_feature]
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):
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
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))
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)
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')
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)
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*')
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/*",
319
379
class TestGetEnvironUnicode(tests.TestCase):
320
380
"""Tests for accessing the environment via the windows wide api"""
322
_test_needs_features = [features.win32_feature]
382
_test_needs_features = [CtypesFeature, features.win32_feature]
325
385
super(TestGetEnvironUnicode, self).setUp()
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
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"))
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"))
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
363
422
def failer(*args, **kwargs):
364
423
SetLastError(ERROR_INVALID_PARAMETER)
366
425
self.overrideAttr(win32utils.get_environ_unicode, "_c_function",
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)