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): |
| 47 | self.d = bb.data.init() |
| 48 | bb.parse.siggen = bb.siggen.init(self.d) |
| 49 | |
| 50 | def parsehelper(self, content, suffix = ".bb"): |
| 51 | |
| 52 | f = tempfile.NamedTemporaryFile(suffix = suffix) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 53 | f.write(bytes(content, "utf-8")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | f.flush() |
| 55 | os.chdir(os.path.dirname(f.name)) |
| 56 | return f |
| 57 | |
| 58 | def test_parse_simple(self): |
| 59 | f = self.parsehelper(self.testfile) |
| 60 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 61 | self.assertEqual(d.getVar("A"), "1") |
| 62 | self.assertEqual(d.getVar("B"), "2") |
| 63 | self.assertEqual(d.getVar("C"), "3") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | |
| 65 | def test_parse_incomplete_function(self): |
| 66 | testfileB = self.testfile.replace("}", "") |
| 67 | f = self.parsehelper(testfileB) |
| 68 | with self.assertRaises(bb.parse.ParseError): |
| 69 | d = bb.parse.handle(f.name, self.d)[''] |
| 70 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 71 | unsettest = """ |
| 72 | A = "1" |
| 73 | B = "2" |
| 74 | B[flag] = "3" |
| 75 | |
| 76 | unset A |
| 77 | unset B[flag] |
| 78 | """ |
| 79 | |
| 80 | def test_parse_unset(self): |
| 81 | f = self.parsehelper(self.unsettest) |
| 82 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 83 | self.assertEqual(d.getVar("A"), None) |
| 84 | self.assertEqual(d.getVarFlag("A","flag"), None) |
| 85 | self.assertEqual(d.getVar("B"), "2") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 86 | |
| 87 | exporttest = """ |
| 88 | A = "a" |
| 89 | export B = "b" |
| 90 | export C |
| 91 | exportD = "d" |
| 92 | """ |
| 93 | |
| 94 | def test_parse_exports(self): |
| 95 | f = self.parsehelper(self.exporttest) |
| 96 | d = bb.parse.handle(f.name, self.d)[''] |
| 97 | self.assertEqual(d.getVar("A"), "a") |
| 98 | self.assertIsNone(d.getVarFlag("A", "export")) |
| 99 | self.assertEqual(d.getVar("B"), "b") |
| 100 | self.assertEqual(d.getVarFlag("B", "export"), 1) |
| 101 | self.assertIsNone(d.getVar("C")) |
| 102 | self.assertEqual(d.getVarFlag("C", "export"), 1) |
| 103 | self.assertIsNone(d.getVar("D")) |
| 104 | self.assertIsNone(d.getVarFlag("D", "export")) |
| 105 | self.assertEqual(d.getVar("exportD"), "d") |
| 106 | self.assertIsNone(d.getVarFlag("exportD", "export")) |
| 107 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 108 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 109 | overridetest = """ |
| 110 | RRECOMMENDS_${PN} = "a" |
| 111 | RRECOMMENDS_${PN}_libc = "b" |
| 112 | OVERRIDES = "libc:${PN}" |
| 113 | PN = "gtk+" |
| 114 | """ |
| 115 | |
| 116 | def test_parse_overrides(self): |
| 117 | f = self.parsehelper(self.overridetest) |
| 118 | d = bb.parse.handle(f.name, self.d)[''] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 119 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 | bb.data.expandKeys(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | self.assertEqual(d.getVar("RRECOMMENDS"), "b") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | d.setVar("RRECOMMENDS_gtk+", "c") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 123 | self.assertEqual(d.getVar("RRECOMMENDS"), "c") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | |
| 125 | overridetest2 = """ |
| 126 | EXTRA_OECONF = "" |
| 127 | EXTRA_OECONF_class-target = "b" |
| 128 | EXTRA_OECONF_append = " c" |
| 129 | """ |
| 130 | |
| 131 | def test_parse_overrides(self): |
| 132 | f = self.parsehelper(self.overridetest2) |
| 133 | d = bb.parse.handle(f.name, self.d)[''] |
| 134 | d.appendVar("EXTRA_OECONF", " d") |
| 135 | d.setVar("OVERRIDES", "class-target") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 136 | self.assertEqual(d.getVar("EXTRA_OECONF"), "b c d") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 137 | |
| 138 | overridetest3 = """ |
| 139 | DESCRIPTION = "A" |
| 140 | DESCRIPTION_${PN}-dev = "${DESCRIPTION} B" |
| 141 | PN = "bc" |
| 142 | """ |
| 143 | |
| 144 | def test_parse_combinations(self): |
| 145 | f = self.parsehelper(self.overridetest3) |
| 146 | d = bb.parse.handle(f.name, self.d)[''] |
| 147 | bb.data.expandKeys(d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 148 | self.assertEqual(d.getVar("DESCRIPTION_bc-dev"), "A B") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 149 | d.setVar("DESCRIPTION", "E") |
| 150 | d.setVar("DESCRIPTION_bc-dev", "C D") |
| 151 | d.setVar("OVERRIDES", "bc-dev") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 152 | self.assertEqual(d.getVar("DESCRIPTION"), "C D") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 153 | |
| 154 | |
| 155 | classextend = """ |
| 156 | VAR_var_override1 = "B" |
| 157 | EXTRA = ":override1" |
| 158 | OVERRIDES = "nothing${EXTRA}" |
| 159 | |
| 160 | BBCLASSEXTEND = "###CLASS###" |
| 161 | """ |
| 162 | classextend_bbclass = """ |
| 163 | EXTRA = "" |
| 164 | python () { |
| 165 | d.renameVar("VAR_var", "VAR_var2") |
| 166 | } |
| 167 | """ |
| 168 | |
| 169 | # |
| 170 | # Test based upon a real world data corruption issue. One |
| 171 | # data store changing a variable poked through into a different data |
| 172 | # store. This test case replicates that issue where the value 'B' would |
| 173 | # become unset/disappear. |
| 174 | # |
| 175 | def test_parse_classextend_contamination(self): |
| 176 | cls = self.parsehelper(self.classextend_bbclass, suffix=".bbclass") |
| 177 | #clsname = os.path.basename(cls.name).replace(".bbclass", "") |
| 178 | self.classextend = self.classextend.replace("###CLASS###", cls.name) |
| 179 | f = self.parsehelper(self.classextend) |
| 180 | alldata = bb.parse.handle(f.name, self.d) |
| 181 | d1 = alldata[''] |
| 182 | d2 = alldata[cls.name] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 183 | self.assertEqual(d1.getVar("VAR_var"), "B") |
| 184 | self.assertEqual(d2.getVar("VAR_var"), None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 185 | |