blob: e667c7c7d359ea2a361c45dcb60054abf51206a7 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#
2# BitBake Tests for the Data Store (data.py/data_smart.py)
3#
4# Copyright (C) 2010 Chris Larson
5# Copyright (C) 2012 Richard Purdie
6#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008#
9
10import unittest
11import bb
12import bb.data
13import bb.parse
14import logging
Andrew Geisslerc9f78652020-09-18 14:11:35 -050015import os
Patrick Williamsc124f4f2015-09-15 14:41:29 -050016
17class LogRecord():
18 def __enter__(self):
19 logs = []
20 class LogHandler(logging.Handler):
21 def emit(self, record):
22 logs.append(record)
23 logger = logging.getLogger("BitBake")
24 handler = LogHandler()
25 self.handler = handler
26 logger.addHandler(handler)
27 return logs
28 def __exit__(self, type, value, traceback):
29 logger = logging.getLogger("BitBake")
30 logger.removeHandler(self.handler)
31 return
32
33def logContains(item, logs):
34 for l in logs:
35 m = l.getMessage()
36 if item in m:
37 return True
38 return False
39
40class DataExpansions(unittest.TestCase):
41 def setUp(self):
42 self.d = bb.data.init()
43 self.d["foo"] = "value_of_foo"
44 self.d["bar"] = "value_of_bar"
45 self.d["value_of_foo"] = "value_of_'value_of_foo'"
46
47 def test_one_var(self):
48 val = self.d.expand("${foo}")
49 self.assertEqual(str(val), "value_of_foo")
50
51 def test_indirect_one_var(self):
52 val = self.d.expand("${${foo}}")
53 self.assertEqual(str(val), "value_of_'value_of_foo'")
54
55 def test_indirect_and_another(self):
56 val = self.d.expand("${${foo}} ${bar}")
57 self.assertEqual(str(val), "value_of_'value_of_foo' value_of_bar")
58
59 def test_python_snippet(self):
60 val = self.d.expand("${@5*12}")
61 self.assertEqual(str(val), "60")
62
63 def test_expand_in_python_snippet(self):
64 val = self.d.expand("${@'boo ' + '${foo}'}")
65 self.assertEqual(str(val), "boo value_of_foo")
66
67 def test_python_snippet_getvar(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050068 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050069 self.assertEqual(str(val), "value_of_foo value_of_bar")
70
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050071 def test_python_unexpanded(self):
72 self.d.setVar("bar", "${unsetvar}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050073 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
74 self.assertEqual(str(val), "${@d.getVar('foo') + ' ${unsetvar}'}")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050075
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076 def test_python_snippet_syntax_error(self):
77 self.d.setVar("FOO", "${@foo = 5}")
78 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
79
80 def test_python_snippet_runtime_error(self):
81 self.d.setVar("FOO", "${@int('test')}")
82 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
83
84 def test_python_snippet_error_path(self):
85 self.d.setVar("FOO", "foo value ${BAR}")
86 self.d.setVar("BAR", "bar value ${@int('test')}")
87 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
88
89 def test_value_containing_value(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091 self.assertEqual(str(val), "value_of_foo value_of_bar")
92
93 def test_reference_undefined_var(self):
94 val = self.d.expand("${undefinedvar} meh")
95 self.assertEqual(str(val), "${undefinedvar} meh")
96
97 def test_double_reference(self):
98 self.d.setVar("BAR", "bar value")
99 self.d.setVar("FOO", "${BAR} foo ${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 val = self.d.getVar("FOO")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101 self.assertEqual(str(val), "bar value foo bar value")
102
103 def test_direct_recursion(self):
104 self.d.setVar("FOO", "${FOO}")
105 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
106
107 def test_indirect_recursion(self):
108 self.d.setVar("FOO", "${BAR}")
109 self.d.setVar("BAR", "${BAZ}")
110 self.d.setVar("BAZ", "${FOO}")
111 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
112
113 def test_recursion_exception(self):
114 self.d.setVar("FOO", "${BAR}")
115 self.d.setVar("BAR", "${${@'FOO'}}")
116 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
117
118 def test_incomplete_varexp_single_quotes(self):
119 self.d.setVar("FOO", "sed -i -e 's:IP{:I${:g' $pc")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500120 val = self.d.getVar("FOO")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121 self.assertEqual(str(val), "sed -i -e 's:IP{:I${:g' $pc")
122
123 def test_nonstring(self):
124 self.d.setVar("TEST", 5)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500125 val = self.d.getVar("TEST")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500126 self.assertEqual(str(val), "5")
127
128 def test_rename(self):
129 self.d.renameVar("foo", "newfoo")
130 self.assertEqual(self.d.getVar("newfoo", False), "value_of_foo")
131 self.assertEqual(self.d.getVar("foo", False), None)
132
133 def test_deletion(self):
134 self.d.delVar("foo")
135 self.assertEqual(self.d.getVar("foo", False), None)
136
137 def test_keys(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600138 keys = list(self.d.keys())
139 self.assertCountEqual(keys, ['value_of_foo', 'foo', 'bar'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140
141 def test_keys_deletion(self):
142 newd = bb.data.createCopy(self.d)
143 newd.delVar("bar")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600144 keys = list(newd.keys())
145 self.assertCountEqual(keys, ['value_of_foo', 'foo'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146
147class TestNestedExpansions(unittest.TestCase):
148 def setUp(self):
149 self.d = bb.data.init()
150 self.d["foo"] = "foo"
151 self.d["bar"] = "bar"
152 self.d["value_of_foobar"] = "187"
153
154 def test_refs(self):
155 val = self.d.expand("${value_of_${foo}${bar}}")
156 self.assertEqual(str(val), "187")
157
158 #def test_python_refs(self):
159 # val = self.d.expand("${@${@3}**2 + ${@4}**2}")
160 # self.assertEqual(str(val), "25")
161
162 def test_ref_in_python_ref(self):
163 val = self.d.expand("${@'${foo}' + 'bar'}")
164 self.assertEqual(str(val), "foobar")
165
166 def test_python_ref_in_ref(self):
167 val = self.d.expand("${${@'f'+'o'+'o'}}")
168 self.assertEqual(str(val), "foo")
169
170 def test_deep_nesting(self):
171 depth = 100
172 val = self.d.expand("${" * depth + "foo" + "}" * depth)
173 self.assertEqual(str(val), "foo")
174
175 #def test_deep_python_nesting(self):
176 # depth = 50
177 # val = self.d.expand("${@" * depth + "1" + "+1}" * depth)
178 # self.assertEqual(str(val), str(depth + 1))
179
180 def test_mixed(self):
181 val = self.d.expand("${value_of_${@('${foo}'+'bar')[0:3]}${${@'BAR'.lower()}}}")
182 self.assertEqual(str(val), "187")
183
184 def test_runtime(self):
185 val = self.d.expand("${${@'value_of' + '_f'+'o'+'o'+'b'+'a'+'r'}}")
186 self.assertEqual(str(val), "187")
187
188class TestMemoize(unittest.TestCase):
189 def test_memoized(self):
190 d = bb.data.init()
191 d.setVar("FOO", "bar")
192 self.assertTrue(d.getVar("FOO", False) is d.getVar("FOO", False))
193
194 def test_not_memoized(self):
195 d1 = bb.data.init()
196 d2 = bb.data.init()
197 d1.setVar("FOO", "bar")
198 d2.setVar("FOO", "bar2")
199 self.assertTrue(d1.getVar("FOO", False) is not d2.getVar("FOO", False))
200
201 def test_changed_after_memoized(self):
202 d = bb.data.init()
203 d.setVar("foo", "value of foo")
204 self.assertEqual(str(d.getVar("foo", False)), "value of foo")
205 d.setVar("foo", "second value of foo")
206 self.assertEqual(str(d.getVar("foo", False)), "second value of foo")
207
208 def test_same_value(self):
209 d = bb.data.init()
210 d.setVar("foo", "value of")
211 d.setVar("bar", "value of")
212 self.assertEqual(d.getVar("foo", False),
213 d.getVar("bar", False))
214
215class TestConcat(unittest.TestCase):
216 def setUp(self):
217 self.d = bb.data.init()
218 self.d.setVar("FOO", "foo")
219 self.d.setVar("VAL", "val")
220 self.d.setVar("BAR", "bar")
221
222 def test_prepend(self):
223 self.d.setVar("TEST", "${VAL}")
224 self.d.prependVar("TEST", "${FOO}:")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500225 self.assertEqual(self.d.getVar("TEST"), "foo:val")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226
227 def test_append(self):
228 self.d.setVar("TEST", "${VAL}")
229 self.d.appendVar("TEST", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500230 self.assertEqual(self.d.getVar("TEST"), "val:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231
232 def test_multiple_append(self):
233 self.d.setVar("TEST", "${VAL}")
234 self.d.prependVar("TEST", "${FOO}:")
235 self.d.appendVar("TEST", ":val2")
236 self.d.appendVar("TEST", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 self.assertEqual(self.d.getVar("TEST"), "foo:val:val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238
239class TestConcatOverride(unittest.TestCase):
240 def setUp(self):
241 self.d = bb.data.init()
242 self.d.setVar("FOO", "foo")
243 self.d.setVar("VAL", "val")
244 self.d.setVar("BAR", "bar")
245
246 def test_prepend(self):
247 self.d.setVar("TEST", "${VAL}")
Patrick Williams213cb262021-08-07 19:21:33 -0500248 self.d.setVar("TEST:prepend", "${FOO}:")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500249 self.assertEqual(self.d.getVar("TEST"), "foo:val")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
251 def test_append(self):
252 self.d.setVar("TEST", "${VAL}")
Patrick Williams213cb262021-08-07 19:21:33 -0500253 self.d.setVar("TEST:append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500254 self.assertEqual(self.d.getVar("TEST"), "val:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255
256 def test_multiple_append(self):
257 self.d.setVar("TEST", "${VAL}")
Patrick Williams213cb262021-08-07 19:21:33 -0500258 self.d.setVar("TEST:prepend", "${FOO}:")
259 self.d.setVar("TEST:append", ":val2")
260 self.d.setVar("TEST:append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 self.assertEqual(self.d.getVar("TEST"), "foo:val:val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262
263 def test_append_unset(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500264 self.d.setVar("TEST:prepend", "${FOO}:")
265 self.d.setVar("TEST:append", ":val2")
266 self.d.setVar("TEST:append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500267 self.assertEqual(self.d.getVar("TEST"), "foo::val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500268
269 def test_remove(self):
270 self.d.setVar("TEST", "${VAL} ${BAR}")
Patrick Williams213cb262021-08-07 19:21:33 -0500271 self.d.setVar("TEST:remove", "val")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800272 self.assertEqual(self.d.getVar("TEST"), " bar")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500273
274 def test_remove_cleared(self):
275 self.d.setVar("TEST", "${VAL} ${BAR}")
Patrick Williams213cb262021-08-07 19:21:33 -0500276 self.d.setVar("TEST:remove", "val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500277 self.d.setVar("TEST", "${VAL} ${BAR}")
278 self.assertEqual(self.d.getVar("TEST"), "val bar")
279
280 # Ensure the value is unchanged if we have an inactive remove override
281 # (including that whitespace is preserved)
282 def test_remove_inactive_override(self):
283 self.d.setVar("TEST", "${VAL} ${BAR} 123")
Patrick Williams213cb262021-08-07 19:21:33 -0500284 self.d.setVar("TEST:remove:inactiveoverride", "val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 self.assertEqual(self.d.getVar("TEST"), "val bar 123")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286
287 def test_doubleref_remove(self):
288 self.d.setVar("TEST", "${VAL} ${BAR}")
Patrick Williams213cb262021-08-07 19:21:33 -0500289 self.d.setVar("TEST:remove", "val")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290 self.d.setVar("TEST_TEST", "${TEST} ${TEST}")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800291 self.assertEqual(self.d.getVar("TEST_TEST"), " bar bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500292
293 def test_empty_remove(self):
294 self.d.setVar("TEST", "")
Patrick Williams213cb262021-08-07 19:21:33 -0500295 self.d.setVar("TEST:remove", "val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500296 self.assertEqual(self.d.getVar("TEST"), "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500297
298 def test_remove_expansion(self):
299 self.d.setVar("BAR", "Z")
300 self.d.setVar("TEST", "${BAR}/X Y")
Patrick Williams213cb262021-08-07 19:21:33 -0500301 self.d.setVar("TEST:remove", "${BAR}/X")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800302 self.assertEqual(self.d.getVar("TEST"), " Y")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500303
304 def test_remove_expansion_items(self):
305 self.d.setVar("TEST", "A B C D")
306 self.d.setVar("BAR", "B D")
Patrick Williams213cb262021-08-07 19:21:33 -0500307 self.d.setVar("TEST:remove", "${BAR}")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800308 self.assertEqual(self.d.getVar("TEST"), "A C ")
309
310 def test_remove_preserve_whitespace(self):
311 # When the removal isn't active, the original value should be preserved
312 self.d.setVar("TEST", " A B")
Patrick Williams213cb262021-08-07 19:21:33 -0500313 self.d.setVar("TEST:remove", "C")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800314 self.assertEqual(self.d.getVar("TEST"), " A B")
315
316 def test_remove_preserve_whitespace2(self):
317 # When the removal is active preserve the whitespace
318 self.d.setVar("TEST", " A B")
Patrick Williams213cb262021-08-07 19:21:33 -0500319 self.d.setVar("TEST:remove", "B")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800320 self.assertEqual(self.d.getVar("TEST"), " A ")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321
322class TestOverrides(unittest.TestCase):
323 def setUp(self):
324 self.d = bb.data.init()
325 self.d.setVar("OVERRIDES", "foo:bar:local")
326 self.d.setVar("TEST", "testvalue")
327
328 def test_no_override(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500329 self.assertEqual(self.d.getVar("TEST"), "testvalue")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500330
331 def test_one_override(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500332 self.d.setVar("TEST:bar", "testvalue2")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500333 self.assertEqual(self.d.getVar("TEST"), "testvalue2")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
335 def test_one_override_unset(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500336 self.d.setVar("TEST2:bar", "testvalue2")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500337
338 self.assertEqual(self.d.getVar("TEST2"), "testvalue2")
Patrick Williams213cb262021-08-07 19:21:33 -0500339 self.assertCountEqual(list(self.d.keys()), ['TEST', 'TEST2', 'OVERRIDES', 'TEST2:bar'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340
341 def test_multiple_override(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500342 self.d.setVar("TEST:bar", "testvalue2")
343 self.d.setVar("TEST:local", "testvalue3")
344 self.d.setVar("TEST:foo", "testvalue4")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500345 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williams213cb262021-08-07 19:21:33 -0500346 self.assertCountEqual(list(self.d.keys()), ['TEST', 'TEST:foo', 'OVERRIDES', 'TEST:bar', 'TEST:local'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500347
348 def test_multiple_combined_overrides(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500349 self.d.setVar("TEST:local:foo:bar", "testvalue3")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500350 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351
352 def test_multiple_overrides_unset(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500353 self.d.setVar("TEST2:local:foo:bar", "testvalue3")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500354 self.assertEqual(self.d.getVar("TEST2"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355
356 def test_keyexpansion_override(self):
357 self.d.setVar("LOCAL", "local")
Patrick Williams213cb262021-08-07 19:21:33 -0500358 self.d.setVar("TEST:bar", "testvalue2")
359 self.d.setVar("TEST:${LOCAL}", "testvalue3")
360 self.d.setVar("TEST:foo", "testvalue4")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500361 bb.data.expandKeys(self.d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500362 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
364 def test_rename_override(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500365 self.d.setVar("ALTERNATIVE:ncurses-tools:class-target", "a")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500366 self.d.setVar("OVERRIDES", "class-target")
Patrick Williams213cb262021-08-07 19:21:33 -0500367 self.d.renameVar("ALTERNATIVE:ncurses-tools", "ALTERNATIVE:lib32-ncurses-tools")
368 self.assertEqual(self.d.getVar("ALTERNATIVE:lib32-ncurses-tools"), "a")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369
370 def test_underscore_override(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500371 self.d.setVar("TEST:bar", "testvalue2")
372 self.d.setVar("TEST:some_val", "testvalue3")
373 self.d.setVar("TEST:foo", "testvalue4")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500374 self.d.setVar("OVERRIDES", "foo:bar:some_val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500375 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800377 def test_remove_with_override(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500378 self.d.setVar("TEST:bar", "testvalue2")
379 self.d.setVar("TEST:some_val", "testvalue3 testvalue5")
380 self.d.setVar("TEST:some_val:remove", "testvalue3")
381 self.d.setVar("TEST:foo", "testvalue4")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800382 self.d.setVar("OVERRIDES", "foo:bar:some_val")
383 self.assertEqual(self.d.getVar("TEST"), " testvalue5")
384
Brad Bishop00e122a2019-10-05 11:10:57 -0400385 def test_append_and_override_1(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500386 self.d.setVar("TEST:append", "testvalue2")
387 self.d.setVar("TEST:bar", "testvalue3")
Brad Bishop00e122a2019-10-05 11:10:57 -0400388 self.assertEqual(self.d.getVar("TEST"), "testvalue3testvalue2")
389
390 def test_append_and_override_2(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500391 self.d.setVar("TEST:append:bar", "testvalue2")
Brad Bishop00e122a2019-10-05 11:10:57 -0400392 self.assertEqual(self.d.getVar("TEST"), "testvaluetestvalue2")
393
394 def test_append_and_override_3(self):
Patrick Williams213cb262021-08-07 19:21:33 -0500395 self.d.setVar("TEST:bar:append", "testvalue2")
Brad Bishop00e122a2019-10-05 11:10:57 -0400396 self.assertEqual(self.d.getVar("TEST"), "testvalue2")
397
Brad Bishop19323692019-04-05 15:28:33 -0400398 # Test an override with _<numeric> in it based on a real world OE issue
399 def test_underscore_override(self):
400 self.d.setVar("TARGET_ARCH", "x86_64")
401 self.d.setVar("PN", "test-${TARGET_ARCH}")
402 self.d.setVar("VERSION", "1")
Patrick Williams213cb262021-08-07 19:21:33 -0500403 self.d.setVar("VERSION:pn-test-${TARGET_ARCH}", "2")
Brad Bishop19323692019-04-05 15:28:33 -0400404 self.d.setVar("OVERRIDES", "pn-${PN}")
405 bb.data.expandKeys(self.d)
406 self.assertEqual(self.d.getVar("VERSION"), "2")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800407
Patrick Williams213cb262021-08-07 19:21:33 -0500408 def test_append_and_unused_override(self):
409 # Had a bug where an unused override append could return "" instead of None
410 self.d.setVar("BAR:append:unusedoverride", "testvalue2")
411 self.assertEqual(self.d.getVar("BAR"), None)
412
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500413class TestKeyExpansion(unittest.TestCase):
414 def setUp(self):
415 self.d = bb.data.init()
416 self.d.setVar("FOO", "foo")
417 self.d.setVar("BAR", "foo")
418
419 def test_keyexpand(self):
420 self.d.setVar("VAL_${FOO}", "A")
421 self.d.setVar("VAL_${BAR}", "B")
422 with LogRecord() as logs:
423 bb.data.expandKeys(self.d)
424 self.assertTrue(logContains("Variable key VAL_${FOO} (A) replaces original key VAL_foo (B)", logs))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500425 self.assertEqual(self.d.getVar("VAL_foo"), "A")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500426
427class TestFlags(unittest.TestCase):
428 def setUp(self):
429 self.d = bb.data.init()
430 self.d.setVar("foo", "value of foo")
431 self.d.setVarFlag("foo", "flag1", "value of flag1")
432 self.d.setVarFlag("foo", "flag2", "value of flag2")
433
434 def test_setflag(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500435 self.assertEqual(self.d.getVarFlag("foo", "flag1", False), "value of flag1")
436 self.assertEqual(self.d.getVarFlag("foo", "flag2", False), "value of flag2")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437
438 def test_delflag(self):
439 self.d.delVarFlag("foo", "flag2")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500440 self.assertEqual(self.d.getVarFlag("foo", "flag1", False), "value of flag1")
441 self.assertEqual(self.d.getVarFlag("foo", "flag2", False), None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500442
443
444class Contains(unittest.TestCase):
445 def setUp(self):
446 self.d = bb.data.init()
447 self.d.setVar("SOMEFLAG", "a b c")
448
449 def test_contains(self):
450 self.assertTrue(bb.utils.contains("SOMEFLAG", "a", True, False, self.d))
451 self.assertTrue(bb.utils.contains("SOMEFLAG", "b", True, False, self.d))
452 self.assertTrue(bb.utils.contains("SOMEFLAG", "c", True, False, self.d))
453
454 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b", True, False, self.d))
455 self.assertTrue(bb.utils.contains("SOMEFLAG", "b c", True, False, self.d))
456 self.assertTrue(bb.utils.contains("SOMEFLAG", "c a", True, False, self.d))
457
458 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b c", True, False, self.d))
459 self.assertTrue(bb.utils.contains("SOMEFLAG", "c b a", True, False, self.d))
460
461 self.assertFalse(bb.utils.contains("SOMEFLAG", "x", True, False, self.d))
462 self.assertFalse(bb.utils.contains("SOMEFLAG", "a x", True, False, self.d))
463 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b", True, False, self.d))
464 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b a", True, False, self.d))
465
466 def test_contains_any(self):
467 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a", True, False, self.d))
468 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b", True, False, self.d))
469 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c", True, False, self.d))
470
471 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a b", True, False, self.d))
472 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b c", True, False, self.d))
473 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c a", True, False, self.d))
474
475 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a x", True, False, self.d))
476 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "x c", True, False, self.d))
477
478 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x", True, False, self.d))
479 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x y z", True, False, self.d))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500480
481
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800482class TaskHash(unittest.TestCase):
483 def test_taskhashes(self):
484 def gettask_bashhash(taskname, d):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500485 tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d, set())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800486 taskdeps, basehash = bb.data.generate_dependency_hash(tasklist, gendeps, lookupcache, set(), "somefile")
487 bb.warn(str(lookupcache))
Brad Bishop08902b02019-08-20 09:16:51 -0400488 return basehash["somefile:" + taskname]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800489
490 d = bb.data.init()
491 d.setVar("__BBTASKS", ["mytask"])
492 d.setVar("__exportlist", [])
493 d.setVar("mytask", "${MYCOMMAND}")
494 d.setVar("MYCOMMAND", "${VAR}; foo; bar; exit 0")
495 d.setVar("VAR", "val")
496 orighash = gettask_bashhash("mytask", d)
497
498 # Changing a variable should change the hash
499 d.setVar("VAR", "val2")
500 nexthash = gettask_bashhash("mytask", d)
501 self.assertNotEqual(orighash, nexthash)
502
503 d.setVar("VAR", "val")
504 # Adding an inactive removal shouldn't change the hash
505 d.setVar("BAR", "notbar")
Patrick Williams213cb262021-08-07 19:21:33 -0500506 d.setVar("MYCOMMAND:remove", "${BAR}")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800507 nexthash = gettask_bashhash("mytask", d)
508 self.assertEqual(orighash, nexthash)
509
510 # Adding an active removal should change the hash
511 d.setVar("BAR", "bar;")
512 nexthash = gettask_bashhash("mytask", d)
513 self.assertNotEqual(orighash, nexthash)
514
515 # Setup an inactive contains()
516 d.setVar("VAR", "${@bb.utils.contains('VAR2', 'A', 'val', '', d)}")
517 orighash = gettask_bashhash("mytask", d)
518
519 # Activate the contains() and the hash should change
520 d.setVar("VAR2", "A")
521 nexthash = gettask_bashhash("mytask", d)
522 self.assertNotEqual(orighash, nexthash)
523
524 # The contains should be inactive but even though VAR2 has a
525 # different value the hash should match the original
526 d.setVar("VAR2", "B")
527 nexthash = gettask_bashhash("mytask", d)
528 self.assertEqual(orighash, nexthash)
529
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500530class Serialize(unittest.TestCase):
531
532 def test_serialize(self):
533 import tempfile
534 import pickle
535 d = bb.data.init()
536 d.enableTracking()
537 d.setVar('HELLO', 'world')
538 d.setVarFlag('HELLO', 'other', 'planet')
539 with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
540 tmpfilename = tmpfile.name
541 pickle.dump(d, tmpfile)
542
543 with open(tmpfilename, 'rb') as f:
544 newd = pickle.load(f)
545
546 os.remove(tmpfilename)
547
548 self.assertEqual(d, newd)
549 self.assertEqual(newd.getVar('HELLO'), 'world')
550 self.assertEqual(newd.getVarFlag('HELLO', 'other'), 'planet')
551
552