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