Repository navigation
Add unit tests for ApiContract - #14305
jankratochvilcz wants to merge 1 commit into
Conversation
Adds coverage for the previously-untested ApiContract helper in src/Utilities/ApiContract.cs, covering IsContainedApiContractsElement, IsVersionedContentElement, and ReadContractsElement. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds a new unit test file to cover the existing Microsoft.Build.Utilities.ApiContract helper, improving regression safety around XML parsing and element-name recognition logic.
Changes:
- Adds tests for
IsContainedApiContractsElementandIsVersionedContentElementcovering matching and non-matching names (including casing differences). - Adds tests for
ReadContractsElementcovering normal parsing, missingversion, ignoring non-ApiContractchildren, and no-op behavior for null / wrong root element.
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| #nullable disable |
| public void ReadContractsElementDoesNothingForNullElement() | ||
| { | ||
| List<ApiContract> contracts = new List<ApiContract>(); | ||
| ApiContract.ReadContractsElement(null, contracts); |
| { | ||
| XmlDocument document = new XmlDocument(); | ||
| document.LoadXml(xml); | ||
| return document.DocumentElement; |
|
Caution agentic threat detected Error Message Quality — LGTM
|
|
Caution agentic threat detected SDK Integration Boundaries — LGTM
|
|
Caution agentic threat detected Security Awareness — LGTM
|
There was a problem hiding this comment.
Caution
agentic threat detected
Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.
Idiomatic C# Patterns — 4 issues found
| # | Line(s) | Severity | Finding |
|---|---|---|---|
| 1 | 10 | High | #nullable disable — forbidden in new files; enable nullable and annotate as needed |
| 2 | 12 | Medium | Block-scoped namespace — use file-scoped namespace Microsoft.Build.UnitTests; |
| 3 | 61, 78, 94, 108, 117 | Medium | new List<ApiContract>() — use C# 14 collection expression [] |
| 4 | 125 | Low | XmlDocument document = new XmlDocument() — use var since type is evident from RHS |
Generated by Expert Code Review (on open) for #14305 · 1.1K AIC · ⊞ 32.2K
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| #nullable disable |
There was a problem hiding this comment.
ISSUE — Idiomatic C# Patterns | Severity: High
#nullable disable must not appear in new files. MSBuild repository rules require all new files to use nullable reference types without disabling the feature.
Recommendation: Remove #nullable disable. Annotate any parameter/return types that can be null with ? (e.g., the null passed to ReadContractsElement in the test at line 118 would require the parameter to be XmlElement?).
|
|
||
| #nullable disable | ||
|
|
||
| namespace Microsoft.Build.UnitTests |
There was a problem hiding this comment.
ISSUE — Idiomatic C# Patterns | Severity: Medium
Block-scoped namespace. MSBuild style requires file-scoped namespace declarations.
Recommendation:
namespace Microsoft.Build.UnitTests;
public sealed class ApiContract_Tests
{
...
}| <ApiContract name=""Windows.Foundation"" version=""1.0.0.0"" /> | ||
| </ContainedApiContracts>"); | ||
|
|
||
| List<ApiContract> contracts = new List<ApiContract>(); |
There was a problem hiding this comment.
ISSUE — Idiomatic C# Patterns | Severity: Medium
new List<ApiContract>() should use the C# 14 collection expression []. This same pattern repeats on lines 78, 94, 108, and 117.
Recommendation:
List<ApiContract> contracts = [];|
|
||
| private static XmlElement CreateElement(string xml) | ||
| { | ||
| XmlDocument document = new XmlDocument(); |
There was a problem hiding this comment.
ISSUE — Idiomatic C# Patterns | Severity: Low
Explicit type where var is preferred. The type is obvious from the new XmlDocument() constructor call on the right-hand side.
Recommendation:
var document = new XmlDocument();| [InlineData("CONTAINEDAPICONTRACTS")] | ||
| [InlineData("ApiContract")] | ||
| [InlineData("SomethingElse")] | ||
| [InlineData("")] |
There was a problem hiding this comment.
NIT — Coverage gap: null not tested
string.Equals(null, "ContainedApiContracts", StringComparison.Ordinal) returns false safely. The Theory already tests "" but not null. Consider adding [InlineData(null)] to document that null is also safely rejected:
[InlineData("containedapicontracts")]
[InlineData("CONTAINEDAPICONTRACTS")]
[InlineData("ApiContract")]
[InlineData("SomethingElse")]
[InlineData("")]
[InlineData(null)] // string.Equals handles null gracefullySame applies to the corresponding IsVersionedContentElementRejectsNonMatchingName Theory (line 47).
| contracts.Count.ShouldBe(1); | ||
| contracts[0].Name.ShouldBe("System"); | ||
| } | ||
|
|
There was a problem hiding this comment.
NIT — Coverage gap: empty ContainedApiContracts not tested
The test suite has ReadContractsElementDoesNothingForNullElement and ReadContractsElementDoesNothingForNonContainedApiContractsElement, but no test for a valid-but-empty root element. This exercises a distinct code path — the if guard passes but the foreach body never executes:
[Fact]
public void ReadContractsElementWithNoChildrenReturnsEmpty()
{
XmlElement element = CreateElement("<ContainedApiContracts />");
List<ApiContract> contracts = new List<ApiContract>();
ApiContract.ReadContractsElement(element, contracts);
contracts.Count.ShouldBe(0);
}|
|
||
| contracts.Count.ShouldBe(1); | ||
| contracts[0].Name.ShouldBe("System"); | ||
| contracts[0].Version.ShouldBe(string.Empty); |
There was a problem hiding this comment.
NIT — Coverage gap: missing name attribute not tested
ReadContractsElementUsesEmptyStringForMissingVersion correctly documents that a missing version attribute yields "" (via XmlElement.GetAttribute). The symmetric case — an ApiContract element without a name attribute — is not tested. GetAttribute("name") would also return "", so contracts[0].Name would be string.Empty.
Consider adding:
[Fact]
public void ReadContractsElementUsesEmptyStringForMissingName()
{
XmlElement element = CreateElement(@"<ContainedApiContracts>
<ApiContract version=""1.0.0.0"" />
</ContainedApiContracts>");
List<ApiContract> contracts = new List<ApiContract>();
ApiContract.ReadContractsElement(element, contracts);
contracts.Count.ShouldBe(1);
contracts[0].Name.ShouldBe(string.Empty);
contracts[0].Version.ShouldBe("1.0.0.0");
}
Adds unit-test coverage for the previously-untested
ApiContracthelper (src/Utilities/ApiContract.cs), which had no dedicated test file.New file:
src/Utilities.UnitTests/ApiContract_Tests.cs, covering:IsContainedApiContractsElement— matching name, case-sensitivity, and non-matching cases.IsVersionedContentElement— matching name, case-sensitivity, and non-matching cases.ReadContractsElement— populates the collection with expected Name/Version from an XML fixture, handles missing version (empty string), ignores non-ApiContractchildren, and no-ops for non-ContainedApiContractsand null elements.Pure test addition — no product code changes. Follows the xUnit + Shouldly style of neighbouring tests. All added tests pass with no new warnings.