Skip to content

Commit 8dd78e1

Browse files
EvangelinkCopilot
andauthored
Fix acceptance tests using property-backed package versions (#12434)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 197a27d commit 8dd78e1

3 files changed

Lines changed: 48 additions & 8 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Text.Json;
5+
6+
namespace Microsoft.Testing.Platform.Acceptance.IntegrationTests;
7+
8+
[TestClass]
9+
public sealed class AcceptancePackageVersionTests : AcceptanceTestBase
10+
{
11+
public TestContext TestContext { get; set; } = null!;
12+
13+
[TestMethod]
14+
public async Task PackageVersions_MatchEvaluatedCentralPackageVersions()
15+
{
16+
DotnetMuxerResult result = await DotnetCli.RunAsync(
17+
"msbuild Directory.Packages.props -getItem:PackageVersion",
18+
workingDirectory: Constants.Root,
19+
environmentVariables: new() { ["DOTNET_CLI_CONTEXT_VERBOSE"] = "false" },
20+
cancellationToken: TestContext.CancellationToken);
21+
22+
using var evaluation = JsonDocument.Parse(result.StandardOutput);
23+
var versions = evaluation.RootElement.GetProperty("Items").GetProperty("PackageVersion")
24+
.EnumerateArray()
25+
.ToDictionary(
26+
package => package.GetProperty("Identity").GetString()!,
27+
package => package.GetProperty("Version").GetString(),
28+
StringComparer.Ordinal);
29+
30+
Assert.AreEqual(versions["Microsoft.Extensions.Hosting"], MicrosoftExtensionsHostingVersion);
31+
Assert.AreEqual(versions["OpenTelemetry"], OpenTelemetryVersion);
32+
}
33+
}

‎test/IntegrationTests/Microsoft.Testing.Platform.Acceptance.IntegrationTests/Helpers/AcceptanceTestBase.cs‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ static AcceptanceTestBase()
1818
var cpmPropFileDoc = XDocument.Load(Path.Combine(RootFinder.Find(), "Directory.Packages.props"));
1919
MicrosoftNETTestSdkVersion = cpmPropFileDoc.Descendants("MicrosoftNETTestSdkVersion").Single().Value;
2020
MicrosoftNETCoreUniversalWindowsPlatformVersion = cpmPropFileDoc.Descendants("MicrosoftNETCoreUniversalWindowsPlatformVersion").Single().Value;
21-
MicrosoftExtensionsHostingVersion = GetPackageVersion(cpmPropFileDoc, "Microsoft.Extensions.Logging");
22-
OpenTelemetryVersion = GetPackageVersion(cpmPropFileDoc, "OpenTelemetry");
21+
MicrosoftExtensionsHostingVersion = GetPackageVersion("Microsoft.Extensions.Hosting");
22+
OpenTelemetryVersion = GetPackageVersion("OpenTelemetry");
2323

2424
using var globalJson = JsonDocument.Parse(File.ReadAllText(Path.Combine(RootFinder.Find(), "global.json")));
2525
MSBuildSdkExtrasVersion = globalJson.RootElement
@@ -171,12 +171,14 @@ .. matches
171171
return packageFullName.Substring(packagePrefixName.Length, packageFullName.Length - packagePrefixName.Length - NuGetPackageExtensionName.Length);
172172
}
173173

174-
private static string GetPackageVersion(XDocument centralPackageManagementDocument, string packageName)
175-
=> centralPackageManagementDocument
176-
.Descendants("PackageVersion")
177-
.Single(element => string.Equals(element.Attribute("Include")?.Value, packageName, StringComparison.Ordinal))
178-
.Attribute("Version")?.Value
179-
?? throw new InvalidOperationException($"Directory.Packages.props does not define a version for '{packageName}'.");
174+
private static string GetPackageVersion(string packageName)
175+
{
176+
string? version = typeof(Constants).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
177+
.SingleOrDefault(attribute => string.Equals(attribute.Key, packageName, StringComparison.Ordinal))?.Value;
178+
return !string.IsNullOrEmpty(version)
179+
? version
180+
: throw new InvalidOperationException($"The test infrastructure does not define an evaluated version for '{packageName}'.");
181+
}
180182

181183
internal static IEnumerable<(string Tfm, BuildConfiguration BuildConfiguration)> GetBuildMatrixTfmBuildConfiguration()
182184
{

‎test/Utilities/Microsoft.Testing.TestInfrastructure/Microsoft.Testing.TestInfrastructure.csproj‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<PackageReference Include="Microsoft.Extensions.Resilience" />
1515
</ItemGroup>
1616

17+
<ItemGroup>
18+
<AssemblyMetadata Include="@(PackageVersion->WithMetadataValue('Identity', 'Microsoft.Extensions.Hosting'))" Value="%(Version)" />
19+
<AssemblyMetadata Include="@(PackageVersion->WithMetadataValue('Identity', 'OpenTelemetry'))" Value="%(Version)" />
20+
</ItemGroup>
21+
1722
<ItemGroup>
1823
<Compile Include="$(RepoRoot)src/Polyfills/**/*.cs" Link="Polyfills\%(RecursiveDir)%(Filename)%(Extension)" />
1924
</ItemGroup>

0 commit comments

Comments
 (0)