294
294
class TestUnicodeShlex(tests.TestCase):
296
def assertAsTokens(self, expected, line):
297
s = win32utils.UnicodeShlex(line)
296
def assertAsTokens(self, expected, line, single_quotes_allowed=False):
297
s = win32utils.UnicodeShlex(line,
298
single_quotes_allowed=single_quotes_allowed)
298
299
self.assertEqual(expected, list(s))
300
301
def test_simple(self):
313
314
def test_nested_quotations(self):
314
315
self.assertAsTokens([(True, u'foo"" bar')], u"\"foo\\\"\\\" bar\"")
315
316
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"")
317
self.assertAsTokens([(True, u'foo\'\' bar')], u"\"foo'' bar\"",
318
single_quotes_allowed=True)
319
self.assertAsTokens([(True, u'foo"" bar')], u"'foo\"\" bar'",
320
single_quotes_allowed=True)
317
322
def test_empty_result(self):
318
323
self.assertAsTokens([], u'')
321
326
def test_quoted_empty(self):
322
327
self.assertAsTokens([(True, '')], u'""')
323
328
self.assertAsTokens([(False, u"''")], u"''")
329
self.assertAsTokens([(True, '')], u"''", single_quotes_allowed=True)
325
331
def test_unicode_chars(self):
326
332
self.assertAsTokens([(False, u'f\xb5\xee'), (False, u'\u1234\u3456')],
329
335
def test_newline_in_quoted_section(self):
330
336
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u'"foo\nbar\nbaz\n"')
337
self.assertAsTokens([(True, u'foo\nbar\nbaz\n')], u"'foo\nbar\nbaz\n'",
338
single_quotes_allowed=True)
332
340
def test_escape_chars(self):
333
341
self.assertAsTokens([(False, u'foo\\bar')], u'foo\\bar')
344
352
def test_multiple_quoted_args(self):
345
353
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
355
self.assertAsTokens([(True, u'x x'), (True, u'y y')],
356
u'"x x" \'y y\'', single_quotes_allowed=True)
349
359
class Test_CommandLineToArgv(tests.TestCaseInTempDir):
351
def assertCommandLine(self, expected, line):
361
def assertCommandLine(self, expected, line, single_quotes_allowed=False):
352
362
# Strictly speaking we should respect parameter order versus glob
353
363
# expansions, but it's not really worth the effort here
354
self.assertEqual(expected,
355
sorted(win32utils.command_line_to_argv(line)))
364
argv = win32utils.command_line_to_argv(line,
365
single_quotes_allowed=single_quotes_allowed)
366
self.assertEqual(expected, sorted(argv))
357
368
def test_glob_paths(self):
358
369
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
368
379
self.build_tree(['a/', 'a/b.c', 'a/c.c', 'a/c.h'])
369
380
self.assertCommandLine([u'a/*.c'], '"a/*.c"')
370
381
self.assertCommandLine([u"'a/*.c'"], "'a/*.c'")
382
self.assertCommandLine([u'a/*.c'], "'a/*.c'",
383
single_quotes_allowed=True)
372
385
def test_slashes_changed(self):
373
386
# Quoting doesn't change the supplied args
374
387
self.assertCommandLine([u'a\\*.c'], '"a\\*.c"')
388
self.assertCommandLine([u'a\\*.c'], "'a\\*.c'",
389
single_quotes_allowed=True)
375
390
# Expands the glob, but nothing matches, swaps slashes
376
391
self.assertCommandLine([u'a/*.c'], 'a\\*.c')
377
392
self.assertCommandLine([u'a/?.c'], 'a\\?.c')
378
393
# No glob, doesn't touch slashes
379
394
self.assertCommandLine([u'a\\foo.c'], 'a\\foo.c')
381
def test_no_single_quote_supported(self):
396
def test_single_quote_support(self):
382
397
self.assertCommandLine(["add", "let's-do-it.txt"],
383
398
"add let's-do-it.txt")
399
self.assertCommandLine(["add", "lets do it.txt"],
400
"add 'lets do it.txt'", single_quotes_allowed=True)
385
402
def test_case_insensitive_globs(self):
386
403
self.requireFeature(tests.CaseInsCasePresFilenameFeature)