xref: /openbmc/openbmc/poky/bitbake/lib/bb/tests/cooker.py (revision 6ce62a20)
1#
2# BitBake Tests for cooker.py
3#
4# SPDX-License-Identifier: GPL-2.0-only
5#
6
7import unittest
8import os
9import bb, bb.cooker
10import re
11import logging
12
13# Cooker tests
14class CookerTest(unittest.TestCase):
15    def setUp(self):
16        # At least one variable needs to be set
17        self.d = bb.data.init()
18        topdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata/cooker")
19        self.d.setVar('TOPDIR', topdir)
20
21    def test_CookerCollectFiles_sublayers(self):
22        '''Test that a sublayer of an existing layer does not trigger
23           No bb files matched ...'''
24
25        def append_collection(topdir, path, d):
26            collection = path.split('/')[-1]
27            pattern = "^" + topdir + "/" + path + "/"
28            regex = re.compile(pattern)
29            priority = 5
30
31            d.setVar('BBFILE_COLLECTIONS', (d.getVar('BBFILE_COLLECTIONS') or "") + " " + collection)
32            d.setVar('BBFILE_PATTERN_%s' % (collection), pattern)
33            d.setVar('BBFILE_PRIORITY_%s' % (collection), priority)
34
35            return (collection, pattern, regex, priority)
36
37        topdir = self.d.getVar("TOPDIR")
38
39        # Priorities: list of (collection, pattern, regex, priority)
40        bbfile_config_priorities = []
41        # Order is important for this test, shortest to longest is typical failure case
42        bbfile_config_priorities.append( append_collection(topdir, 'first', self.d) )
43        bbfile_config_priorities.append( append_collection(topdir, 'second', self.d) )
44        bbfile_config_priorities.append( append_collection(topdir, 'second/third', self.d) )
45
46        pkgfns = [ topdir + '/first/recipes/sample1_1.0.bb',
47                   topdir + '/second/recipes/sample2_1.0.bb',
48                   topdir + '/second/third/recipes/sample3_1.0.bb' ]
49
50        class LogHandler(logging.Handler):
51            def __init__(self):
52                logging.Handler.__init__(self)
53                self.logdata = []
54
55            def emit(self, record):
56                self.logdata.append(record.getMessage())
57
58        # Move cooker to use my special logging
59        logger = bb.cooker.logger
60        log_handler = LogHandler()
61        logger.addHandler(log_handler)
62        collection = bb.cooker.CookerCollectFiles(bbfile_config_priorities)
63        collection.collection_priorities(pkgfns, pkgfns, self.d)
64        logger.removeHandler(log_handler)
65
66        # Should be empty (no generated messages)
67        expected = []
68
69        self.assertEqual(log_handler.logdata, expected)
70