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 = """ |
| 101 | RRECOMMENDS_${PN} = "a" |
| 102 | RRECOMMENDS_${PN}_libc = "b" |
| 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 | c124f4f | 2015-09-15 14:41:29 -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 = "" |
| 118 | EXTRA_OECONF_class-target = "b" |
| 119 | EXTRA_OECONF_append = " c" |
| 120 | """ |
| 121 | |
| 122 | def test_parse_overrides(self): |
| 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" |
| 131 | DESCRIPTION_${PN}-dev = "${DESCRIPTION} B" |
| 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) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -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") |
| 141 | d.setVar("DESCRIPTION_bc-dev", "C D") |
| 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 = """ |
| 147 | VAR_var_override1 = "B" |
| 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): |
| 167 | cls = self.parsehelper(self.classextend_bbclass, suffix=".bbclass") |
| 168 | #clsname = os.path.basename(cls.name).replace(".bbclass", "") |
| 169 | self.classextend = self.classextend.replace("###CLASS###", cls.name) |
| 170 | f = self.parsehelper(self.classextend) |
| 171 | alldata = bb.parse.handle(f.name, self.d) |
| 172 | d1 = alldata[''] |
| 173 | d2 = alldata[cls.name] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 174 | self.assertEqual(d1.getVar("VAR_var"), "B") |
| 175 | self.assertEqual(d2.getVar("VAR_var"), None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 176 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 177 | addtask_deltask = """ |
| 178 | addtask do_patch after do_foo after do_unpack before do_configure before do_compile |
| 179 | addtask do_fetch do_patch |
| 180 | |
Andrew Geissler | b7d2861 | 2020-07-24 16:15:54 -0500 | [diff] [blame] | 181 | MYVAR = "do_patch" |
| 182 | EMPTYVAR = "" |
| 183 | deltask do_fetch ${MYVAR} ${EMPTYVAR} |
| 184 | deltask ${EMPTYVAR} |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 185 | """ |
| 186 | def test_parse_addtask_deltask(self): |
| 187 | import sys |
| 188 | f = self.parsehelper(self.addtask_deltask) |
| 189 | d = bb.parse.handle(f.name, self.d)[''] |
| 190 | |
| 191 | stdout = sys.stdout.getvalue() |
| 192 | self.assertTrue("addtask contained multiple 'before' keywords" in stdout) |
| 193 | self.assertTrue("addtask contained multiple 'after' keywords" in stdout) |
| 194 | self.assertTrue('addtask ignored: " do_patch"' in stdout) |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 195 | #self.assertTrue('dependent task do_foo for do_patch does not exist' in stdout) |
| 196 | |