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

enable updating transitive dependencies with solver #10776

Merged
merged 3 commits into from
Oct 17, 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
@@ -0,0 +1,12 @@
namespace NuGetUpdater.Core.Test;

/// <summary>
/// Prepares the environment to use the new dependency solver.
/// </summary>
public class DependencySolverEnvironment : TemporaryEnvironment
{
public DependencySolverEnvironment(bool useDependencySolver = true)
: base([("UseNewNugetPackageResolver", useDependencySolver ? "true" : "false")])
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ await TestUpdateForProject("Some.Package", "9.0.1", "13.0.1",
}

[Theory]
[InlineData("true")]
[InlineData(null)]
public async Task UpdateVersionChildElement_InProjectFile_ForPackageReferenceIncludeTheory(string variableValue)
[InlineData(true)]
[InlineData(false)]
public async Task UpdateVersionChildElement_InProjectFile_ForPackageReferenceIncludeTheory(bool useDependencySolver)
{
// update Some.Package from 9.0.1 to 13.0.1
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", variableValue)]);
using var _ = new DependencySolverEnvironment(useDependencySolver);
await TestUpdateForProject("Some.Package", "9.0.1", "13.0.1",
packages:
[
Expand Down Expand Up @@ -95,11 +95,72 @@ await TestUpdateForProject("Some.Package", "9.0.1", "13.0.1",
);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task PeerDependenciesAreUpdatedEvenWhenNotExplicit(bool useDependencySolver)
{
using var _ = new DependencySolverEnvironment(useDependencySolver);
await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0",
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", [(null, [("Transitive.Package", "[1.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0", [(null, [("Transitive.Package", "[2.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Transitive.Package", "1.0.0", "net8.0"),
MockNuGetPackage.CreateSimplePackage("Transitive.Package", "2.0.0", "net8.0"),
],
projectFile: ("a/a.csproj", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
"""),
additionalFiles:
[
("Directory.Packages.props", """
<Project>
<ItemGroup>
<PackageVersion Include="Some.Package" Version="1.0.0" />
<PackageVersion Include="Transitive.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""")
],
expectedProjectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" />
</ItemGroup>
</Project>
""",
additionalFilesExpected:
[
("Directory.Packages.props", """
<Project>
<ItemGroup>
<PackageVersion Include="Some.Package" Version="2.0.0" />
<PackageVersion Include="Transitive.Package" Version="2.0.0" />
</ItemGroup>
</Project>
""")
]
);
}

[Fact]
public async Task CallingResolveDependencyConflictsNew()
{
// update Microsoft.CodeAnalysis.Common from 4.9.2 to 4.10.0
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", "true")]);
using var _ = new DependencySolverEnvironment();
await TestUpdateForProject("Microsoft.CodeAnalysis.Common", "4.9.2", "4.10.0",
// initial
projectContents: $"""
Expand Down Expand Up @@ -535,11 +596,11 @@ await TestUpdateForProject("Some.Package", "9.0.1", "13.0.1",
}

[Theory]
[InlineData(null)]
[InlineData("true")]
public async Task AddPackageReference_InProjectFile_ForTransientDependency(string variableValue)
[InlineData(true)]
[InlineData(false)]
public async Task AddPackageReference_InProjectFile_ForTransientDependency(bool useDependencySolver)
{
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", variableValue)]);
using var _ = new DependencySolverEnvironment(useDependencySolver);
// add transient package Some.Transient.Dependency from 5.0.1 to 5.0.2
await TestUpdateForProject("Some.Transient.Dependency", "5.0.1", "5.0.2", isTransitive: true,
packages:
Expand Down Expand Up @@ -2914,12 +2975,51 @@ await TestUpdateForProject("Some.Package", "12.0.1", "13.0.1",
);
}

[Fact]
public async Task UpdatingTransitiveDependencyWithNewSolverCanUpdateJustTheTopLevelPackage()
{
// we've been asked to explicitly update a transitive dependency, but we can solve it by updating the top-level package instead
using var _ = new DependencySolverEnvironment();
await TestUpdateForProject("Transitive.Package", "1.0.0", "2.0.0",
isTransitive: true,
packages:
[
MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0", [("net8.0", [("Transitive.Package", "[1.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0", [("net8.0", [("Transitive.Package", "[2.0.0]")])]),
MockNuGetPackage.CreateSimplePackage("Transitive.Package", "1.0.0", "net8.0"),
MockNuGetPackage.CreateSimplePackage("Transitive.Package", "2.0.0", "net8.0"),
],
projectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" Version="1.0.0" />
</ItemGroup>
</Project>
""",
expectedProjectContents: """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Some.Package" Version="2.0.0" />
</ItemGroup>
</Project>
"""
);
}

[Theory]
[InlineData("true")]
[InlineData(null)]
public async Task NoChange_IfThereAreIncoherentVersions(string variableValue)
[InlineData(true)]
[InlineData(false)]
public async Task NoChange_IfThereAreIncoherentVersions(bool useDependencySolver)
{
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", variableValue)]);
using var _ = new DependencySolverEnvironment(useDependencySolver);

// trying to update `Transitive.Dependency` to 1.1.0 would normally pull `Some.Package` from 1.0.0 to 1.1.0,
// but the TFM doesn't allow it
Expand Down Expand Up @@ -3005,11 +3105,11 @@ await TestUpdateForProject("Some.Package", "7.0.1", "13.0.1",
}

[Theory]
[InlineData("true")]
[InlineData(null)]
public async Task UnresolvablePropertyDoesNotStopOtherUpdates(string variableValue)
[InlineData(true)]
[InlineData(false)]
public async Task UnresolvablePropertyDoesNotStopOtherUpdates(bool useDependencySolver)
{
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", variableValue)]);
using var _ = new DependencySolverEnvironment(useDependencySolver);

// the property `$(SomeUnresolvableProperty)` cannot be resolved
await TestUpdateForProject("Some.Package", "7.0.1", "13.0.1",
Expand Down Expand Up @@ -3045,11 +3145,11 @@ await TestUpdateForProject("Some.Package", "7.0.1", "13.0.1",
}

[Theory]
[InlineData("true")]
[InlineData(null)]
public async Task UpdatingPackageAlsoUpdatesAnythingWithADependencyOnTheUpdatedPackage(string variableValue)
[InlineData(true)]
[InlineData(false)]
public async Task UpdatingPackageAlsoUpdatesAnythingWithADependencyOnTheUpdatedPackage(bool useDependencySolver)
{
using var env = new TemporaryEnvironment([("UseNewNugetPackageResolver", variableValue)]);
using var _ = new DependencySolverEnvironment(useDependencySolver);

// updating Some.Package from 3.3.30 requires that Some.Package.Extensions also be updated
await TestUpdateForProject("Some.Package", "3.3.30", "3.4.3",
Expand Down
Loading
Loading