Skip to content

Commit

Permalink
Merge branch 'personal/klautner/add_panic_library' of https://github.…
Browse files Browse the repository at this point in the history
…com/kenlautner/mu_basecore into personal/klautner/add_panic_library
  • Loading branch information
kenlautner committed Sep 29, 2023
2 parents ee31bcc + f361fec commit 0c1eadd
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 17 deletions.
16 changes: 10 additions & 6 deletions .pytool/Plugin/RustHostUnitTestPlugin/Readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
# Rust Host Unit Test Plugin

This CI plugin runs all unit tests with coverage enabled, calculating coverage results on a per package basis. It filters results to only calculate coverage on files within the package.

This CI plugin will also calculate coverage for the entire workspace.
This CI plugin runs all unit tests and requires that they pass. If code coverage is turned on with
`"CalculateCoverage": true`, then coverage percentages will be calculated on a per rust crate basis.

## Plugin Customizations

As a default, this plugin requires 75% coverage, though this can be configured within a packages ci.yaml file by adding the entry `RustCoverageCheck`. The required coverage percent can also be customized on a per (rust) package bases.
- `CalculateCoverage`: true / false - Whether or not to calculate coverage results
- `Coverage`: int (0, 1) - The percentage of coverage required to pass the CI check, if `CalculateCoverage` is enabled
- `CoverageOverrides` int (0, 1) - Crate specific override of percentage needed to pass

As a default, Calculating Coverage is enabled and at least 75% (.75) code coverage is required to pass.

### Example ci settings

``` yaml
"RustHostUnitTestPlugin": {
"CalculateCoverage": true,
"Coverage": 1,
"CoverageOverrides": {
"DxeRust": 0.0,
"UefiEventLib": 0.0,
"DxeRust": 0.4,
"UefiEventLib": 0.67,
}
}
```
15 changes: 13 additions & 2 deletions .pytool/Plugin/RustHostUnitTestPlugin/RustHostUnitTestPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
import re
import logging
import platform

class RustHostUnitTestPlugin(ICiBuildPlugin):
def GetTestName(self, packagename: str, environment: object) -> tuple[str, str]:
Expand All @@ -20,9 +21,14 @@ def RunsOnTargetList(self) -> List[str]:
return ["NO-TARGET"]

def RunBuildPlugin(self, packagename, Edk2pathObj, pkgconfig, environment, PLM, PLMHelper, tc, output_stream):

ws = Edk2pathObj.WorkspacePath
rust_ws = PLMHelper.RustWorkspace(ws) # .pytool/Plugin/RustPackageHelper

with_coverage = pkgconfig.get("CalculateCoverage", True)

if platform.system() == "Windows" and platform.machine() == 'aarch64':
logging.debug("Coverage skipped by plugin, not supported on Windows ARM")
with_coverage = False

# Build list of packages that are in the EDK2 package we are running CI on
pp = Path(Edk2pathObj.GetAbsolutePathOnThisSystemFromEdk2RelativePath(packagename))
Expand All @@ -43,7 +49,7 @@ def RunBuildPlugin(self, packagename, Edk2pathObj, pkgconfig, environment, PLM,

# Run tests and evaluate results
try:
results = rust_ws.coverage(crate_name_list, ignore_list = ignore_list, report_type = "xml")
results = rust_ws.test(crate_name_list, ignore_list = ignore_list, report_type = "xml", coverage=with_coverage)
except RuntimeError as e:
logging.warning(str(e))
tc.LogStdError(str(e))
Expand All @@ -66,6 +72,11 @@ def RunBuildPlugin(self, packagename, Edk2pathObj, pkgconfig, environment, PLM,
tc.SetFailed(f'Host unit tests failed. Failures {failed}', "CHECK_FAILED")
return failed

# Return if we are not running with coverage
if not with_coverage:
tc.SetSuccess()
return 0

# Calculate coverage
coverage = {}
for file, cov in results["coverage"].items():
Expand Down
25 changes: 16 additions & 9 deletions .pytool/Plugin/RustPackageHelper/RustPackageHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,26 @@ def __set_members(self):

self.members = list(members)

def coverage(self, pkg_list = None, ignore_list = None, report_type: str = "html" ):
"""Runs coverage at the workspace level.
def test(self, pkg_list: list[str] = None, ignore_list: list[str] = None, report_type: str = "html", coverage: bool = True):
"""Runs tests on a list of rust packages / crates.
Generates a single report that provides coverage information for all
packages in the workspace.
Will additionally calculate code coverage if coverage is set to True.
"""
if pkg_list is None:
pkg_list = [pkg.name for pkg in self.members]

# Set up the command
command = "cargo"
params = "make"
if ignore_list:
params += f' -e COV_FLAGS="--out {report_type} --exclude-files {" --exclude-files ".join(ignore_list)}"'

if coverage:
if ignore_list:
params += f' -e COV_FLAGS="--out {report_type} --exclude-files {" --exclude-files ".join(ignore_list)}"'
else:
params += f' -e COV_FLAGS="--out {report_type}"'
params += f" coverage {','.join(pkg_list)}"
else:
params += f' -e COV_FLAGS="--out {report_type}"'
params += f" coverage {','.join(pkg_list)}"
params += f" test {','.join(pkg_list)}"

# Run the command
output = io.StringIO()
Expand All @@ -96,9 +99,10 @@ def coverage(self, pkg_list = None, ignore_list = None, report_type: str = "html
line = line.replace("test ", "")
if line.endswith("... ok"):
result["pass"].append(line.replace(" ... ok", ""))
elif line.endswith("... ignored"):
continue
else:
result["fail"].append(line.replace(" ... FAILED", ""))
continue

# Command failed, but we didn't parse any failed tests
if return_value != 0 and len(result["fail"]) == 0:
Expand All @@ -107,6 +111,9 @@ def coverage(self, pkg_list = None, ignore_list = None, report_type: str = "html
if len(result["fail"]) > 0:
return result

if not coverage:
return result

# Determine coverage if all tests passed
for line in lines:
line = line.strip().strip("\n")
Expand Down
49 changes: 49 additions & 0 deletions MdePkg/Test/Mock/Include/GoogleTest/Ppi/MockReadOnlyVariable2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/** @file MockReadOnlyVariable2.h
This file declares a mock of Read-only Variable Service2 PPI.
This PPI permits read-only access to the UEFI variable store during the PEI phase.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#ifndef MOCK_PEI_READ_ONLY_VARIABLE2_PPI_H
#define MOCK_PEI_READ_ONLY_VARIABLE2_PPI_H

#include <Library/GoogleTestLib.h>
#include <Library/FunctionMockLib.h>
extern "C" {
#include <Uefi.h>
#include <Pi/PiPeiCis.h>
#include <Ppi/ReadOnlyVariable2.h>
}

struct MockReadOnlyVariable2 {
MOCK_INTERFACE_DECLARATION (MockReadOnlyVariable2);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
GetVariable,
(IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
IN CONST CHAR16 *VariableName,
IN CONST EFI_GUID *VariableGuid,
OUT UINT32 *Attributes,
IN OUT UINTN *DataSize,
OUT VOID *Data OPTIONAL)
);

MOCK_FUNCTION_DECLARATION (
EFI_STATUS,
NextVariableName,
(IN CONST EFI_PEI_READ_ONLY_VARIABLE2_PPI *This,
IN OUT UINTN *VariableNameSize,
IN OUT CHAR16 *VariableName,
IN OUT EFI_GUID *VariableGuid)
);
};

extern "C" {
extern EFI_PEI_READ_ONLY_VARIABLE2_PPI *PpiReadOnlyVariableServices;
}

#endif
23 changes: 23 additions & 0 deletions MdePkg/Test/Mock/Library/GoogleTest/Ppi/MockReadOnlyVariable2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @file MockReadOnlyVariable2.cpp
Google Test mock for ReadOnlyVariable2
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include <GoogleTest/Ppi/MockReadOnlyVariable2.h>

MOCK_INTERFACE_DEFINITION (MockReadOnlyVariable2);
MOCK_FUNCTION_DEFINITION (MockReadOnlyVariable2, GetVariable, 6, EFIAPI);
MOCK_FUNCTION_DEFINITION (MockReadOnlyVariable2, NextVariableName, 4, EFIAPI);

// Normally PpiVariableServices is "found"
// This will be defined INSIDE the test, with its definition pointing to the mock function GetVariable
EFI_PEI_READ_ONLY_VARIABLE2_PPI PeiReadOnlyVariablePpi = {
GetVariable, // EFI_PEI_GET_VARIABLE2
NextVariableName // EFI_PEI_GET_NEXT_VARIABLE_NAME2
};

extern "C" {
EFI_PEI_READ_ONLY_VARIABLE2_PPI *PpiReadOnlyVariableServices = &PeiReadOnlyVariablePpi;
}

0 comments on commit 0c1eadd

Please sign in to comment.