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')
32
from .features import backslashdir_feature
33
from ..win32utils import glob_expand, get_app_path
39
Win32RegistryFeature = features.ModuleAvailableFeature('_winreg')
40
CtypesFeature = features.ModuleAvailableFeature('ctypes')
41
Win32comShellFeature = features.ModuleAvailableFeature('win32com.shell')
42
Win32ApiFeature = features.ModuleAvailableFeature('win32api')
195
169
for a in ('iexplore', 'iexplore.exe'):
196
170
p = get_app_path(a)
197
171
d, b = os.path.split(p)
198
self.assertEquals('iexplore.exe', b.lower())
199
self.assertNotEquals('', d)
172
self.assertEqual('iexplore.exe', b.lower())
173
self.assertNotEqual('', d)
201
175
def test_wordpad(self):
202
176
# typical windows users should have wordpad in the system
203
177
# but there is problem: its path has the format REG_EXPAND_SZ
204
178
# so naive attempt to get the path is not working
179
self.requireFeature(Win32ApiFeature)
205
180
for a in ('wordpad', 'wordpad.exe'):
206
181
p = get_app_path(a)
207
182
d, b = os.path.split(p)
208
self.assertEquals('wordpad.exe', b.lower())
209
self.assertNotEquals('', d)
183
self.assertEqual('wordpad.exe', b.lower())
184
self.assertNotEqual('', d)
211
186
def test_not_existing(self):
212
187
p = get_app_path('not-existing')
213
self.assertEquals('not-existing', p)
188
self.assertEqual('not-existing', p)
216
191
class TestLocationsCtypes(TestCase):
218
_test_needs_features = [CtypesFeature]
193
_test_needs_features = [CtypesFeature, features.win32_feature]
220
195
def assertPathsEqual(self, p1, p2):
221
196
# TODO: The env var values in particular might return the "short"
222
197
# version (ie, "C:\DOCUME~1\..."). Its even possible the returned
223
198
# values will differ only by case - handle these situations as we
224
199
# come across them.
225
self.assertEquals(p1, p2)
200
self.assertEqual(p1, p2)
227
202
def test_appdata_not_using_environment(self):
228
203
# Test that we aren't falling back to the environment
229
204
first = win32utils.get_appdata_location()
230
self._captureVar("APPDATA", None)
205
self.overrideEnv("APPDATA", None)
231
206
self.assertPathsEqual(first, win32utils.get_appdata_location())
233
208
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,
273
argv = win32utils._command_line_to_argv(line, argv,
298
274
single_quotes_allowed=single_quotes_allowed)
299
275
self.assertEqual(expected, sorted(argv))
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'", ["add", "'lets", "do", "it.txt'"],
312
single_quotes_allowed=True)
335
314
def test_case_insensitive_globs(self):
336
self.requireFeature(tests.CaseInsCasePresFilenameFeature)
315
if os.path.normcase("AbC") == "AbC":
316
self.skipTest("Test requires case insensitive globbing function")
337
317
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
338
318
self.assertCommandLine([u'A/b.c'], 'A/B*')
340
320
def test_backslashes(self):
341
self.requireFeature(BackslashDirSeparatorFeature)
321
self.requireFeature(backslashdir_feature)
342
322
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
343
323
self.assertCommandLine([u'a/b.c'], 'a\\b*')
325
def test_with_pdb(self):
326
"""Check stripping Python arguments before bzr script per lp:587868"""
327
self.assertCommandLine([u"rocks"], "-m pdb rocks", ["rocks"])
328
self.build_tree(['d/', 'd/f1', 'd/f2'])
329
self.assertCommandLine([u"rm", u"x*"], "-m pdb rm x*", ["rm", u"x*"])
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)