diff --git a/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs new file mode 100644 index 000000000..39b31553a --- /dev/null +++ b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs @@ -0,0 +1,45 @@ +using System; +using System.Threading.Tasks; +using NuKeeper.NuGet.Api; +using NUnit.Framework; + +namespace NuKeeper.Tests.Nuget.Api +{ + [TestFixture] + public class ApiPackageLookupTests + { + [Test] + public async Task UnknownPackageName_ShouldNotReturnResult() + { + IApiPackageLookup lookup = new ApiPackageLookup(); + + var package = await lookup.LookupLatest(Guid.NewGuid().ToString()); + + Assert.That(package, Is.Null); + } + + [Test] + public async Task WellKnownPackageName_ShouldReturnResult() + { + IApiPackageLookup lookup = new ApiPackageLookup(); + + var package = await lookup.LookupLatest("Newtonsoft.Json"); + + Assert.That(package, Is.Not.Null); + Assert.That(package.Identity, Is.Not.Null); + Assert.That(package.Identity.Id, Is.EqualTo("Newtonsoft.Json")); + } + + [Test] + public async Task AmbigousPackageName_ShouldReturnCorrectResult() + { + IApiPackageLookup lookup = new ApiPackageLookup(); + + var package = await lookup.LookupLatest("AWSSDK"); + + Assert.That(package, Is.Not.Null); + Assert.That(package.Identity, Is.Not.Null); + Assert.That(package.Identity.Id, Is.EqualTo("AWSSDK")); + } + } +} diff --git a/NuKeeper/NuGet/Api/ApiPackageLookup.cs b/NuKeeper/NuGet/Api/ApiPackageLookup.cs index 73b968fb4..12a533413 100644 --- a/NuKeeper/NuGet/Api/ApiPackageLookup.cs +++ b/NuKeeper/NuGet/Api/ApiPackageLookup.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; +using NuGet.Common; using NuGet.Configuration; using NuGet.Protocol; using NuGet.Protocol.Core.Types; @@ -11,18 +12,21 @@ namespace NuKeeper.NuGet.Api { public class ApiPackageLookup : IApiPackageLookup { + private readonly ILogger _logger = new ConsoleLogger(); + public async Task LookupLatest(string packageName) { var versions = await Lookup(packageName); - return versions.FirstOrDefault(); + return versions + .OrderByDescending(p => p?.Identity?.Version) + .FirstOrDefault(); } private async Task> Lookup(string packageName) { var sourceRepository = BuildSourceRepository(); - var searchResource = await sourceRepository.GetResourceAsync(); - - return await SearchForPackages(searchResource, packageName); + var metadataResource = await sourceRepository.GetResourceAsync(); + return await FindPackage(metadataResource, packageName); } private static SourceRepository BuildSourceRepository() @@ -35,15 +39,11 @@ private static SourceRepository BuildSourceRepository() return new SourceRepository(packageSource, providers); } - private static async Task> SearchForPackages(PackageSearchResource searchResource, string packageName) - { - var logger = new ConsoleLogger(); - var filter = new SearchFilter(false); - - var packages = await searchResource - .SearchAsync(packageName, filter, 0, 10, logger, CancellationToken.None); - - return packages; + private async Task> FindPackage( + PackageMetadataResource metadataResource, string packageName) + { + return await metadataResource + .GetMetadataAsync(packageName, false, false, _logger, CancellationToken.None); } } }