summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/tests/data.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/tests/data.py')
-rw-r--r--bitbake/lib/bb/tests/data.py296
1 files changed, 296 insertions, 0 deletions
diff --git a/bitbake/lib/bb/tests/data.py b/bitbake/lib/bb/tests/data.py
new file mode 100644
index 0000000..ee66b22
--- /dev/null
+++ b/bitbake/lib/bb/tests/data.py
@@ -0,0 +1,296 @@
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# BitBake Tests for the Data Store (data.py/data_smart.py)
5#
6# Copyright (C) 2010 Chris Larson
7# Copyright (C) 2012 Richard Purdie
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21#
22
23import unittest
24import bb
25import bb.data
26import bb.parse
27
28class DataExpansions(unittest.TestCase):
29 def setUp(self):
30 self.d = bb.data.init()
31 self.d["foo"] = "value_of_foo"
32 self.d["bar"] = "value_of_bar"
33 self.d["value_of_foo"] = "value_of_'value_of_foo'"
34
35 def test_one_var(self):
36 val = self.d.expand("${foo}")
37 self.assertEqual(str(val), "value_of_foo")
38
39 def test_indirect_one_var(self):
40 val = self.d.expand("${${foo}}")
41 self.assertEqual(str(val), "value_of_'value_of_foo'")
42
43 def test_indirect_and_another(self):
44 val = self.d.expand("${${foo}} ${bar}")
45 self.assertEqual(str(val), "value_of_'value_of_foo' value_of_bar")
46
47 def test_python_snippet(self):
48 val = self.d.expand("${@5*12}")
49 self.assertEqual(str(val), "60")
50
51 def test_expand_in_python_snippet(self):
52 val = self.d.expand("${@'boo ' + '${foo}'}")
53 self.assertEqual(str(val), "boo value_of_foo")
54
55 def test_python_snippet_getvar(self):
56 val = self.d.expand("${@d.getVar('foo', True) + ' ${bar}'}")
57 self.assertEqual(str(val), "value_of_foo value_of_bar")
58
59 def test_python_snippet_syntax_error(self):
60 self.d.setVar("FOO", "${@foo = 5}")
61 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
62
63 def test_python_snippet_runtime_error(self):
64 self.d.setVar("FOO", "${@int('test')}")
65 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
66
67 def test_python_snippet_error_path(self):
68 self.d.setVar("FOO", "foo value ${BAR}")
69 self.d.setVar("BAR", "bar value ${@int('test')}")
70 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
71
72 def test_value_containing_value(self):
73 val = self.d.expand("${@d.getVar('foo', True) + ' ${bar}'}")
74 self.assertEqual(str(val), "value_of_foo value_of_bar")
75
76 def test_reference_undefined_var(self):
77 val = self.d.expand("${undefinedvar} meh")
78 self.assertEqual(str(val), "${undefinedvar} meh")
79
80 def test_double_reference(self):
81 self.d.setVar("BAR", "bar value")
82 self.d.setVar("FOO", "${BAR} foo ${BAR}")
83 val = self.d.getVar("FOO", True)
84 self.assertEqual(str(val), "bar value foo bar value")
85
86 def test_direct_recursion(self):
87 self.d.setVar("FOO", "${FOO}")
88 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
89
90 def test_indirect_recursion(self):
91 self.d.setVar("FOO", "${BAR}")
92 self.d.setVar("BAR", "${BAZ}")
93 self.d.setVar("BAZ", "${FOO}")
94 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
95
96 def test_recursion_exception(self):
97 self.d.setVar("FOO", "${BAR}")
98 self.d.setVar("BAR", "${${@'FOO'}}")
99 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
100
101 def test_incomplete_varexp_single_quotes(self):
102 self.d.setVar("FOO", "sed -i -e 's:IP{:I${:g' $pc")
103 val = self.d.getVar("FOO", True)
104 self.assertEqual(str(val), "sed -i -e 's:IP{:I${:g' $pc")
105
106 def test_nonstring(self):
107 self.d.setVar("TEST", 5)
108 val = self.d.getVar("TEST", True)
109 self.assertEqual(str(val), "5")
110
111 def test_rename(self):
112 self.d.renameVar("foo", "newfoo")
113 self.assertEqual(self.d.getVar("newfoo"), "value_of_foo")
114 self.assertEqual(self.d.getVar("foo"), None)
115
116 def test_deletion(self):
117 self.d.delVar("foo")
118 self.assertEqual(self.d.getVar("foo"), None)
119
120 def test_keys(self):
121 keys = self.d.keys()
122 self.assertEqual(keys, ['value_of_foo', 'foo', 'bar'])
123
124class TestNestedExpansions(unittest.TestCase):
125 def setUp(self):
126 self.d = bb.data.init()
127 self.d["foo"] = "foo"
128 self.d["bar"] = "bar"
129 self.d["value_of_foobar"] = "187"
130
131 def test_refs(self):
132 val = self.d.expand("${value_of_${foo}${bar}}")
133 self.assertEqual(str(val), "187")
134
135 #def test_python_refs(self):
136 # val = self.d.expand("${@${@3}**2 + ${@4}**2}")
137 # self.assertEqual(str(val), "25")
138
139 def test_ref_in_python_ref(self):
140 val = self.d.expand("${@'${foo}' + 'bar'}")
141 self.assertEqual(str(val), "foobar")
142
143 def test_python_ref_in_ref(self):
144 val = self.d.expand("${${@'f'+'o'+'o'}}")
145 self.assertEqual(str(val), "foo")
146
147 def test_deep_nesting(self):
148 depth = 100
149 val = self.d.expand("${" * depth + "foo" + "}" * depth)
150 self.assertEqual(str(val), "foo")
151
152 #def test_deep_python_nesting(self):
153 # depth = 50
154 # val = self.d.expand("${@" * depth + "1" + "+1}" * depth)
155 # self.assertEqual(str(val), str(depth + 1))
156
157 def test_mixed(self):
158 val = self.d.expand("${value_of_${@('${foo}'+'bar')[0:3]}${${@'BAR'.lower()}}}")
159 self.assertEqual(str(val), "187")
160
161 def test_runtime(self):
162 val = self.d.expand("${${@'value_of' + '_f'+'o'+'o'+'b'+'a'+'r'}}")
163 self.assertEqual(str(val), "187")
164
165class TestMemoize(unittest.TestCase):
166 def test_memoized(self):
167 d = bb.data.init()
168 d.setVar("FOO", "bar")
169 self.assertTrue(d.getVar("FOO") is d.getVar("FOO"))
170
171 def test_not_memoized(self):
172 d1 = bb.data.init()
173 d2 = bb.data.init()
174 d1.setVar("FOO", "bar")
175 d2.setVar("FOO", "bar2")
176 self.assertTrue(d1.getVar("FOO") is not d2.getVar("FOO"))
177
178 def test_changed_after_memoized(self):
179 d = bb.data.init()
180 d.setVar("foo", "value of foo")
181 self.assertEqual(str(d.getVar("foo")), "value of foo")
182 d.setVar("foo", "second value of foo")
183 self.assertEqual(str(d.getVar("foo")), "second value of foo")
184
185 def test_same_value(self):
186 d = bb.data.init()
187 d.setVar("foo", "value of")
188 d.setVar("bar", "value of")
189 self.assertEqual(d.getVar("foo"),
190 d.getVar("bar"))
191
192class TestConcat(unittest.TestCase):
193 def setUp(self):
194 self.d = bb.data.init()
195 self.d.setVar("FOO", "foo")
196 self.d.setVar("VAL", "val")
197 self.d.setVar("BAR", "bar")
198
199 def test_prepend(self):
200 self.d.setVar("TEST", "${VAL}")
201 self.d.prependVar("TEST", "${FOO}:")
202 self.assertEqual(self.d.getVar("TEST", True), "foo:val")
203
204 def test_append(self):
205 self.d.setVar("TEST", "${VAL}")
206 self.d.appendVar("TEST", ":${BAR}")
207 self.assertEqual(self.d.getVar("TEST", True), "val:bar")
208
209 def test_multiple_append(self):
210 self.d.setVar("TEST", "${VAL}")
211 self.d.prependVar("TEST", "${FOO}:")
212 self.d.appendVar("TEST", ":val2")
213 self.d.appendVar("TEST", ":${BAR}")
214 self.assertEqual(self.d.getVar("TEST", True), "foo:val:val2:bar")
215
216class TestConcatOverride(unittest.TestCase):
217 def setUp(self):
218 self.d = bb.data.init()
219 self.d.setVar("FOO", "foo")
220 self.d.setVar("VAL", "val")
221 self.d.setVar("BAR", "bar")
222
223 def test_prepend(self):
224 self.d.setVar("TEST", "${VAL}")
225 self.d.setVar("TEST_prepend", "${FOO}:")
226 bb.data.update_data(self.d)
227 self.assertEqual(self.d.getVar("TEST", True), "foo:val")
228
229 def test_append(self):
230 self.d.setVar("TEST", "${VAL}")
231 self.d.setVar("TEST_append", ":${BAR}")
232 bb.data.update_data(self.d)
233 self.assertEqual(self.d.getVar("TEST", True), "val:bar")
234
235 def test_multiple_append(self):
236 self.d.setVar("TEST", "${VAL}")
237 self.d.setVar("TEST_prepend", "${FOO}:")
238 self.d.setVar("TEST_append", ":val2")
239 self.d.setVar("TEST_append", ":${BAR}")
240 bb.data.update_data(self.d)
241 self.assertEqual(self.d.getVar("TEST", True), "foo:val:val2:bar")
242
243 def test_remove(self):
244 self.d.setVar("TEST", "${VAL} ${BAR}")
245 self.d.setVar("TEST_remove", "val")
246 bb.data.update_data(self.d)
247 self.assertEqual(self.d.getVar("TEST", True), "bar")
248
249 def test_doubleref_remove(self):
250 self.d.setVar("TEST", "${VAL} ${BAR}")
251 self.d.setVar("TEST_remove", "val")
252 self.d.setVar("TEST_TEST", "${TEST} ${TEST}")
253 bb.data.update_data(self.d)
254 self.assertEqual(self.d.getVar("TEST_TEST", True), "bar bar")
255
256
257class TestOverrides(unittest.TestCase):
258 def setUp(self):
259 self.d = bb.data.init()
260 self.d.setVar("OVERRIDES", "foo:bar:local")
261 self.d.setVar("TEST", "testvalue")
262
263 def test_no_override(self):
264 bb.data.update_data(self.d)
265 self.assertEqual(self.d.getVar("TEST", True), "testvalue")
266
267 def test_one_override(self):
268 self.d.setVar("TEST_bar", "testvalue2")
269 bb.data.update_data(self.d)
270 self.assertEqual(self.d.getVar("TEST", True), "testvalue2")
271
272 def test_multiple_override(self):
273 self.d.setVar("TEST_bar", "testvalue2")
274 self.d.setVar("TEST_local", "testvalue3")
275 self.d.setVar("TEST_foo", "testvalue4")
276 bb.data.update_data(self.d)
277 self.assertEqual(self.d.getVar("TEST", True), "testvalue3")
278
279
280class TestFlags(unittest.TestCase):
281 def setUp(self):
282 self.d = bb.data.init()
283 self.d.setVar("foo", "value of foo")
284 self.d.setVarFlag("foo", "flag1", "value of flag1")
285 self.d.setVarFlag("foo", "flag2", "value of flag2")
286
287 def test_setflag(self):
288 self.assertEqual(self.d.getVarFlag("foo", "flag1"), "value of flag1")
289 self.assertEqual(self.d.getVarFlag("foo", "flag2"), "value of flag2")
290
291 def test_delflag(self):
292 self.d.delVarFlag("foo", "flag2")
293 self.assertEqual(self.d.getVarFlag("foo", "flag1"), "value of flag1")
294 self.assertEqual(self.d.getVarFlag("foo", "flag2"), None)
295
296