181
188
self.assertEqual('not-existing', p)
184
class TestLocations(TestCase):
191
class TestLocationsCtypes(TestCase):
186
_test_needs_features = [features.win32_feature]
193
_test_needs_features = [CtypesFeature, features.win32_feature]
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")
224
# XXX - See bug 262874, which asserts the correct encoding is
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))
236
class TestLocationsPywin32(TestLocationsCtypes):
238
_test_needs_features = [Win32comShellFeature]
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
230
248
class TestSetHidden(TestCaseInTempDir):
232
_test_needs_features = [features.win32_feature]
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):
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
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))
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)
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')
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)
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/*",
334
class TestGetEnvironUnicode(tests.TestCase):
335
"""Tests for accessing the environment via the windows wide api"""
337
_test_needs_features = [CtypesFeature, features.win32_feature]
340
super(TestGetEnvironUnicode, self).setUp()
341
self.overrideEnv("TEST", "1")
344
"""In the normal case behaves the same as os.environ access"""
345
self.assertEqual("1", win32utils.get_environ_unicode("TEST"))
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"))
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"))
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
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"))
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"))
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)
380
self.overrideAttr(win32utils.get_environ_unicode, "_c_function",
382
e = self.assertRaises(WindowsError,
383
win32utils.get_environ_unicode, "TEST")
384
self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)