From 5995affd69bd08501c4257d5ce6a8b2d8f08abac Mon Sep 17 00:00:00 2001 From: Anthony Steele Date: Sun, 23 Jul 2017 16:23:42 +0100 Subject: [PATCH 1/2] Testing a failure that I saw this week This is an integration test but it fails predictably: Expected: "AWSSDK" But was: "AWSSDK.Core" --- .../Nuget/Api/ApiPackageLookupTests.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs diff --git a/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs new file mode 100644 index 000000000..f3b4c4321 --- /dev/null +++ b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs @@ -0,0 +1,46 @@ +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")); + } + } +} From 1cbc2cd40f52c266df3aed72ac7341c5d3a60e6f Mon Sep 17 00:00:00 2001 From: Anthony Steele Date: Sun, 23 Jul 2017 16:36:22 +0100 Subject: [PATCH 2/2] Make the tests pass --- .../Nuget/Api/ApiPackageLookupTests.cs | 1 - NuKeeper/NuGet/Api/ApiPackageLookup.cs | 26 +++++++++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs index f3b4c4321..39b31553a 100644 --- a/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs +++ b/NuKeeper.Tests/Nuget/Api/ApiPackageLookupTests.cs @@ -30,7 +30,6 @@ public async Task WellKnownPackageName_ShouldReturnResult() Assert.That(package.Identity.Id, Is.EqualTo("Newtonsoft.Json")); } - [Test] public async Task AmbigousPackageName_ShouldReturnCorrectResult() { 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); } } }