blob: c34ad688703ccd6be5038ae9efb31e2ada8646ad [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001import os
2import logging
3import tempfile
4import urlparse
5
6from oeqa.utils.commands import runCmd, bitbake, get_bb_var, create_temp_layer
7from oeqa.utils.decorators import testcase
8from oeqa.selftest import devtool
9
10
11templayerdir = None
12
13
14def setUpModule():
15 global templayerdir
16 templayerdir = tempfile.mkdtemp(prefix='recipetoolqa')
17 create_temp_layer(templayerdir, 'selftestrecipetool')
18 runCmd('bitbake-layers add-layer %s' % templayerdir)
19
20
21def tearDownModule():
22 runCmd('bitbake-layers remove-layer %s' % templayerdir, ignore_status=True)
23 runCmd('rm -rf %s' % templayerdir)
24
25
26class RecipetoolBase(devtool.DevtoolBase):
27 def setUpLocal(self):
28 self.templayerdir = templayerdir
29 self.tempdir = tempfile.mkdtemp(prefix='recipetoolqa')
30 self.track_for_cleanup(self.tempdir)
31 self.testfile = os.path.join(self.tempdir, 'testfile')
32 with open(self.testfile, 'w') as f:
33 f.write('Test file\n')
34
35 def tearDownLocal(self):
36 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
37
38 def _try_recipetool_appendcmd(self, cmd, testrecipe, expectedfiles, expectedlines=None):
39 result = runCmd(cmd)
40 self.assertNotIn('Traceback', result.output)
41
42 # Check the bbappend was created and applies properly
43 recipefile = get_bb_var('FILE', testrecipe)
44 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
45
46 # Check the bbappend contents
47 if expectedlines is not None:
48 with open(bbappendfile, 'r') as f:
49 self.assertEqual(expectedlines, f.readlines(), "Expected lines are not present in %s" % bbappendfile)
50
51 # Check file was copied
52 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
53 for expectedfile in expectedfiles:
54 self.assertTrue(os.path.isfile(os.path.join(filesdir, expectedfile)), 'Expected file %s to be copied next to bbappend, but it wasn\'t' % expectedfile)
55
56 # Check no other files created
57 createdfiles = []
58 for root, _, files in os.walk(filesdir):
59 for f in files:
60 createdfiles.append(os.path.relpath(os.path.join(root, f), filesdir))
61 self.assertTrue(sorted(createdfiles), sorted(expectedfiles))
62
63 return bbappendfile, result.output
64
65
66class RecipetoolTests(RecipetoolBase):
67 @classmethod
68 def setUpClass(cls):
69 # Ensure we have the right data in shlibs/pkgdata
70 logger = logging.getLogger("selftest")
71 logger.info('Running bitbake to generate pkgdata')
72 bitbake('-c packagedata base-files coreutils busybox selftest-recipetool-appendfile')
73
74 @classmethod
75 def tearDownClass(cls):
76 # Shouldn't leave any traces of this artificial recipe behind
77 bitbake('-c cleansstate selftest-recipetool-appendfile')
78
79 def _try_recipetool_appendfile(self, testrecipe, destfile, newfile, options, expectedlines, expectedfiles):
80 cmd = 'recipetool appendfile %s %s %s %s' % (self.templayerdir, destfile, newfile, options)
81 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
82
83 def _try_recipetool_appendfile_fail(self, destfile, newfile, checkerror):
84 cmd = 'recipetool appendfile %s %s %s' % (self.templayerdir, destfile, newfile)
85 result = runCmd(cmd, ignore_status=True)
86 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
87 self.assertNotIn('Traceback', result.output)
88 for errorstr in checkerror:
89 self.assertIn(errorstr, result.output)
90
91 @testcase(1177)
92 def test_recipetool_appendfile_basic(self):
93 # Basic test
94 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
95 '\n']
96 _, output = self._try_recipetool_appendfile('base-files', '/etc/motd', self.testfile, '', expectedlines, ['motd'])
97 self.assertNotIn('WARNING: ', output)
98
99 @testcase(1183)
100 def test_recipetool_appendfile_invalid(self):
101 # Test some commands that should error
102 self._try_recipetool_appendfile_fail('/etc/passwd', self.testfile, ['ERROR: /etc/passwd cannot be handled by this tool', 'useradd', 'extrausers'])
103 self._try_recipetool_appendfile_fail('/etc/timestamp', self.testfile, ['ERROR: /etc/timestamp cannot be handled by this tool'])
104 self._try_recipetool_appendfile_fail('/dev/console', self.testfile, ['ERROR: /dev/console cannot be handled by this tool'])
105
106 @testcase(1176)
107 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 corebase = get_bb_var('COREBASE')
112 # Need a test file - should be executable
113 testfile2 = os.path.join(corebase, 'oe-init-build-env')
114 testfile2name = os.path.basename(testfile2)
115 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
116 '\n',
117 'SRC_URI += "file://%s"\n' % testfile2name,
118 '\n',
119 'do_install_append() {\n',
120 ' install -d ${D}${base_bindir}\n',
121 ' install -m 0755 ${WORKDIR}/%s ${D}${base_bindir}/ls\n' % testfile2name,
122 '}\n']
123 self._try_recipetool_appendfile('coreutils', '/bin/ls', testfile2, '-r coreutils', expectedlines, [testfile2name])
124 # Now try bbappending the same file again, contents should not change
125 bbappendfile, _ = self._try_recipetool_appendfile('coreutils', '/bin/ls', self.testfile, '-r coreutils', expectedlines, [testfile2name])
126 # But file should have
127 copiedfile = os.path.join(os.path.dirname(bbappendfile), 'coreutils', testfile2name)
128 result = runCmd('diff -q %s %s' % (testfile2, copiedfile), ignore_status=True)
129 self.assertNotEqual(result.status, 0, 'New file should have been copied but was not %s' % result.output)
130
131 @testcase(1178)
132 def test_recipetool_appendfile_binary(self):
133 # Try appending a binary file
134 # /bin/ls can be a symlink to /usr/bin/ls
135 ls = os.path.realpath("/bin/ls")
136 result = runCmd('recipetool appendfile %s /bin/ls %s -r coreutils' % (self.templayerdir, ls))
137 self.assertIn('WARNING: ', result.output)
138 self.assertIn('is a binary', result.output)
139
140 @testcase(1173)
141 def test_recipetool_appendfile_add(self):
142 corebase = get_bb_var('COREBASE')
143 # Try arbitrary file add to a recipe
144 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
145 '\n',
146 'SRC_URI += "file://testfile"\n',
147 '\n',
148 'do_install_append() {\n',
149 ' install -d ${D}${datadir}\n',
150 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
151 '}\n']
152 self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase', expectedlines, ['testfile'])
153 # Try adding another file, this time where the source file is executable
154 # (so we're testing that, plus modifying an existing bbappend)
155 testfile2 = os.path.join(corebase, 'oe-init-build-env')
156 testfile2name = os.path.basename(testfile2)
157 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
158 '\n',
159 'SRC_URI += "file://testfile \\\n',
160 ' file://%s \\\n' % testfile2name,
161 ' "\n',
162 '\n',
163 'do_install_append() {\n',
164 ' install -d ${D}${datadir}\n',
165 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
166 ' install -m 0755 ${WORKDIR}/%s ${D}${datadir}/scriptname\n' % testfile2name,
167 '}\n']
168 self._try_recipetool_appendfile('netbase', '/usr/share/scriptname', testfile2, '-r netbase', expectedlines, ['testfile', testfile2name])
169
170 @testcase(1174)
171 def test_recipetool_appendfile_add_bindir(self):
172 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
173 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
174 '\n',
175 'SRC_URI += "file://testfile"\n',
176 '\n',
177 'do_install_append() {\n',
178 ' install -d ${D}${bindir}\n',
179 ' install -m 0755 ${WORKDIR}/testfile ${D}${bindir}/selftest-recipetool-testbin\n',
180 '}\n']
181 _, output = self._try_recipetool_appendfile('netbase', '/usr/bin/selftest-recipetool-testbin', self.testfile, '-r netbase', expectedlines, ['testfile'])
182 self.assertNotIn('WARNING: ', output)
183
184 @testcase(1175)
185 def test_recipetool_appendfile_add_machine(self):
186 # Try arbitrary file add to a recipe, this time to a location such that should be installed as executable
187 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
188 '\n',
189 'PACKAGE_ARCH = "${MACHINE_ARCH}"\n',
190 '\n',
191 'SRC_URI_append_mymachine = " file://testfile"\n',
192 '\n',
193 'do_install_append_mymachine() {\n',
194 ' install -d ${D}${datadir}\n',
195 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/something\n',
196 '}\n']
197 _, output = self._try_recipetool_appendfile('netbase', '/usr/share/something', self.testfile, '-r netbase -m mymachine', expectedlines, ['mymachine/testfile'])
198 self.assertNotIn('WARNING: ', output)
199
200 @testcase(1184)
201 def test_recipetool_appendfile_orig(self):
202 # A file that's in SRC_URI and in do_install with the same name
203 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
204 '\n']
205 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-orig', self.testfile, '', expectedlines, ['selftest-replaceme-orig'])
206 self.assertNotIn('WARNING: ', output)
207
208 @testcase(1191)
209 def test_recipetool_appendfile_todir(self):
210 # A file that's in SRC_URI and in do_install with destination directory rather than file
211 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
212 '\n']
213 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-todir', self.testfile, '', expectedlines, ['selftest-replaceme-todir'])
214 self.assertNotIn('WARNING: ', output)
215
216 @testcase(1187)
217 def test_recipetool_appendfile_renamed(self):
218 # A file that's in SRC_URI with a different name to the destination file
219 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
220 '\n']
221 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-renamed', self.testfile, '', expectedlines, ['file1'])
222 self.assertNotIn('WARNING: ', output)
223
224 @testcase(1190)
225 def test_recipetool_appendfile_subdir(self):
226 # A file that's in SRC_URI in a subdir
227 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
228 '\n',
229 'SRC_URI += "file://testfile"\n',
230 '\n',
231 'do_install_append() {\n',
232 ' install -d ${D}${datadir}\n',
233 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-subdir\n',
234 '}\n']
235 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-subdir', self.testfile, '', expectedlines, ['testfile'])
236 self.assertNotIn('WARNING: ', output)
237
238 @testcase(1189)
239 def test_recipetool_appendfile_src_glob(self):
240 # A file that's in SRC_URI as a glob
241 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
242 '\n',
243 'SRC_URI += "file://testfile"\n',
244 '\n',
245 'do_install_append() {\n',
246 ' install -d ${D}${datadir}\n',
247 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-src-globfile\n',
248 '}\n']
249 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-src-globfile', self.testfile, '', expectedlines, ['testfile'])
250 self.assertNotIn('WARNING: ', output)
251
252 @testcase(1181)
253 def test_recipetool_appendfile_inst_glob(self):
254 # A file that's in do_install as a glob
255 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
256 '\n']
257 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-globfile'])
258 self.assertNotIn('WARNING: ', output)
259
260 @testcase(1182)
261 def test_recipetool_appendfile_inst_todir_glob(self):
262 # A file that's in do_install as a glob with destination as a directory
263 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
264 '\n']
265 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-todir-globfile', self.testfile, '', expectedlines, ['selftest-replaceme-inst-todir-globfile'])
266 self.assertNotIn('WARNING: ', output)
267
268 @testcase(1185)
269 def test_recipetool_appendfile_patch(self):
270 # A file that's added by a patch in SRC_URI
271 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
272 '\n',
273 'SRC_URI += "file://testfile"\n',
274 '\n',
275 'do_install_append() {\n',
276 ' install -d ${D}${sysconfdir}\n',
277 ' install -m 0644 ${WORKDIR}/testfile ${D}${sysconfdir}/selftest-replaceme-patched\n',
278 '}\n']
279 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/etc/selftest-replaceme-patched', self.testfile, '', expectedlines, ['testfile'])
280 for line in output.splitlines():
281 if line.startswith('WARNING: '):
282 self.assertIn('add-file.patch', line, 'Unexpected warning found in output:\n%s' % line)
283 break
284 else:
285 self.fail('Patch warning not found in output:\n%s' % output)
286
287 @testcase(1188)
288 def test_recipetool_appendfile_script(self):
289 # Now, a file that's in SRC_URI but installed by a script (so no mention in do_install)
290 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
291 '\n',
292 'SRC_URI += "file://testfile"\n',
293 '\n',
294 'do_install_append() {\n',
295 ' install -d ${D}${datadir}\n',
296 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-scripted\n',
297 '}\n']
298 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-scripted', self.testfile, '', expectedlines, ['testfile'])
299 self.assertNotIn('WARNING: ', output)
300
301 @testcase(1180)
302 def test_recipetool_appendfile_inst_func(self):
303 # A file that's installed from a function called by do_install
304 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
305 '\n']
306 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-inst-func', self.testfile, '', expectedlines, ['selftest-replaceme-inst-func'])
307 self.assertNotIn('WARNING: ', output)
308
309 @testcase(1186)
310 def test_recipetool_appendfile_postinstall(self):
311 # A file that's created by a postinstall script (and explicitly mentioned in it)
312 # First try without specifying recipe
313 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'])
314 # Now specify recipe
315 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
316 '\n',
317 'SRC_URI += "file://testfile"\n',
318 '\n',
319 'do_install_append() {\n',
320 ' install -d ${D}${datadir}\n',
321 ' install -m 0644 ${WORKDIR}/testfile ${D}${datadir}/selftest-replaceme-postinst\n',
322 '}\n']
323 _, output = self._try_recipetool_appendfile('selftest-recipetool-appendfile', '/usr/share/selftest-replaceme-postinst', self.testfile, '-r selftest-recipetool-appendfile', expectedlines, ['testfile'])
324
325 @testcase(1179)
326 def test_recipetool_appendfile_extlayer(self):
327 # Try creating a bbappend in a layer that's not in bblayers.conf and has a different structure
328 exttemplayerdir = os.path.join(self.tempdir, 'extlayer')
329 self._create_temp_layer(exttemplayerdir, False, 'oeselftestextlayer', recipepathspec='metadata/recipes/recipes-*/*')
330 result = runCmd('recipetool appendfile %s /usr/share/selftest-replaceme-orig %s' % (exttemplayerdir, self.testfile))
331 self.assertNotIn('Traceback', result.output)
332 createdfiles = []
333 for root, _, files in os.walk(exttemplayerdir):
334 for f in files:
335 createdfiles.append(os.path.relpath(os.path.join(root, f), exttemplayerdir))
336 createdfiles.remove('conf/layer.conf')
337 expectedfiles = ['metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile.bbappend',
338 'metadata/recipes/recipes-test/selftest-recipetool-appendfile/selftest-recipetool-appendfile/selftest-replaceme-orig']
339 self.assertEqual(sorted(createdfiles), sorted(expectedfiles))
340
341 @testcase(1192)
342 def test_recipetool_appendfile_wildcard(self):
343
344 def try_appendfile_wc(options):
345 result = runCmd('recipetool appendfile %s /etc/profile %s %s' % (self.templayerdir, self.testfile, options))
346 self.assertNotIn('Traceback', result.output)
347 bbappendfile = None
348 for root, _, files in os.walk(self.templayerdir):
349 for f in files:
350 if f.endswith('.bbappend'):
351 bbappendfile = f
352 break
353 if not bbappendfile:
354 self.fail('No bbappend file created')
355 runCmd('rm -rf %s/recipes-*' % self.templayerdir)
356 return bbappendfile
357
358 # Check without wildcard option
359 recipefn = os.path.basename(get_bb_var('FILE', 'base-files'))
360 filename = try_appendfile_wc('')
361 self.assertEqual(filename, recipefn.replace('.bb', '.bbappend'))
362 # Now check with wildcard option
363 filename = try_appendfile_wc('-w')
364 self.assertEqual(filename, recipefn.split('_')[0] + '_%.bbappend')
365
366 @testcase(1193)
367 def test_recipetool_create(self):
368 # Try adding a recipe
369 tempsrc = os.path.join(self.tempdir, 'srctree')
370 os.makedirs(tempsrc)
371 recipefile = os.path.join(self.tempdir, 'logrotate_3.8.7.bb')
372 srcuri = 'https://fedorahosted.org/releases/l/o/logrotate/logrotate-3.8.7.tar.gz'
373 result = runCmd('recipetool create -o %s %s -x %s' % (recipefile, srcuri, tempsrc))
374 self.assertTrue(os.path.isfile(recipefile))
375 checkvars = {}
376 checkvars['LICENSE'] = 'GPLv2'
377 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=18810669f13b87348459e611d31ab760'
378 checkvars['SRC_URI'] = 'https://fedorahosted.org/releases/l/o/logrotate/logrotate-${PV}.tar.gz'
379 checkvars['SRC_URI[md5sum]'] = '99e08503ef24c3e2e3ff74cc5f3be213'
380 checkvars['SRC_URI[sha256sum]'] = 'f6ba691f40e30e640efa2752c1f9499a3f9738257660994de70a45fe00d12b64'
381 self._test_recipe_contents(recipefile, checkvars, [])
382
383 @testcase(1194)
384 def test_recipetool_create_git(self):
385 # Ensure we have the right data in shlibs/pkgdata
386 bitbake('libpng pango libx11 libxext jpeg')
387 # Try adding a recipe
388 tempsrc = os.path.join(self.tempdir, 'srctree')
389 os.makedirs(tempsrc)
390 recipefile = os.path.join(self.tempdir, 'libmatchbox.bb')
391 srcuri = 'git://git.yoctoproject.org/libmatchbox'
392 result = runCmd('recipetool create -o %s %s -x %s' % (recipefile, srcuri, tempsrc))
393 self.assertTrue(os.path.isfile(recipefile), 'recipetool did not create recipe file; output:\n%s' % result.output)
394 checkvars = {}
395 checkvars['LICENSE'] = 'LGPLv2.1'
396 checkvars['LIC_FILES_CHKSUM'] = 'file://COPYING;md5=7fbc338309ac38fefcd64b04bb903e34'
397 checkvars['S'] = '${WORKDIR}/git'
398 checkvars['PV'] = '1.0+git${SRCPV}'
399 checkvars['SRC_URI'] = srcuri
400 checkvars['DEPENDS'] = 'libpng pango libx11 libxext jpeg'
401 inherits = ['autotools', 'pkgconfig']
402 self._test_recipe_contents(recipefile, checkvars, inherits)
403
404
405class RecipetoolAppendsrcBase(RecipetoolBase):
406 def _try_recipetool_appendsrcfile(self, testrecipe, newfile, destfile, options, expectedlines, expectedfiles):
407 cmd = 'recipetool appendsrcfile %s %s %s %s %s' % (options, self.templayerdir, testrecipe, newfile, destfile)
408 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
409
410 def _try_recipetool_appendsrcfiles(self, testrecipe, newfiles, expectedlines=None, expectedfiles=None, destdir=None, options=''):
411
412 if destdir:
413 options += ' -D %s' % destdir
414
415 if expectedfiles is None:
416 expectedfiles = [os.path.basename(f) for f in newfiles]
417
418 cmd = 'recipetool appendsrcfiles %s %s %s %s' % (options, self.templayerdir, testrecipe, ' '.join(newfiles))
419 return self._try_recipetool_appendcmd(cmd, testrecipe, expectedfiles, expectedlines)
420
421 def _try_recipetool_appendsrcfile_fail(self, testrecipe, newfile, destfile, checkerror):
422 cmd = 'recipetool appendsrcfile %s %s %s %s' % (self.templayerdir, testrecipe, newfile, destfile or '')
423 result = runCmd(cmd, ignore_status=True)
424 self.assertNotEqual(result.status, 0, 'Command "%s" should have failed but didn\'t' % cmd)
425 self.assertNotIn('Traceback', result.output)
426 for errorstr in checkerror:
427 self.assertIn(errorstr, result.output)
428
429 @staticmethod
430 def _get_first_file_uri(recipe):
431 '''Return the first file:// in SRC_URI for the specified recipe.'''
432 src_uri = get_bb_var('SRC_URI', recipe).split()
433 for uri in src_uri:
434 p = urlparse.urlparse(uri)
435 if p.scheme == 'file':
436 return p.netloc + p.path
437
438 def _test_appendsrcfile(self, testrecipe, filename=None, destdir=None, has_src_uri=True, srcdir=None, newfile=None, options=''):
439 if newfile is None:
440 newfile = self.testfile
441
442 if srcdir:
443 if destdir:
444 expected_subdir = os.path.join(srcdir, destdir)
445 else:
446 expected_subdir = srcdir
447 else:
448 options += " -W"
449 expected_subdir = destdir
450
451 if filename:
452 if destdir:
453 destpath = os.path.join(destdir, filename)
454 else:
455 destpath = filename
456 else:
457 filename = os.path.basename(newfile)
458 if destdir:
459 destpath = destdir + os.sep
460 else:
461 destpath = '.' + os.sep
462
463 expectedlines = ['FILESEXTRAPATHS_prepend := "${THISDIR}/${PN}:"\n',
464 '\n']
465 if has_src_uri:
466 uri = 'file://%s' % filename
467 if expected_subdir:
468 uri += ';subdir=%s' % expected_subdir
469 expectedlines[0:0] = ['SRC_URI += "%s"\n' % uri,
470 '\n']
471
472 return self._try_recipetool_appendsrcfile(testrecipe, newfile, destpath, options, expectedlines, [filename])
473
474 def _test_appendsrcfiles(self, testrecipe, newfiles, expectedfiles=None, destdir=None, options=''):
475 if expectedfiles is None:
476 expectedfiles = [os.path.basename(n) for n in newfiles]
477
478 self._try_recipetool_appendsrcfiles(testrecipe, newfiles, expectedfiles=expectedfiles, destdir=destdir, options=options)
479
480 src_uri = get_bb_var('SRC_URI', testrecipe).split()
481 for f in expectedfiles:
482 if destdir:
483 self.assertIn('file://%s;subdir=%s' % (f, destdir), src_uri)
484 else:
485 self.assertIn('file://%s' % f, src_uri)
486
487 recipefile = get_bb_var('FILE', testrecipe)
488 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
489 filesdir = os.path.join(os.path.dirname(bbappendfile), testrecipe)
490 filesextrapaths = get_bb_var('FILESEXTRAPATHS', testrecipe).split(':')
491 self.assertIn(filesdir, filesextrapaths)
492
493
494class RecipetoolAppendsrcTests(RecipetoolAppendsrcBase):
495 def test_recipetool_appendsrcfile_basic(self):
496 self._test_appendsrcfile('base-files', 'a-file')
497
498 def test_recipetool_appendsrcfile_basic_wildcard(self):
499 testrecipe = 'base-files'
500 self._test_appendsrcfile(testrecipe, 'a-file', options='-w')
501 recipefile = get_bb_var('FILE', testrecipe)
502 bbappendfile = self._check_bbappend(testrecipe, recipefile, self.templayerdir)
503 self.assertEqual(os.path.basename(bbappendfile), '%s_%%.bbappend' % testrecipe)
504
505 def test_recipetool_appendsrcfile_subdir_basic(self):
506 self._test_appendsrcfile('base-files', 'a-file', 'tmp')
507
508 def test_recipetool_appendsrcfile_subdir_basic_dirdest(self):
509 self._test_appendsrcfile('base-files', destdir='tmp')
510
511 def test_recipetool_appendsrcfile_srcdir_basic(self):
512 testrecipe = 'bash'
513 srcdir = get_bb_var('S', testrecipe)
514 workdir = get_bb_var('WORKDIR', testrecipe)
515 subdir = os.path.relpath(srcdir, workdir)
516 self._test_appendsrcfile(testrecipe, 'a-file', srcdir=subdir)
517
518 def test_recipetool_appendsrcfile_existing_in_src_uri(self):
519 testrecipe = 'base-files'
520 filepath = self._get_first_file_uri(testrecipe)
521 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
522 self._test_appendsrcfile(testrecipe, filepath, has_src_uri=False)
523
524 def test_recipetool_appendsrcfile_existing_in_src_uri_diff_params(self):
525 testrecipe = 'base-files'
526 subdir = 'tmp'
527 filepath = self._get_first_file_uri(testrecipe)
528 self.assertTrue(filepath, 'Unable to test, no file:// uri found in SRC_URI for %s' % testrecipe)
529
530 output = self._test_appendsrcfile(testrecipe, filepath, subdir, has_src_uri=False)
531 self.assertTrue(any('with different parameters' in l for l in output))
532
533 def test_recipetool_appendsrcfile_replace_file_srcdir(self):
534 testrecipe = 'bash'
535 filepath = 'Makefile.in'
536 srcdir = get_bb_var('S', testrecipe)
537 workdir = get_bb_var('WORKDIR', testrecipe)
538 subdir = os.path.relpath(srcdir, workdir)
539
540 self._test_appendsrcfile(testrecipe, filepath, srcdir=subdir)
541 bitbake('%s:do_unpack' % testrecipe)
542 self.assertEqual(open(self.testfile, 'r').read(), open(os.path.join(srcdir, filepath), 'r').read())
543
544 def test_recipetool_appendsrcfiles_basic(self, destdir=None):
545 newfiles = [self.testfile]
546 for i in range(1, 5):
547 testfile = os.path.join(self.tempdir, 'testfile%d' % i)
548 with open(testfile, 'w') as f:
549 f.write('Test file %d\n' % i)
550 newfiles.append(testfile)
551 self._test_appendsrcfiles('gcc', newfiles, destdir=destdir, options='-W')
552
553 def test_recipetool_appendsrcfiles_basic_subdir(self):
554 self.test_recipetool_appendsrcfiles_basic(destdir='testdir')