Skip to content

Add unit tests for ApiContract - #14305

Closed
jankratochvilcz wants to merge 1 commit into
mainfrom
jankratochvilcz-add-apicontract-tests
Closed

jankratochvilcz wants to merge 1 commit into
mainfrom
jankratochvilcz-add-apicontract-tests

Conversation

@jankratochvilcz

Copy link
Copy Markdown
Contributor

Adds unit-test coverage for the previously-untested ApiContract helper (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-ApiContract children, and no-ops for non-ContainedApiContracts and 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.

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]>
Copilot AI review requested due to automatic review settings July 9, 2026 12:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 IsContainedApiContractsElement and IsVersionedContentElement covering matching and non-matching names (including casing differences).
  • Adds tests for ReadContractsElement covering normal parsing, missing version, ignoring non-ApiContract children, 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;
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Caution

agentic threat detected
Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.

Details

The threat detection results could not be parsed.

Review the workflow run logs for details.

Error Message Quality — LGTM

Generated by Expert Code Review (on open) for #14305 · 1.1K AIC · ⊞ 32.2K · ◷

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Caution

agentic threat detected
Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.

Details

The threat detection results could not be parsed.

Review the workflow run logs for details.

SDK Integration Boundaries — LGTM

Generated by Expert Code Review (on open) for #14305 · 1.1K AIC · ⊞ 32.2K · ◷

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Caution

agentic threat detected
Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.

Details

The threat detection results could not be parsed.

Review the workflow run logs for details.

Security Awareness — LGTM

Generated by Expert Code Review (on open) for #14305 · 1.1K AIC · ⊞ 32.2K · ◷

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

agentic threat detected
Threat detection flagged this output in warn mode. Manual review is REQUIRED before any follow-up automation.

Details

The threat detection results could not be parsed.

Review the workflow run logs for details.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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("")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 gracefully

Same applies to the corresponding IsVersionedContentElementRejectsNonMatchingName Theory (line 47).

contracts.Count.ShouldBe(1);
contracts[0].Name.ShouldBe("System");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
}

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 7b27e82a Deployed Jul 9, 2026 by jankratochvilcz via conclusion #418
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants