Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures that the registration index is sorted by version #535

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/BaGet.Core/Metadata/RegistrationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public RegistrationBuilder(IUrlGenerator url)

public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration registration)
{
var versions = registration.Packages.Select(p => p.Version).ToList();
var sortedPackages = registration.Packages.OrderBy(p => p.Version).ToList();

// TODO: Paging of registration items.
// "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
Expand All @@ -33,9 +33,9 @@ public virtual BaGetRegistrationIndexResponse BuildIndex(PackageRegistration reg
{
RegistrationPageUrl = _url.GetRegistrationIndexUrl(registration.PackageId),
Count = registration.Packages.Count(),
Lower = versions.Min().ToNormalizedString().ToLowerInvariant(),
Upper = versions.Max().ToNormalizedString().ToLowerInvariant(),
ItemsOrNull = registration.Packages.Select(ToRegistrationIndexPageItem).ToList(),
Lower = sortedPackages.First().Version.ToNormalizedString().ToLowerInvariant(),
Upper = sortedPackages.Last().Version.ToNormalizedString().ToLowerInvariant(),
ItemsOrNull = sortedPackages.Select(ToRegistrationIndexPageItem).ToList(),
}
}
};
Expand Down
66 changes: 66 additions & 0 deletions tests/BaGet.Core.Tests/Metadata/RegistrationBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.Linq;
using Moq;
using NuGet.Versioning;
using Xunit;

namespace BaGet.Core.Tests.Metadata
{
public class RegistrationBuilderTests
{
private readonly Mock<IUrlGenerator> _urlGenerator;

public RegistrationBuilderTests()
{
_urlGenerator = new Mock<IUrlGenerator>();
}

[Fact]
public void TheRegistrationIndexResponseIsSortedByVersion()
{
// Arrange
var packageId = "BaGet.Test";
var authors = new string[] { "test" };

var packages = new List<Package>
{
GetTestPackage(packageId, "3.1.0"),
GetTestPackage(packageId, "10.0.5"),
GetTestPackage(packageId, "3.2.0"),
GetTestPackage(packageId, "3.1.0-pre"),
GetTestPackage(packageId, "1.0.0-beta1"),
GetTestPackage(packageId, "1.0.0"),
};

var registration = new PackageRegistration(packageId, packages);

var registrationBuilder = new RegistrationBuilder(_urlGenerator.Object);

// Act
var response = registrationBuilder.BuildIndex(registration);

// Assert
Assert.Equal(packages.Count, response.Pages[0].ItemsOrNull.Count);
var index = 0;
foreach (var package in packages.OrderBy(p => p.Version))
{
Assert.Equal(package.Version.ToFullString(), response.Pages[0].ItemsOrNull[index++].PackageMetadata.Version);
}
}

/// <summary>
/// Create a fake <see cref="Package"></see> with the minimum metadata needed by the <see cref="RegistrationBuilder"></see>.
/// </summary>
private Package GetTestPackage(string packageId, string version)
{
return new Package
{
Id = packageId,
Authors = new string[] { "test" },
PackageTypes = new List<PackageType> { new PackageType { Name = "test" } },
Dependencies = new List<PackageDependency> { },
Version = new NuGetVersion(version),
};
}
}
}