]>
git.proxmox.com Git - mirror_edk2.git/blob - 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.
4 # Copyright (c) Microsoft Corporation.
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
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
19 class HostBasedUnitTestRunner(IUefiBuildPlugin
):
21 def do_pre_build(self
, thebuilder
):
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.
27 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
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'
34 ci_type
= thebuilder
.env
.GetValue('CI_BUILD_TYPE')
35 if ci_type
!= 'host_unit_test':
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
)
48 shell_env
.set_shell_var(k
, v
)
50 # Set up the reporting type for Cmocka.
51 shell_env
.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')
54 def do_post_build(self
, thebuilder
):
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.
60 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
63 - Shell Var 'CMOCKA_XML_FILE'
65 ci_type
= thebuilder
.env
.GetValue('CI_BUILD_TYPE')
66 if ci_type
!= 'host_unit_test':
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")
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
)
81 # If any old results XML files exist, clean them up.
82 for old_result
in glob
.iglob(os
.path
.join(cp
, "*.result.xml")):
85 # Determine whether any tests exist.
86 testList
= glob
.glob(os
.path
.join(cp
, "*Test*.exe"))
88 # Configure output name.
89 shell_env
.set_shell_var(
90 'CMOCKA_XML_FILE', test
+ ".%g." + arch
+ ".result.xml")
93 ret
= RunCmd('"' + test
+ '"', "", workingdir
=cp
)
95 logging
.error("UnitTest Execution Error: " +
96 os
.path
.basename(test
))
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()
108 if result
.tag
== 'failure':
110 "%s Test Failed" % os
.path
.basename(test
))
112 " %s - %s" % (case
.attrib
['name'], result
.text
))