Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | # |
| 4 | # BitBake Test for lib/bb/parse/ |
| 5 | # |
| 6 | # Copyright (C) 2015 Richard Purdie |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | # |
| 21 | |
| 22 | import unittest |
| 23 | import tempfile |
| 24 | import logging |
| 25 | import bb |
| 26 | import os |
| 27 | |
| 28 | logger = logging.getLogger('BitBake.TestParse') |
| 29 | |
| 30 | import bb.parse |
| 31 | import bb.data |
| 32 | import bb.siggen |
| 33 | |
| 34 | class ParseTest(unittest.TestCase): |
| 35 | |
| 36 | testfile = """ |
| 37 | A = "1" |
| 38 | B = "2" |
| 39 | do_install() { |
| 40 | echo "hello" |
| 41 | } |
| 42 | |
| 43 | C = "3" |
| 44 | """ |
| 45 | |
| 46 | def setUp(self): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 47 | self.origdir = os.getcwd() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | self.d = bb.data.init() |
| 49 | bb.parse.siggen = bb.siggen.init(self.d) |
| 50 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 51 | def tearDown(self): |
| 52 | os.chdir(self.origdir) |
| 53 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | def parsehelper(self, content, suffix = ".bb"): |
| 55 | |
| 56 | f = tempfile.NamedTemporaryFile(suffix = suffix) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 57 | f.write(bytes(content, "utf-8")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | f.flush() |
| 59 | os.chdir(os.path.dirname(f.name)) |
| 60 | return f |
| 61 | |
| 62 | def test_parse_simple(self): |
| 63 | f = self.parsehelper(self.testfile) |
| 64 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 65 | self.assertEqual(d.getVar("A"), "1") |
| 66 | self.assertEqual(d.getVar("B"), "2") |
| 67 | self.assertEqual(d.getVar("C"), "3") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | |
| 69 | def test_parse_incomplete_function(self): |
| 70 | testfileB = self.testfile.replace("}", "") |
| 71 | f = self.parsehelper(testfileB) |
| 72 | with self.assertRaises(bb.parse.ParseError): |
| 73 | d = bb.parse.handle(f.name, self.d)[''] |
| 74 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 75 | unsettest = """ |
| 76 | A = "1" |
| 77 | B = "2" |
| 78 | B[flag] = "3" |
| 79 | |
| 80 | unset A |
| 81 | unset B[flag] |
| 82 | """ |
| 83 | |
| 84 | def test_parse_unset(self): |
| 85 | f = self.parsehelper(self.unsettest) |
| 86 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | self.assertEqual(d.getVar("A"), None) |
| 88 | self.assertEqual(d.getVarFlag("A","flag"), None) |
| 89 | self.assertEqual(d.getVar("B"), "2") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 90 | |
| 91 | exporttest = """ |
| 92 | A = "a" |
| 93 | export B = "b" |
| 94 | export C |
| 95 | exportD = "d" |
| 96 | """ |
| 97 | |
| 98 | def test_parse_exports(self): |
| 99 | f = self.parsehelper(self.exporttest) |
| 100 | d = bb.parse.handle(f.name, self.d)[''] |
| 101 | self.assertEqual(d.getVar("A"), "a") |
| 102 | self.assertIsNone(d.getVarFlag("A", "export")) |
| 103 | self.assertEqual(d.getVar("B"), "b") |
| 104 | self.assertEqual(d.getVarFlag("B", "export"), 1) |
| 105 | self.assertIsNone(d.getVar("C")) |
| 106 | self.assertEqual(d.getVarFlag("C", "export"), 1) |
| 107 | self.assertIsNone(d.getVar("D")) |
| 108 | self.assertIsNone(d.getVarFlag("D", "export")) |
| 109 | self.assertEqual(d.getVar("exportD"), "d") |
| 110 | self.assertIsNone(d.getVarFlag("exportD", "export")) |
| 111 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 112 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 113 | overridetest = """ |
| 114 | RRECOMMENDS_${PN} = "a" |
| 115 | RRECOMMENDS_${PN}_libc = "b" |
| 116 | OVERRIDES = "libc:${PN}" |
| 117 | PN = "gtk+" |
| 118 | """ |
| 119 | |
| 120 | def test_parse_overrides(self): |
| 121 | f = self.parsehelper(self.overridetest) |
| 122 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | bb.data.expandKeys(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 125 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | d.setVar("RRECOMMENDS_gtk+", "c") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 127 | self.assertEqual(d.getVar("RRECOMMENDS"), "c") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 128 | |
| 129 | overridetest2 = """ |
| 130 | EXTRA_OECONF = "" |
| 131 | EXTRA_OECONF_class-target = "b" |
| 132 | EXTRA_OECONF_append = " c" |
| 133 | """ |
| 134 | |
| 135 | def test_parse_overrides(self): |
| 136 | f = self.parsehelper(self.overridetest2) |
| 137 | d = bb.parse.handle(f.name, self.d)[''] |
| 138 | d.appendVar("EXTRA_OECONF", " d") |
| 139 | d.setVar("OVERRIDES", "class-target") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 140 | self.assertEqual(d.getVar("EXTRA_OECONF"), "b c d") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
| 142 | overridetest3 = """ |
| 143 | DESCRIPTION = "A" |
| 144 | DESCRIPTION_${PN}-dev = "${DESCRIPTION} B" |
| 145 | PN = "bc" |
| 146 | """ |
| 147 | |
| 148 | def test_parse_combinations(self): |
| 149 | f = self.parsehelper(self.overridetest3) |
| 150 | d = bb.parse.handle(f.name, self.d)[''] |
| 151 | bb.data.expandKeys(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 152 | self.assertEqual(d.getVar("DESCRIPTION_bc-dev"), "A B") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | d.setVar("DESCRIPTION", "E") |
| 154 | d.setVar("DESCRIPTION_bc-dev", "C D") |
| 155 | d.setVar("OVERRIDES", "bc-dev") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 156 | self.assertEqual(d.getVar("DESCRIPTION"), "C D") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 157 | |
| 158 | |
| 159 | classextend = """ |
| 160 | VAR_var_override1 = "B" |
| 161 | EXTRA = ":override1" |
| 162 | OVERRIDES = "nothing${EXTRA}" |
| 163 | |
| 164 | BBCLASSEXTEND = "###CLASS###" |
| 165 | """ |
| 166 | classextend_bbclass = """ |
| 167 | EXTRA = "" |
| 168 | python () { |
| 169 | d.renameVar("VAR_var", "VAR_var2") |
| 170 | } |
| 171 | """ |
| 172 | |
| 173 | # |
| 174 | # Test based upon a real world data corruption issue. One |
| 175 | # data store changing a variable poked through into a different data |
| 176 | # store. This test case replicates that issue where the value 'B' would |
| 177 | # become unset/disappear. |
| 178 | # |
| 179 | def test_parse_classextend_contamination(self): |
| 180 | cls = self.parsehelper(self.classextend_bbclass, suffix=".bbclass") |
| 181 | #clsname = os.path.basename(cls.name).replace(".bbclass", "") |
| 182 | self.classextend = self.classextend.replace("###CLASS###", cls.name) |
| 183 | f = self.parsehelper(self.classextend) |
| 184 | alldata = bb.parse.handle(f.name, self.d) |
| 185 | d1 = alldata[''] |
| 186 | d2 = alldata[cls.name] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 187 | self.assertEqual(d1.getVar("VAR_var"), "B") |
| 188 | self.assertEqual(d2.getVar("VAR_var"), None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 189 | |