diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs index 3372eceb78..9785bfcc16 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs @@ -47,7 +47,7 @@ protected static Task TestUpdate( { return useSolution ? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [(projectFilePath, projectContents)], projectFilesExpected: [(projectFilePath, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager) - : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile: (projectFilePath, projectContents), expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager); + : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile: (projectFilePath, projectContents), expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, additionalChecks: null, packages: packages, experimentsManager: experimentsManager); } protected static Task TestUpdate( @@ -65,7 +65,7 @@ protected static Task TestUpdate( { return useSolution ? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [projectFile], projectFilesExpected: [(projectFile.Path, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager) - : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile, expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager); + : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile, expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, additionalChecks: null, packages: packages, experimentsManager: experimentsManager); } protected static Task TestNoChangeforProject( @@ -101,6 +101,7 @@ protected static Task TestUpdateForProject( bool isTransitive = false, TestFile[]? additionalFiles = null, TestFile[]? additionalFilesExpected = null, + Action? additionalChecks = null, MockNuGetPackage[]? packages = null, ExperimentsManager? experimentsManager = null, string projectFilePath = "test-project.csproj", @@ -114,6 +115,7 @@ protected static Task TestUpdateForProject( isTransitive, additionalFiles, additionalFilesExpected, + additionalChecks, packages, experimentsManager, expectedResult); @@ -127,6 +129,7 @@ protected static async Task TestUpdateForProject( bool isTransitive = false, TestFile[]? additionalFiles = null, TestFile[]? additionalFilesExpected = null, + Action? additionalChecks = null, MockNuGetPackage[]? packages = null, ExperimentsManager? experimentsManager = null, ExpectedUpdateOperationResult? expectedResult = null) @@ -156,6 +159,17 @@ protected static async Task TestUpdateForProject( { ValidateUpdateOperationResult(expectedResult, actualResult!); } + + if (additionalChecks is not null) + { + var sourcesDirectory = temporaryDirectory; + if (placeFilesInSrc) + { + sourcesDirectory = Path.Combine(temporaryDirectory, "src"); + } + + additionalChecks(sourcesDirectory); + } }); var expectedResultFiles = additionalFilesExpected.Prepend((projectFilePath, expectedProjectContents)).ToArray(); diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs new file mode 100644 index 0000000000..efa02e9299 --- /dev/null +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs @@ -0,0 +1,251 @@ +using Xunit; + +namespace NuGetUpdater.Core.Test.Update; + +public partial class UpdateWorkerTests +{ + public class LockFile : UpdateWorkerTestBase + { + [Fact] + public async Task UpdateSingleDependency() + { + await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0", + packages: + [ + MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"), + MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"), + ], + // initial + projectContents: $""" + + + net8.0 + true + + + + + + + """, + additionalFiles: + [ + ("packages.lock.json", "{}") + ], + // expected + expectedProjectContents: $""" + + + net8.0 + true + + + + + + + """, + additionalChecks: path => + { + var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json")); + Assert.Contains("\"resolved\": \"2.0.0\"", lockContents); + } + ); + } + + [Fact] + public async Task UpdateSingleDependency_CentralPackageManagement() + { + await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0", + packages: + [ + MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"), + MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"), + ], + // initial + projectContents: $""" + + + net8.0 + true + + + + + + + """, + additionalFiles: + [ + ("packages.lock.json", "{}"), + ("Directory.Packages.props", """ + + + true + + + + + + + """) + ], + // expected + expectedProjectContents: $""" + + + net8.0 + true + + + + + + + """, + additionalFilesExpected: + [ + ("Directory.Packages.props", """ + + + true + + + + + + + """) + ], + additionalChecks: path => + { + var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json")); + Assert.Contains("\"resolved\": \"2.0.0\"", lockContents); + } + ); + } + + [Fact] + public async Task UpdateSingleDependency_WindowsSpecific() + { + await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0", + packages: + [ + MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"), + MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"), + ], + // initial + projectContents: $""" + + + net8.0-windows + true + true + + + + + + + """, + additionalFiles: + [ + ("packages.lock.json", "{}") + ], + // expected + expectedProjectContents: $""" + + + net8.0-windows + true + true + + + + + + + """, + additionalChecks: path => + { + var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json")); + Assert.Contains("\"resolved\": \"2.0.0\"", lockContents); + } + ); + } + + [Fact] + public async Task UpdateSingleDependency_CentralPackageManagement_WindowsSpecific() + { + await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0", + packages: + [ + MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"), + MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"), + ], + // initial + projectContents: $""" + + + net8.0-windows + true + true + + + + + + + """, + additionalFiles: + [ + ("packages.lock.json", "{}"), + ("Directory.Packages.props", """ + + + true + + + + + + + """) + ], + // expected + expectedProjectContents: $""" + + + net8.0-windows + true + true + + + + + + + """, + additionalFilesExpected: + [ + ("Directory.Packages.props", """ + + + true + + + + + + + """) + ], + additionalChecks: path => + { + var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json")); + Assert.Contains("\"resolved\": \"2.0.0\"", lockContents); + } + ); + } + } +} diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/LockFileUpdater.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/LockFileUpdater.cs index 693b5b879c..8b54ed60da 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/LockFileUpdater.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Updater/LockFileUpdater.cs @@ -11,7 +11,7 @@ public static async Task UpdateLockFileAsync( var projectDirectory = Path.GetDirectoryName(projectPath)!; await MSBuildHelper.HandleGlobalJsonAsync(projectDirectory, repoRootPath, experimentsManager, async () => { - var (exitCode, stdout, stderr) = await ProcessEx.RunDotnetWithoutMSBuildEnvironmentVariablesAsync(["restore", "--force-evaluate", projectPath], projectDirectory, experimentsManager); + var (exitCode, stdout, stderr) = await ProcessEx.RunDotnetWithoutMSBuildEnvironmentVariablesAsync(["restore", "--force-evaluate", "-p:EnableWindowsTargeting=true", projectPath], projectDirectory, experimentsManager); if (exitCode != 0) { logger.Error($" Lock file update failed.\nSTDOUT:\n{stdout}\nSTDERR:\n{stderr}");