Skip to content

Commit

Permalink
allow packages.config update when project file doesn't list assembly …
Browse files Browse the repository at this point in the history
…version (#9534)
  • Loading branch information
brettfo committed Apr 18, 2024
1 parent f0f68d2 commit 823417b
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,62 @@ public async Task UpdateSingleDependencyInPackagesConfig()
""");
}

[Fact]
public async Task UpdateSingleDependencyInPackagesConfig_ReferenceHasNoAssemblyVersion()
{
// update Newtonsoft.Json from 7.0.1 to 13.0.1
await TestUpdateForProject("Newtonsoft.Json", "7.0.1", "13.0.1",
// existing
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="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
""",
packagesConfigContents: """
<packages>
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />
</packages>
""",
// expected
expectedProjectContents: """
<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="packages.config" />
</ItemGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed">
<HintPath>packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
""",
expectedPackagesConfigContents: """
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
</packages>
""");
}

[Fact]
public async Task UpdateSingleDependencyInPackagesConfigButNotToLatest()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,7 @@ private static void RunNuget(List<string> updateArgs, List<string> restoreArgs,
var hintPathSubString = $"{dependencyName}.{dependencyVersion}";

string? partialPathMatch = null;
var hintPathNodes = projectBuildFile.Contents.Descendants()
.Where(e =>
e.Name.Equals("HintPath", StringComparison.OrdinalIgnoreCase) &&
e.Parent.Name.Equals("Reference", StringComparison.OrdinalIgnoreCase) &&
e.Parent.GetAttributeValue("Include", StringComparison.OrdinalIgnoreCase)?.StartsWith($"{dependencyName},", StringComparison.OrdinalIgnoreCase) == true);
var hintPathNodes = projectBuildFile.Contents.Descendants().Where(e => e.IsHintPathNodeForDependency(dependencyName));
foreach (var hintPathNode in hintPathNodes)
{
var hintPath = hintPathNode.GetContentValue();
Expand Down Expand Up @@ -210,6 +206,26 @@ private static void RunNuget(List<string> updateArgs, List<string> restoreArgs,
return partialPathMatch;
}

private static bool IsHintPathNodeForDependency(this IXmlElementSyntax element, string dependencyName)
{
if (element.Name.Equals("HintPath", StringComparison.OrdinalIgnoreCase) &&
element.Parent.Name.Equals("Reference", StringComparison.OrdinalIgnoreCase))
{
// the include attribute will look like one of the following:
// <Reference Include="Some.Dependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=abcd">
// or
// <Reference Include="Some.Dependency">
string includeAttributeValue = element.Parent.GetAttributeValue("Include", StringComparison.OrdinalIgnoreCase);
if (includeAttributeValue.Equals(dependencyName, StringComparison.OrdinalIgnoreCase) ||
includeAttributeValue.StartsWith($"{dependencyName},", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

private static string GetUpToIndexWithoutTrailingDirectorySeparator(string path, int index)
{
var subpath = path[..index];
Expand Down

0 comments on commit 823417b

Please sign in to comment.