forked from BobBuildTool/bob
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_input_stringparser.py
208 lines (176 loc) · 9 KB
/
test_input_stringparser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Bob build tool
# Copyright (C) 2016 Jan Klötzke
#
# SPDX-License-Identifier: GPL-3.0-or-later
from unittest import TestCase
from unittest.mock import MagicMock
from bob.stringparser import StringParser
from bob.stringparser import funEqual, funNotEqual, funNot, funOr, \
funAnd, funMatch, funIfThenElse, funSubst, funStrip, \
funSandboxEnabled, funToolDefined
from bob.errors import ParseError
def echo(args, **options):
i = 1
res = []
for a in args:
res.append("{}:{}".format(i, a))
i += 1
return ";".join(res)
class TestStringParser(TestCase):
def setUp(self):
self.p = StringParser(
{
"asdf": "qwer",
"xyz" : "123",
"null" : "",
"indirect" : "asdf",
"%%" : "percent",
},
{"echo" : echo},
{}, True)
def tearDown(self):
self.p = None
def testNoSubst(self):
self.assertEqual(self.p.parse("string"), "string")
self.assertEqual(self.p.parse("'string'"), "string")
self.assertEqual(self.p.parse('asdf"123"gf'), "asdf123gf")
self.assertEqual(self.p.parse('a\'sd\'f"123"gf'), "asdf123gf")
self.assertEqual(self.p.parse("'${asdf}'"), "${asdf}")
self.assertEqual(self.p.parse("'$(echo,foo,bar)'"), "$(echo,foo,bar)")
self.assertEqual(self.p.parse("a's\"d\"f'g"), "as\"d\"fg")
def testVariables(self):
self.assertEqual(self.p.parse("${asdf}"), "qwer")
self.assertEqual(self.p.parse("${%%}"), "percent")
self.assertEqual(self.p.parse(">${asdf}<"), ">qwer<")
self.assertEqual(self.p.parse("..${asdf}..${xyz}.."), "..qwer..123..")
self.assertEqual(self.p.parse("${asdf:-foobar}"), "qwer")
self.assertEqual(self.p.parse("${asdf:+foobar}"), "foobar")
self.assertEqual(self.p.parse("${asdf-foobar}"), "qwer")
self.assertEqual(self.p.parse("${asdf+foobar}"), "foobar")
self.assertEqual(self.p.parse("${null:-foobar}"), "foobar")
self.assertEqual(self.p.parse("${null:+foobar}"), "")
self.assertEqual(self.p.parse("${null-foobar}"), "")
self.assertEqual(self.p.parse("${null+foobar}"), "foobar")
self.assertEqual(self.p.parse("${unset:-foobar}"), "foobar")
self.assertEqual(self.p.parse("${unset:+foobar}"), "")
self.assertEqual(self.p.parse("${unset-foobar}"), "foobar")
self.assertEqual(self.p.parse("${unset+foobar}"), "")
# bare variables
self.assertEqual(self.p.parse("$asdf"), "qwer")
self.assertEqual(self.p.parse(">$asdf<"), ">qwer<")
self.assertEqual(self.p.parse("..$asdf..$xyz.."), "..qwer..123..")
def testUnsetOk(self):
u = StringParser({}, {}, {}, False)
self.assertEqual(u.parse("${asdf}"), "")
self.assertEqual(u.parse(">${asdf}<"), "><")
self.assertEqual(u.parse(">$asdf<"), "><")
def testAdvancedVariabled(self):
self.assertEqual(self.p.parse("${unset:->${asdf}}"), ">qwer")
self.assertEqual(self.p.parse("""${unset:-">${asdf}"}"""), ">qwer")
def testIndirectVariables(self):
self.assertEqual(self.p.parse("${${indirect}}"), "qwer")
self.assertEqual(self.p.parse("${${indirect}:+alternate}"), "alternate")
self.assertEqual(self.p.parse("${${asdf}:-default}"), "default")
def testCommandSubst(self):
self.assertEqual(self.p.parse("$(echo,foo,bar)"), "1:foo;2:bar")
self.assertEqual(self.p.parse("$(echo,foo bar )"), "1:foo bar ")
self.assertEqual(self.p.parse("$(echo,\"foo,bar\" )"), "1:foo,bar ")
self.assertEqual(self.p.parse("$(echo,foo \"${asdf} bar\" )"), "1:foo qwer bar ")
self.assertEqual(self.p.parse("$(echo,\'foo ${asdf} bar)\' )"), "1:foo ${asdf} bar) ")
self.assertEqual(self.p.parse("$(echo,a,${null})"), "1:a;2:")
self.assertEqual(self.p.parse("$(echo,a \"${null}\" )"), "1:a ")
def testEscaping(self):
self.assertEqual(self.p.parse("as\\df"), "asdf")
self.assertEqual(self.p.parse("as\\'df"), "as'df")
self.assertEqual(self.p.parse("\\${null+foobar}"), "${null+foobar}")
self.assertEqual(self.p.parse("${null:-\\}}"), "}")
self.assertEqual(self.p.parse("$(echo,foo\\,bar)"), "1:foo,bar")
def testFails(self):
self.assertRaises(ParseError, self.p.parse, "$")
self.assertRaises(ParseError, self.p.parse, "asdf\\")
self.assertRaises(ParseError, self.p.parse, "as'df")
self.assertRaises(ParseError, self.p.parse, "$<asdf>")
self.assertRaises(ParseError, self.p.parse, "${asdf")
self.assertRaises(ParseError, self.p.parse, "${unknown}")
self.assertRaises(ParseError, self.p.parse, "${asdf:")
self.assertRaises(ParseError, self.p.parse, "$()")
self.assertRaises(ParseError, self.p.parse, "$(unknown)")
# bare variables that should fail even if unset variables are allowed
u = StringParser({}, {}, {}, False)
self.assertRaises(ParseError, u.parse, "$1")
self.assertRaises(ParseError, u.parse, "$%%")
class TestStringFunctions(TestCase):
def testEqual(self):
self.assertRaises(ParseError, funEqual, [])
self.assertEqual(funEqual(["a", "a"]), "true")
self.assertEqual(funEqual(["a", "b"]), "false")
def testNotEqual(self):
self.assertRaises(ParseError, funNotEqual, [])
self.assertEqual(funNotEqual(["a", "a"]), "false")
self.assertEqual(funNotEqual(["a", "b"]), "true")
def testNot(self):
self.assertRaises(ParseError, funNot, [])
self.assertEqual(funNot(["true"]), "false")
self.assertEqual(funNot(["TRUE"]), "false")
self.assertEqual(funNot(["1"]), "false")
self.assertEqual(funNot(["foobar"]), "false")
self.assertEqual(funNot([""]), "true")
self.assertEqual(funNot(["0"]), "true")
self.assertEqual(funNot(["false"]), "true")
self.assertEqual(funNot(["FaLsE"]), "true")
def testOr(self):
self.assertEqual(funOr(["true", "false"]), "true")
self.assertEqual(funOr(["false", "true"]), "true")
self.assertEqual(funOr(["false", "false"]), "false")
self.assertEqual(funOr(["1", "2", "3", "4"]), "true")
self.assertEqual(funOr(["0", "0", "0", "0"]), "false")
self.assertEqual(funOr(["0", "", "false"]), "false")
def testAnd(self):
self.assertEqual(funAnd(["true", "true"]), "true")
self.assertEqual(funAnd(["true", "false"]), "false")
self.assertEqual(funAnd(["false", "true"]), "false")
self.assertEqual(funAnd(["true", "true", "true"]), "true")
self.assertEqual(funAnd(["true", "1", "abq"]), "true")
self.assertEqual(funAnd(["true", ""]), "false")
def testMatch(self):
self.assertEqual(funMatch(["string", "pattern"]), "false")
self.assertEqual(funMatch(["string", "trin"]), "true")
self.assertEqual(funMatch(["string", "tr(i|j|k)n"]), "true")
self.assertEqual(funMatch(["string", "tr[ijk]n"]), "true")
self.assertEqual(funMatch(["xyyz", "^xy{2}z$"]), "true")
self.assertEqual(funMatch(["xyyz", "^xy{1}z$"]), "false")
self.assertEqual(funMatch(["abc", "."]), "true")
self.assertEqual(funMatch(["abc", ".+"]), "true")
self.assertEqual(funMatch([".", r"\."]), "true")
self.assertEqual(funMatch(["a.", r"^\."]), "false")
self.assertEqual(funMatch(["(a)", r"\(a"]), "true")
self.assertEqual(funMatch(["(a)", r"\(a$"]), "false")
self.assertEqual(funMatch([r"\a)", r"\\a\)"]), "true")
self.assertEqual(funMatch(["ABC", "a", "i"]), "true")
self.assertRaises(ParseError, funMatch, ["a"])
self.assertRaises(ParseError, funMatch, ["a","b","x"])
self.assertRaises(ParseError, funMatch, ["a","b","i","y"])
def testIfThenElse(self):
self.assertRaises(ParseError, funIfThenElse, ["a", "b"])
self.assertEqual(funIfThenElse(["true", "a", "b"]), "a")
self.assertEqual(funIfThenElse(["qwer", "a", "b"]), "a")
self.assertEqual(funIfThenElse(["false", "a", "b"]), "b")
self.assertEqual(funIfThenElse(["0", "a", "b"]), "b")
self.assertEqual(funIfThenElse(["", "a", "b"]), "b")
def testSubst(self):
self.assertRaises(ParseError, funSubst, ["a"])
self.assertEqual(funSubst(["ee","EE","feet on the street"]),
"fEEt on the strEEt")
def testStrip(self):
self.assertRaises(ParseError, funStrip, ["a", "b"])
self.assertEqual(funStrip([" asdf "]), "asdf")
def testSandboxEnabled(self):
with self.assertRaises(ParseError):
funSandboxEnabled(["1", "2"], sandbox=None)
self.assertEqual(funSandboxEnabled([], sandbox=False), "false")
self.assertEqual(funSandboxEnabled([], sandbox=True), "true")
def testToolDefined(self):
with self.assertRaises(ParseError):
funToolDefined([], __tools={})
self.assertEqual(funToolDefined(["a"], __tools={"a":1, "b":2}), "true")
self.assertEqual(funToolDefined(["c"], __tools={"a":1, "b":2}), "false")