Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Test for codeparser.py |
| 3 | # |
| 4 | # Copyright (C) 2010 Chris Larson |
| 5 | # Copyright (C) 2012 Richard Purdie |
| 6 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | # |
| 9 | |
| 10 | import unittest |
| 11 | import logging |
| 12 | import bb |
| 13 | |
| 14 | logger = logging.getLogger('BitBake.TestCodeParser') |
| 15 | |
| 16 | # bb.data references bb.parse but can't directly import due to circular dependencies. |
| 17 | # Hack around it for now :( |
| 18 | import bb.parse |
| 19 | import bb.data |
| 20 | |
| 21 | class ReferenceTest(unittest.TestCase): |
| 22 | def setUp(self): |
| 23 | self.d = bb.data.init() |
| 24 | |
| 25 | def setEmptyVars(self, varlist): |
| 26 | for k in varlist: |
| 27 | self.d.setVar(k, "") |
| 28 | |
| 29 | def setValues(self, values): |
| 30 | for k, v in values.items(): |
| 31 | self.d.setVar(k, v) |
| 32 | |
| 33 | def assertReferences(self, refs): |
| 34 | self.assertEqual(self.references, refs) |
| 35 | |
| 36 | def assertExecs(self, execs): |
| 37 | self.assertEqual(self.execs, execs) |
| 38 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 39 | def assertContains(self, contains): |
| 40 | self.assertEqual(self.contains, contains) |
| 41 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 42 | class VariableReferenceTest(ReferenceTest): |
| 43 | |
| 44 | def parseExpression(self, exp): |
| 45 | parsedvar = self.d.expandWithRefs(exp, None) |
| 46 | self.references = parsedvar.references |
| 47 | |
| 48 | def test_simple_reference(self): |
| 49 | self.setEmptyVars(["FOO"]) |
| 50 | self.parseExpression("${FOO}") |
| 51 | self.assertReferences(set(["FOO"])) |
| 52 | |
| 53 | def test_nested_reference(self): |
| 54 | self.setEmptyVars(["BAR"]) |
| 55 | self.d.setVar("FOO", "BAR") |
| 56 | self.parseExpression("${${FOO}}") |
| 57 | self.assertReferences(set(["FOO", "BAR"])) |
| 58 | |
| 59 | def test_python_reference(self): |
| 60 | self.setEmptyVars(["BAR"]) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | self.parseExpression("${@d.getVar('BAR') + 'foo'}") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | self.assertReferences(set(["BAR"])) |
| 63 | |
| 64 | class ShellReferenceTest(ReferenceTest): |
| 65 | |
| 66 | def parseExpression(self, exp): |
| 67 | parsedvar = self.d.expandWithRefs(exp, None) |
| 68 | parser = bb.codeparser.ShellParser("ParserTest", logger) |
| 69 | parser.parse_shell(parsedvar.value) |
| 70 | |
| 71 | self.references = parsedvar.references |
| 72 | self.execs = parser.execs |
| 73 | |
| 74 | def test_quotes_inside_assign(self): |
| 75 | self.parseExpression('foo=foo"bar"baz') |
| 76 | self.assertReferences(set([])) |
| 77 | |
| 78 | def test_quotes_inside_arg(self): |
| 79 | self.parseExpression('sed s#"bar baz"#"alpha beta"#g') |
| 80 | self.assertExecs(set(["sed"])) |
| 81 | |
| 82 | def test_arg_continuation(self): |
| 83 | self.parseExpression("sed -i -e s,foo,bar,g \\\n *.pc") |
| 84 | self.assertExecs(set(["sed"])) |
| 85 | |
| 86 | def test_dollar_in_quoted(self): |
| 87 | self.parseExpression('sed -i -e "foo$" *.pc') |
| 88 | self.assertExecs(set(["sed"])) |
| 89 | |
| 90 | def test_quotes_inside_arg_continuation(self): |
| 91 | self.setEmptyVars(["bindir", "D", "libdir"]) |
| 92 | self.parseExpression(""" |
| 93 | sed -i -e s#"moc_location=.*$"#"moc_location=${bindir}/moc4"# \\ |
| 94 | -e s#"uic_location=.*$"#"uic_location=${bindir}/uic4"# \\ |
| 95 | ${D}${libdir}/pkgconfig/*.pc |
| 96 | """) |
| 97 | self.assertReferences(set(["bindir", "D", "libdir"])) |
| 98 | |
| 99 | def test_assign_subshell_expansion(self): |
| 100 | self.parseExpression("foo=$(echo bar)") |
| 101 | self.assertExecs(set(["echo"])) |
| 102 | |
| 103 | def test_shell_unexpanded(self): |
| 104 | self.setEmptyVars(["QT_BASE_NAME"]) |
| 105 | self.parseExpression('echo "${QT_BASE_NAME}"') |
| 106 | self.assertExecs(set(["echo"])) |
| 107 | self.assertReferences(set(["QT_BASE_NAME"])) |
| 108 | |
| 109 | def test_incomplete_varexp_single_quotes(self): |
| 110 | self.parseExpression("sed -i -e 's:IP{:I${:g' $pc") |
| 111 | self.assertExecs(set(["sed"])) |
| 112 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 113 | def test_parameter_expansion_modifiers(self): |
| 114 | # - and + are also valid modifiers for parameter expansion, but are |
| 115 | # valid characters in bitbake variable names, so are not included here |
| 116 | for i in ('=', ':-', ':=', '?', ':?', ':+', '#', '%', '##', '%%'): |
| 117 | name = "foo%sbar" % i |
| 118 | self.parseExpression("${%s}" % name) |
| 119 | self.assertNotIn(name, self.references) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | |
| 121 | def test_until(self): |
| 122 | self.parseExpression("until false; do echo true; done") |
| 123 | self.assertExecs(set(["false", "echo"])) |
| 124 | self.assertReferences(set()) |
| 125 | |
| 126 | def test_case(self): |
| 127 | self.parseExpression(""" |
| 128 | case $foo in |
| 129 | *) |
| 130 | bar |
| 131 | ;; |
| 132 | esac |
| 133 | """) |
| 134 | self.assertExecs(set(["bar"])) |
| 135 | self.assertReferences(set()) |
| 136 | |
| 137 | def test_assign_exec(self): |
| 138 | self.parseExpression("a=b c='foo bar' alpha 1 2 3") |
| 139 | self.assertExecs(set(["alpha"])) |
| 140 | |
| 141 | def test_redirect_to_file(self): |
| 142 | self.setEmptyVars(["foo"]) |
| 143 | self.parseExpression("echo foo >${foo}/bar") |
| 144 | self.assertExecs(set(["echo"])) |
| 145 | self.assertReferences(set(["foo"])) |
| 146 | |
| 147 | def test_heredoc(self): |
| 148 | self.setEmptyVars(["theta"]) |
| 149 | self.parseExpression(""" |
| 150 | cat <<END |
| 151 | alpha |
| 152 | beta |
| 153 | ${theta} |
| 154 | END |
| 155 | """) |
| 156 | self.assertReferences(set(["theta"])) |
| 157 | |
| 158 | def test_redirect_from_heredoc(self): |
| 159 | v = ["B", "SHADOW_MAILDIR", "SHADOW_MAILFILE", "SHADOW_UTMPDIR", "SHADOW_LOGDIR", "bindir"] |
| 160 | self.setEmptyVars(v) |
| 161 | self.parseExpression(""" |
| 162 | cat <<END >${B}/cachedpaths |
| 163 | shadow_cv_maildir=${SHADOW_MAILDIR} |
| 164 | shadow_cv_mailfile=${SHADOW_MAILFILE} |
| 165 | shadow_cv_utmpdir=${SHADOW_UTMPDIR} |
| 166 | shadow_cv_logdir=${SHADOW_LOGDIR} |
| 167 | shadow_cv_passwd_dir=${bindir} |
| 168 | END |
| 169 | """) |
| 170 | self.assertReferences(set(v)) |
| 171 | self.assertExecs(set(["cat"])) |
| 172 | |
| 173 | # def test_incomplete_command_expansion(self): |
| 174 | # self.assertRaises(reftracker.ShellSyntaxError, reftracker.execs, |
| 175 | # bbvalue.shparse("cp foo`", self.d), self.d) |
| 176 | |
| 177 | # def test_rogue_dollarsign(self): |
| 178 | # self.setValues({"D" : "/tmp"}) |
| 179 | # self.parseExpression("install -d ${D}$") |
| 180 | # self.assertReferences(set(["D"])) |
| 181 | # self.assertExecs(set(["install"])) |
| 182 | |
| 183 | |
| 184 | class PythonReferenceTest(ReferenceTest): |
| 185 | |
| 186 | def setUp(self): |
| 187 | self.d = bb.data.init() |
| 188 | if hasattr(bb.utils, "_context"): |
| 189 | self.context = bb.utils._context |
| 190 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 191 | import builtins |
| 192 | self.context = builtins.__dict__ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 193 | |
| 194 | def parseExpression(self, exp): |
| 195 | parsedvar = self.d.expandWithRefs(exp, None) |
| 196 | parser = bb.codeparser.PythonParser("ParserTest", logger) |
| 197 | parser.parse_python(parsedvar.value) |
| 198 | |
| 199 | self.references = parsedvar.references | parser.references |
| 200 | self.execs = parser.execs |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 201 | self.contains = parser.contains |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 202 | |
| 203 | @staticmethod |
| 204 | def indent(value): |
| 205 | """Python Snippets have to be indented, python values don't have to |
| 206 | be. These unit tests are testing snippets.""" |
| 207 | return " " + value |
| 208 | |
| 209 | def test_getvar_reference(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 210 | self.parseExpression("d.getVar('foo')") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 211 | self.assertReferences(set(["foo"])) |
| 212 | self.assertExecs(set()) |
| 213 | |
| 214 | def test_getvar_computed_reference(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 215 | self.parseExpression("d.getVar('f' + 'o' + 'o')") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 216 | self.assertReferences(set()) |
| 217 | self.assertExecs(set()) |
| 218 | |
| 219 | def test_getvar_exec_reference(self): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 220 | self.parseExpression("eval('d.getVar(\"foo\")')") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | self.assertReferences(set()) |
| 222 | self.assertExecs(set(["eval"])) |
| 223 | |
| 224 | def test_var_reference(self): |
| 225 | self.context["foo"] = lambda x: x |
| 226 | self.setEmptyVars(["FOO"]) |
| 227 | self.parseExpression("foo('${FOO}')") |
| 228 | self.assertReferences(set(["FOO"])) |
| 229 | self.assertExecs(set(["foo"])) |
| 230 | del self.context["foo"] |
| 231 | |
| 232 | def test_var_exec(self): |
| 233 | for etype in ("func", "task"): |
| 234 | self.d.setVar("do_something", "echo 'hi mom! ${FOO}'") |
| 235 | self.d.setVarFlag("do_something", etype, True) |
| 236 | self.parseExpression("bb.build.exec_func('do_something', d)") |
| 237 | self.assertReferences(set([])) |
| 238 | self.assertExecs(set(["do_something"])) |
| 239 | |
| 240 | def test_function_reference(self): |
| 241 | self.context["testfunc"] = lambda msg: bb.msg.note(1, None, msg) |
| 242 | self.d.setVar("FOO", "Hello, World!") |
| 243 | self.parseExpression("testfunc('${FOO}')") |
| 244 | self.assertReferences(set(["FOO"])) |
| 245 | self.assertExecs(set(["testfunc"])) |
| 246 | del self.context["testfunc"] |
| 247 | |
| 248 | def test_qualified_function_reference(self): |
| 249 | self.parseExpression("time.time()") |
| 250 | self.assertExecs(set(["time.time"])) |
| 251 | |
| 252 | def test_qualified_function_reference_2(self): |
| 253 | self.parseExpression("os.path.dirname('/foo/bar')") |
| 254 | self.assertExecs(set(["os.path.dirname"])) |
| 255 | |
| 256 | def test_qualified_function_reference_nested(self): |
| 257 | self.parseExpression("time.strftime('%Y%m%d',time.gmtime())") |
| 258 | self.assertExecs(set(["time.strftime", "time.gmtime"])) |
| 259 | |
| 260 | def test_function_reference_chained(self): |
| 261 | self.context["testget"] = lambda: "\tstrip me " |
| 262 | self.parseExpression("testget().strip()") |
| 263 | self.assertExecs(set(["testget"])) |
| 264 | del self.context["testget"] |
| 265 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 266 | def test_contains(self): |
| 267 | self.parseExpression('bb.utils.contains("TESTVAR", "one", "true", "false", d)') |
| 268 | self.assertContains({'TESTVAR': {'one'}}) |
| 269 | |
| 270 | def test_contains_multi(self): |
| 271 | self.parseExpression('bb.utils.contains("TESTVAR", "one two", "true", "false", d)') |
| 272 | self.assertContains({'TESTVAR': {'one two'}}) |
| 273 | |
| 274 | def test_contains_any(self): |
| 275 | self.parseExpression('bb.utils.contains_any("TESTVAR", "hello", "true", "false", d)') |
| 276 | self.assertContains({'TESTVAR': {'hello'}}) |
| 277 | |
| 278 | def test_contains_any_multi(self): |
| 279 | self.parseExpression('bb.utils.contains_any("TESTVAR", "one two three", "true", "false", d)') |
| 280 | self.assertContains({'TESTVAR': {'one', 'two', 'three'}}) |
| 281 | |
| 282 | def test_contains_filter(self): |
| 283 | self.parseExpression('bb.utils.filter("TESTVAR", "hello there world", d)') |
| 284 | self.assertContains({'TESTVAR': {'hello', 'there', 'world'}}) |
| 285 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 286 | |
| 287 | class DependencyReferenceTest(ReferenceTest): |
| 288 | |
| 289 | pydata = """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 290 | d.getVar('somevar') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 291 | def test(d): |
| 292 | foo = 'bar %s' % 'foo' |
| 293 | def test2(d): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 294 | d.getVar(foo) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 295 | d.getVar('bar', False) |
| 296 | test2(d) |
| 297 | |
| 298 | def a(): |
| 299 | \"\"\"some |
| 300 | stuff |
| 301 | \"\"\" |
| 302 | return "heh" |
| 303 | |
| 304 | test(d) |
| 305 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 306 | d.expand(d.getVar("something", False)) |
| 307 | d.expand("${inexpand} somethingelse") |
| 308 | d.getVar(a(), False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | """ |
| 310 | |
| 311 | def test_python(self): |
| 312 | self.d.setVar("FOO", self.pydata) |
| 313 | self.setEmptyVars(["inexpand", "a", "test2", "test"]) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 314 | self.d.setVarFlags("FOO", { |
| 315 | "func": True, |
| 316 | "python": True, |
| 317 | "lineno": 1, |
| 318 | "filename": "example.bb", |
| 319 | }) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 320 | |
| 321 | deps, values = bb.data.build_dependencies("FOO", set(self.d.keys()), set(), set(), self.d) |
| 322 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 323 | self.assertEqual(deps, set(["somevar", "bar", "something", "inexpand", "test", "test2", "a"])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 324 | |
| 325 | |
| 326 | shelldata = """ |
| 327 | foo () { |
| 328 | bar |
| 329 | } |
| 330 | { |
| 331 | echo baz |
| 332 | $(heh) |
| 333 | eval `moo` |
| 334 | } |
| 335 | a=b |
| 336 | c=d |
| 337 | ( |
| 338 | true && false |
| 339 | test -f foo |
| 340 | testval=something |
| 341 | $testval |
| 342 | ) || aiee |
| 343 | ! inverted |
| 344 | echo ${somevar} |
| 345 | |
| 346 | case foo in |
| 347 | bar) |
| 348 | echo bar |
| 349 | ;; |
| 350 | baz) |
| 351 | echo baz |
| 352 | ;; |
| 353 | foo*) |
| 354 | echo foo |
| 355 | ;; |
| 356 | esac |
| 357 | """ |
| 358 | |
| 359 | def test_shell(self): |
| 360 | execs = ["bar", "echo", "heh", "moo", "true", "aiee"] |
| 361 | self.d.setVar("somevar", "heh") |
| 362 | self.d.setVar("inverted", "echo inverted...") |
| 363 | self.d.setVarFlag("inverted", "func", True) |
| 364 | self.d.setVar("FOO", self.shelldata) |
| 365 | self.d.setVarFlags("FOO", {"func": True}) |
| 366 | self.setEmptyVars(execs) |
| 367 | |
| 368 | deps, values = bb.data.build_dependencies("FOO", set(self.d.keys()), set(), set(), self.d) |
| 369 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 370 | self.assertEqual(deps, set(["somevar", "inverted"] + execs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 371 | |
| 372 | |
| 373 | def test_vardeps(self): |
| 374 | self.d.setVar("oe_libinstall", "echo test") |
| 375 | self.d.setVar("FOO", "foo=oe_libinstall; eval $foo") |
| 376 | self.d.setVarFlag("FOO", "vardeps", "oe_libinstall") |
| 377 | |
| 378 | deps, values = bb.data.build_dependencies("FOO", set(self.d.keys()), set(), set(), self.d) |
| 379 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 380 | self.assertEqual(deps, set(["oe_libinstall"])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 381 | |
| 382 | def test_vardeps_expand(self): |
| 383 | self.d.setVar("oe_libinstall", "echo test") |
| 384 | self.d.setVar("FOO", "foo=oe_libinstall; eval $foo") |
| 385 | self.d.setVarFlag("FOO", "vardeps", "${@'oe_libinstall'}") |
| 386 | |
| 387 | deps, values = bb.data.build_dependencies("FOO", set(self.d.keys()), set(), set(), self.d) |
| 388 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 389 | self.assertEqual(deps, set(["oe_libinstall"])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 390 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 391 | def test_contains_vardeps(self): |
| 392 | expr = '${@bb.utils.filter("TESTVAR", "somevalue anothervalue", d)} \ |
| 393 | ${@bb.utils.contains("TESTVAR", "testval testval2", "yetanothervalue", "", d)} \ |
| 394 | ${@bb.utils.contains("TESTVAR", "testval2 testval3", "blah", "", d)} \ |
| 395 | ${@bb.utils.contains_any("TESTVAR", "testval2 testval3", "lastone", "", d)}' |
| 396 | parsedvar = self.d.expandWithRefs(expr, None) |
| 397 | # Check contains |
| 398 | self.assertEqual(parsedvar.contains, {'TESTVAR': {'testval2 testval3', 'anothervalue', 'somevalue', 'testval testval2', 'testval2', 'testval3'}}) |
| 399 | # Check dependencies |
| 400 | self.d.setVar('ANOTHERVAR', expr) |
| 401 | self.d.setVar('TESTVAR', 'anothervalue testval testval2') |
| 402 | deps, values = bb.data.build_dependencies("ANOTHERVAR", set(self.d.keys()), set(), set(), self.d) |
| 403 | self.assertEqual(sorted(values.splitlines()), |
| 404 | sorted([expr, |
| 405 | 'TESTVAR{anothervalue} = Set', |
| 406 | 'TESTVAR{somevalue} = Unset', |
| 407 | 'TESTVAR{testval testval2} = Set', |
| 408 | 'TESTVAR{testval2 testval3} = Unset', |
| 409 | 'TESTVAR{testval2} = Set', |
| 410 | 'TESTVAR{testval3} = Unset' |
| 411 | ])) |
| 412 | # Check final value |
| 413 | self.assertEqual(self.d.getVar('ANOTHERVAR').split(), ['anothervalue', 'yetanothervalue', 'lastone']) |
| 414 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 415 | #Currently no wildcard support |
| 416 | #def test_vardeps_wildcards(self): |
| 417 | # self.d.setVar("oe_libinstall", "echo test") |
| 418 | # self.d.setVar("FOO", "foo=oe_libinstall; eval $foo") |
| 419 | # self.d.setVarFlag("FOO", "vardeps", "oe_*") |
| 420 | # self.assertEquals(deps, set(["oe_libinstall"])) |
| 421 | |
| 422 | |