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