Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Throw exception for missing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouke committed Jan 13, 2021
1 parent 18d6cdb commit b6e59f9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 51 deletions.
19 changes: 9 additions & 10 deletions NuKeeper.Inspection.Tests/NuGetApi/ApiPackageLookupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NSubstitute;
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Abstractions.NuGetApi;
Expand All @@ -16,20 +17,18 @@ namespace NuKeeper.Inspection.Tests.NuGetApi
public class ApiPackageLookupTests
{
[Test]
public async Task WhenNoPackagesAreFound()
public void WhenNoPackagesAreFound()
{
var allVersionsLookup = MockVersionLookup(new List<PackageSearchMetadata>());
IApiPackageLookup lookup = new ApiPackageLookup(allVersionsLookup);

var updates = await lookup.FindVersionUpdate(
CurrentVersion123("TestPackage"),
NuGetSources.GlobalFeed,
VersionChange.Major,
UsePrerelease.FromPrerelease);

Assert.That(updates, Is.Not.Null);
Assert.That(updates.Major, Is.Null);
Assert.That(updates.Selected(), Is.Null);
var exception = Assert.ThrowsAsync<NuKeeperException>(async () =>
await lookup.FindVersionUpdate(
CurrentVersion123("TestPackage"),
NuGetSources.GlobalFeed,
VersionChange.Major,
UsePrerelease.FromPrerelease));
Assert.That(exception.Message, Is.EqualTo("Could not find package TestPackage.1.2.3 in these sources: https://api.nuget.org/v3/index.json"));
}

[Test]
Expand Down
5 changes: 5 additions & 0 deletions NuKeeper.Inspection/NuGetApi/ApiPackageLookup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using NuGet.Packaging.Core;
using NuKeeper.Abstractions;
Expand Down Expand Up @@ -31,6 +32,10 @@ public async Task<PackageLookupResult> FindVersionUpdate(
var includePrerelease = ShouldAllowPrerelease(package, usePrerelease);

var foundVersions = await _packageVersionsLookup.Lookup(package.Id, includePrerelease, sources);

if (!foundVersions.Any())
throw new NuKeeperException($"Could not find package {package} in these sources: {sources}");

return VersionChanges.MakeVersions(package.Version, foundVersions, allowedChange);
}

Expand Down
19 changes: 9 additions & 10 deletions NuKeeper.Integration.Tests/NuGet/Api/ApiPackageLookupTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Inspection.NuGetApi;
Expand Down Expand Up @@ -33,19 +34,17 @@ public async Task AmbiguousPackageName_ShouldReturnCorrectResult()
}

[Test]
public async Task UnknownPackageName_ShouldNotReturnResult()
public void UnknownPackageName_ShouldNotReturnResult()
{
var lookup = BuildPackageLookup();

var package = await lookup.FindVersionUpdate(
Current(Guid.NewGuid().ToString()),
NuGetSources.GlobalFeed,
VersionChange.Major,
UsePrerelease.FromPrerelease);

Assert.That(package, Is.Not.Null);
Assert.That(package.Major, Is.Null);
Assert.That(package.Selected(), Is.Null);
var exception = Assert.ThrowsAsync<NuKeeperException>(async () =>
await lookup.FindVersionUpdate(
Current(Guid.NewGuid().ToString()),
NuGetSources.GlobalFeed,
VersionChange.Major,
UsePrerelease.FromPrerelease));
Assert.That(exception.Message, Does.Match("Could not find package (.+?) in these sources: https://api.nuget.org/v3/index.json"));
}

[Test]
Expand Down
38 changes: 7 additions & 31 deletions NuKeeper.Integration.Tests/NuGet/Api/BulkPackageLookupTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NuGet.Packaging.Core;
using NuGet.Versioning;
using NuKeeper.Abstractions;
using NuKeeper.Abstractions.Configuration;
using NuKeeper.Abstractions.NuGet;
using NuKeeper.Inspection.NuGetApi;
Expand Down Expand Up @@ -73,7 +74,7 @@ public async Task FindsSingleUpdateForPackageDifferingOnlyByCase()
}

[Test]
public async Task InvalidPackageIsIgnored()
public void InvalidPackageThrows()
{
var packages = new List<PackageIdentity>
{
Expand All @@ -82,12 +83,11 @@ public async Task InvalidPackageIsIgnored()

var lookup = BuildBulkPackageLookup();

var results = await lookup.FindVersionUpdates(
packages, NuGetSources.GlobalFeed, VersionChange.Major,
UsePrerelease.FromPrerelease);

Assert.That(results, Is.Not.Null);
Assert.That(results, Is.Empty);
var exception = Assert.ThrowsAsync<NuKeeperException>(async () =>
await lookup.FindVersionUpdates(
packages, NuGetSources.GlobalFeed, VersionChange.Major,
UsePrerelease.FromPrerelease));
Assert.That(exception.Message, Does.Match("Could not find package (.+?) in these sources: https://api.nuget.org/v3/index.json"));
}

[Test]
Expand All @@ -103,30 +103,6 @@ public async Task TestEmptyList()
Assert.That(results, Is.Empty);
}

[Test]
public async Task ValidPackagesWorkDespiteInvalidPackages()
{
var packages = new List<PackageIdentity>
{
Current("Moq"),
Current(Guid.NewGuid().ToString()),
Current("Newtonsoft.Json"),
Current(Guid.NewGuid().ToString())
};

var lookup = BuildBulkPackageLookup();

var results = await lookup.FindVersionUpdates(
packages, NuGetSources.GlobalFeed, VersionChange.Major,
UsePrerelease.FromPrerelease);

var updatedPackages = results.Select(p => p.Key);
Assert.That(results, Is.Not.Null);
Assert.That(results.Count, Is.EqualTo(2));
Assert.That(updatedPackages, Has.Some.Matches<PackageIdentity>(p => p.Id == "Moq"));
Assert.That(updatedPackages, Has.Some.Matches<PackageIdentity>(p => p.Id == "Newtonsoft.Json"));
}

private BulkPackageLookup BuildBulkPackageLookup()
{
var lookup = new ApiPackageLookup(new PackageVersionsLookup(NugetLogger, NukeeperLogger));
Expand Down
1 change: 1 addition & 0 deletions NuKeeper/ExitCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ enum ExitCodes
Success = 0,
UnknownError = 1 << 0,
InvalidArguments = 1 << 1,
OtherError = 1 << 2,
}
}
6 changes: 6 additions & 0 deletions NuKeeper/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using McMaster.Extensions.CommandLineUtils;
using NuKeeper.Abstractions;
using NuKeeper.Commands;
using System;
using System.Reflection;
Expand Down Expand Up @@ -39,6 +40,11 @@ public static async Task<int> Main(string[] args)
Console.WriteLine(cpe.Message);
return (int)ExitCodes.InvalidArguments;
}
catch (NuKeeperException nke)
{
Console.WriteLine(nke.Message);
return (int)ExitCodes.OtherError;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Expand Down

0 comments on commit b6e59f9

Please sign in to comment.