blob: 510dae6badfd28ad9dbc83c95b70815988782614 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005import os
6import shutil
7import tempfile
8import urllib.parse
9
10from oeqa.utils.commands import runCmd, bitbake, get_bb_var
11from oeqa.utils.commands import get_bb_vars, create_temp_layer
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from oeqa.selftest.cases import devtool
13
14templayerdir = None
15
16def setUpModule():
17 global templayerdir
18 templayerdir = tempfile.mkdtemp(prefix='recipetoolqa')
19 create_temp_layer(templayerdir, 'selftestrecipetool')
20 runCmd('bitbake-layers add-layer %s' % templayerdir)
21
22
23def tearDownModule():
24 runCmd('bitbake-layers remove-layer %s' % templayerdir, ignore_status=True)
25 runCmd('rm -rf %s' % templayerdir)
26
27
Andrew Geissler595f6302022-01-24 19:11:47 +000028class RecipetoolBase(devtool.DevtoolTestCase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029
30 def setUpLocal(self):
31 super(RecipetoolBase, self).setUpLocal()
32 self.templayerdir = templayerdir
33 self.tempdir = tempfile.mkdtemp(prefix='recipetoolqa')
34 self.track_for_cleanup(self.tempdir)
35 self.testfile = os.path.join(self.tempdir, 'testfile')
36 with open(self.testfile, 'w') as f:
37 f.write('Test file\n')
38
39 def tearDownLocal(self):
40 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
41 super(RecipetoolBase, self).tearDownLocal()
42
43 def _try_recipetool_appendcmd(self, cmd, testrecipe, expectedfiles, expectedlines=None):
44 result = runCmd(cmd)
45 self.assertNotIn('Traceback', result.output)
46
47 # Check the bbappend was created and applies properly
48 recipefile = get_bb_var('FILE', testrecipe)
49 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
50
51 # Check the bbappend contents
52 if expectedlines is not None:
53 with open(bbappendfile, 'r') as f:
54 self.assertEqual(expectedlines, f.readlines(), "Expected lines are not present in %s" % bbappendfile)
55
56 # Check file was copied
57 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
58 for expectedfile in expectedfiles:
59 self.assertTrue(os.path.isfile(os.path.join(filesdir, expectedfile)), 'Expected file %s to be copied next to bbappend, but it wasn\'t' % expectedfile)
60
61 # Check no other files created
62 createdfiles = []
63 for root, _, files in os.walk(filesdir):
64 for f in files:
65 createdfiles.append(os.path.relpath(os.path.join(root, f), filesdir))
66 self.assertTrue(sorted(createdfiles), sorted(expectedfiles))
67
68 return bbappendfile, result.output
69
70
Andrew Geissler595f6302022-01-24 19:11:47 +000071class RecipetoolAppendTests(RecipetoolBase):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072
73 @classmethod
74 def setUpClass(cls):
Andrew Geissler595f6302022-01-24 19:11:47 +000075 super(RecipetoolAppendTests, cls).setUpClass()
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 # Ensure we have the right data in shlibs/pkgdata
77 cls.logger.info('Running bitbake to generate pkgdata')
78 bitbake('-c packagedata base-files coreutils busybox selftest-recipetool-appendfile')
Andrew Geissler595f6302022-01-24 19:11:47 +000079 bb_vars = get_bb_vars(['COREBASE'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080 cls.corebase = bb_vars['COREBASE']
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081
82 def _try_recipetool_appendfile(self, testrecipe, destfile, newfile, options, expectedlines, expectedfiles):
83 cmd = 'recipetool appendfile %s %s %s %s' % (self.templayerdir, destfile, newfile, options)
84 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
85
86 def _try_recipetool_appendfile_fail(self, destfile, newfile, checkerror):
87 cmd = 'recipetool appendfile %s %s %s' % (self.templayerdir, destfile, newfile)
88 result = runCmd(cmd, ignore_status=True)
89 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
90 self.assertNotIn('Traceback', result.output)
91 for errorstr in checkerror:
92 self.assertIn(errorstr, result.output)
93
Brad Bishopd7bf8c12018-02-25 22:55:05 -050094 def test_recipetool_appendfile_basic(self):
95 # Basic test
Patrick Williams213cb262021-08-07 19:21:33 -050096 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 '\n']
98 _, output = self._try_recipetool_appendfile('base-files', '/etc/motd', self.testfile, '', expectedlines, ['motd'])
99 self.assertNotIn('WARNING: ', output)
100
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500101 def test_recipetool_appendfile_invalid(self):
102 # Test some commands that should error
103 self._try_recipetool_appendfile_fail('/etc/passwd', self.testfile, ['ERROR: /etc/passwd cannot be handled by this tool', 'useradd', 'extrausers'])
104 self._try_recipetool_appendfile_fail('/etc/timestamp', self.testfile, ['ERROR: /etc/timestamp cannot be handled by this tool'])
105 self._try_recipetool_appendfile_fail('/dev/console', self.testfile, ['ERROR: /dev/console cannot be handled by this tool'])
106
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 def test_recipetool_appendfile_alternatives(self):
108 # Now try with a file we know should be an alternative
109 # (this is very much a fake example, but one we know is reliably an alternative)
110 self._try_recipetool_appendfile_fail('/bin/ls', self.testfile, ['ERROR: File /bin/ls is an alternative possibly provided by the following recipes:', 'coreutils', 'busybox'])
111 # Need a test file - should be executable
112 testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
113 testfile2name = os.path.basename(testfile2)
Patrick Williams213cb262021-08-07 19:21:33 -0500114 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 '\n',
116 'SRC_URI += "file://%s"\n' % testfile2name,
117 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500118 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 ' install -d ${D}${base_bindir}\n',
120 ' install -m 0755 ${WORKDIR}/%s ${D}${base_bindir}/ls\n' % testfile2name,
121 '}\n']
122 self._try_recipetool_appendfile('coreutils', '/bin/ls', testfile2, '-r coreutils', expectedlines, [testfile2name])
123 # Now try bbappending the same file again, contents should not change
124 bbappendfile, _ = self._try_recipetool_appendfile('coreutils', '/bin/ls', self.testfile, '-r coreutils', expectedlines, [testfile2name])
125 # But file should have
126 copiedfile = os.path.join(os.path.dirname(bbappendfile), 'coreutils', testfile2name)
127 result = runCmd('diff -q %s %s' % (testfile2, copiedfile), ignore_status=True)
128 self.assertNotEqual(result.status, 0, 'New file should have been copied but was not %s' % result.output)
129
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130 def test_recipetool_appendfile_binary(self):
131 # Try appending a binary file
132 # /bin/ls can be a symlink to /usr/bin/ls
133 ls = os.path.realpath("/bin/ls")
134 result = runCmd('recipetool appendfile %s /bin/ls %s -r coreutils' % (self.templayerdir, ls))
135 self.assertIn('WARNING: ', result.output)
136 self.assertIn('is a binary', result.output)
137
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500138 def test_recipetool_appendfile_add(self):
139 # Try arbitrary file add to a recipe
Patrick Williams213cb262021-08-07 19:21:33 -0500140 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500141 '\n',
142 'SRC_URI += "file://testfile"\n',
143 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500144 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500145 ' install -d ${D}${datadir}\n',
146 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
147 '}\n']
148 self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase', expectedlines, ['testfile'])
149 # Try adding another file, this time where the source file is executable
150 # (so we're testing that, plus modifying an existing bbappend)
151 testfile2 = os.path.join(self.corebase, 'oe-init-build-env')
152 testfile2name = os.path.basename(testfile2)
Patrick Williams213cb262021-08-07 19:21:33 -0500153 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500154 '\n',
155 'SRC_URI += "file://testfile \\\n',
156 ' file://%s \\\n' % testfile2name,
157 ' "\n',
158 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500159 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500160 ' install -d ${D}${datadir}\n',
161 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
162 ' install -m 0755 ${WORKDIR}/%s ${D}${datadir}/scriptname\n' % testfile2name,
163 '}\n']
164 self._try_recipetool_appendfile('netbase', '/usr/share/scriptname', testfile2, '-r netbase', expectedlines, ['testfile', testfile2name])
165
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500166 def test_recipetool_appendfile_add_bindir(self):
167 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
Patrick Williams213cb262021-08-07 19:21:33 -0500168 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500169 '\n',
170 'SRC_URI += "file://testfile"\n',
171 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500172 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500173 ' install -d ${D}${bindir}\n',
174 ' install -m 0755 ${WORKDIR}/testfile ${D}${bindir}/selftest-recipetool-testbin\n',
175 '}\n']
176 _, output = self._try_recipetool_appendfile('netbase', '/usr/bin/selftest-recipetool-testbin', self.testfile, '-r netbase', expectedlines, ['testfile'])
177 self.assertNotIn('WARNING: ', output)
178
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 def test_recipetool_appendfile_add_machine(self):
180 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
Patrick Williams213cb262021-08-07 19:21:33 -0500181 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500182 '\n',
183 'PACKAGE_ARCH = "${MACHINE_ARCH}"\n',
184 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500185 'SRC_URI:append:mymachine = " file://testfile"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500186 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500187 'do_install:append:mymachine() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500188 ' install -d ${D}${datadir}\n',
189 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
190 '}\n']
191 _, output = self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase -m mymachine', expectedlines, ['mymachine/testfile'])
192 self.assertNotIn('WARNING: ', output)
193
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500194 def test_recipetool_appendfile_orig(self):
195 # A file that's in SRC_URI and in do_install with the same name
Patrick Williams213cb262021-08-07 19:21:33 -0500196 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500197 '\n']
198 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-orig', self.testfile, '', expectedlines, ['selftest-replaceme-orig'])
199 self.assertNotIn('WARNING: ', output)
200
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500201 def test_recipetool_appendfile_todir(self):
202 # A file that's in SRC_URI and in do_install with destination directory rather than file
Patrick Williams213cb262021-08-07 19:21:33 -0500203 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500204 '\n']
205 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-todir', self.testfile, '', expectedlines, ['selftest-replaceme-todir'])
206 self.assertNotIn('WARNING: ', output)
207
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500208 def test_recipetool_appendfile_renamed(self):
209 # A file that's in SRC_URI with a different name to the destination file
Patrick Williams213cb262021-08-07 19:21:33 -0500210 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500211 '\n']
212 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-renamed', self.testfile, '', expectedlines, ['file1'])
213 self.assertNotIn('WARNING: ', output)
214
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500215 def test_recipetool_appendfile_subdir(self):
216 # A file that's in SRC_URI in a subdir
Patrick Williams213cb262021-08-07 19:21:33 -0500217 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500218 '\n',
219 'SRC_URI += "file://testfile"\n',
220 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500221 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500222 ' install -d ${D}${datadir}\n',
223 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-subdir\n',
224 '}\n']
225 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-subdir', self.testfile, '', expectedlines, ['testfile'])
226 self.assertNotIn('WARNING: ', output)
227
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500228 def test_recipetool_appendfile_inst_glob(self):
229 # A file that's in do_install as a glob
Patrick Williams213cb262021-08-07 19:21:33 -0500230 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500231 '\n']
232 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-globfile'])
233 self.assertNotIn('WARNING: ', output)
234
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500235 def test_recipetool_appendfile_inst_todir_glob(self):
236 # A file that's in do_install as a glob with destination as a directory
Patrick Williams213cb262021-08-07 19:21:33 -0500237 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500238 '\n']
239 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-todir-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-todir-globfile'])
240 self.assertNotIn('WARNING: ', output)
241
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500242 def test_recipetool_appendfile_patch(self):
243 # A file that's added by a patch in SRC_URI
Patrick Williams213cb262021-08-07 19:21:33 -0500244 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500245 '\n',
246 'SRC_URI += "file://testfile"\n',
247 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500248 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500249 ' install -d ${D}${sysconfdir}\n',
250 ' install -m 0644 ${WORKDIR}/testfile ${D}${sysconfdir}/selftest-replaceme-patched\n',
251 '}\n']
252 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/etc/selftest-replaceme-patched', self.testfile, '', expectedlines, ['testfile'])
253 for line in output.splitlines():
254 if 'WARNING: ' in line:
255 self.assertIn('add-file.patch', line, 'Unexpected warning found in output:\n%s' % line)
256 break
257 else:
258 self.fail('Patch warning not found in output:\n%s' % output)
259
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500260 def test_recipetool_appendfile_script(self):
261 # Now, a file that's in SRC_URI but installed by a script (so no mention in do_install)
Patrick Williams213cb262021-08-07 19:21:33 -0500262 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500263 '\n',
264 'SRC_URI += "file://testfile"\n',
265 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500266 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500267 ' install -d ${D}${datadir}\n',
268 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-scripted\n',
269 '}\n']
270 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-scripted', self.testfile, '', expectedlines, ['testfile'])
271 self.assertNotIn('WARNING: ', output)
272
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500273 def test_recipetool_appendfile_inst_func(self):
274 # A file that's installed from a function called by do_install
Patrick Williams213cb262021-08-07 19:21:33 -0500275 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500276 '\n']
277 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-func', self.testfile, '', expectedlines, ['selftest-replaceme-inst-func'])
278 self.assertNotIn('WARNING: ', output)
279
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500280 def test_recipetool_appendfile_postinstall(self):
281 # A file that's created by a postinstall script (and explicitly mentioned in it)
282 # First try without specifying recipe
283 self._try_recipetool_appendfile_fail('/usr/share/selftest-replaceme-postinst', self.testfile, ['File /usr/share/selftest-replaceme-postinst may be written out in a pre/postinstall script of the following recipes:', 'selftest-recipetool-appendfile'])
284 # Now specify recipe
Patrick Williams213cb262021-08-07 19:21:33 -0500285 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500286 '\n',
287 'SRC_URI += "file://testfile"\n',
288 '\n',
Patrick Williams213cb262021-08-07 19:21:33 -0500289 'do_install:append() {\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500290 ' install -d ${D}${datadir}\n',
291 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-postinst\n',
292 '}\n']
293 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-postinst', self.testfile, '-r selftest-recipetool-appendfile', expectedlines, ['testfile'])
294
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500295 def test_recipetool_appendfile_extlayer(self):
296 # Try creating a bbappend in a layer that's not in bblayers.conf and has a different structure
297 exttemplayerdir = os.path.join(self.tempdir, 'extlayer')
298 self._create_temp_layer(exttemplayerdir, False, 'oeselftestextlayer', recipepathspec='metadata/recipes/recipes-*/*')
299 result = runCmd('recipetool appendfile %s /usr/share/selftest-replaceme-orig %s' % (exttemplayerdir, self.testfile))
300 self.assertNotIn('Traceback', result.output)
301 createdfiles = []
302 for root, _, files in os.walk(exttemplayerdir):
303 for f in files:
304 createdfiles.append(os.path.relpath(os.path.join(root, f), exttemplayerdir))
305 createdfiles.remove('conf/layer.conf')
306 expectedfiles = ['metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile.bbappend',
307 'metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile/selftest-replaceme-orig']
308 self.assertEqual(sorted(createdfiles), sorted(expectedfiles))
309
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500310 def test_recipetool_appendfile_wildcard(self):
311
312 def try_appendfile_wc(options):
313 result = runCmd('recipetool appendfile %s /etc/profile %s %s' % (self.templayerdir, self.testfile, options))
314 self.assertNotIn('Traceback', result.output)
315 bbappendfile = None
316 for root, _, files in os.walk(self.templayerdir):
317 for f in files:
318 if f.endswith('.bbappend'):
319 bbappendfile = f
320 break
321 if not bbappendfile:
322 self.fail('No bbappend file created')
323 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
324 return bbappendfile
325
326 # Check without wildcard option
327 recipefn = os.path.basename(get_bb_var('FILE', 'base-files'))
328 filename = try_appendfile_wc('')
329 self.assertEqual(filename, recipefn.replace('.bb', '.bbappend'))
330 # Now check with wildcard option
331 filename = try_appendfile_wc('-w')
332 self.assertEqual(filename, recipefn.split('_')[0] + '_%.bbappend')
333
Andrew Geissler595f6302022-01-24 19:11:47 +0000334
335class RecipetoolCreateTests(RecipetoolBase):
336
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500337 def test_recipetool_create(self):
338 # Try adding a recipe
339 tempsrc = os.path.join(self.tempdir, 'srctree')
340 os.makedirs(tempsrc)
341 recipefile = os.path.join(self.tempdir, 'logrotate_3.12.3.bb')
342 srcuri = 'https://github.com/logrotate/logrotate/releases/download/3.12.3/logrotate-3.12.3.tar.xz'
343 result = runCmd('recipetool create -o %s %s -x %s' % (recipefile, srcuri, tempsrc))
344 self.assertTrue(os.path.isfile(recipefile))
345 checkvars = {}
Andrew Geissler9aee5002022-03-30 16:27:02 +0000346 checkvars['LICENSE'] = 'GPL-2.0-only'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500347 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'
348 checkvars['SRC_URI'] = 'https://github.com/logrotate/logrotate/releases/download/${PV}/logrotate-${PV}.tar.xz'
349 checkvars['SRC_URI[md5sum]'] = 'a560c57fac87c45b2fc17406cdf79288'
350 checkvars['SRC_URI[sha256sum]'] = '2e6a401cac9024db2288297e3be1a8ab60e7401ba8e91225218aaf4a27e82a07'
351 self._test_recipe_contents(recipefile, checkvars, [])
352
Andrew Geissler595f6302022-01-24 19:11:47 +0000353 def test_recipetool_create_autotools(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500354 if 'x11' not in get_bb_var('DISTRO_FEATURES'):
355 self.skipTest('Test requires x11 as distro feature')
356 # Ensure we have the right data in shlibs/pkgdata
357 bitbake('libpng pango libx11 libxext jpeg libcheck')
358 # Try adding a recipe
359 tempsrc = os.path.join(self.tempdir, 'srctree')
360 os.makedirs(tempsrc)
361 recipefile = os.path.join(self.tempdir, 'libmatchbox.bb')
362 srcuri = 'git://git.yoctoproject.org/libmatchbox'
363 result = runCmd(['recipetool', 'create', '-o', recipefile, srcuri + ";rev=9f7cf8895ae2d39c465c04cc78e918c157420269", '-x', tempsrc])
364 self.assertTrue(os.path.isfile(recipefile), 'recipetool did not create recipe file; output:\n%s' % result.output)
365 checkvars = {}
Andrew Geissler9aee5002022-03-30 16:27:02 +0000366 checkvars['LICENSE'] = 'LGPL-2.1-only'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500367 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=7fbc338309ac38fefcd64b04bb903e34'
368 checkvars['S'] = '${WORKDIR}/git'
369 checkvars['PV'] = '1.11+git${SRCPV}'
Andrew Geissler595f6302022-01-24 19:11:47 +0000370 checkvars['SRC_URI'] = srcuri + ';branch=master'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500371 checkvars['DEPENDS'] = set(['libcheck', 'libjpeg-turbo', 'libpng', 'libx11', 'libxext', 'pango'])
372 inherits = ['autotools', 'pkgconfig']
373 self._test_recipe_contents(recipefile, checkvars, inherits)
374
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500375 def test_recipetool_create_simple(self):
376 # Try adding a recipe
377 temprecipe = os.path.join(self.tempdir, 'recipe')
378 os.makedirs(temprecipe)
Andrew Geissler5f350902021-07-23 13:09:54 -0400379 pv = '1.7.4.1'
Andrew Geissler9aee5002022-03-30 16:27:02 +0000380 srcuri = 'http://www.dest-unreach.org/socat/download/Archive/socat-%s.tar.bz2' % pv
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500381 result = runCmd('recipetool create %s -o %s' % (srcuri, temprecipe))
382 dirlist = os.listdir(temprecipe)
383 if len(dirlist) > 1:
384 self.fail('recipetool created more than just one file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
385 if len(dirlist) < 1 or not os.path.isfile(os.path.join(temprecipe, dirlist[0])):
386 self.fail('recipetool did not create recipe file; output:\n%s\ndirlist:\n%s' % (result.output, str(dirlist)))
387 self.assertEqual(dirlist[0], 'socat_%s.bb' % pv, 'Recipe file incorrectly named')
388 checkvars = {}
Andrew Geissler9aee5002022-03-30 16:27:02 +0000389 checkvars['LICENSE'] = set(['Unknown', 'GPL-2.0-only'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500390 checkvars['LIC_FILES_CHKSUM'] = set(['file://COPYING.OpenSSL;md5=5c9bccc77f67a8328ef4ebaf468116f4', 'file://COPYING;md5=b234ee4d69f5fce4486a80fdaf4a4263'])
391 # We don't check DEPENDS since they are variable for this recipe depending on what's in the sysroot
392 checkvars['S'] = None
393 checkvars['SRC_URI'] = srcuri.replace(pv, '${PV}')
394 inherits = ['autotools']
395 self._test_recipe_contents(os.path.join(temprecipe, dirlist[0]), checkvars, inherits)
396
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500397 def test_recipetool_create_cmake(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500398 temprecipe = os.path.join(self.tempdir, 'recipe')
399 os.makedirs(temprecipe)
Brad Bishop96ff1982019-08-19 13:50:42 -0400400 recipefile = os.path.join(temprecipe, 'taglib_1.11.1.bb')
401 srcuri = 'http://taglib.github.io/releases/taglib-1.11.1.tar.gz'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500402 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
403 self.assertTrue(os.path.isfile(recipefile))
404 checkvars = {}
Andrew Geissler9aee5002022-03-30 16:27:02 +0000405 checkvars['LICENSE'] = set(['LGPL-2.1-only', 'MPL-1.1-only'])
Brad Bishop96ff1982019-08-19 13:50:42 -0400406 checkvars['SRC_URI'] = 'http://taglib.github.io/releases/taglib-${PV}.tar.gz'
407 checkvars['SRC_URI[md5sum]'] = 'cee7be0ccfc892fa433d6c837df9522a'
408 checkvars['SRC_URI[sha256sum]'] = 'b6d1a5a610aae6ff39d93de5efd0fdc787aa9e9dc1e7026fa4c961b26563526b'
409 checkvars['DEPENDS'] = set(['boost', 'zlib'])
410 inherits = ['cmake']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 self._test_recipe_contents(recipefile, checkvars, inherits)
412
Andrew Geissler82c905d2020-04-13 13:39:40 -0500413 def test_recipetool_create_npm(self):
Andrew Geisslerf0343792020-11-18 10:42:21 -0600414 collections = get_bb_var('BBFILE_COLLECTIONS').split()
415 if "openembedded-layer" not in collections:
416 self.skipTest("Test needs meta-oe for nodejs")
417
Andrew Geissler82c905d2020-04-13 13:39:40 -0500418 temprecipe = os.path.join(self.tempdir, 'recipe')
419 os.makedirs(temprecipe)
420 recipefile = os.path.join(temprecipe, 'savoirfairelinux-node-server-example_1.0.0.bb')
421 shrinkwrap = os.path.join(temprecipe, 'savoirfairelinux-node-server-example', 'npm-shrinkwrap.json')
422 srcuri = 'npm://registry.npmjs.org;package=@savoirfairelinux/node-server-example;version=1.0.0'
423 result = runCmd('recipetool create -o %s \'%s\'' % (temprecipe, srcuri))
424 self.assertTrue(os.path.isfile(recipefile))
425 self.assertTrue(os.path.isfile(shrinkwrap))
426 checkvars = {}
427 checkvars['SUMMARY'] = 'Node Server Example'
428 checkvars['HOMEPAGE'] = 'https://github.com/savoirfairelinux/node-server-example#readme'
Andrew Geissler595f6302022-01-24 19:11:47 +0000429 checkvars['LICENSE'] = 'BSD-3-Clause & ISC & MIT & Unknown'
Andrew Geissler82c905d2020-04-13 13:39:40 -0500430 urls = []
431 urls.append('npm://registry.npmjs.org/;package=@savoirfairelinux/node-server-example;version=${PV}')
432 urls.append('npmsw://${THISDIR}/${BPN}/npm-shrinkwrap.json')
433 checkvars['SRC_URI'] = set(urls)
434 checkvars['S'] = '${WORKDIR}/npm'
Patrick Williams213cb262021-08-07 19:21:33 -0500435 checkvars['LICENSE:${PN}'] = 'MIT'
436 checkvars['LICENSE:${PN}-base64'] = 'Unknown'
437 checkvars['LICENSE:${PN}-accepts'] = 'MIT'
438 checkvars['LICENSE:${PN}-inherits'] = 'ISC'
Andrew Geissler82c905d2020-04-13 13:39:40 -0500439 inherits = ['npm']
440 self._test_recipe_contents(recipefile, checkvars, inherits)
441
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500442 def test_recipetool_create_github(self):
443 # Basic test to see if github URL mangling works
444 temprecipe = os.path.join(self.tempdir, 'recipe')
445 os.makedirs(temprecipe)
446 recipefile = os.path.join(temprecipe, 'meson_git.bb')
447 srcuri = 'https://github.com/mesonbuild/meson;rev=0.32.0'
448 result = runCmd(['recipetool', 'create', '-o', temprecipe, srcuri])
449 self.assertTrue(os.path.isfile(recipefile))
450 checkvars = {}
451 checkvars['LICENSE'] = set(['Apache-2.0'])
Andrew Geissler595f6302022-01-24 19:11:47 +0000452 checkvars['SRC_URI'] = 'git://github.com/mesonbuild/meson;protocol=https;branch=master'
Brad Bishop15ae2502019-06-18 21:44:24 -0400453 inherits = ['setuptools3']
454 self._test_recipe_contents(recipefile, checkvars, inherits)
455
456 def test_recipetool_create_python3_setuptools(self):
457 # Test creating python3 package from tarball (using setuptools3 class)
458 temprecipe = os.path.join(self.tempdir, 'recipe')
459 os.makedirs(temprecipe)
460 pn = 'python-magic'
461 pv = '0.4.15'
462 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
463 srcuri = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-%s.tar.gz' % pv
464 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
465 self.assertTrue(os.path.isfile(recipefile))
466 checkvars = {}
467 checkvars['LICENSE'] = set(['MIT'])
468 checkvars['LIC_FILES_CHKSUM'] = 'file://LICENSE;md5=16a934f165e8c3245f241e77d401bb88'
469 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/30/80932401906eaf787f2e9bd86dc458f1d2e75b064b4c187341f29516945c/python-magic-${PV}.tar.gz'
470 checkvars['SRC_URI[md5sum]'] = 'e384c95a47218f66c6501cd6dd45ff59'
471 checkvars['SRC_URI[sha256sum]'] = 'f3765c0f582d2dfc72c15f3b5a82aecfae9498bd29ca840d72f37d7bd38bfcd5'
472 inherits = ['setuptools3']
473 self._test_recipe_contents(recipefile, checkvars, inherits)
474
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500475 def test_recipetool_create_github_tarball(self):
476 # Basic test to ensure github URL mangling doesn't apply to release tarballs
477 temprecipe = os.path.join(self.tempdir, 'recipe')
478 os.makedirs(temprecipe)
479 pv = '0.32.0'
480 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
481 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
482 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
483 self.assertTrue(os.path.isfile(recipefile))
484 checkvars = {}
485 checkvars['LICENSE'] = set(['Apache-2.0'])
486 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
Brad Bishop15ae2502019-06-18 21:44:24 -0400487 inherits = ['setuptools3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500488 self._test_recipe_contents(recipefile, checkvars, inherits)
489
Andrew Geissler595f6302022-01-24 19:11:47 +0000490 def _test_recipetool_create_git(self, srcuri, branch=None):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500491 # Basic test to check http git URL mangling works
492 temprecipe = os.path.join(self.tempdir, 'recipe')
493 os.makedirs(temprecipe)
Andrew Geissler595f6302022-01-24 19:11:47 +0000494 name = srcuri.split(';')[0].split('/')[-1]
495 recipefile = os.path.join(temprecipe, name + '_git.bb')
496 options = ' -B %s' % branch if branch else ''
497 result = runCmd('recipetool create -o %s%s "%s"' % (temprecipe, options, srcuri))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500498 self.assertTrue(os.path.isfile(recipefile))
499 checkvars = {}
Andrew Geissler595f6302022-01-24 19:11:47 +0000500 checkvars['SRC_URI'] = srcuri
501 for scheme in ['http', 'https']:
502 if srcuri.startswith(scheme + ":"):
503 checkvars['SRC_URI'] = 'git%s;protocol=%s' % (srcuri[len(scheme):], scheme)
504 if ';branch=' not in srcuri:
505 checkvars['SRC_URI'] += ';branch=' + (branch or 'master')
506 self._test_recipe_contents(recipefile, checkvars, [])
507
508 def test_recipetool_create_git_http(self):
509 self._test_recipetool_create_git('http://git.yoctoproject.org/git/matchbox-keyboard')
510
511 def test_recipetool_create_git_srcuri_master(self):
512 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard;branch=master')
513
514 def test_recipetool_create_git_srcuri_branch(self):
515 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard;branch=matchbox-keyboard-0-1')
516
517 def test_recipetool_create_git_srcbranch(self):
518 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard', 'matchbox-keyboard-0-1')
519
520
521class RecipetoolTests(RecipetoolBase):
522
523 @classmethod
524 def setUpClass(cls):
525 import sys
526
527 super(RecipetoolTests, cls).setUpClass()
528 bb_vars = get_bb_vars(['BBPATH'])
529 cls.bbpath = bb_vars['BBPATH']
530 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'recipetool')
531 sys.path.insert(0, libpath)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500532
533 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
534 dstdir = basedstdir
535 self.assertTrue(os.path.exists(dstdir))
536 for p in paths:
537 dstdir = os.path.join(dstdir, p)
538 if not os.path.exists(dstdir):
539 os.makedirs(dstdir)
Andrew Geissler475cb722020-07-10 16:00:51 -0500540 if p == "lib":
541 # Can race with other tests
542 self.add_command_to_tearDown('rmdir --ignore-fail-on-non-empty %s' % dstdir)
543 else:
544 self.track_for_cleanup(dstdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500545 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
546 if srcfile != dstfile:
547 shutil.copy(srcfile, dstfile)
548 self.track_for_cleanup(dstfile)
549
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500550 def test_recipetool_load_plugin(self):
551 """Test that recipetool loads only the first found plugin in BBPATH."""
552
553 recipetool = runCmd("which recipetool")
554 fromname = runCmd("recipetool --quiet pluginfile")
555 srcfile = fromname.output
556 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
557 plugincontent = []
558 with open(srcfile) as fh:
559 plugincontent = fh.readlines()
560 try:
561 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
562 for path in searchpath:
563 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
564 result = runCmd("recipetool --quiet count")
565 self.assertEqual(result.output, '1')
566 result = runCmd("recipetool --quiet multiloaded")
567 self.assertEqual(result.output, "no")
568 for path in searchpath:
569 result = runCmd("recipetool --quiet bbdir")
570 self.assertEqual(result.output, path)
571 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
572 finally:
573 with open(srcfile, 'w') as fh:
574 fh.writelines(plugincontent)
575
Andrew Geissler595f6302022-01-24 19:11:47 +0000576 def test_recipetool_handle_license_vars(self):
577 from create import handle_license_vars
578 from unittest.mock import Mock
579
580 commonlicdir = get_bb_var('COMMON_LICENSE_DIR')
581
582 d = bb.tinfoil.TinfoilDataStoreConnector
583 d.getVar = Mock(return_value=commonlicdir)
584
585 srctree = tempfile.mkdtemp(prefix='recipetoolqa')
586 self.track_for_cleanup(srctree)
587
588 # Multiple licenses
589 licenses = ['MIT', 'ISC', 'BSD-3-Clause', 'Apache-2.0']
590 for licence in licenses:
591 shutil.copy(os.path.join(commonlicdir, licence), os.path.join(srctree, 'LICENSE.' + licence))
592 # Duplicate license
593 shutil.copy(os.path.join(commonlicdir, 'MIT'), os.path.join(srctree, 'LICENSE'))
594
595 extravalues = {
596 # Duplicate and missing licenses
597 'LICENSE': 'Zlib & BSD-2-Clause & Zlib',
598 'LIC_FILES_CHKSUM': [
599 'file://README.md;md5=0123456789abcdef0123456789abcd'
600 ]
601 }
602 lines_before = []
603 handled = []
604 licvalues = handle_license_vars(srctree, lines_before, handled, extravalues, d)
605 expected_lines_before = [
606 '# WARNING: the following LICENSE and LIC_FILES_CHKSUM values are best guesses - it is',
607 '# your responsibility to verify that the values are complete and correct.',
608 '# NOTE: Original package / source metadata indicates license is: BSD-2-Clause & Zlib',
609 '#',
610 '# NOTE: multiple licenses have been detected; they have been separated with &',
611 '# in the LICENSE value for now since it is a reasonable assumption that all',
612 '# of the licenses apply. If instead there is a choice between the multiple',
613 '# licenses then you should change the value to separate the licenses with |',
614 '# instead of &. If there is any doubt, check the accompanying documentation',
615 '# to determine which situation is applicable.',
616 'LICENSE = "Apache-2.0 & BSD-2-Clause & BSD-3-Clause & ISC & MIT & Zlib"',
617 'LIC_FILES_CHKSUM = "file://LICENSE;md5=0835ade698e0bcf8506ecda2f7b4f302 \\\n'
618 ' file://LICENSE.Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10 \\\n'
619 ' file://LICENSE.BSD-3-Clause;md5=550794465ba0ec5312d6919e203a55f9 \\\n'
620 ' file://LICENSE.ISC;md5=f3b90e78ea0cffb20bf5cca7947a896d \\\n'
621 ' file://LICENSE.MIT;md5=0835ade698e0bcf8506ecda2f7b4f302 \\\n'
622 ' file://README.md;md5=0123456789abcdef0123456789abcd"',
623 ''
624 ]
625 self.assertEqual(lines_before, expected_lines_before)
626 expected_licvalues = [
627 ('MIT', 'LICENSE', '0835ade698e0bcf8506ecda2f7b4f302'),
628 ('Apache-2.0', 'LICENSE.Apache-2.0', '89aea4e17d99a7cacdbeed46a0096b10'),
629 ('BSD-3-Clause', 'LICENSE.BSD-3-Clause', '550794465ba0ec5312d6919e203a55f9'),
630 ('ISC', 'LICENSE.ISC', 'f3b90e78ea0cffb20bf5cca7947a896d'),
631 ('MIT', 'LICENSE.MIT', '0835ade698e0bcf8506ecda2f7b4f302')
632 ]
633 self.assertEqual(handled, [('license', expected_licvalues)])
634 self.assertEqual(extravalues, {})
635 self.assertEqual(licvalues, expected_licvalues)
636
637
638 def test_recipetool_split_pkg_licenses(self):
639 from create import split_pkg_licenses
640 licvalues = [
641 # Duplicate licenses
642 ('BSD-2-Clause', 'x/COPYING', None),
643 ('BSD-2-Clause', 'x/LICENSE', None),
644 # Multiple licenses
645 ('MIT', 'x/a/LICENSE.MIT', None),
646 ('ISC', 'x/a/LICENSE.ISC', None),
647 # Alternative licenses
648 ('(MIT | ISC)', 'x/b/LICENSE', None),
649 # Alternative licenses without brackets
650 ('MIT | BSD-2-Clause', 'x/c/LICENSE', None),
651 # Multi licenses with alternatives
652 ('MIT', 'x/d/COPYING', None),
653 ('MIT | BSD-2-Clause', 'x/d/LICENSE', None),
654 # Multi licenses with alternatives and brackets
655 ('Apache-2.0 & ((MIT | ISC) & BSD-3-Clause)', 'x/e/LICENSE', None)
656 ]
657 packages = {
658 '${PN}': '',
659 'a': 'x/a',
660 'b': 'x/b',
661 'c': 'x/c',
662 'd': 'x/d',
663 'e': 'x/e',
664 'f': 'x/f',
665 'g': 'x/g',
666 }
667 fallback_licenses = {
668 # Ignored
669 'a': 'BSD-3-Clause',
670 # Used
671 'f': 'BSD-3-Clause'
672 }
673 outlines = []
674 outlicenses = split_pkg_licenses(licvalues, packages, outlines, fallback_licenses)
675 expected_outlicenses = {
676 '${PN}': ['BSD-2-Clause'],
677 'a': ['ISC', 'MIT'],
678 'b': ['(ISC | MIT)'],
679 'c': ['(BSD-2-Clause | MIT)'],
680 'd': ['(BSD-2-Clause | MIT)', 'MIT'],
681 'e': ['(ISC | MIT)', 'Apache-2.0', 'BSD-3-Clause'],
682 'f': ['BSD-3-Clause'],
683 'g': ['Unknown']
684 }
685 self.assertEqual(outlicenses, expected_outlicenses)
686 expected_outlines = [
687 'LICENSE:${PN} = "BSD-2-Clause"',
688 'LICENSE:a = "ISC & MIT"',
689 'LICENSE:b = "(ISC | MIT)"',
690 'LICENSE:c = "(BSD-2-Clause | MIT)"',
691 'LICENSE:d = "(BSD-2-Clause | MIT) & MIT"',
692 'LICENSE:e = "(ISC | MIT) & Apache-2.0 & BSD-3-Clause"',
693 'LICENSE:f = "BSD-3-Clause"',
694 'LICENSE:g = "Unknown"'
695 ]
696 self.assertEqual(outlines, expected_outlines)
697
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500698
699class RecipetoolAppendsrcBase(RecipetoolBase):
700 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
701 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
702 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
703
704 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
705
706 if destdir:
707 options += ' -D %s' % destdir
708
709 if expectedfiles is None:
710 expectedfiles = [os.path.basename(f) for f in newfiles]
711
712 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
713 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
714
715 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
716 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
717 result = runCmd(cmd, ignore_status=True)
718 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
719 self.assertNotIn('Traceback', result.output)
720 for errorstr in checkerror:
721 self.assertIn(errorstr, result.output)
722
723 @staticmethod
724 def _get_first_file_uri(recipe):
725 '''Return the first file:// in SRC_URI for the specified recipe.'''
726 src_uri = get_bb_var('SRC_URI', recipe).split()
727 for uri in src_uri:
728 p = urllib.parse.urlparse(uri)
729 if p.scheme == 'file':
730 return p.netloc + p.path
731
732 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
733 if newfile is None:
734 newfile = self.testfile
735
736 if srcdir:
737 if destdir:
738 expected_subdir = os.path.join(srcdir, destdir)
739 else:
740 expected_subdir = srcdir
741 else:
742 options += " -W"
743 expected_subdir = destdir
744
745 if filename:
746 if destdir:
747 destpath = os.path.join(destdir, filename)
748 else:
749 destpath = filename
750 else:
751 filename = os.path.basename(newfile)
752 if destdir:
753 destpath = destdir + os.sep
754 else:
755 destpath = '.' + os.sep
756
Patrick Williams213cb262021-08-07 19:21:33 -0500757 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500758 '\n']
759 if has_src_uri:
760 uri = 'file://%s' % filename
761 if expected_subdir:
762 uri += ';subdir=%s' % expected_subdir
763 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
764 '\n']
765
766 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
767
768 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
769 if expectedfiles is None:
770 expectedfiles = [os.path.basename(n) for n in newfiles]
771
772 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
773
774 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
775 src_uri = bb_vars['SRC_URI'].split()
776 for f in expectedfiles:
777 if destdir:
778 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
779 else:
780 self.assertIn('file://%s' % f, src_uri)
781
782 recipefile = bb_vars['FILE']
783 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
784 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
785 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
786 self.assertIn(filesdir, filesextrapaths)
787
788
789
790
791class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
792
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500793 def test_recipetool_appendsrcfile_basic(self):
794 self._test_appendsrcfile('base-files', 'a-file')
795
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500796 def test_recipetool_appendsrcfile_basic_wildcard(self):
797 testrecipe = 'base-files'
798 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
799 recipefile = get_bb_var('FILE', testrecipe)
800 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
801 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
802
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500803 def test_recipetool_appendsrcfile_subdir_basic(self):
804 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
805
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500806 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
807 self._test_appendsrcfile('base-files', destdir='tmp')
808
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500809 def test_recipetool_appendsrcfile_srcdir_basic(self):
810 testrecipe = 'bash'
811 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
812 srcdir = bb_vars['S']
813 workdir = bb_vars['WORKDIR']
814 subdir = os.path.relpath(srcdir, workdir)
815 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
816
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500817 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
818 testrecipe = 'base-files'
819 filepath = self._get_first_file_uri(testrecipe)
820 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
821 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
822
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500823 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
824 testrecipe = 'base-files'
825 subdir = 'tmp'
826 filepath = self._get_first_file_uri(testrecipe)
827 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
828
829 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
830 self.assertTrue(any('with different parameters' in l for l in output))
831
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500832 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
833 testrecipe = 'bash'
834 filepath = 'Makefile.in'
835 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
836 srcdir = bb_vars['S']
837 workdir = bb_vars['WORKDIR']
838 subdir = os.path.relpath(srcdir, workdir)
839
840 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
841 bitbake('%s:do_unpack' % testrecipe)
Brad Bishop64c979e2019-11-04 13:55:29 -0500842 with open(self.testfile, 'r') as testfile:
843 with open(os.path.join(srcdir, filepath), 'r') as makefilein:
844 self.assertEqual(testfile.read(), makefilein.read())
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500845
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500846 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
847 newfiles = [self.testfile]
848 for i in range(1, 5):
849 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
850 with open(testfile, 'w') as f:
851 f.write('Test file %d\n' % i)
852 newfiles.append(testfile)
853 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
854
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500855 def test_recipetool_appendsrcfiles_basic_subdir(self):
856 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')