5 from lit
.ShUtil
import Command
, Pipeline
, Seq
, ShLexer
, ShParser
8 class TestShLexer(unittest
.TestCase
):
9 def lex(self
, str, *args
, **kwargs
):
10 return list(ShLexer(str, *args
, **kwargs
).lex())
14 self
.lex("a|b>c&d<e;f"),
15 ["a", ("|",), "b", (">",), "c", ("&",), "d", ("<",), "e", (";",), "f"],
18 def test_redirection_tokens(self
):
19 self
.assertEqual(self
.lex("a2>c"), ["a2", (">",), "c"])
20 self
.assertEqual(self
.lex("a 2>c"), ["a", (">", 2), "c"])
22 def test_quoting(self
):
23 self
.assertEqual(self
.lex(""" 'a' """), ["a"])
24 self
.assertEqual(self
.lex(""" "hello\\"world" """), ['hello"world'])
25 self
.assertEqual(self
.lex(""" "hello\\'world" """), ["hello\\'world"])
26 self
.assertEqual(self
.lex(""" "hello\\\\world" """), ["hello\\world"])
27 self
.assertEqual(self
.lex(""" he"llo wo"rld """), ["hello world"])
28 self
.assertEqual(self
.lex(""" a\\ b a\\\\b """), ["a b", "a\\b"])
29 self
.assertEqual(self
.lex(""" "" "" """), ["", ""])
30 self
.assertEqual(self
.lex(""" a\\ b """, win32Escapes
=True), ["a\\", "b"])
33 class TestShParse(unittest
.TestCase
):
35 return ShParser(str).parse()
39 self
.parse("echo hello"), Pipeline([Command(["echo", "hello"], [])], False)
42 self
.parse('echo ""'), Pipeline([Command(["echo", ""], [])], False)
45 self
.parse("""echo -DFOO='a'"""),
46 Pipeline([Command(["echo", "-DFOO=a"], [])], False),
49 self
.parse('echo -DFOO="a"'),
50 Pipeline([Command(["echo", "-DFOO=a"], [])], False),
53 def test_redirection(self
):
55 self
.parse("echo hello > c"),
56 Pipeline([Command(["echo", "hello"], [(((">"),), "c")])], False),
59 self
.parse("echo hello > c >> d"),
61 [Command(["echo", "hello"], [((">",), "c"), ((">>",), "d")])], False
65 self
.parse("a 2>&1"), Pipeline([Command(["a"], [((">&", 2), "1")])], False)
68 def test_pipeline(self
):
71 Pipeline([Command(["a"], []), Command(["b"], [])], False),
75 self
.parse("a | b | c"),
77 [Command(["a"], []), Command(["b"], []), Command(["c"], [])], False
85 Pipeline([Command(["a"], [])], False),
87 Pipeline([Command(["b"], [])], False),
94 Pipeline([Command(["a"], [])], False),
96 Pipeline([Command(["b"], [])], False),
101 self
.parse("a && b"),
103 Pipeline([Command(["a"], [])], False),
105 Pipeline([Command(["b"], [])], False),
110 self
.parse("a || b"),
112 Pipeline([Command(["a"], [])], False),
114 Pipeline([Command(["b"], [])], False),
119 self
.parse("a && b || c"),
122 Pipeline([Command(["a"], [])], False),
124 Pipeline([Command(["b"], [])], False),
127 Pipeline([Command(["c"], [])], False),
134 Pipeline([Command(["a"], [])], False),
136 Pipeline([Command(["b"], [])], False),
141 if __name__
== "__main__":