Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # BitBake Test for lib/bb/parse/ |
| 3 | # |
| 4 | # Copyright (C) 2015 Richard Purdie |
| 5 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 6 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 7 | # |
| 8 | |
| 9 | import unittest |
| 10 | import tempfile |
| 11 | import logging |
| 12 | import bb |
| 13 | import os |
| 14 | |
| 15 | logger = logging.getLogger('BitBake.TestParse') |
| 16 | |
| 17 | import bb.parse |
| 18 | import bb.data |
| 19 | import bb.siggen |
| 20 | |
| 21 | class ParseTest(unittest.TestCase): |
| 22 | |
| 23 | testfile = """ |
| 24 | A = "1" |
| 25 | B = "2" |
| 26 | do_install() { |
| 27 | echo "hello" |
| 28 | } |
| 29 | |
| 30 | C = "3" |
| 31 | """ |
| 32 | |
| 33 | def setUp(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 34 | self.origdir = os.getcwd() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | self.d = bb.data.init() |
| 36 | bb.parse.siggen = bb.siggen.init(self.d) |
| 37 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 38 | def tearDown(self): |
| 39 | os.chdir(self.origdir) |
| 40 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 41 | def parsehelper(self, content, suffix = ".bb"): |
| 42 | |
| 43 | f = tempfile.NamedTemporaryFile(suffix = suffix) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 44 | f.write(bytes(content, "utf-8")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | f.flush() |
| 46 | os.chdir(os.path.dirname(f.name)) |
| 47 | return f |
| 48 | |
| 49 | def test_parse_simple(self): |
| 50 | f = self.parsehelper(self.testfile) |
| 51 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 52 | self.assertEqual(d.getVar("A"), "1") |
| 53 | self.assertEqual(d.getVar("B"), "2") |
| 54 | self.assertEqual(d.getVar("C"), "3") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 55 | |
| 56 | def test_parse_incomplete_function(self): |
| 57 | testfileB = self.testfile.replace("}", "") |
| 58 | f = self.parsehelper(testfileB) |
| 59 | with self.assertRaises(bb.parse.ParseError): |
| 60 | d = bb.parse.handle(f.name, self.d)[''] |
| 61 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 62 | unsettest = """ |
| 63 | A = "1" |
| 64 | B = "2" |
| 65 | B[flag] = "3" |
| 66 | |
| 67 | unset A |
| 68 | unset B[flag] |
| 69 | """ |
| 70 | |
| 71 | def test_parse_unset(self): |
| 72 | f = self.parsehelper(self.unsettest) |
| 73 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 74 | self.assertEqual(d.getVar("A"), None) |
| 75 | self.assertEqual(d.getVarFlag("A","flag"), None) |
| 76 | self.assertEqual(d.getVar("B"), "2") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | |
| 78 | exporttest = """ |
| 79 | A = "a" |
| 80 | export B = "b" |
| 81 | export C |
| 82 | exportD = "d" |
| 83 | """ |
| 84 | |
| 85 | def test_parse_exports(self): |
| 86 | f = self.parsehelper(self.exporttest) |
| 87 | d = bb.parse.handle(f.name, self.d)[''] |
| 88 | self.assertEqual(d.getVar("A"), "a") |
| 89 | self.assertIsNone(d.getVarFlag("A", "export")) |
| 90 | self.assertEqual(d.getVar("B"), "b") |
| 91 | self.assertEqual(d.getVarFlag("B", "export"), 1) |
| 92 | self.assertIsNone(d.getVar("C")) |
| 93 | self.assertEqual(d.getVarFlag("C", "export"), 1) |
| 94 | self.assertIsNone(d.getVar("D")) |
| 95 | self.assertIsNone(d.getVarFlag("D", "export")) |
| 96 | self.assertEqual(d.getVar("exportD"), "d") |
| 97 | self.assertIsNone(d.getVarFlag("exportD", "export")) |
| 98 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 99 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | overridetest = """ |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 101 | RRECOMMENDS:${PN} = "a" |
| 102 | RRECOMMENDS:${PN}:libc = "b" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | OVERRIDES = "libc:${PN}" |
| 104 | PN = "gtk+" |
| 105 | """ |
| 106 | |
| 107 | def test_parse_overrides(self): |
| 108 | f = self.parsehelper(self.overridetest) |
| 109 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 110 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 111 | bb.data.expandKeys(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 112 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 113 | d.setVar("RRECOMMENDS:gtk+", "c") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 114 | self.assertEqual(d.getVar("RRECOMMENDS"), "c") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 115 | |
| 116 | overridetest2 = """ |
| 117 | EXTRA_OECONF = "" |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 118 | EXTRA_OECONF:class-target = "b" |
| 119 | EXTRA_OECONF:append = " c" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | """ |
| 121 | |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 122 | def test_parse_overrides2(self): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | f = self.parsehelper(self.overridetest2) |
| 124 | d = bb.parse.handle(f.name, self.d)[''] |
| 125 | d.appendVar("EXTRA_OECONF", " d") |
| 126 | d.setVar("OVERRIDES", "class-target") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | self.assertEqual(d.getVar("EXTRA_OECONF"), "b c d") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | overridetest3 = """ |
| 130 | DESCRIPTION = "A" |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 131 | DESCRIPTION:${PN}-dev = "${DESCRIPTION} B" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 132 | PN = "bc" |
| 133 | """ |
| 134 | |
| 135 | def test_parse_combinations(self): |
| 136 | f = self.parsehelper(self.overridetest3) |
| 137 | d = bb.parse.handle(f.name, self.d)[''] |
| 138 | bb.data.expandKeys(d) |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 139 | self.assertEqual(d.getVar("DESCRIPTION:bc-dev"), "A B") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | d.setVar("DESCRIPTION", "E") |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 141 | d.setVar("DESCRIPTION:bc-dev", "C D") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 142 | d.setVar("OVERRIDES", "bc-dev") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 143 | self.assertEqual(d.getVar("DESCRIPTION"), "C D") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | |
| 145 | |
| 146 | classextend = """ |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 147 | VAR_var:override1 = "B" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | EXTRA = ":override1" |
| 149 | OVERRIDES = "nothing${EXTRA}" |
| 150 | |
| 151 | BBCLASSEXTEND = "###CLASS###" |
| 152 | """ |
| 153 | classextend_bbclass = """ |
| 154 | EXTRA = "" |
| 155 | python () { |
| 156 | d.renameVar("VAR_var", "VAR_var2") |
| 157 | } |
| 158 | """ |
| 159 | |
| 160 | # |
| 161 | # Test based upon a real world data corruption issue. One |
| 162 | # data store changing a variable poked through into a different data |
| 163 | # store. This test case replicates that issue where the value 'B' would |
| 164 | # become unset/disappear. |
| 165 | # |
| 166 | def test_parse_classextend_contamination(self): |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 167 | self.d.setVar("__bbclasstype", "recipe") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | cls = self.parsehelper(self.classextend_bbclass, suffix=".bbclass") |
| 169 | #clsname = os.path.basename(cls.name).replace(".bbclass", "") |
| 170 | self.classextend = self.classextend.replace("###CLASS###", cls.name) |
| 171 | f = self.parsehelper(self.classextend) |
| 172 | alldata = bb.parse.handle(f.name, self.d) |
| 173 | d1 = alldata[''] |
| 174 | d2 = alldata[cls.name] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 175 | self.assertEqual(d1.getVar("VAR_var"), "B") |
| 176 | self.assertEqual(d2.getVar("VAR_var"), None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 178 | addtask_deltask = """ |
| 179 | addtask do_patch after do_foo after do_unpack before do_configure before do_compile |
| 180 | addtask do_fetch do_patch |
| 181 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 182 | MYVAR = "do_patch" |
| 183 | EMPTYVAR = "" |
| 184 | deltask do_fetch ${MYVAR} ${EMPTYVAR} |
| 185 | deltask ${EMPTYVAR} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 186 | """ |
| 187 | def test_parse_addtask_deltask(self): |
| 188 | import sys |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 189 | |
Andrew Geissler | 8f84068 | 2023-07-21 09:09:43 -0500 | [diff] [blame] | 190 | with self.assertLogs() as logs: |
| 191 | f = self.parsehelper(self.addtask_deltask) |
| 192 | d = bb.parse.handle(f.name, self.d)[''] |
| 193 | |
| 194 | output = "".join(logs.output) |
| 195 | self.assertTrue("addtask contained multiple 'before' keywords" in output) |
| 196 | self.assertTrue("addtask contained multiple 'after' keywords" in output) |
| 197 | self.assertTrue('addtask ignored: " do_patch"' in output) |
| 198 | #self.assertTrue('dependent task do_foo for do_patch does not exist' in output) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 199 | |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 200 | broken_multiline_comment = """ |
| 201 | # First line of comment \\ |
| 202 | # Second line of comment \\ |
| 203 | |
| 204 | """ |
| 205 | def test_parse_broken_multiline_comment(self): |
| 206 | f = self.parsehelper(self.broken_multiline_comment) |
| 207 | with self.assertRaises(bb.BBHandledException): |
| 208 | d = bb.parse.handle(f.name, self.d)[''] |
| 209 | |
| 210 | |
| 211 | comment_in_var = """ |
| 212 | VAR = " \\ |
| 213 | SOMEVAL \\ |
| 214 | # some comment \\ |
| 215 | SOMEOTHERVAL \\ |
| 216 | " |
| 217 | """ |
| 218 | def test_parse_comment_in_var(self): |
| 219 | f = self.parsehelper(self.comment_in_var) |
| 220 | with self.assertRaises(bb.BBHandledException): |
| 221 | d = bb.parse.handle(f.name, self.d)[''] |
| 222 | |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 223 | |
| 224 | at_sign_in_var_flag = """ |
| 225 | A[flag@.service] = "nonet" |
| 226 | B[flag@.target] = "ntb" |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 227 | C[f] = "flag" |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 228 | |
| 229 | unset A[flag@.service] |
| 230 | """ |
| 231 | def test_parse_at_sign_in_var_flag(self): |
| 232 | f = self.parsehelper(self.at_sign_in_var_flag) |
| 233 | d = bb.parse.handle(f.name, self.d)[''] |
| 234 | self.assertEqual(d.getVar("A"), None) |
| 235 | self.assertEqual(d.getVar("B"), None) |
| 236 | self.assertEqual(d.getVarFlag("A","flag@.service"), None) |
| 237 | self.assertEqual(d.getVarFlag("B","flag@.target"), "ntb") |
Patrick Williams | 8e7b46e | 2023-05-01 14:19:06 -0500 | [diff] [blame] | 238 | self.assertEqual(d.getVarFlag("C","f"), "flag") |
Andrew Geissler | fc113ea | 2023-03-31 09:59:46 -0500 | [diff] [blame] | 239 | |
| 240 | def test_parse_invalid_at_sign_in_var_flag(self): |
| 241 | invalid_at_sign = self.at_sign_in_var_flag.replace("B[f", "B[@f") |
| 242 | f = self.parsehelper(invalid_at_sign) |
| 243 | with self.assertRaises(bb.parse.ParseError): |
| 244 | d = bb.parse.handle(f.name, self.d)[''] |
| 245 | |
Patrick Williams | 705982a | 2024-01-12 09:51:57 -0600 | [diff] [blame] | 246 | export_function_recipe = """ |
| 247 | inherit someclass |
| 248 | """ |
| 249 | |
| 250 | export_function_recipe2 = """ |
| 251 | inherit someclass |
| 252 | |
| 253 | do_compile () { |
| 254 | false |
| 255 | } |
| 256 | |
| 257 | python do_compilepython () { |
| 258 | bb.note("Something else") |
| 259 | } |
| 260 | |
| 261 | """ |
| 262 | export_function_class = """ |
| 263 | someclass_do_compile() { |
| 264 | true |
| 265 | } |
| 266 | |
| 267 | python someclass_do_compilepython () { |
| 268 | bb.note("Something") |
| 269 | } |
| 270 | |
| 271 | EXPORT_FUNCTIONS do_compile do_compilepython |
| 272 | """ |
| 273 | |
| 274 | export_function_class2 = """ |
| 275 | secondclass_do_compile() { |
| 276 | true |
| 277 | } |
| 278 | |
| 279 | python secondclass_do_compilepython () { |
| 280 | bb.note("Something") |
| 281 | } |
| 282 | |
| 283 | EXPORT_FUNCTIONS do_compile do_compilepython |
| 284 | """ |
| 285 | |
| 286 | def test_parse_export_functions(self): |
| 287 | def check_function_flags(d): |
| 288 | self.assertEqual(d.getVarFlag("do_compile", "func"), 1) |
| 289 | self.assertEqual(d.getVarFlag("do_compilepython", "func"), 1) |
| 290 | self.assertEqual(d.getVarFlag("do_compile", "python"), None) |
| 291 | self.assertEqual(d.getVarFlag("do_compilepython", "python"), "1") |
| 292 | |
| 293 | with tempfile.TemporaryDirectory() as tempdir: |
| 294 | self.d.setVar("__bbclasstype", "recipe") |
| 295 | recipename = tempdir + "/recipe.bb" |
| 296 | os.makedirs(tempdir + "/classes") |
| 297 | with open(tempdir + "/classes/someclass.bbclass", "w") as f: |
| 298 | f.write(self.export_function_class) |
| 299 | f.flush() |
| 300 | with open(tempdir + "/classes/secondclass.bbclass", "w") as f: |
| 301 | f.write(self.export_function_class2) |
| 302 | f.flush() |
| 303 | |
| 304 | with open(recipename, "w") as f: |
| 305 | f.write(self.export_function_recipe) |
| 306 | f.flush() |
| 307 | os.chdir(tempdir) |
| 308 | d = bb.parse.handle(recipename, bb.data.createCopy(self.d))[''] |
| 309 | self.assertIn("someclass_do_compile", d.getVar("do_compile")) |
| 310 | self.assertIn("someclass_do_compilepython", d.getVar("do_compilepython")) |
| 311 | check_function_flags(d) |
| 312 | |
| 313 | recipename2 = tempdir + "/recipe2.bb" |
| 314 | with open(recipename2, "w") as f: |
| 315 | f.write(self.export_function_recipe2) |
| 316 | f.flush() |
| 317 | |
| 318 | d = bb.parse.handle(recipename2, bb.data.createCopy(self.d))[''] |
| 319 | self.assertNotIn("someclass_do_compile", d.getVar("do_compile")) |
| 320 | self.assertNotIn("someclass_do_compilepython", d.getVar("do_compilepython")) |
| 321 | self.assertIn("false", d.getVar("do_compile")) |
| 322 | self.assertIn("else", d.getVar("do_compilepython")) |
| 323 | check_function_flags(d) |
| 324 | |
| 325 | with open(recipename, "a+") as f: |
| 326 | f.write("\ninherit secondclass\n") |
| 327 | f.flush() |
| 328 | with open(recipename2, "a+") as f: |
| 329 | f.write("\ninherit secondclass\n") |
| 330 | f.flush() |
| 331 | |
| 332 | d = bb.parse.handle(recipename, bb.data.createCopy(self.d))[''] |
| 333 | self.assertIn("secondclass_do_compile", d.getVar("do_compile")) |
| 334 | self.assertIn("secondclass_do_compilepython", d.getVar("do_compilepython")) |
| 335 | check_function_flags(d) |
| 336 | |
| 337 | d = bb.parse.handle(recipename2, bb.data.createCopy(self.d))[''] |
| 338 | self.assertNotIn("someclass_do_compile", d.getVar("do_compile")) |
| 339 | self.assertNotIn("someclass_do_compilepython", d.getVar("do_compilepython")) |
| 340 | self.assertIn("false", d.getVar("do_compile")) |
| 341 | self.assertIn("else", d.getVar("do_compilepython")) |
| 342 | check_function_flags(d) |
| 343 | |