blob: 1bc47405d98fc3f3dc2e27ea44f21c3bf44bfc0c [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001# 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
22import unittest
23import tempfile
24import logging
25import bb
26import os
27
28logger = logging.getLogger('BitBake.TestParse')
29
30import bb.parse
31import bb.data
32import bb.siggen
33
34class ParseTest(unittest.TestCase):
35
36 testfile = """
37A = "1"
38B = "2"
39do_install() {
40 echo "hello"
41}
42
43C = "3"
44"""
45
46 def setUp(self):
47 self.origdir = os.getcwd()
48 self.d = bb.data.init()
49 bb.parse.siggen = bb.siggen.init(self.d)
50
51 def tearDown(self):
52 os.chdir(self.origdir)
53
54 def parsehelper(self, content, suffix = ".bb"):
55
56 f = tempfile.NamedTemporaryFile(suffix = suffix)
57 f.write(bytes(content, "utf-8"))
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)['']
65 self.assertEqual(d.getVar("A"), "1")
66 self.assertEqual(d.getVar("B"), "2")
67 self.assertEqual(d.getVar("C"), "3")
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
75 unsettest = """
76A = "1"
77B = "2"
78B[flag] = "3"
79
80unset A
81unset 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)['']
87 self.assertEqual(d.getVar("A"), None)
88 self.assertEqual(d.getVarFlag("A","flag"), None)
89 self.assertEqual(d.getVar("B"), "2")
90
91 exporttest = """
92A = "a"
93export B = "b"
94export C
95exportD = "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
112
113 overridetest = """
114RRECOMMENDS_${PN} = "a"
115RRECOMMENDS_${PN}_libc = "b"
116OVERRIDES = "libc:${PN}"
117PN = "gtk+"
118"""
119
120 def test_parse_overrides(self):
121 f = self.parsehelper(self.overridetest)
122 d = bb.parse.handle(f.name, self.d)['']
123 self.assertEqual(d.getVar("RRECOMMENDS"), "b")
124 bb.data.expandKeys(d)
125 self.assertEqual(d.getVar("RRECOMMENDS"), "b")
126 d.setVar("RRECOMMENDS_gtk+", "c")
127 self.assertEqual(d.getVar("RRECOMMENDS"), "c")
128
129 overridetest2 = """
130EXTRA_OECONF = ""
131EXTRA_OECONF_class-target = "b"
132EXTRA_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")
140 self.assertEqual(d.getVar("EXTRA_OECONF"), "b c d")
141
142 overridetest3 = """
143DESCRIPTION = "A"
144DESCRIPTION_${PN}-dev = "${DESCRIPTION} B"
145PN = "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)
152 self.assertEqual(d.getVar("DESCRIPTION_bc-dev"), "A B")
153 d.setVar("DESCRIPTION", "E")
154 d.setVar("DESCRIPTION_bc-dev", "C D")
155 d.setVar("OVERRIDES", "bc-dev")
156 self.assertEqual(d.getVar("DESCRIPTION"), "C D")
157
158
159 classextend = """
160VAR_var_override1 = "B"
161EXTRA = ":override1"
162OVERRIDES = "nothing${EXTRA}"
163
164BBCLASSEXTEND = "###CLASS###"
165"""
166 classextend_bbclass = """
167EXTRA = ""
168python () {
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]
187 self.assertEqual(d1.getVar("VAR_var"), "B")
188 self.assertEqual(d2.getVar("VAR_var"), None)
189