-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restrict when we add binding redirects (#10833)
* add binding redirect tests * only add binding redirects for assemblies from the updated package
- Loading branch information
Showing
5 changed files
with
366 additions
and
12 deletions.
There are no files selected for viewing
225 changes: 225 additions & 0 deletions
225
nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/BindingRedirectsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
using Xunit; | ||
|
||
namespace NuGetUpdater.Core.Test.Update; | ||
|
||
public class BindingRedirectsTests | ||
{ | ||
[Fact] | ||
public async Task SimpleBindingRedirectIsPerformed() | ||
{ | ||
await VerifyBindingRedirectsAsync( | ||
projectContents: """ | ||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="app.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Some.Package, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>packages\Some.Package.2.0.0\lib\net45\Some.Package.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> | ||
""", | ||
configContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""", | ||
updatedPackageName: "Some.Package", | ||
updatedPackageVersion: "2.0.0", | ||
expectedConfigContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task ConfigFileIndentationIsPreserved() | ||
{ | ||
await VerifyBindingRedirectsAsync( | ||
projectContents: """ | ||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="app.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Some.Package, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>packages\Some.Package.2.0.0\lib\net45\Some.Package.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> | ||
""", | ||
configContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""", | ||
updatedPackageName: "Some.Package", | ||
updatedPackageVersion: "2.0.0", | ||
expectedConfigContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task NoExtraBindingsAreAdded() | ||
{ | ||
await VerifyBindingRedirectsAsync( | ||
projectContents: """ | ||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="app.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Some.Package, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>packages\Some.Package.2.0.0\lib\net45\Some.Package.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="Some.Unrelated.Package, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>packages\Some.Unrelated.Package.3.0.0\lib\net45\Some.Package.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> | ||
""", | ||
configContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""", | ||
updatedPackageName: "Some.Package", | ||
updatedPackageVersion: "2.0.0", | ||
expectedConfigContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task NewBindingIsAdded() | ||
{ | ||
await VerifyBindingRedirectsAsync( | ||
projectContents: """ | ||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="app.config" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Some.Package, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"> | ||
<HintPath>packages\Some.Package.2.0.0\lib\net45\Some.Package.dll</HintPath> | ||
<Private>True</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
</Project> | ||
""", | ||
configContents: """ | ||
<configuration> | ||
<runtime /> | ||
</configuration> | ||
""", | ||
updatedPackageName: "Some.Package", | ||
updatedPackageVersion: "2.0.0", | ||
expectedConfigContents: """ | ||
<configuration> | ||
<runtime> | ||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<dependentAssembly> | ||
<assemblyIdentity name="Some.Package" publicKeyToken="null" culture="neutral" /> | ||
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" /> | ||
</dependentAssembly> | ||
</assemblyBinding> | ||
</runtime> | ||
</configuration> | ||
""" | ||
); | ||
} | ||
|
||
private static async Task VerifyBindingRedirectsAsync(string projectContents, string configContents, string expectedConfigContents, string updatedPackageName, string updatedPackageVersion, string configFileName = "app.config") | ||
{ | ||
using var tempDir = new TemporaryDirectory(); | ||
var projectFileName = "project.csproj"; | ||
var projectFilePath = Path.Combine(tempDir.DirectoryPath, projectFileName); | ||
var configFilePath = Path.Combine(tempDir.DirectoryPath, configFileName); | ||
|
||
await File.WriteAllTextAsync(projectFilePath, projectContents); | ||
await File.WriteAllTextAsync(configFilePath, configContents); | ||
|
||
var projectBuildFile = ProjectBuildFile.Open(tempDir.DirectoryPath, projectFilePath); | ||
await BindingRedirectManager.UpdateBindingRedirectsAsync(projectBuildFile, updatedPackageName, updatedPackageVersion); | ||
|
||
var actualConfigContents = (await File.ReadAllTextAsync(configFilePath)).Replace("\r", ""); | ||
expectedConfigContents = expectedConfigContents.Replace("\r", ""); | ||
Assert.Equal(expectedConfigContents, actualConfigContents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.