]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Plugin/HostBasedUnitTestRunner/HostBasedUnitTestRunner.py
c1eeaf26251eeb0a79a8cbe14494d0cb88bc739a
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
12 import xml
.etree
.ElementTree
13 from edk2toolext
.environment
.plugintypes
.uefi_build_plugin
import IUefiBuildPlugin
14 from edk2toolext
import edk2_logging
15 import edk2toollib
.windows
.locate_tools
as locate_tools
16 from edk2toolext
.environment
import shell_environment
17 from edk2toollib
.utility_functions
import RunCmd
18 from edk2toollib
.utility_functions
import GetHostInfo
21 class HostBasedUnitTestRunner(IUefiBuildPlugin
):
23 def do_pre_build(self
, thebuilder
):
30 def do_post_build(self
, thebuilder
):
32 After a build, will automatically locate and run all host-based unit tests. Logs any
33 failures with Warning severity and will return a count of the failures as the return code.
36 - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.
39 - Shell Var 'CMOCKA_XML_FILE'
41 ci_type
= thebuilder
.env
.GetValue('CI_BUILD_TYPE')
42 if ci_type
!= 'host_unit_test':
45 shell_env
= shell_environment
.GetEnvironment()
46 logging
.log(edk2_logging
.get_section_level(),
47 "Run Host based Unit Tests")
48 path
= thebuilder
.env
.GetValue("BUILD_OUTPUT_BASE")
52 # Set up the reporting type for Cmocka.
53 shell_env
.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')
55 for arch
in thebuilder
.env
.GetValue("TARGET_ARCH").split():
56 logging
.log(edk2_logging
.get_subsection_level(),
57 "Testing for architecture: " + arch
)
58 cp
= os
.path
.join(path
, arch
)
60 # If any old results XML files exist, clean them up.
61 for old_result
in glob
.iglob(os
.path
.join(cp
, "*.result.xml")):
64 # Find and Run any Host Tests
65 if GetHostInfo().os
.upper() == "LINUX":
66 testList
= glob
.glob(os
.path
.join(cp
, "*Test*"))
68 p
= os
.path
.join(cp
, a
)
70 if not os
.path
.isfile(p
):
72 logging
.debug(f
"Remove directory file: {p}")
74 # It must be executable
75 if os
.stat(p
).st_mode
& (stat
.S_IEXEC | stat
.S_IXGRP | stat
.S_IXOTH
) == 0:
77 logging
.debug(f
"Remove non-executable file: {p}")
80 logging
.info(f
"Test file found: {p}")
82 elif GetHostInfo().os
.upper() == "WINDOWS":
83 testList
= glob
.glob(os
.path
.join(cp
, "*Test*.exe"))
85 raise NotImplementedError("Unsupported Operating System")
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
))