blob: 9db1ddb53209b0fd1924607f7b217bbf8522057f [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 = {}
346 checkvars['LICENSE'] = 'GPLv2'
347 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 = {}
366 checkvars['LICENSE'] = 'LGPLv2.1'
367 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'
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500380 srcuri = 'http://www.dest-unreach.org/socat/download/socat-%s.tar.bz2' % pv
381 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 = {}
389 checkvars['LICENSE'] = set(['Unknown', 'GPLv2'])
390 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 = {}
Brad Bishop96ff1982019-08-19 13:50:42 -0400405 checkvars['LICENSE'] = set(['LGPLv2.1', 'MPL-1.1'])
406 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
475 def test_recipetool_create_python3_distutils(self):
476 # Test creating python3 package from tarball (using distutils3 class)
477 temprecipe = os.path.join(self.tempdir, 'recipe')
478 os.makedirs(temprecipe)
479 pn = 'docutils'
480 pv = '0.14'
481 recipefile = os.path.join(temprecipe, '%s_%s.bb' % (pn, pv))
482 srcuri = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-%s.tar.gz' % pv
483 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
484 self.assertTrue(os.path.isfile(recipefile))
485 checkvars = {}
Andrew Geissler595f6302022-01-24 19:11:47 +0000486 checkvars['LICENSE'] = 'BSD-3-Clause & GPL & PSF'
Brad Bishop15ae2502019-06-18 21:44:24 -0400487 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING.txt;md5=35a23d42b615470583563132872c97d6'
488 checkvars['SRC_URI'] = 'https://files.pythonhosted.org/packages/84/f4/5771e41fdf52aabebbadecc9381d11dea0fa34e4759b4071244fa094804c/docutils-${PV}.tar.gz'
489 checkvars['SRC_URI[md5sum]'] = 'c53768d63db3873b7d452833553469de'
490 checkvars['SRC_URI[sha256sum]'] = '51e64ef2ebfb29cae1faa133b3710143496eca21c530f3f71424d77687764274'
491 inherits = ['distutils3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500492 self._test_recipe_contents(recipefile, checkvars, inherits)
493
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500494 def test_recipetool_create_github_tarball(self):
495 # Basic test to ensure github URL mangling doesn't apply to release tarballs
496 temprecipe = os.path.join(self.tempdir, 'recipe')
497 os.makedirs(temprecipe)
498 pv = '0.32.0'
499 recipefile = os.path.join(temprecipe, 'meson_%s.bb' % pv)
500 srcuri = 'https://github.com/mesonbuild/meson/releases/download/%s/meson-%s.tar.gz' % (pv, pv)
501 result = runCmd('recipetool create -o %s %s' % (temprecipe, srcuri))
502 self.assertTrue(os.path.isfile(recipefile))
503 checkvars = {}
504 checkvars['LICENSE'] = set(['Apache-2.0'])
505 checkvars['SRC_URI'] = 'https://github.com/mesonbuild/meson/releases/download/${PV}/meson-${PV}.tar.gz'
Brad Bishop15ae2502019-06-18 21:44:24 -0400506 inherits = ['setuptools3']
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500507 self._test_recipe_contents(recipefile, checkvars, inherits)
508
Andrew Geissler595f6302022-01-24 19:11:47 +0000509 def _test_recipetool_create_git(self, srcuri, branch=None):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500510 # Basic test to check http git URL mangling works
511 temprecipe = os.path.join(self.tempdir, 'recipe')
512 os.makedirs(temprecipe)
Andrew Geissler595f6302022-01-24 19:11:47 +0000513 name = srcuri.split(';')[0].split('/')[-1]
514 recipefile = os.path.join(temprecipe, name + '_git.bb')
515 options = ' -B %s' % branch if branch else ''
516 result = runCmd('recipetool create -o %s%s "%s"' % (temprecipe, options, srcuri))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500517 self.assertTrue(os.path.isfile(recipefile))
518 checkvars = {}
Andrew Geissler595f6302022-01-24 19:11:47 +0000519 checkvars['SRC_URI'] = srcuri
520 for scheme in ['http', 'https']:
521 if srcuri.startswith(scheme + ":"):
522 checkvars['SRC_URI'] = 'git%s;protocol=%s' % (srcuri[len(scheme):], scheme)
523 if ';branch=' not in srcuri:
524 checkvars['SRC_URI'] += ';branch=' + (branch or 'master')
525 self._test_recipe_contents(recipefile, checkvars, [])
526
527 def test_recipetool_create_git_http(self):
528 self._test_recipetool_create_git('http://git.yoctoproject.org/git/matchbox-keyboard')
529
530 def test_recipetool_create_git_srcuri_master(self):
531 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard;branch=master')
532
533 def test_recipetool_create_git_srcuri_branch(self):
534 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard;branch=matchbox-keyboard-0-1')
535
536 def test_recipetool_create_git_srcbranch(self):
537 self._test_recipetool_create_git('git://git.yoctoproject.org/matchbox-keyboard', 'matchbox-keyboard-0-1')
538
539
540class RecipetoolTests(RecipetoolBase):
541
542 @classmethod
543 def setUpClass(cls):
544 import sys
545
546 super(RecipetoolTests, cls).setUpClass()
547 bb_vars = get_bb_vars(['BBPATH'])
548 cls.bbpath = bb_vars['BBPATH']
549 libpath = os.path.join(get_bb_var('COREBASE'), 'scripts', 'lib', 'recipetool')
550 sys.path.insert(0, libpath)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500551
552 def _copy_file_with_cleanup(self, srcfile, basedstdir, *paths):
553 dstdir = basedstdir
554 self.assertTrue(os.path.exists(dstdir))
555 for p in paths:
556 dstdir = os.path.join(dstdir, p)
557 if not os.path.exists(dstdir):
558 os.makedirs(dstdir)
Andrew Geissler475cb722020-07-10 16:00:51 -0500559 if p == "lib":
560 # Can race with other tests
561 self.add_command_to_tearDown('rmdir --ignore-fail-on-non-empty %s' % dstdir)
562 else:
563 self.track_for_cleanup(dstdir)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500564 dstfile = os.path.join(dstdir, os.path.basename(srcfile))
565 if srcfile != dstfile:
566 shutil.copy(srcfile, dstfile)
567 self.track_for_cleanup(dstfile)
568
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500569 def test_recipetool_load_plugin(self):
570 """Test that recipetool loads only the first found plugin in BBPATH."""
571
572 recipetool = runCmd("which recipetool")
573 fromname = runCmd("recipetool --quiet pluginfile")
574 srcfile = fromname.output
575 searchpath = self.bbpath.split(':') + [os.path.dirname(recipetool.output)]
576 plugincontent = []
577 with open(srcfile) as fh:
578 plugincontent = fh.readlines()
579 try:
580 self.assertIn('meta-selftest', srcfile, 'wrong bbpath plugin found')
581 for path in searchpath:
582 self._copy_file_with_cleanup(srcfile, path, 'lib', 'recipetool')
583 result = runCmd("recipetool --quiet count")
584 self.assertEqual(result.output, '1')
585 result = runCmd("recipetool --quiet multiloaded")
586 self.assertEqual(result.output, "no")
587 for path in searchpath:
588 result = runCmd("recipetool --quiet bbdir")
589 self.assertEqual(result.output, path)
590 os.unlink(os.path.join(result.output, 'lib', 'recipetool', 'bbpath.py'))
591 finally:
592 with open(srcfile, 'w') as fh:
593 fh.writelines(plugincontent)
594
Andrew Geissler595f6302022-01-24 19:11:47 +0000595 def test_recipetool_handle_license_vars(self):
596 from create import handle_license_vars
597 from unittest.mock import Mock
598
599 commonlicdir = get_bb_var('COMMON_LICENSE_DIR')
600
601 d = bb.tinfoil.TinfoilDataStoreConnector
602 d.getVar = Mock(return_value=commonlicdir)
603
604 srctree = tempfile.mkdtemp(prefix='recipetoolqa')
605 self.track_for_cleanup(srctree)
606
607 # Multiple licenses
608 licenses = ['MIT', 'ISC', 'BSD-3-Clause', 'Apache-2.0']
609 for licence in licenses:
610 shutil.copy(os.path.join(commonlicdir, licence), os.path.join(srctree, 'LICENSE.' + licence))
611 # Duplicate license
612 shutil.copy(os.path.join(commonlicdir, 'MIT'), os.path.join(srctree, 'LICENSE'))
613
614 extravalues = {
615 # Duplicate and missing licenses
616 'LICENSE': 'Zlib & BSD-2-Clause & Zlib',
617 'LIC_FILES_CHKSUM': [
618 'file://README.md;md5=0123456789abcdef0123456789abcd'
619 ]
620 }
621 lines_before = []
622 handled = []
623 licvalues = handle_license_vars(srctree, lines_before, handled, extravalues, d)
624 expected_lines_before = [
625 '# WARNING: the following LICENSE and LIC_FILES_CHKSUM values are best guesses - it is',
626 '# your responsibility to verify that the values are complete and correct.',
627 '# NOTE: Original package / source metadata indicates license is: BSD-2-Clause & Zlib',
628 '#',
629 '# NOTE: multiple licenses have been detected; they have been separated with &',
630 '# in the LICENSE value for now since it is a reasonable assumption that all',
631 '# of the licenses apply. If instead there is a choice between the multiple',
632 '# licenses then you should change the value to separate the licenses with |',
633 '# instead of &. If there is any doubt, check the accompanying documentation',
634 '# to determine which situation is applicable.',
635 'LICENSE = "Apache-2.0 & BSD-2-Clause & BSD-3-Clause & ISC & MIT & Zlib"',
636 'LIC_FILES_CHKSUM = "file://LICENSE;md5=0835ade698e0bcf8506ecda2f7b4f302 \\\n'
637 ' file://LICENSE.Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10 \\\n'
638 ' file://LICENSE.BSD-3-Clause;md5=550794465ba0ec5312d6919e203a55f9 \\\n'
639 ' file://LICENSE.ISC;md5=f3b90e78ea0cffb20bf5cca7947a896d \\\n'
640 ' file://LICENSE.MIT;md5=0835ade698e0bcf8506ecda2f7b4f302 \\\n'
641 ' file://README.md;md5=0123456789abcdef0123456789abcd"',
642 ''
643 ]
644 self.assertEqual(lines_before, expected_lines_before)
645 expected_licvalues = [
646 ('MIT', 'LICENSE', '0835ade698e0bcf8506ecda2f7b4f302'),
647 ('Apache-2.0', 'LICENSE.Apache-2.0', '89aea4e17d99a7cacdbeed46a0096b10'),
648 ('BSD-3-Clause', 'LICENSE.BSD-3-Clause', '550794465ba0ec5312d6919e203a55f9'),
649 ('ISC', 'LICENSE.ISC', 'f3b90e78ea0cffb20bf5cca7947a896d'),
650 ('MIT', 'LICENSE.MIT', '0835ade698e0bcf8506ecda2f7b4f302')
651 ]
652 self.assertEqual(handled, [('license', expected_licvalues)])
653 self.assertEqual(extravalues, {})
654 self.assertEqual(licvalues, expected_licvalues)
655
656
657 def test_recipetool_split_pkg_licenses(self):
658 from create import split_pkg_licenses
659 licvalues = [
660 # Duplicate licenses
661 ('BSD-2-Clause', 'x/COPYING', None),
662 ('BSD-2-Clause', 'x/LICENSE', None),
663 # Multiple licenses
664 ('MIT', 'x/a/LICENSE.MIT', None),
665 ('ISC', 'x/a/LICENSE.ISC', None),
666 # Alternative licenses
667 ('(MIT | ISC)', 'x/b/LICENSE', None),
668 # Alternative licenses without brackets
669 ('MIT | BSD-2-Clause', 'x/c/LICENSE', None),
670 # Multi licenses with alternatives
671 ('MIT', 'x/d/COPYING', None),
672 ('MIT | BSD-2-Clause', 'x/d/LICENSE', None),
673 # Multi licenses with alternatives and brackets
674 ('Apache-2.0 & ((MIT | ISC) & BSD-3-Clause)', 'x/e/LICENSE', None)
675 ]
676 packages = {
677 '${PN}': '',
678 'a': 'x/a',
679 'b': 'x/b',
680 'c': 'x/c',
681 'd': 'x/d',
682 'e': 'x/e',
683 'f': 'x/f',
684 'g': 'x/g',
685 }
686 fallback_licenses = {
687 # Ignored
688 'a': 'BSD-3-Clause',
689 # Used
690 'f': 'BSD-3-Clause'
691 }
692 outlines = []
693 outlicenses = split_pkg_licenses(licvalues, packages, outlines, fallback_licenses)
694 expected_outlicenses = {
695 '${PN}': ['BSD-2-Clause'],
696 'a': ['ISC', 'MIT'],
697 'b': ['(ISC | MIT)'],
698 'c': ['(BSD-2-Clause | MIT)'],
699 'd': ['(BSD-2-Clause | MIT)', 'MIT'],
700 'e': ['(ISC | MIT)', 'Apache-2.0', 'BSD-3-Clause'],
701 'f': ['BSD-3-Clause'],
702 'g': ['Unknown']
703 }
704 self.assertEqual(outlicenses, expected_outlicenses)
705 expected_outlines = [
706 'LICENSE:${PN} = "BSD-2-Clause"',
707 'LICENSE:a = "ISC & MIT"',
708 'LICENSE:b = "(ISC | MIT)"',
709 'LICENSE:c = "(BSD-2-Clause | MIT)"',
710 'LICENSE:d = "(BSD-2-Clause | MIT) & MIT"',
711 'LICENSE:e = "(ISC | MIT) & Apache-2.0 & BSD-3-Clause"',
712 'LICENSE:f = "BSD-3-Clause"',
713 'LICENSE:g = "Unknown"'
714 ]
715 self.assertEqual(outlines, expected_outlines)
716
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500717
718class RecipetoolAppendsrcBase(RecipetoolBase):
719 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
720 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
721 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
722
723 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
724
725 if destdir:
726 options += ' -D %s' % destdir
727
728 if expectedfiles is None:
729 expectedfiles = [os.path.basename(f) for f in newfiles]
730
731 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
732 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
733
734 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
735 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
736 result = runCmd(cmd, ignore_status=True)
737 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
738 self.assertNotIn('Traceback', result.output)
739 for errorstr in checkerror:
740 self.assertIn(errorstr, result.output)
741
742 @staticmethod
743 def _get_first_file_uri(recipe):
744 '''Return the first file:// in SRC_URI for the specified recipe.'''
745 src_uri = get_bb_var('SRC_URI', recipe).split()
746 for uri in src_uri:
747 p = urllib.parse.urlparse(uri)
748 if p.scheme == 'file':
749 return p.netloc + p.path
750
751 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
752 if newfile is None:
753 newfile = self.testfile
754
755 if srcdir:
756 if destdir:
757 expected_subdir = os.path.join(srcdir, destdir)
758 else:
759 expected_subdir = srcdir
760 else:
761 options += " -W"
762 expected_subdir = destdir
763
764 if filename:
765 if destdir:
766 destpath = os.path.join(destdir, filename)
767 else:
768 destpath = filename
769 else:
770 filename = os.path.basename(newfile)
771 if destdir:
772 destpath = destdir + os.sep
773 else:
774 destpath = '.' + os.sep
775
Patrick Williams213cb262021-08-07 19:21:33 -0500776 expectedlines = ['FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"\n',
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500777 '\n']
778 if has_src_uri:
779 uri = 'file://%s' % filename
780 if expected_subdir:
781 uri += ';subdir=%s' % expected_subdir
782 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
783 '\n']
784
785 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
786
787 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
788 if expectedfiles is None:
789 expectedfiles = [os.path.basename(n) for n in newfiles]
790
791 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
792
793 bb_vars = get_bb_vars(['SRC_URI', 'FILE', 'FILESEXTRAPATHS'], testrecipe)
794 src_uri = bb_vars['SRC_URI'].split()
795 for f in expectedfiles:
796 if destdir:
797 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
798 else:
799 self.assertIn('file://%s' % f, src_uri)
800
801 recipefile = bb_vars['FILE']
802 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
803 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
804 filesextrapaths = bb_vars['FILESEXTRAPATHS'].split(':')
805 self.assertIn(filesdir, filesextrapaths)
806
807
808
809
810class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
811
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500812 def test_recipetool_appendsrcfile_basic(self):
813 self._test_appendsrcfile('base-files', 'a-file')
814
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500815 def test_recipetool_appendsrcfile_basic_wildcard(self):
816 testrecipe = 'base-files'
817 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
818 recipefile = get_bb_var('FILE', testrecipe)
819 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
820 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
821
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500822 def test_recipetool_appendsrcfile_subdir_basic(self):
823 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
824
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500825 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
826 self._test_appendsrcfile('base-files', destdir='tmp')
827
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500828 def test_recipetool_appendsrcfile_srcdir_basic(self):
829 testrecipe = 'bash'
830 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
831 srcdir = bb_vars['S']
832 workdir = bb_vars['WORKDIR']
833 subdir = os.path.relpath(srcdir, workdir)
834 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
835
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500836 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
837 testrecipe = 'base-files'
838 filepath = self._get_first_file_uri(testrecipe)
839 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
840 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
841
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500842 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
843 testrecipe = 'base-files'
844 subdir = 'tmp'
845 filepath = self._get_first_file_uri(testrecipe)
846 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
847
848 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
849 self.assertTrue(any('with different parameters' in l for l in output))
850
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500851 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
852 testrecipe = 'bash'
853 filepath = 'Makefile.in'
854 bb_vars = get_bb_vars(['S', 'WORKDIR'], testrecipe)
855 srcdir = bb_vars['S']
856 workdir = bb_vars['WORKDIR']
857 subdir = os.path.relpath(srcdir, workdir)
858
859 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
860 bitbake('%s:do_unpack' % testrecipe)
Brad Bishop64c979e2019-11-04 13:55:29 -0500861 with open(self.testfile, 'r') as testfile:
862 with open(os.path.join(srcdir, filepath), 'r') as makefilein:
863 self.assertEqual(testfile.read(), makefilein.read())
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500864
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500865 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
866 newfiles = [self.testfile]
867 for i in range(1, 5):
868 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
869 with open(testfile, 'w') as f:
870 f.write('Test file %d\n' % i)
871 newfiles.append(testfile)
872 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
873
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500874 def test_recipetool_appendsrcfiles_basic_subdir(self):
875 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')