Skip to content

Commit

Permalink
Fixed issue when package version is range, e.g. [6.2.*,) . Also, fixe…
Browse files Browse the repository at this point in the history
…d NullReferenceException issue when packages doesn't exists in local cache. (#183)
  • Loading branch information
levanoz authored Feb 16, 2023
1 parent cd15113 commit a0b6fbf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ private string GetVersionFromRange(string versionRange, IEnumerable<NuGetVersion
try
{
VersionRange vRange = VersionRange.Parse(versionRange);
return vRange.FindBestMatch(versionList).ToString();
return vRange.FindBestMatch(versionList)?.ToString() ?? "";
}
catch (NullReferenceException)
{
Expand Down Expand Up @@ -348,7 +348,7 @@ public async Task<Dictionary<string, PackageList>> GetPackages()
var references = this.GetProjectReferences(projectFile);
var referencedPackages = references.Select((package) =>
{
var split = package.Split(',');
var split = package.Split(',', 2);
return new PackageNameAndVersion { Name = split[0], Version = split[1] };
});
WriteOutput(Environment.NewLine + "Project:" + projectFile + Environment.NewLine, logLevel: LogLevel.Information);
Expand Down
30 changes: 27 additions & 3 deletions tests/NugetUtility.Tests/MethodsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using CommandLine.Text;
using FluentAssertions;
using FluentAssertions.Execution;
using NUnit.Framework;

namespace NugetUtility.Tests
Expand Down Expand Up @@ -315,7 +316,8 @@ public async Task GetProjectReferencesFromAssetsFile_Should_Resolve_Transitive_A
{
UseProjectAssetsJson = true,
IncludeTransitive = true,
ProjectDirectory = @"../../../",
ProjectDirectory = @"../../../NugetUtility.Tests.csproj",
Timeout = 10
});

var packages = await methods.GetPackages();
Expand All @@ -328,10 +330,10 @@ public async Task GetProjectReferencesFromAssetsFile_Should_Resolve_Transitive_A

// Some second-order refs:
list.Should().ContainKey($"CommandLineParser,{typeof(UsageAttribute).Assembly.GetName().Version.ToString(3)}");
list.Should().ContainKey("System.IO.Compression,4.3.0");
list.Should().Contain(x => x.Key.StartsWith("System.IO.Compression,"));

// Some third-order refs:
list.Should().ContainKey("System.Buffers,4.3.0");
list.Should().Contain(x => x.Key.StartsWith("System.Buffers,"));
}

[TestCase("BenchmarkDotNet", "0.12.1", "https://licenses.nuget.org/MIT", "MIT")]
Expand Down Expand Up @@ -395,5 +397,27 @@ public void HttpClient_IgnoreSslError_GetNugetInformationAsync(string package, s

Assert.DoesNotThrowAsync(async () => await _methods.GetNugetInformationAsync(_projectPath, referencedpackages));
}

[Test]
public async Task GetPackages_ShouldSupportPackageReferencesWithVersionRange()
{
var methods = new Methods(new PackageOptions
{
ProjectDirectory = "../../../TestProjectFiles/PackageRangeReference.csproj",
Timeout = 10
});

var result = await methods.GetPackages();

using (new AssertionScope())
{
result.Should()
.HaveCount(1);

result.First().Value.Should().HaveCount(1);

result.First().Value.First().Value.Metadata.Id.Should().Be("FluentAssertions");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0;</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="[6.2.*,)" />
</ItemGroup>
</Project>

0 comments on commit a0b6fbf

Please sign in to comment.