1#
2# Copyright OpenEmbedded Contributors
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import re
9import datetime
10
11from oeqa.selftest.case import OESelftestTestCase
12from oeqa.utils.commands import bitbake, get_bb_vars
13
14
15class BuildhistoryBase(OESelftestTestCase):
16
17    def config_buildhistory(self, tmp_bh_location=False):
18        bb_vars = get_bb_vars(['USER_CLASSES', 'INHERIT'])
19        if (not 'buildhistory' in bb_vars['USER_CLASSES']) and (not 'buildhistory' in bb_vars['INHERIT']):
20            add_buildhistory_config = 'INHERIT += "buildhistory"\nBUILDHISTORY_COMMIT = "1"'
21            self.append_config(add_buildhistory_config)
22
23        if tmp_bh_location:
24            # Using a temporary buildhistory location for testing
25            tmp_bh_dir = os.path.join(self.builddir, "tmp_buildhistory_%s" % datetime.datetime.now().strftime('%Y%m%d%H%M%S'))
26            buildhistory_dir_config = "BUILDHISTORY_DIR = \"%s\"" % tmp_bh_dir
27            self.append_config(buildhistory_dir_config)
28            self.track_for_cleanup(tmp_bh_dir)
29
30    def run_buildhistory_operation(self, target, global_config='', target_config='', change_bh_location=False, expect_error=False, error_regex=''):
31        if change_bh_location:
32            tmp_bh_location = True
33        else:
34            tmp_bh_location = False
35        self.config_buildhistory(tmp_bh_location)
36
37        self.append_config(global_config)
38        self.append_recipeinc(target, target_config)
39        bitbake("-cclean %s" % target)
40        result = bitbake(target, ignore_status=True)
41        self.remove_config(global_config)
42        self.remove_recipeinc(target, target_config)
43
44        if expect_error:
45            self.assertEqual(result.status, 1, msg="Error expected for global config '%s' and target config '%s'" % (global_config, target_config))
46            search_for_error = re.search(error_regex, result.output)
47            self.assertTrue(search_for_error, msg="Could not find desired error in output: %s (%s)" % (error_regex, result.output))
48        else:
49            self.assertEqual(result.status, 0, msg="Command 'bitbake %s' has failed unexpectedly: %s" % (target, result.output))
50
51    # No tests should be added to the base class.
52    # Please create a new class that inherit this one, or use one of those already available for adding tests.
53