From 009589051e2f153051b75949128e438b58e3c82a Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Fri, 20 Dec 2024 12:01:51 -0700 Subject: [PATCH] make tests SDK agnostic --- .../MockNuGetPackage.cs | 43 +++----- .../UpdateWorkerTests.PackageReference.cs | 97 ++++++++++++------- .../Discover/SdkProjectDiscovery.cs | 4 +- 3 files changed, 77 insertions(+), 67 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/MockNuGetPackage.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/MockNuGetPackage.cs index eae2e1237f..2ceeaf2d03 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/MockNuGetPackage.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/MockNuGetPackage.cs @@ -388,6 +388,17 @@ public static MockNuGetPackage WellKnownReferencePackage(string packageName, str return WellKnownPackages[key]; } + public static MockNuGetPackage GetMicrosoftNETCoreAppRefPackage(int majorRuntimeVersion) + { + return WellKnownReferencePackage("Microsoft.NETCore.App", $"net{majorRuntimeVersion}.0", + [ + ("data/FrameworkList.xml", Encoding.UTF8.GetBytes($""" + + + """)) + ]); + } + public static MockNuGetPackage WellKnownHostPackage(string packageName, string targetFramework, (string Path, byte[] Content)[]? files = null) { string key = $"{packageName}/{targetFramework}"; @@ -437,34 +448,10 @@ public static MockNuGetPackage WellKnownHostPackage(string packageName, string t WellKnownReferencePackage("Microsoft.AspNetCore.App", "net7.0"), WellKnownReferencePackage("Microsoft.AspNetCore.App", "net8.0"), WellKnownReferencePackage("Microsoft.AspNetCore.App", "net9.0"), - WellKnownReferencePackage("Microsoft.NETCore.App", "net6.0", - [ - ("data/FrameworkList.xml", Encoding.UTF8.GetBytes(""" - - - """)) - ]), - WellKnownReferencePackage("Microsoft.NETCore.App", "net7.0", - [ - ("data/FrameworkList.xml", Encoding.UTF8.GetBytes(""" - - - """)) - ]), - WellKnownReferencePackage("Microsoft.NETCore.App", "net8.0", - [ - ("data/FrameworkList.xml", Encoding.UTF8.GetBytes(""" - - - """)) - ]), - WellKnownReferencePackage("Microsoft.NETCore.App", "net9.0", - [ - ("data/FrameworkList.xml", Encoding.UTF8.GetBytes(""" - - - """)) - ]), + GetMicrosoftNETCoreAppRefPackage(6), + GetMicrosoftNETCoreAppRefPackage(7), + GetMicrosoftNETCoreAppRefPackage(8), + GetMicrosoftNETCoreAppRefPackage(9), WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net6.0"), WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net7.0"), WellKnownReferencePackage("Microsoft.WindowsDesktop.App", "net8.0"), diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs index 0eed0a11fb..03c8cbe542 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.PackageReference.cs @@ -3491,16 +3491,20 @@ await TestUpdateForProject("Some.Package", "1.0.0", "1.1.0", [Fact] public async Task UpdateSdkManagedPackage_DirectDependency() { - // To avoid a unit test that's tightly coupled to the installed SDK, the package correlation file is faked. - // Doing this requires a temporary file and environment variable override. + // To avoid a unit test that's tightly coupled to the installed SDK, several values are simulated, + // including the runtime major version, the current Microsoft.NETCore.App.Ref package, and the package + // correlation file. Doing this requires a temporary file and environment variable override. + var runtimeMajorVersion = Environment.Version.Major; + var netCoreAppRefPackage = MockNuGetPackage.GetMicrosoftNETCoreAppRefPackage(runtimeMajorVersion); using var tempDirectory = new TemporaryDirectory(); var packageCorrelationFile = Path.Combine(tempDirectory.DirectoryPath, "dotnet-package-correlation.json"); - await File.WriteAllTextAsync(packageCorrelationFile, """ + await File.WriteAllTextAsync(packageCorrelationFile, $$""" { "Runtimes": { - "1": { + "{{runtimeMajorVersion}}.0.0": { "Packages": { - "System.Text.Json": "8.0.98" + "{{netCoreAppRefPackage.Id}}": "{{netCoreAppRefPackage.Version}}", + "System.Text.Json": "{{runtimeMajorVersion}}.0.98" } } } @@ -3510,52 +3514,68 @@ await File.WriteAllTextAsync(packageCorrelationFile, """ // In the `packages` section below, we fake a `System.Text.Json` package with a low assembly version that // will always trigger the replacement so that can be detected and then the equivalent version is pulled - // from the correlation file specified above. In the original project contents, package version `8.0.98` - // is reported which makes the update to `8.0.99` always possible. - await TestUpdateForProject("System.Text.Json", "8.0.0", "8.0.99", + // from the correlation file specified above. In the original project contents, package version `x.0.98` + // is reported which makes the update to `x.0.99` always possible. + await TestUpdateForProject("System.Text.Json", $"{runtimeMajorVersion}.0.98", $"{runtimeMajorVersion}.0.99", experimentsManager: new ExperimentsManager() { UseDirectDiscovery = true, InstallDotnetSdks = true }, packages: [ - MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.0", "net8.0", assemblyVersion: "8.0.0.0"), // this assembly version is lower than what the SDK will have - MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.99", "net8.0", assemblyVersion: "8.99.99.99"), // this assembly version is greater than what the SDK will have + // this assembly version is lower than what the SDK will have + MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.0", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.0.0.0"), + // this assembly version is greater than what the SDK will have + MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.99", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.99.99.99"), ], - projectContents: """ + projectContents: $""" - net8.0 + net{runtimeMajorVersion}.0 - + """, - expectedProjectContents: """ + additionalFiles: [ + ("global.json", $$""" + { + "sdk": { + "version": "{{runtimeMajorVersion}}.0.100", + "allowPrerelease": true, + "rollForward": "latestMinor" + } + } + """) + ], + expectedProjectContents: $""" - net8.0 + net{runtimeMajorVersion}.0 - + """ ); } - [Fact(Skip = "https://github.com/dependabot/dependabot-core/issues/11140")] + [Fact] public async Task UpdateSdkManagedPackage_TransitiveDependency() { - // To avoid a unit test that's tightly coupled to the installed SDK, the package correlation file is faked. - // Doing this requires a temporary file and environment variable override. Note that SDK version 8.0.100 - // or greater is required. + // To avoid a unit test that's tightly coupled to the installed SDK, several values are simulated, + // including the runtime major version, the current Microsoft.NETCore.App.Ref package, and the package + // correlation file. Doing this requires a temporary file and environment variable override. + var runtimeMajorVersion = Environment.Version.Major; + var netCoreAppRefPackage = MockNuGetPackage.GetMicrosoftNETCoreAppRefPackage(runtimeMajorVersion); using var tempDirectory = new TemporaryDirectory(); var packageCorrelationFile = Path.Combine(tempDirectory.DirectoryPath, "dotnet-package-correlation.json"); - await File.WriteAllTextAsync(packageCorrelationFile, """ + await File.WriteAllTextAsync(packageCorrelationFile, $$""" { - "Sdks": { - "8.0.100": { + "Runtimes": { + "{{runtimeMajorVersion}}.0.0": { "Packages": { - "System.Text.Json": "8.0.98" + "{{netCoreAppRefPackage.Id}}": "{{netCoreAppRefPackage.Version}}", + "System.Text.Json": "{{runtimeMajorVersion}}.0.98" } } } @@ -3565,22 +3585,24 @@ await File.WriteAllTextAsync(packageCorrelationFile, """ // In the `packages` section below, we fake a `System.Text.Json` package with a low assembly version that // will always trigger the replacement so that can be detected and then the equivalent version is pulled - // from the correlation file specified above. In the original project contents, package version `8.0.98` - // is reported which makes the update to `8.0.99` always possible. - await TestUpdateForProject("System.Text.Json", "8.0.98", "8.0.99", + // from the correlation file specified above. In the original project contents, package version `x.0.98` + // is reported which makes the update to `x.0.99` always possible. + await TestUpdateForProject("System.Text.Json", $"{runtimeMajorVersion}.0.98", $"{runtimeMajorVersion}.0.99", isTransitive: true, experimentsManager: new ExperimentsManager() { UseDirectDiscovery = true, InstallDotnetSdks = true }, packages: [ - MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", [(null, [("System.Text.Json", "[8.0.0]")])]), - MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0", [(null, [("System.Text.Json", "[8.0.99]")])]), - MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.0", "net8.0", assemblyVersion: "8.0.0.0"), // this assembly version is lower than what the SDK will have - MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", "8.0.99", "net8.0", assemblyVersion: "8.99.99.99"), // this assembly version is greater than what the SDK will have + MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", $"net{runtimeMajorVersion}.0", [(null, [("System.Text.Json", $"[{runtimeMajorVersion}.0.0]")])]), + MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", $"net{runtimeMajorVersion}.0", [(null, [("System.Text.Json", $"[{runtimeMajorVersion}.0.99]")])]), + // this assembly version is lower than what the SDK will have + MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.0", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.0.0.0"), + // this assembly version is greater than what the SDK will have + MockNuGetPackage.CreatePackageWithAssembly("System.Text.Json", $"{runtimeMajorVersion}.0.99", $"net{runtimeMajorVersion}.0", assemblyVersion: $"{runtimeMajorVersion}.99.99.99"), ], - projectContents: """ + projectContents: $""" - net8.0 + net{runtimeMajorVersion}.0 @@ -3588,19 +3610,20 @@ await TestUpdateForProject("System.Text.Json", "8.0.98", "8.0.99", """, additionalFiles: [ - ("global.json", """ + ("global.json", $$""" { "sdk": { - "version": "8.0.100", + "version": "{{runtimeMajorVersion}}.0.100", + "allowPrerelease": true, "rollForward": "latestMinor" } } """) ], - expectedProjectContents: """ + expectedProjectContents: $""" - net8.0 + net{runtimeMajorVersion}.0 diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs index 1d143d006c..dc60a5f42a 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs @@ -243,8 +243,8 @@ runtimePackageVersion is not null && var replacementPackageVersion = packageMapper.GetPackageVersionThatShippedWithOtherPackage(runtimePackageName, parsedRuntimePackageVersion, removedPackageName); if (replacementPackageVersion is not null) { - var packagesPerProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); - var packagesPerTfm = packagesPerProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); + var packagesPerThisProject = packagesReplacedBySdkPerProject.GetOrAdd(projectEvaluation.ProjectFile, () => new(PathComparer.Instance)); + var packagesPerTfm = packagesPerThisProject.GetOrAdd(tfm, () => new(StringComparer.OrdinalIgnoreCase)); packagesPerTfm[removedPackageName] = replacementPackageVersion.ToString(); } }