blob: 1d4a64b10943279ba5cdc57a33040412e0c05502 [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}")
248 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}")
253 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}")
258 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):
264 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}")
271 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}")
276 self.d.setVar("TEST_remove", "val")
277 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")
284 self.d.setVar("TEST_remove_inactiveoverride", "val")
285 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}")
289 self.d.setVar("TEST_remove", "val")
290 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", "")
295 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")
301 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")
307 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")
313 self.d.setVar("TEST_remove", "C")
314 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")
319 self.d.setVar("TEST_remove", "B")
320 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):
332 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):
336 self.d.setVar("TEST2_bar", "testvalue2")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500337
338 self.assertEqual(self.d.getVar("TEST2"), "testvalue2")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600339 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):
342 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 Williamsc0f7c042017-02-23 20:41:17 -0600346 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):
349 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):
353 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")
358 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):
365 self.d.setVar("ALTERNATIVE_ncurses-tools_class-target", "a")
366 self.d.setVar("OVERRIDES", "class-target")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367 self.d.renameVar("ALTERNATIVE_ncurses-tools", "ALTERNATIVE_lib32-ncurses-tools")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500368 self.assertEqual(self.d.getVar("ALTERNATIVE_lib32-ncurses-tools"), "a")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369
370 def test_underscore_override(self):
371 self.d.setVar("TEST_bar", "testvalue2")
372 self.d.setVar("TEST_some_val", "testvalue3")
373 self.d.setVar("TEST_foo", "testvalue4")
374 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):
378 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")
382 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):
386 self.d.setVar("TEST_append", "testvalue2")
387 self.d.setVar("TEST_bar", "testvalue3")
388 self.assertEqual(self.d.getVar("TEST"), "testvalue3testvalue2")
389
390 def test_append_and_override_2(self):
391 self.d.setVar("TEST_append_bar", "testvalue2")
392 self.assertEqual(self.d.getVar("TEST"), "testvaluetestvalue2")
393
394 def test_append_and_override_3(self):
395 self.d.setVar("TEST_bar_append", "testvalue2")
396 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")
403 self.d.setVar("VERSION_pn-test-${TARGET_ARCH}", "2")
404 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 Williamsc124f4f2015-09-15 14:41:29 -0500408class TestKeyExpansion(unittest.TestCase):
409 def setUp(self):
410 self.d = bb.data.init()
411 self.d.setVar("FOO", "foo")
412 self.d.setVar("BAR", "foo")
413
414 def test_keyexpand(self):
415 self.d.setVar("VAL_${FOO}", "A")
416 self.d.setVar("VAL_${BAR}", "B")
417 with LogRecord() as logs:
418 bb.data.expandKeys(self.d)
419 self.assertTrue(logContains("Variable key VAL_${FOO} (A) replaces original key VAL_foo (B)", logs))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500420 self.assertEqual(self.d.getVar("VAL_foo"), "A")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500421
422class TestFlags(unittest.TestCase):
423 def setUp(self):
424 self.d = bb.data.init()
425 self.d.setVar("foo", "value of foo")
426 self.d.setVarFlag("foo", "flag1", "value of flag1")
427 self.d.setVarFlag("foo", "flag2", "value of flag2")
428
429 def test_setflag(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500430 self.assertEqual(self.d.getVarFlag("foo", "flag1", False), "value of flag1")
431 self.assertEqual(self.d.getVarFlag("foo", "flag2", False), "value of flag2")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500432
433 def test_delflag(self):
434 self.d.delVarFlag("foo", "flag2")
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), None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500437
438
439class Contains(unittest.TestCase):
440 def setUp(self):
441 self.d = bb.data.init()
442 self.d.setVar("SOMEFLAG", "a b c")
443
444 def test_contains(self):
445 self.assertTrue(bb.utils.contains("SOMEFLAG", "a", True, False, self.d))
446 self.assertTrue(bb.utils.contains("SOMEFLAG", "b", True, False, self.d))
447 self.assertTrue(bb.utils.contains("SOMEFLAG", "c", True, False, self.d))
448
449 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b", True, False, self.d))
450 self.assertTrue(bb.utils.contains("SOMEFLAG", "b c", True, False, self.d))
451 self.assertTrue(bb.utils.contains("SOMEFLAG", "c a", True, False, self.d))
452
453 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b c", True, False, self.d))
454 self.assertTrue(bb.utils.contains("SOMEFLAG", "c b a", True, False, self.d))
455
456 self.assertFalse(bb.utils.contains("SOMEFLAG", "x", True, False, self.d))
457 self.assertFalse(bb.utils.contains("SOMEFLAG", "a x", True, False, self.d))
458 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b", True, False, self.d))
459 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b a", True, False, self.d))
460
461 def test_contains_any(self):
462 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a", True, False, self.d))
463 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b", True, False, self.d))
464 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c", True, False, self.d))
465
466 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a b", True, False, self.d))
467 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b c", True, False, self.d))
468 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c a", True, False, self.d))
469
470 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a x", True, False, self.d))
471 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "x c", True, False, self.d))
472
473 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x", True, False, self.d))
474 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x y z", True, False, self.d))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500475
476
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800477class TaskHash(unittest.TestCase):
478 def test_taskhashes(self):
479 def gettask_bashhash(taskname, d):
Andrew Geissler82c905d2020-04-13 13:39:40 -0500480 tasklist, gendeps, lookupcache = bb.data.generate_dependencies(d, set())
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800481 taskdeps, basehash = bb.data.generate_dependency_hash(tasklist, gendeps, lookupcache, set(), "somefile")
482 bb.warn(str(lookupcache))
Brad Bishop08902b02019-08-20 09:16:51 -0400483 return basehash["somefile:" + taskname]
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800484
485 d = bb.data.init()
486 d.setVar("__BBTASKS", ["mytask"])
487 d.setVar("__exportlist", [])
488 d.setVar("mytask", "${MYCOMMAND}")
489 d.setVar("MYCOMMAND", "${VAR}; foo; bar; exit 0")
490 d.setVar("VAR", "val")
491 orighash = gettask_bashhash("mytask", d)
492
493 # Changing a variable should change the hash
494 d.setVar("VAR", "val2")
495 nexthash = gettask_bashhash("mytask", d)
496 self.assertNotEqual(orighash, nexthash)
497
498 d.setVar("VAR", "val")
499 # Adding an inactive removal shouldn't change the hash
500 d.setVar("BAR", "notbar")
501 d.setVar("MYCOMMAND_remove", "${BAR}")
502 nexthash = gettask_bashhash("mytask", d)
503 self.assertEqual(orighash, nexthash)
504
505 # Adding an active removal should change the hash
506 d.setVar("BAR", "bar;")
507 nexthash = gettask_bashhash("mytask", d)
508 self.assertNotEqual(orighash, nexthash)
509
510 # Setup an inactive contains()
511 d.setVar("VAR", "${@bb.utils.contains('VAR2', 'A', 'val', '', d)}")
512 orighash = gettask_bashhash("mytask", d)
513
514 # Activate the contains() and the hash should change
515 d.setVar("VAR2", "A")
516 nexthash = gettask_bashhash("mytask", d)
517 self.assertNotEqual(orighash, nexthash)
518
519 # The contains should be inactive but even though VAR2 has a
520 # different value the hash should match the original
521 d.setVar("VAR2", "B")
522 nexthash = gettask_bashhash("mytask", d)
523 self.assertEqual(orighash, nexthash)
524
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500525class Serialize(unittest.TestCase):
526
527 def test_serialize(self):
528 import tempfile
529 import pickle
530 d = bb.data.init()
531 d.enableTracking()
532 d.setVar('HELLO', 'world')
533 d.setVarFlag('HELLO', 'other', 'planet')
534 with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
535 tmpfilename = tmpfile.name
536 pickle.dump(d, tmpfile)
537
538 with open(tmpfilename, 'rb') as f:
539 newd = pickle.load(f)
540
541 os.remove(tmpfilename)
542
543 self.assertEqual(d, newd)
544 self.assertEqual(newd.getVar('HELLO'), 'world')
545 self.assertEqual(newd.getVarFlag('HELLO', 'other'), 'planet')
546
547