1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8
9from oeqa.selftest.case import OESelftestTestCase
10from oeqa.utils.commands import bitbake, get_bb_var
11import oeqa.utils.ftools as ftools
12
13class LayerAppendTests(OESelftestTestCase):
14    layerconf = """
15# We have a conf and classes directory, append to BBPATH
16BBPATH .= ":${LAYERDIR}"
17
18# We have a recipes directory, add to BBFILES
19BBFILES += "${LAYERDIR}/recipes*/*.bb ${LAYERDIR}/recipes*/*.bbappend"
20
21BBFILE_COLLECTIONS += "meta-layerINT"
22BBFILE_PATTERN_meta-layerINT := "^${LAYERDIR}/"
23BBFILE_PRIORITY_meta-layerINT = "6"
24"""
25    recipe = """
26LICENSE="CLOSED"
27INHIBIT_DEFAULT_DEPS = "1"
28
29python do_build() {
30    bb.plain('Building ...')
31}
32addtask build
33"""
34    append = """
35FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
36
37SRC_URI:append = " file://appendtest.txt"
38
39sysroot_stage_all:append() {
40	install -m 644 ${WORKDIR}/appendtest.txt ${SYSROOT_DESTDIR}/
41}
42
43"""
44
45    append2 = """
46FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
47
48SRC_URI:append = " file://appendtest.txt"
49"""
50    layerappend = ''
51
52    def tearDownLocal(self):
53        if self.layerappend:
54            ftools.remove_from_file(self.builddir + "/conf/bblayers.conf", self.layerappend)
55        super(LayerAppendTests, self).tearDownLocal()
56
57    def test_layer_appends(self):
58        corebase = get_bb_var("COREBASE")
59
60        for l in ["0", "1", "2"]:
61            layer = os.path.join(corebase, "meta-layertest" + l)
62            self.assertFalse(os.path.exists(layer))
63            os.mkdir(layer)
64            os.mkdir(layer + "/conf")
65            with open(layer + "/conf/layer.conf", "w") as f:
66                f.write(self.layerconf.replace("INT", l))
67            os.mkdir(layer + "/recipes-test")
68            if l == "0":
69                with open(layer + "/recipes-test/layerappendtest.bb", "w") as f:
70                    f.write(self.recipe)
71            elif l == "1":
72                with open(layer + "/recipes-test/layerappendtest.bbappend", "w") as f:
73                    f.write(self.append)
74                os.mkdir(layer + "/recipes-test/layerappendtest")
75                with open(layer + "/recipes-test/layerappendtest/appendtest.txt", "w") as f:
76                    f.write("Layer 1 test")
77            elif l == "2":
78                with open(layer + "/recipes-test/layerappendtest.bbappend", "w") as f:
79                    f.write(self.append2)
80                os.mkdir(layer + "/recipes-test/layerappendtest")
81                with open(layer + "/recipes-test/layerappendtest/appendtest.txt", "w") as f:
82                    f.write("Layer 2 test")
83            self.track_for_cleanup(layer)
84
85        self.layerappend = "BBLAYERS += \"{0}/meta-layertest0 {0}/meta-layertest1 {0}/meta-layertest2\"".format(corebase)
86        ftools.append_file(self.builddir + "/conf/bblayers.conf", self.layerappend)
87        stagingdir = get_bb_var("SYSROOT_DESTDIR", "layerappendtest")
88        bitbake("layerappendtest")
89        data = ftools.read_file(stagingdir + "/appendtest.txt")
90        self.assertEqual(data, "Layer 2 test")
91        os.remove(corebase + "/meta-layertest2/recipes-test/layerappendtest/appendtest.txt")
92        bitbake("layerappendtest")
93        data = ftools.read_file(stagingdir + "/appendtest.txt")
94        self.assertEqual(data, "Layer 1 test")
95        with open(corebase + "/meta-layertest2/recipes-test/layerappendtest/appendtest.txt", "w") as f:
96            f.write("Layer 2 test")
97        bitbake("layerappendtest")
98        data = ftools.read_file(stagingdir + "/appendtest.txt")
99        self.assertEqual(data, "Layer 2 test")
100