def test_shlex_without_control(self): TESTS = ( ("", []), ("a", [("a", "a")]), ("a && b\n", [("a", "a"), ("&", "a"), ("&", "a"), ("b", "a")]), ( "a | b; c>/fred/jim-sheila.txt|&d;e&", [ ("a", "a"), ("|", "a"), ("b", "a"), (";", "a"), ("c", "a"), (">", "a"), ("/fred/jim-sheila.txt", "a"), ("|", "a"), ("&", "a"), ("d", "a"), (";", "a"), ("e", "a"), ("&", "a"), ], ), ) for posix in False, True: for s, expected in TESTS: s = shell_shlex(s, posix=posix) actual = [] while True: t, tt = s.get_token(), s.token_type if not t: break actual.append((t, tt)) self.assertEqual(actual, expected)
def test_shlex_with_misc_chars(self): TESTS = ( ("rsync [email protected]:path dest", ("rsync", "[email protected]:path", "dest")), (r"c:\Python26\Python lister.py -d 0.01", (r"c:\Python26\Python", "lister.py", "-d", "0.01")), ) for s, t in TESTS: sh = shell_shlex(s) self.assertEqual(tuple(sh), t)
def test_shlex_with_quoting(self): TESTS = ( ('"a b"', False, [('"a b"', '"')]), ('"a b"', True, [('a b', 'a')]), ('"a b" c# comment', False, [('"a b"', '"'), ('c', 'a')]), ('"a b" c# comment', True, [('a b', 'a'), ('c', 'a')]), ) for s, posix, expected in TESTS: s = shell_shlex(s, posix=posix) actual = [] while True: t, tt = s.get_token(), s.token_type if not t: break actual.append((t, tt)) self.assertEqual(actual, expected) s = shell_shlex('"abc') self.assertRaises(ValueError, s.get_token)
def test_shlex_with_misc_chars(self): TESTS = ( ('rsync [email protected]:path dest', ('rsync', '[email protected]:path', 'dest')), (r'c:\Python26\Python lister.py -d 0.01', (r'c:\Python26\Python', 'lister.py', '-d', '0.01')), ) for s, t in TESTS: sh = shell_shlex(s) self.assertEqual(tuple(sh), t)
def test_shlex(self): TESTS = (('', []), ('a', [('a', 'a')]), ('a && b\n', [('a', 'a'), ('&&', 'c'), ('b', 'a')]), ('a | b; c>/fred/jim-sheila.txt|&d;e&', [('a', 'a'), ('|', 'c'), ('b', 'a'), (';', 'c'), ('c', 'a'), ('>', 'c'), ('/fred/jim-sheila.txt', 'a'), ('|&', 'c'), ('d', 'a'), (';', 'c'), ('e', 'a'), ('&', 'c')])) for posix in False, True: for s, expected in TESTS: s = shell_shlex(s, posix=posix, control=True) actual = [] while True: t, tt = s.get_token(), s.token_type if not t: break actual.append((t, tt)) self.assertEqual(actual, expected)
def test_shlex_without_control(self): TESTS = ( ('', []), ('a', [('a', 'a')]), ('a && b\n', [('a', 'a'), ('&', 'a'), ('&', 'a'), ('b', 'a')]), ('a | b; c>/fred/jim-sheila.txt|&d;e&', [('a', 'a'), ('|', 'a'), ('b', 'a'), (';', 'a'), ('c', 'a'), ('>', 'a'), ('/fred/jim-sheila.txt', 'a'), ('|', 'a'), ('&', 'a'), ('d', 'a'), (';', 'a'), ('e', 'a'), ('&', 'a')]) ) for posix in False, True: for s, expected in TESTS: s = shell_shlex(s, posix=posix) actual = [] while True: t, tt = s.get_token(), s.token_type if not t: break actual.append((t, tt)) self.assertEqual(actual, expected)
def test_shlex_issue_34(self): cmd = "ls foo,bar" actual = list(shell_shlex(cmd)) self.assertEqual(actual, ['ls', 'foo,bar'])
def test_shlex_issue_31(self): cmd = "python -c 'print('\''ok'\'')'" list(shell_shlex(cmd, control='();>|&', posix=True)) shell_format("python -c {0}", "print('ok')") list(shell_shlex(cmd, control='();>|&', posix=True))