--- /dev/null
+# @file HostBasedUnitTestRunner.py\r
+# Plugin to located any host-based unit tests in the output directory and execute them.\r
+##\r
+# Copyright (c) Microsoft Corporation.\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+import os\r
+import logging\r
+import glob\r
+import xml.etree.ElementTree\r
+from edk2toolext.environment.plugintypes.uefi_build_plugin import IUefiBuildPlugin\r
+from edk2toolext import edk2_logging\r
+import edk2toollib.windows.locate_tools as locate_tools\r
+from edk2toolext.environment import shell_environment\r
+from edk2toollib.utility_functions import RunCmd\r
+\r
+\r
+class HostBasedUnitTestRunner(IUefiBuildPlugin):\r
+\r
+ def do_pre_build(self, thebuilder):\r
+ '''\r
+ Works with the compiler (either the HostBasedCompilerPlugin or an other Builder) to set\r
+ up the environment that will be needed to build host-based unit tests.\r
+\r
+ EXPECTS:\r
+ - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.\r
+\r
+ UPDATES:\r
+ - Shell Var (Several) - Updates the shell with all vars listed in interesting_keys.\r
+ - Shell Path - Updated from QueryVcVariables()\r
+ - Shell Var 'CMOCKA_MESSAGE_OUTPUT'\r
+ '''\r
+ ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')\r
+ if ci_type != 'host_unit_test':\r
+ return 0\r
+\r
+ shell_env = shell_environment.GetEnvironment()\r
+ # Use the tools lib to determine the correct values for the vars that interest us.\r
+ interesting_keys = ["ExtensionSdkDir", "INCLUDE", "LIB", "LIBPATH", "UniversalCRTSdkDir",\r
+ "UCRTVersion", "WindowsLibPath", "WindowsSdkBinPath", "WindowsSdkDir", "WindowsSdkVerBinPath",\r
+ "WindowsSDKVersion", "VCToolsInstallDir"]\r
+ vs_vars = locate_tools.QueryVcVariables(interesting_keys, "amd64")\r
+ for (k, v) in vs_vars.items():\r
+ if k.upper() == "PATH":\r
+ shell_env.append_path(v)\r
+ else:\r
+ shell_env.set_shell_var(k, v)\r
+\r
+ # Set up the reporting type for Cmocka.\r
+ shell_env.set_shell_var('CMOCKA_MESSAGE_OUTPUT', 'xml')\r
+ return 0\r
+\r
+ def do_post_build(self, thebuilder):\r
+ '''\r
+ After a build, will automatically locate and run all host-based unit tests. Logs any\r
+ failures with Warning severity and will return a count of the failures as the return code.\r
+\r
+ EXPECTS:\r
+ - Build Var 'CI_BUILD_TYPE' - If not set to 'host_unit_test', will not do anything.\r
+\r
+ UPDATES:\r
+ - Shell Var 'CMOCKA_XML_FILE'\r
+ '''\r
+ ci_type = thebuilder.env.GetValue('CI_BUILD_TYPE')\r
+ if ci_type != 'host_unit_test':\r
+ return 0\r
+\r
+ shell_env = shell_environment.GetEnvironment()\r
+ logging.log(edk2_logging.get_section_level(),\r
+ "Run Host based Unit Tests")\r
+ path = thebuilder.env.GetValue("BUILD_OUTPUT_BASE")\r
+\r
+ failure_count = 0\r
+\r
+ for arch in thebuilder.env.GetValue("TARGET_ARCH").split():\r
+ logging.log(edk2_logging.get_subsection_level(),\r
+ "Testing for architecture: " + arch)\r
+ cp = os.path.join(path, arch)\r
+\r
+ # If any old results XML files exist, clean them up.\r
+ for old_result in glob.iglob(os.path.join(cp, "*.result.xml")):\r
+ os.remove(old_result)\r
+\r
+ # Determine whether any tests exist.\r
+ testList = glob.glob(os.path.join(cp, "*Test*.exe"))\r
+ for test in testList:\r
+ # Configure output name.\r
+ shell_env.set_shell_var(\r
+ 'CMOCKA_XML_FILE', test + ".%g." + arch + ".result.xml")\r
+\r
+ # Run the test.\r
+ ret = RunCmd('"' + test + '"', "", workingdir=cp)\r
+ if(ret != 0):\r
+ logging.error("UnitTest Execution Error: " +\r
+ os.path.basename(test))\r
+ else:\r
+ logging.info("UnitTest Completed: " +\r
+ os.path.basename(test))\r
+ file_match_pattern = test + ".*." + arch + ".result.xml"\r
+ xml_results_list = glob.glob(file_match_pattern)\r
+ for xml_result_file in xml_results_list:\r
+ root = xml.etree.ElementTree.parse(\r
+ xml_result_file).getroot()\r
+ for suite in root:\r
+ for case in suite:\r
+ for result in case:\r
+ if result.tag == 'failure':\r
+ logging.warning(\r
+ "%s Test Failed" % os.path.basename(test))\r
+ logging.warning(\r
+ " %s - %s" % (case.attrib['name'], result.text))\r
+ failure_count += 1\r
+\r
+ return failure_count\r