1
# Copyright (C) 2007 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
from bzrlib.tests import (
30
UnicodeFilenameFeature,
32
from bzrlib.win32utils import glob_expand, get_app_path
38
class _NeedsGlobExpansionFeature(Feature):
41
return sys.platform == 'win32'
43
def feature_name(self):
44
return 'Internally performed glob expansion'
46
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
49
class _RequiredModuleFeature(Feature):
51
def __init__(self, mod_name):
52
self.mod_name = mod_name
53
super(_RequiredModuleFeature, self).__init__()
57
__import__(self.mod_name)
62
def feature_name(self):
65
Win32RegistryFeature = _RequiredModuleFeature('_winreg')
66
CtypesFeature = _RequiredModuleFeature('ctypes')
67
Win32comShellFeature = _RequiredModuleFeature('win32com.shell')
73
class TestNeedsGlobExpansionFeature(TestCase):
75
def test_available(self):
76
self.assertEqual(sys.platform == 'win32',
77
NeedsGlobExpansionFeature.available())
80
self.assertTrue("performed" in str(NeedsGlobExpansionFeature))
83
class TestWin32UtilsGlobExpand(TestCaseInTempDir):
85
_test_needs_features = [NeedsGlobExpansionFeature]
87
def test_empty_tree(self):
93
[['a', 'a'], ['a', 'a']]])
95
def test_tree_ascii(self):
96
"""Checks the glob expansion and path separation char
98
self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
99
'b', 'b1', 'b2', 'b3',
100
'c/', 'c/c1', 'c/c2',
101
'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
105
[[u'a', u'a' ], [u'a', u'a']],
113
[[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
114
[[u'?'], [u'a', u'b', u'c', u'd']],
115
[[u'a?'], [u'a1', u'a2']],
116
[[u'a??'], [u'a11', u'a.1']],
117
[[u'b[1-2]'], [u'b1', u'b2']],
118
[[u'A?'], [u'a1', u'a2']],
120
[[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
121
[[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
122
[[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
123
[[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
124
[[u'*/'], [u'c/', u'd/']],
125
[[u'*\\'], [u'c/', u'd/']]])
127
def test_tree_unicode(self):
128
"""Checks behaviour with non-ascii filenames"""
129
self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
132
[[u'\u1234'], [u'\u1234']],
133
[[u'\u1235'], [u'\u1235']],
135
[[u'\u1235/'], [u'\u1235/']],
136
[[u'\u1235/\u1235'], [u'\u1235/\u1235']],
139
[[u'?'], [u'\u1234', u'\u1235']],
140
[[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
141
[[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
143
[[u'\u1235/?'], [u'\u1235/\u1235']],
144
[[u'\u1235/*'], [u'\u1235/\u1235']],
145
[[u'\u1235\\?'], [u'\u1235/\u1235']],
146
[[u'\u1235\\*'], [u'\u1235/\u1235']],
147
[[u'?/'], [u'\u1235/']],
148
[[u'*/'], [u'\u1235/']],
149
[[u'?\\'], [u'\u1235/']],
150
[[u'*\\'], [u'\u1235/']],
151
[[u'?/?'], [u'\u1235/\u1235']],
152
[[u'*/*'], [u'\u1235/\u1235']],
153
[[u'?\\?'], [u'\u1235/\u1235']],
154
[[u'*\\*'], [u'\u1235/\u1235']]])
156
def _run_testset(self, testset):
157
for pattern, expected in testset:
158
result = glob_expand(pattern)
161
self.assertEqual(expected, result, 'pattern %s' % pattern)
164
class TestAppPaths(TestCase):
166
_test_needs_features = [Win32RegistryFeature]
168
def test_iexplore(self):
169
# typical windows users should have IE installed
170
for a in ('iexplore', 'iexplore.exe'):
172
d, b = os.path.split(p)
173
self.assertEquals('iexplore.exe', b.lower())
174
self.assertNotEquals('', d)
176
def test_wordpad(self):
177
# typical windows users should have wordpad in the system
178
# but there is problem: its path has the format REG_EXPAND_SZ
179
# so naive attempt to get the path is not working
180
for a in ('wordpad', 'wordpad.exe'):
182
d, b = os.path.split(p)
183
self.assertEquals('wordpad.exe', b.lower())
184
self.assertNotEquals('', d)
186
def test_not_existing(self):
187
p = get_app_path('not-existing')
188
self.assertEquals('not-existing', p)
191
class TestLocationsCtypes(TestCase):
193
_test_needs_features = [CtypesFeature]
195
def assertPathsEqual(self, p1, p2):
196
# TODO: The env var values in particular might return the "short"
197
# version (ie, "C:\DOCUME~1\..."). Its even possible the returned
198
# values will differ only by case - handle these situations as we
200
self.assertEquals(p1, p2)
202
def test_appdata_not_using_environment(self):
203
# Test that we aren't falling back to the environment
204
first = win32utils.get_appdata_location()
205
self._captureVar("APPDATA", None)
206
self.assertPathsEqual(first, win32utils.get_appdata_location())
208
def test_appdata_matches_environment(self):
209
# Typically the APPDATA environment variable will match
210
# get_appdata_location
211
# XXX - See bug 262874, which asserts the correct encoding is 'mbcs',
212
encoding = osutils.get_user_encoding()
213
env_val = os.environ.get("APPDATA", None)
215
raise TestSkipped("No APPDATA environment variable exists")
216
self.assertPathsEqual(win32utils.get_appdata_location(),
217
env_val.decode(encoding))
219
def test_local_appdata_not_using_environment(self):
220
# Test that we aren't falling back to the environment
221
first = win32utils.get_local_appdata_location()
222
self._captureVar("LOCALAPPDATA", None)
223
self.assertPathsEqual(first, win32utils.get_local_appdata_location())
225
def test_local_appdata_matches_environment(self):
226
# LOCALAPPDATA typically only exists on Vista, so we only attempt to
227
# compare when it exists.
228
lad = win32utils.get_local_appdata_location()
229
env = os.environ.get("LOCALAPPDATA")
231
# XXX - See bug 262874, which asserts the correct encoding is 'mbcs'
232
encoding = osutils.get_user_encoding()
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.old_ctypes = win32utils.has_ctypes
245
win32utils.has_ctypes = False
246
self.addCleanup(self.restoreCtypes)
248
def restoreCtypes(self):
249
win32utils.has_ctypes = self.old_ctypes
252
class TestSetHidden(TestCaseInTempDir):
254
def test_unicode_dir(self):
255
# we should handle unicode paths without errors
256
self.requireFeature(UnicodeFilenameFeature)
258
win32utils.set_file_attr_hidden(u'\u1234')
260
def test_dot_bzr_in_unicode_dir(self):
261
# we should not raise traceback if we try to set hidden attribute
262
# on .bzr directory below unicode path
263
self.requireFeature(UnicodeFilenameFeature)
264
os.makedirs(u'\u1234\\.bzr')
265
path = osutils.abspath(u'\u1234\\.bzr')
266
win32utils.set_file_attr_hidden(path)
270
class TestUnicodeShlex(tests.TestCase):
272
def assertAsTokens(self, expected, line):
273
s = win32utils.UnicodeShlex(line)
274
self.assertEqual(expected, list(s))
276
def test_simple(self):
277
self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
280
def test_ignore_multiple_spaces(self):
281
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar')
283
def test_ignore_leading_space(self):
284
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u' foo bar')
286
def test_ignore_trailing_space(self):
287
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar ')
289
def test_posix_quotations(self):
290
self.assertAsTokens([(True, u'foo bar')], u'"foo bar"')
291
self.assertAsTokens([(False, u"'fo''o"), (False, u"b''ar'")],
293
self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"')
294
self.assertAsTokens([(True, u"fo'o"), (True, u"b'ar")],
297
def test_nested_quotations(self):
298
self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
299
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
301
def test_empty_result(self):
302
self.assertAsTokens([], u'')
303
self.assertAsTokens([], u' ')
305
def test_quoted_empty(self):
306
self.assertAsTokens([(True, '')], u'""')
307
self.assertAsTokens([(False, u"''")], u"''")
309
def test_unicode_chars(self):
310
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
311
u'f\xb5\xee \u1234\u3456')
313
def test_newline_in_quoted_section(self):
314
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
316
def test_escape_chars(self):
317
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
319
def test_escape_quote(self):
320
self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
322
def test_double_escape(self):
323
self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\bar"')
324
self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
327
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
329
def assertCommandLine(self, expected, line):
330
self.assertEqual(expected, win32utils._command_line_to_argv(line))
332
def test_glob_paths(self):
333
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
334
self.assertCommandLine([u'a/b.c', u'a/c.c'], 'a/*.c')
335
self.build_tree(['b/', 'b/b.c', 'b/d.c', 'b/d.h'])
336
self.assertCommandLine([u'a/b.c', u'b/b.c'], '*/b.c')
337
self.assertCommandLine([u'a/b.c', u'a/c.c', u'b/b.c', u'b/d.c'],
339
# Bash style, just pass through the argument if nothing matches
340
self.assertCommandLine([u'*/*.qqq'], '*/*.qqq')
342
def test_quoted_globs(self):
343
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
344
self.assertCommandLine([u'a/*.c'], '"a/*.c"')
345
self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
347
def test_slashes_changed(self):
348
self.assertCommandLine([u'a/*.c'], '"a\\*.c"')
349
# Expands the glob, but nothing matches
350
self.assertCommandLine([u'a/*.c'], 'a\\*.c')
351
self.assertCommandLine([u'a/foo.c'], 'a\\foo.c')
353
def test_no_single_quote_supported(self):
354
self.assertCommandLine(["add", "let's-do-it.txt"],
355
"add let's-do-it.txt")