]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py
92426760ae0f67f9cb1cd6df4258939963693bb6
[mirror_edk2.git] / BaseTools / Plugin / HostBasedUnitTestRunner / HostBasedUnitTestRunner.py
1 # @file HostBasedUnitTestRunner.py
2 # Plugin to located any host-based unit tests in the output directory and execute them.
3 ##
4 # Copyright (c) Microsoft Corporation.
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
6 #
7 ##
8 import os
9 import logging
10 import glob
11 import xml.etree.ElementTree
12 from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin
13 from edk2toolext import edk2_logging
14 import edk2toollib.windows.locate_tools as locate_tools
15 from edk2toolext.environment import shell_environment
16 from edk2toollib.utility_functions import RunCmd
17
18
19 class HostBasedUnitTestRunner(IUefiBuildPlugin):
20
21 def do_pre_build(self, thebuilder):
22 '''
23 Works with the compiler (either the HostBasedCompilerPlugin or an other Builder) to set
24 up the environment that will be needed to build host-based unit tests.
25
26 EXPECTS:
27 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
28
29 UPDATES:
30 - Shell Var (Several) - Updates the shell with all vars listed in interesting_keys.
31 - Shell Path - Updated from QueryVcVariables()
32 - Shell Var 'CMOCKA_MESSAGE_OUTPUT'
33 '''
34 ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')
35 if ci_type != 'host_unit_test':
36 return 0
37
38 shell_env = shell_environment.GetEnvironment()
39 # Use the tools lib to determine the correct values for the vars that interest us.
40 interesting_keys = ["ExtensionSdkDir", "INCLUDE", "LIB", "LIBPATH", "UniversalCRTSdkDir",
41 "UCRTVersion", "WindowsLibPath", "WindowsSdkBinPath", "WindowsSdkDir", "WindowsSdkVerBinPath",
42 "WindowsSDKVersion", "VCToolsInstallDir"]
43 vs_vars = locate_tools.QueryVcVariables(interesting_keys, "amd64")
44 for (k, v) in vs_vars.items():
45 if k.upper() == "PATH":
46 shell_env.append_path(v)
47 else:
48 shell_env.set_shell_var(k, v)
49
50 # Set up the reporting type for Cmocka.
51 shell_env.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')
52 return 0
53
54 def do_post_build(self, thebuilder):
55 '''
56 After a build, will automatically locate and run all host-based unit tests. Logs any
57 failures with Warning severity and will return a count of the failures as the return code.
58
59 EXPECTS:
60 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
61
62 UPDATES:
63 - Shell Var 'CMOCKA_XML_FILE'
64 '''
65 ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')
66 if ci_type != 'host_unit_test':
67 return 0
68
69 shell_env = shell_environment.GetEnvironment()
70 logging.log(edk2_logging.get_section_level(),
71 "Run Host based Unit Tests")
72 path = thebuilder.env.GetValue("BUILD_OUTPUT_BASE")
73
74 failure_count = 0
75
76 for arch in thebuilder.env.GetValue("TARGET_ARCH").split():
77 logging.log(edk2_logging.get_subsection_level(),
78 "Testing for architecture: " + arch)
79 cp = os.path.join(path, arch)
80
81 # If any old results XML files exist, clean them up.
82 for old_result in glob.iglob(os.path.join(cp, "*.result.xml")):
83 os.remove(old_result)
84
85 # Determine whether any tests exist.
86 testList = glob.glob(os.path.join(cp, "*Test*.exe"))
87 for test in testList:
88 # Configure output name.
89 shell_env.set_shell_var(
90 'CMOCKA_XML_FILE', test + ".%g." + arch + ".result.xml")
91
92 # Run the test.
93 ret = RunCmd('"' + test + '"', "", workingdir=cp)
94 if(ret != 0):
95 logging.error("UnitTest Execution Error: " +
96 os.path.basename(test))
97 else:
98 logging.info("UnitTest Completed: " +
99 os.path.basename(test))
100 file_match_pattern = test + ".*." + arch + ".result.xml"
101 xml_results_list = glob.glob(file_match_pattern)
102 for xml_result_file in xml_results_list:
103 root = xml.etree.ElementTree.parse(
104 xml_result_file).getroot()
105 for suite in root:
106 for case in suite:
107 for result in case:
108 if result.tag == 'failure':
109 logging.warning(
110 "%s Test Failed" % os.path.basename(test))
111 logging.warning(
112 " %s - %s" % (case.attrib['name'], result.text))
113 failure_count += 1
114
115 return failure_count