13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
from bzrlib import osutils
21
from bzrlib.tests import TestCase, TestCaseInTempDir, TestSkipped, Feature
25
from bzrlib.tests import (
30
UnicodeFilenameFeature,
22
32
from bzrlib.win32utils import glob_expand, get_app_path
23
from bzrlib import win32utils
29
class _NeedsGlobExpansionFeature(Feature):
35
class _BackslashDirSeparatorFeature(tests.Feature):
32
return sys.platform == 'win32'
39
os.lstat(os.getcwd() + '\\')
34
45
def feature_name(self):
35
return 'Internally performed glob expansion'
46
return "Filesystem treats '\\' as a directory separator."
37
NeedsGlobExpansionFeature = _NeedsGlobExpansionFeature()
48
BackslashDirSeparatorFeature = _BackslashDirSeparatorFeature()
40
51
class _RequiredModuleFeature(Feature):
84
85
[['a', 'a'], ['a', 'a']]])
87
def build_ascii_tree(self):
88
self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
89
'b', 'b1', 'b2', 'b3',
91
'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
93
def build_unicode_tree(self):
94
self.requireFeature(UnicodeFilenameFeature)
95
self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/',
86
98
def test_tree_ascii(self):
87
99
"""Checks the glob expansion and path separation char
89
self.build_tree(['a', 'a1', 'a2', 'a11', 'a.1',
90
'b', 'b1', 'b2', 'b3',
92
'd/', 'd/d1', 'd/d2', 'd/e/', 'd/e/e1'])
101
self.build_ascii_tree()
93
102
self._run_testset([
96
105
[[u'a', u'a' ], [u'a', u'a']],
100
108
[[u'd/'], [u'd/']],
104
111
[[u'a*'], [u'a', u'a1', u'a2', u'a11', u'a.1']],
105
112
[[u'?'], [u'a', u'b', u'c', u'd']],
106
113
[[u'a?'], [u'a1', u'a2']],
107
114
[[u'a??'], [u'a11', u'a.1']],
108
115
[[u'b[1-2]'], [u'b1', u'b2']],
109
[[u'A?'], [u'a1', u'a2']],
111
117
[[u'd/*'], [u'd/d1', u'd/d2', u'd/e']],
118
[[u'?/*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
119
[[u'*/*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
120
[[u'*/'], [u'c/', u'd/']],
123
def test_backslash_globbing(self):
124
self.requireFeature(BackslashDirSeparatorFeature)
125
self.build_ascii_tree()
112
128
[[u'd\\*'], [u'd/d1', u'd/d2', u'd/e']],
113
129
[[u'?\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
114
130
[[u'*\\*'], [u'c/c1', u'c/c2', u'd/d1', u'd/d2', u'd/e']],
115
[[u'*/'], [u'c/', u'd/']],
116
[[u'*\\'], [u'c/', u'd/']]])
131
[[u'*\\'], [u'c/', u'd/']],
134
def test_case_insensitive_globbing(self):
135
self.requireFeature(tests.CaseInsCasePresFilenameFeature)
136
self.build_ascii_tree()
139
[[u'A?'], [u'a1', u'a2']],
118
142
def test_tree_unicode(self):
119
143
"""Checks behaviour with non-ascii filenames"""
120
self.build_tree([u'\u1234', u'\u1234\u1234', u'\u1235/', u'\u1235/\u1235'])
144
self.build_unicode_tree()
121
145
self._run_testset([
123
147
[[u'\u1234'], [u'\u1234']],
124
148
[[u'\u1235'], [u'\u1235']],
126
150
[[u'\u1235/'], [u'\u1235/']],
127
151
[[u'\u1235/\u1235'], [u'\u1235/\u1235']],
130
154
[[u'?'], [u'\u1234', u'\u1235']],
131
155
[[u'*'], [u'\u1234', u'\u1234\u1234', u'\u1235']],
132
156
[[u'\u1234*'], [u'\u1234', u'\u1234\u1234']],
134
158
[[u'\u1235/?'], [u'\u1235/\u1235']],
135
159
[[u'\u1235/*'], [u'\u1235/\u1235']],
160
[[u'?/'], [u'\u1235/']],
161
[[u'*/'], [u'\u1235/']],
162
[[u'?/?'], [u'\u1235/\u1235']],
163
[[u'*/*'], [u'\u1235/\u1235']],
166
def test_unicode_backslashes(self):
167
self.requireFeature(BackslashDirSeparatorFeature)
168
self.build_unicode_tree()
171
[[u'\u1235\\'], [u'\u1235/']],
172
[[u'\u1235\\\u1235'], [u'\u1235/\u1235']],
136
173
[[u'\u1235\\?'], [u'\u1235/\u1235']],
137
174
[[u'\u1235\\*'], [u'\u1235/\u1235']],
138
[[u'?/'], [u'\u1235/']],
139
[[u'*/'], [u'\u1235/']],
140
175
[[u'?\\'], [u'\u1235/']],
141
176
[[u'*\\'], [u'\u1235/']],
142
[[u'?/?'], [u'\u1235/\u1235']],
143
[[u'*/*'], [u'\u1235/\u1235']],
144
177
[[u'?\\?'], [u'\u1235/\u1235']],
145
[[u'*\\*'], [u'\u1235/\u1235']]])
178
[[u'*\\*'], [u'\u1235/\u1235']],
147
181
def _run_testset(self, testset):
148
182
for pattern, expected in testset:
229
273
def restoreCtypes(self):
230
274
win32utils.has_ctypes = self.old_ctypes
277
class TestSetHidden(TestCaseInTempDir):
279
def test_unicode_dir(self):
280
# we should handle unicode paths without errors
281
self.requireFeature(UnicodeFilenameFeature)
283
win32utils.set_file_attr_hidden(u'\u1234')
285
def test_dot_bzr_in_unicode_dir(self):
286
# we should not raise traceback if we try to set hidden attribute
287
# on .bzr directory below unicode path
288
self.requireFeature(UnicodeFilenameFeature)
289
os.makedirs(u'\u1234\\.bzr')
290
path = osutils.abspath(u'\u1234\\.bzr')
291
win32utils.set_file_attr_hidden(path)
295
class TestUnicodeShlex(tests.TestCase):
297
def assertAsTokens(self, expected, line):
298
s = win32utils.UnicodeShlex(line)
299
self.assertEqual(expected, list(s))
301
def test_simple(self):
302
self.assertAsTokens([(False, u'foo'), (False, u'bar'), (False, u'baz')],
305
def test_ignore_multiple_spaces(self):
306
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar')
308
def test_ignore_leading_space(self):
309
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u' foo bar')
311
def test_ignore_trailing_space(self):
312
self.assertAsTokens([(False, u'foo'), (False, u'bar')], u'foo bar ')
314
def test_posix_quotations(self):
315
self.assertAsTokens([(True, u'foo bar')], u'"foo bar"')
316
self.assertAsTokens([(False, u"'fo''o"), (False, u"b''ar'")],
318
self.assertAsTokens([(True, u'foo bar')], u'"fo""o b""ar"')
319
self.assertAsTokens([(True, u"fo'o"), (True, u"b'ar")],
322
def test_nested_quotations(self):
323
self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
324
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
326
def test_empty_result(self):
327
self.assertAsTokens([], u'')
328
self.assertAsTokens([], u' ')
330
def test_quoted_empty(self):
331
self.assertAsTokens([(True, '')], u'""')
332
self.assertAsTokens([(False, u"''")], u"''")
334
def test_unicode_chars(self):
335
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
336
u'f\xb5\xee \u1234\u3456')
338
def test_newline_in_quoted_section(self):
339
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
341
def test_escape_chars(self):
342
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
344
def test_escape_quote(self):
345
self.assertAsTokens([(True, u'foo"bar')], u'"foo\\"bar"')
347
def test_double_escape(self):
348
self.assertAsTokens([(True, u'foo\\bar')], u'"foo\\\\bar"')
349
self.assertAsTokens([(False, u'foo\\\\bar')], u"foo\\\\bar")
352
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
354
def assertCommandLine(self, expected, line):
355
# Strictly speaking we should respect parameter order versus glob
356
# expansions, but it's not really worth the effort here
357
self.assertEqual(expected,
358
sorted(win32utils._command_line_to_argv(line)))
360
def test_glob_paths(self):
361
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
362
self.assertCommandLine([u'a/b.c', u'a/c.c'], 'a/*.c')
363
self.build_tree(['b/', 'b/b.c', 'b/d.c', 'b/d.h'])
364
self.assertCommandLine([u'a/b.c', u'b/b.c'], '*/b.c')
365
self.assertCommandLine([u'a/b.c', u'a/c.c', u'b/b.c', u'b/d.c'],
367
# Bash style, just pass through the argument if nothing matches
368
self.assertCommandLine([u'*/*.qqq'], '*/*.qqq')
370
def test_quoted_globs(self):
371
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
372
self.assertCommandLine([u'a/*.c'], '"a/*.c"')
373
self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
375
def test_slashes_changed(self):
376
# Quoting doesn't change the supplied args
377
self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
378
# Expands the glob, but nothing matches, swaps slashes
379
self.assertCommandLine([u'a/*.c'], 'a\\*.c')
380
self.assertCommandLine([u'a/?.c'], 'a\\?.c')
381
# No glob, doesn't touch slashes
382
self.assertCommandLine([u'a\\foo.c'], 'a\\foo.c')
384
def test_no_single_quote_supported(self):
385
self.assertCommandLine(["add", "let's-do-it.txt"],
386
"add let's-do-it.txt")
388
def test_case_insensitive_globs(self):
389
self.requireFeature(tests.CaseInsCasePresFilenameFeature)
390
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
391
self.assertCommandLine([u'A/b.c'], 'A/B*')
393
def test_backslashes(self):
394
self.requireFeature(BackslashDirSeparatorFeature)
395
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
396
self.assertCommandLine([u'a/b.c'], 'a\\b*')