Skip to content
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

Set EnableWindowsTargeting to true when updating the lock file #11037

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -101,6 +101,7 @@ protected static Task TestUpdateForProject(
bool isTransitive = false,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null,
Action<string>? additionalChecks = null,
MockNuGetPackage[]? packages = null,
ExperimentsManager? experimentsManager = null,
string projectFilePath = "test-project.csproj",
Expand All @@ -114,6 +115,7 @@ protected static Task TestUpdateForProject(
isTransitive,
additionalFiles,
additionalFilesExpected,
additionalChecks,
packages,
experimentsManager,
expectedResult);
Expand All @@ -127,6 +129,7 @@ protected static async Task TestUpdateForProject(
bool isTransitive = false,
TestFile[]? additionalFiles = null,
TestFile[]? additionalFilesExpected = null,
Action<string>? additionalChecks = null,
MockNuGetPackage[]? packages = null,
ExperimentsManager? experimentsManager = null,
ExpectedUpdateOperationResult? expectedResult = null)
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
("packages.lock.json", "{}")
],
// expected
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="2.0.0" />
</ItemGroup>
</Project>
""",
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: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
("packages.lock.json", "{}"),
("Directory.Packages.props", """
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""")
],
// expected
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
""",
additionalFilesExpected:
[
("Directory.Packages.props", """
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Some.Package" Version="2.0.0" />
</ItemGroup>
</Project>
""")
],
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: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
("packages.lock.json", "{}")
],
// expected
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" Version="2.0.0" />
</ItemGroup>
</Project>
""",
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: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
""",
additionalFiles:
[
("packages.lock.json", "{}"),
("Directory.Packages.props", """
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""")
],
// expected
expectedProjectContents: $"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
""",
additionalFilesExpected:
[
("Directory.Packages.props", """
<Project>
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<PackageVersion Include="Some.Package" Version="2.0.0" />
</ItemGroup>
</Project>
""")
],
additionalChecks: path =>
{
var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json"));
Assert.Contains("\"resolved\": \"2.0.0\"", lockContents);
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
Loading