14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for win32utils."""
25
from bzrlib.tests import (
30
UnicodeFilenameFeature,
32
from bzrlib.win32utils import glob_expand, get_app_path
35
class _BackslashDirSeparatorFeature(tests.Feature):
39
os.lstat(os.getcwd() + '\\')
45
def feature_name(self):
46
return "Filesystem treats '\\' as a directory separator."
48
BackslashDirSeparatorFeature = _BackslashDirSeparatorFeature()
51
class _RequiredModuleFeature(Feature):
53
def __init__(self, mod_name):
54
self.mod_name = mod_name
55
super(_RequiredModuleFeature, self).__init__()
59
__import__(self.mod_name)
64
def feature_name(self):
67
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
68
CtypesFeature = _RequiredModuleFeature('ctypes')
69
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
31
from .features import backslashdir_feature
32
from ..win32utils import glob_expand, get_app_path
38
Win32RegistryFeature = features.ModuleAvailableFeature('_winreg')
39
CtypesFeature = features.ModuleAvailableFeature('ctypes')
40
Win32comShellFeature = features.ModuleAvailableFeature('win32com.shell')
41
Win32ApiFeature = features.ModuleAvailableFeature('win32api')
195
168
for a in ('iexplore', 'iexplore.exe'):
196
169
p = get_app_path(a)
197
170
d, b = os.path.split(p)
198
self.assertEquals('iexplore.exe', b.lower())
199
self.assertNotEquals('', d)
171
self.assertEqual('iexplore.exe', b.lower())
172
self.assertNotEqual('', d)
201
174
def test_wordpad(self):
202
175
# typical windows users should have wordpad in the system
203
176
# but there is problem: its path has the format REG_EXPAND_SZ
204
177
# so naive attempt to get the path is not working
178
self.requireFeature(Win32ApiFeature)
205
179
for a in ('wordpad', 'wordpad.exe'):
206
180
p = get_app_path(a)
207
181
d, b = os.path.split(p)
208
self.assertEquals('wordpad.exe', b.lower())
209
self.assertNotEquals('', d)
182
self.assertEqual('wordpad.exe', b.lower())
183
self.assertNotEqual('', d)
211
185
def test_not_existing(self):
212
186
p = get_app_path('not-existing')
213
self.assertEquals('not-existing', p)
187
self.assertEqual('not-existing', p)
216
190
class TestLocationsCtypes(TestCase):
218
_test_needs_features = [CtypesFeature]
192
_test_needs_features = [CtypesFeature, features.win32_feature]
220
194
def assertPathsEqual(self, p1, p2):
221
195
# TODO: The env var values in particular might return the "short"
222
196
# version (ie, "C:\DOCUME~1\..."). Its even possible the returned
223
197
# values will differ only by case - handle these situations as we
224
198
# come across them.
225
self.assertEquals(p1, p2)
199
self.assertEqual(p1, p2)
227
201
def test_appdata_not_using_environment(self):
228
202
# Test that we aren't falling back to the environment
229
203
first = win32utils.get_appdata_location()
230
self._captureVar("APPDATA", None)
204
self.overrideEnv("APPDATA", None)
231
205
self.assertPathsEqual(first, win32utils.get_appdata_location())
233
207
def test_appdata_matches_environment(self):
275
250
def test_unicode_dir(self):
276
251
# we should handle unicode paths without errors
277
self.requireFeature(UnicodeFilenameFeature)
252
self.requireFeature(features.UnicodeFilenameFeature)
278
253
os.mkdir(u'\u1234')
279
254
win32utils.set_file_attr_hidden(u'\u1234')
281
256
def test_dot_bzr_in_unicode_dir(self):
282
257
# we should not raise traceback if we try to set hidden attribute
283
258
# on .bzr directory below unicode path
284
self.requireFeature(UnicodeFilenameFeature)
259
self.requireFeature(features.UnicodeFilenameFeature)
285
260
os.makedirs(u'\u1234\\.bzr')
286
261
path = osutils.abspath(u'\u1234\\.bzr')
287
262
win32utils.set_file_attr_hidden(path)
292
265
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
294
def assertCommandLine(self, expected, line, single_quotes_allowed=False):
267
def assertCommandLine(self, expected, line, argv=None,
268
single_quotes_allowed=False):
295
269
# Strictly speaking we should respect parameter order versus glob
296
270
# expansions, but it's not really worth the effort here
297
argv = win32utils._command_line_to_argv(line,
298
single_quotes_allowed=single_quotes_allowed)
273
argv = win32utils._command_line_to_argv(
274
line, argv, single_quotes_allowed=single_quotes_allowed)
299
275
self.assertEqual(expected, sorted(argv))
301
277
def test_glob_paths(self):
313
289
self.assertCommandLine([u'a/*.c'], '"a/*.c"')
314
290
self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
315
291
self.assertCommandLine([u'a/*.c'], "'a/*.c'",
316
single_quotes_allowed=True)
292
single_quotes_allowed=True)
318
294
def test_slashes_changed(self):
319
295
# Quoting doesn't change the supplied args
320
296
self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
321
297
self.assertCommandLine([u'a\\*.c'], "'a\\*.c'",
322
single_quotes_allowed=True)
298
single_quotes_allowed=True)
323
299
# Expands the glob, but nothing matches, swaps slashes
324
300
self.assertCommandLine([u'a/*.c'], 'a\\*.c')
325
301
self.assertCommandLine([u'a/?.c'], 'a\\?.c')
329
305
def test_single_quote_support(self):
330
306
self.assertCommandLine(["add", "let's-do-it.txt"],
331
"add let's-do-it.txt")
332
self.assertCommandLine(["add", "lets do it.txt"],
333
"add 'lets do it.txt'", single_quotes_allowed=True)
307
"add let's-do-it.txt",
308
["add", "let's-do-it.txt"])
309
self.expectFailure("Using single quotes breaks trimming from argv",
310
self.assertCommandLine, ["add", "lets do it.txt"],
311
"add 'lets do it.txt'", [
312
"add", "'lets", "do", "it.txt'"],
313
single_quotes_allowed=True)
335
315
def test_case_insensitive_globs(self):
336
self.requireFeature(tests.CaseInsCasePresFilenameFeature)
316
if os.path.normcase("AbC") == "AbC":
317
self.skipTest("Test requires case insensitive globbing function")
337
318
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
338
319
self.assertCommandLine([u'A/b.c'], 'A/B*')
340
321
def test_backslashes(self):
341
self.requireFeature(BackslashDirSeparatorFeature)
322
self.requireFeature(backslashdir_feature)
342
323
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
343
324
self.assertCommandLine([u'a/b.c'], 'a\\b*')
326
def test_with_pdb(self):
327
"""Check stripping Python arguments before bzr script per lp:587868"""
328
self.assertCommandLine([u"rocks"], "-m pdb rocks", ["rocks"])
329
self.build_tree(['d/', 'd/f1', 'd/f2'])
330
self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
331
self.assertCommandLine([u"add", u"d/f1", u"d/f2"], "-m pdb add d/*",
335
class TestGetEnvironUnicode(tests.TestCase):
336
"""Tests for accessing the environment via the windows wide api"""
338
_test_needs_features = [CtypesFeature, features.win32_feature]
341
super(TestGetEnvironUnicode, self).setUp()
342
self.overrideEnv("TEST", "1")
345
"""In the normal case behaves the same as os.environ access"""
346
self.assertEqual("1", win32utils.get_environ_unicode("TEST"))
348
def test_unset(self):
349
"""A variable not present in the environment gives None by default"""
350
del os.environ["TEST"]
351
self.assertIs(None, win32utils.get_environ_unicode("TEST"))
353
def test_unset_default(self):
354
"""A variable not present in the environment gives passed default"""
355
del os.environ["TEST"]
356
self.assertIs("a", win32utils.get_environ_unicode("TEST", "a"))
358
def test_unicode(self):
359
"""A non-ascii variable is returned as unicode"""
360
unicode_val = u"\xa7" # non-ascii character present in many encodings
362
bytes_val = unicode_val.encode(osutils.get_user_encoding())
363
except UnicodeEncodeError:
364
self.skipTest("Couldn't encode non-ascii string for environ")
365
os.environ["TEST"] = bytes_val
366
self.assertEqual(unicode_val, win32utils.get_environ_unicode("TEST"))
369
"""A variable bigger than heuristic buffer size is still accessible"""
370
big_val = "x" * (2 << 10)
371
os.environ["TEST"] = big_val
372
self.assertEqual(big_val, win32utils.get_environ_unicode("TEST"))
374
def test_unexpected_error(self):
375
"""An error from the underlying platform function is propogated"""
376
ERROR_INVALID_PARAMETER = 87
377
SetLastError = win32utils.ctypes.windll.kernel32.SetLastError
379
def failer(*args, **kwargs):
380
SetLastError(ERROR_INVALID_PARAMETER)
382
self.overrideAttr(win32utils.get_environ_unicode, "_c_function",
384
e = self.assertRaises(WindowsError,
385
win32utils.get_environ_unicode, "TEST")
386
self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)