Repository navigation
Add [self, nextMajor) upper bound on extensions' Microsoft.Testing.Platform dependency #9730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amaury Levé (Evangelink)
merged 2 commits into
main
from
dev/amauryleve/platform-dep-upper-bounds
Jul 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <Project> | ||
|
|
||
| <Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))" /> | ||
|
|
||
| <!-- | ||
| Add a [self, nextMajor) upper bound on the in-repo Microsoft.Testing.Platform package dependency that | ||
| the extensions emit through their ProjectReference to Microsoft.Testing.Platform.csproj. | ||
|
|
||
| By default NuGet packs a project reference as an open-ended minimum-version dependency (e.g. | ||
| "2.4.0" == "[2.4.0, )"). That lets an extension built against Testing Platform 2.x silently resolve a | ||
| future breaking 3.0.0. Because the extensions and the platform are versioned and ship in lockstep, we | ||
| cap the dependency at the next major as a safety net: "[2.4.0, 3.0.0)". | ||
|
|
||
| This runs after NuGet's _GetProjectReferenceVersions, which populates @(_ProjectReferencesWithVersions) | ||
| (identity = referenced project's full path, so %(Filename) is the project name) with the resolved | ||
| %(ProjectVersion). We only rewrite the Microsoft.Testing.Platform entry, and only when it is still a | ||
| bare version (not already a range), so the change is idempotent. | ||
| --> | ||
| <Target Name="_AddTestingPlatformDependencyUpperBound" | ||
| AfterTargets="_GetProjectReferenceVersions" | ||
| Condition=" '$(TestingPlatformNextMajorVersion)' != '' "> | ||
| <ItemGroup> | ||
| <_ProjectReferencesWithVersions Condition=" '%(Filename)' == 'Microsoft.Testing.Platform' and !$([System.String]::new('%(ProjectVersion)').StartsWith('[')) "> | ||
| <ProjectVersion>[%(ProjectVersion), $(TestingPlatformNextMajorVersion))</ProjectVersion> | ||
| </_ProjectReferencesWithVersions> | ||
| </ItemGroup> | ||
| </Target> | ||
|
|
||
| </Project> | ||
121 changes: 121 additions & 0 deletions
121
...icrosoft.Testing.Platform.Acceptance.IntegrationTests/PackageDependencyUpperBoundTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.IO.Compression; | ||
|
|
||
| namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests; | ||
|
|
||
| /// <summary> | ||
| /// Guards the pack-time target in <c>src/Platform/Directory.Build.targets</c> that caps the packed | ||
| /// <c>Microsoft.Testing.Platform</c> package dependency of the in-repo extensions at a <c>[self, nextMajor)</c> | ||
| /// range. That target hooks NuGet-internal targets (<c>_GetProjectReferenceVersions</c> / | ||
| /// <c>_ProjectReferencesWithVersions</c>); if a future NuGet SDK renames them, the target would silently stop | ||
| /// firing and the dependency would revert to an open-ended minimum-version (<c>2.4.0</c> == <c>[2.4.0, )</c>). | ||
| /// These tests inspect the actually-produced <c>.nupkg</c> files so such a regression fails the build. | ||
| /// </summary> | ||
| [TestClass] | ||
| public sealed class PackageDependencyUpperBoundTests | ||
| { | ||
| private const string PlatformPackageId = "Microsoft.Testing.Platform"; | ||
|
|
||
| // The exclusive upper bound the pack-time target applies is the next major after the current Testing | ||
| // Platform version. Read the source of truth (eng/Versions.props) rather than a packed version so the test | ||
| // stays independent of which packages happen to be present. | ||
| private static readonly string ExpectedUpperBound = $"{GetTestingPlatformMajorVersion() + 1}.0.0"; | ||
|
|
||
| public TestContext TestContext { get; set; } = null!; | ||
|
|
||
| /// <summary> | ||
| /// Every package that declares a direct <c>Microsoft.Testing.Platform</c> dependency must express it as a | ||
| /// closed <c>[min, max)</c> range whose upper bound is the next major, never an open-ended minimum version. | ||
| /// At least one such package is expected, so an empty artifacts folder (or a target that silently dropped | ||
| /// all dependencies) fails rather than vacuously passing. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void PackedExtensions_CapMicrosoftTestingPlatformDependency_AtNextMajor() | ||
| { | ||
| List<string> checkedPackages = []; | ||
| List<string> violations = []; | ||
|
|
||
| foreach (string nupkg in EnumerateProducedPackages()) | ||
| { | ||
| foreach (string version in GetPlatformDependencyVersions(nupkg)) | ||
| { | ||
| string packageName = Path.GetFileName(nupkg); | ||
| checkedPackages.Add($"{packageName} -> {version}"); | ||
|
|
||
| // Expected shape: "[<min>, <nextMajor>)". A bare version like "2.4.0" (no brackets) means the | ||
| // upper-bound target did not run — exactly the regression we are guarding against. | ||
| if (!version.StartsWith('[') || !version.EndsWith($", {ExpectedUpperBound})", StringComparison.Ordinal)) | ||
| { | ||
| violations.Add($"{packageName}: expected a '[<min>, {ExpectedUpperBound})' range but found '{version}'."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Assert.IsNotEmpty( | ||
| checkedPackages, | ||
| $"Expected at least one produced package to declare a '{PlatformPackageId}' dependency, but none was found. " + | ||
| $"Ensure the solution was packed (build with '-pack') before running this test. Searched:{Environment.NewLine}" + | ||
| $" {Constants.ArtifactsPackagesShipping}{Environment.NewLine} {Constants.ArtifactsPackagesNonShipping}"); | ||
|
|
||
| Assert.IsEmpty( | ||
| violations, | ||
| $"The following packages do not cap their '{PlatformPackageId}' dependency at '[<min>, {ExpectedUpperBound})':{Environment.NewLine}" + | ||
| string.Join(Environment.NewLine, violations)); | ||
| } | ||
|
|
||
| private static int GetTestingPlatformMajorVersion() | ||
| { | ||
| var versionsProps = XDocument.Load(Path.Combine(Constants.Root, "eng", "Versions.props")); | ||
| string versionPrefix = versionsProps.Descendants() | ||
| .Single(e => e.Name.LocalName == "TestingPlatformVersionPrefix") | ||
| .Value; | ||
| return int.Parse(versionPrefix.Split('.')[0], CultureInfo.InvariantCulture); | ||
| } | ||
|
|
||
| private static IEnumerable<string> EnumerateProducedPackages() | ||
| { | ||
| foreach (string folder in new[] { Constants.ArtifactsPackagesShipping, Constants.ArtifactsPackagesNonShipping }) | ||
| { | ||
| if (!Directory.Exists(folder)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| // Only the Microsoft.Testing.* family can depend on Microsoft.Testing.Platform; scoping the search | ||
| // keeps unrelated packages (and the platform package itself, which has no self-dependency) out. | ||
| foreach (string nupkg in Directory.EnumerateFiles(folder, "Microsoft.Testing.*.nupkg", SearchOption.TopDirectoryOnly)) | ||
| { | ||
| yield return nupkg; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<string> GetPlatformDependencyVersions(string nupkgPath) | ||
| { | ||
| using ZipArchive archive = ZipFile.OpenRead(nupkgPath); | ||
| ZipArchiveEntry? nuspecEntry = archive.Entries.FirstOrDefault(e => e.FullName.EndsWith(".nuspec", StringComparison.OrdinalIgnoreCase)); | ||
| if (nuspecEntry is null) | ||
| { | ||
| yield break; | ||
| } | ||
|
|
||
| XDocument nuspec; | ||
| using (Stream stream = nuspecEntry.Open()) | ||
| { | ||
| nuspec = XDocument.Load(stream); | ||
| } | ||
|
|
||
| // Match by LocalName to ignore the nuspec XML namespace, and target the exact dependency id so | ||
| // siblings such as 'Microsoft.Testing.Platform.MSBuild' or 'Microsoft.Testing.Platform.AI' are excluded. | ||
| foreach (XElement dependency in nuspec.Descendants().Where(e => e.Name.LocalName == "dependency")) | ||
| { | ||
| if (string.Equals((string?)dependency.Attribute("id"), PlatformPackageId, StringComparison.Ordinal) | ||
| && (string?)dependency.Attribute("version") is { } version) | ||
| { | ||
| yield return version; | ||
| } | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.