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

Refactored FrameworkCompatibilityService for use in NuGet.Jobs #9733

Merged
merged 1 commit into from
Nov 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace NuGetGallery.Frameworks
{
public class FrameworkCompatibilityService : IFrameworkCompatibilityService
public static class FrameworkCompatibilityService
{
private static readonly IFrameworkCompatibilityProvider CompatibilityProvider = DefaultCompatibilityProvider.Instance;
private static readonly IReadOnlyList<NuGetFramework> AllSupportedFrameworks = SupportedFrameworks.AllSupportedNuGetFrameworks;
private static readonly IReadOnlyDictionary<NuGetFramework, ISet<NuGetFramework>> CompatibilityMatrix = GetCompatibilityMatrix();

public ISet<NuGetFramework> GetCompatibleFrameworks(IEnumerable<NuGetFramework> packageFrameworks)
public static ISet<NuGetFramework> GetCompatibleFrameworks(IEnumerable<NuGetFramework> packageFrameworks)
{
if (packageFrameworks == null)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ public class PackageFrameworkCompatibilityFactory : IPackageFrameworkCompatibili
private readonly NuGetFrameworkSorter Sorter = new NuGetFrameworkSorter();
private readonly int NetStartingMajorVersion = 5;

private readonly IFrameworkCompatibilityService _compatibilityService;

public PackageFrameworkCompatibilityFactory(IFrameworkCompatibilityService compatibilityService)
{
_compatibilityService = compatibilityService ?? throw new ArgumentNullException();
}

public PackageFrameworkCompatibility Create(ICollection<PackageFramework> packageFrameworks)
{
if (packageFrameworks == null)
Expand All @@ -53,7 +46,7 @@ public PackageFrameworkCompatibility Create(ICollection<PackageFramework> packag

private IReadOnlyDictionary<string, IReadOnlyCollection<PackageFrameworkCompatibilityTableData>> CreateFrameworkCompatibilityTable(ICollection<NuGetFramework> filteredPackageFrameworks)
{
var compatibleFrameworks = _compatibilityService.GetCompatibleFrameworks(filteredPackageFrameworks);
var compatibleFrameworks = FrameworkCompatibilityService.GetCompatibleFrameworks(filteredPackageFrameworks);

var table = new Dictionary<string, SortedSet<PackageFrameworkCompatibilityTableData>>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class PackageFrameworkCompatibilityTableData
public NuGetFramework Framework { get; set; }

/// <summary>
/// <see langword="true"/> if the <see cref="Framework"/> was computed from <see cref="IFrameworkCompatibilityService"/>.<br></br>
/// <see langword="true"/> if the <see cref="Framework"/> was computed from <see cref="FrameworkCompatibilityService"/>.<br></br>
/// <see langword="false"/> if the <see cref="Framework"/> was retrieved from the package asset frameworks.
/// </summary>
public bool IsComputed { get; set; }
Expand Down
5 changes: 0 additions & 5 deletions src/NuGetGallery/App_Start/DefaultDependenciesModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,6 @@ protected override void Load(ContainerBuilder builder)
.As<IPackageVulnerabilitiesCacheService>()
.SingleInstance();

builder.RegisterType<FrameworkCompatibilityService>()
.AsSelf()
.As<IFrameworkCompatibilityService>()
.SingleInstance();

builder.RegisterType<PackageFrameworkCompatibilityFactory>()
.AsSelf()
.As<IPackageFrameworkCompatibilityFactory>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ namespace NuGetGallery.Frameworks
{
public class FrameworkCompatibilityServiceFacts
{
private readonly IFrameworkCompatibilityService _service;
private readonly IFrameworkCompatibilityProvider CompatibilityProvider = DefaultCompatibilityProvider.Instance;

public FrameworkCompatibilityServiceFacts()
{
_service = new FrameworkCompatibilityService();
}

[Fact]
public void NullPackageFrameworksThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _service.GetCompatibleFrameworks(null));
Assert.Throws<ArgumentNullException>(() => FrameworkCompatibilityService.GetCompatibleFrameworks(null));
}

[Fact]
public void EmptyPackageFrameworksReturnsEmptySet()
{
var result = _service.GetCompatibleFrameworks(new List<NuGetFramework>());
var result = FrameworkCompatibilityService.GetCompatibleFrameworks(new List<NuGetFramework>());

Assert.Empty(result);
}
Expand All @@ -38,7 +32,7 @@ public void UnknownSupportedPackageReturnsSetWithSameFramework()
{
var framework = NuGetFramework.Parse("net45-client");
var frameworks = new List<NuGetFramework>() { framework };
var compatible = _service.GetCompatibleFrameworks(frameworks);
var compatible = FrameworkCompatibilityService.GetCompatibleFrameworks(frameworks);

Assert.False(framework.IsUnsupported);
Assert.Equal(expected: 1, compatible.Count);
Expand All @@ -53,7 +47,7 @@ public void UnsupportedPackageFrameworksReturnsEmptySet(string unsupportedFramew
{
var unsupportedFramework = NuGetFramework.Parse(unsupportedFrameworkName);

var result = _service.GetCompatibleFrameworks(new List<NuGetFramework>() { unsupportedFramework });
var result = FrameworkCompatibilityService.GetCompatibleFrameworks(new List<NuGetFramework>() { unsupportedFramework });

Assert.True(unsupportedFramework.IsUnsupported);
Assert.Equal(expected: 0, actual: result.Count);
Expand All @@ -67,7 +61,7 @@ public void PCLPackageFrameworksReturnsEmptySet(string pclFrameworkName)
{
var portableFramework = NuGetFramework.Parse(pclFrameworkName);

var result = _service.GetCompatibleFrameworks(new List<NuGetFramework>() { portableFramework });
var result = FrameworkCompatibilityService.GetCompatibleFrameworks(new List<NuGetFramework>() { portableFramework });

Assert.True(portableFramework.IsPCL);
Assert.Equal(expected: 0, actual: result.Count);
Expand All @@ -85,7 +79,7 @@ public void ValidPackageFrameworksReturnsFrameworksCompatibleForAtLeastOne(param
frameworks.Add(NuGetFramework.Parse(frameworkName));
}

var compatibleFrameworks = _service.GetCompatibleFrameworks(frameworks);
var compatibleFrameworks = FrameworkCompatibilityService.GetCompatibleFrameworks(frameworks);

Assert.True(compatibleFrameworks.Count > 0);

Expand All @@ -108,7 +102,7 @@ public void WindowsPlatformVersionsShouldContainAllSpecifiedFrameworks(string wi
projectFrameworks.Add(NuGetFramework.Parse(frameworkName));
}

var compatibleFrameworks = _service.GetCompatibleFrameworks(new HashSet<NuGetFramework>() { packageFramework });
var compatibleFrameworks = FrameworkCompatibilityService.GetCompatibleFrameworks(new HashSet<NuGetFramework>() { packageFramework });
Assert.Equal(windowsProjectFrameworks.Length, compatibleFrameworks.Count);

var containsAllCompatibleFrameworks = compatibleFrameworks.All(cf => projectFrameworks.Contains(cf));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,10 @@ namespace NuGetGallery.Frameworks
public class PackageFrameworkCompatibilityFactoryFacts
{
private readonly PackageFrameworkCompatibilityFactory _factory;
private readonly IFrameworkCompatibilityService _service;

public PackageFrameworkCompatibilityFactoryFacts()
{
_service = new FrameworkCompatibilityService();
_factory = new PackageFrameworkCompatibilityFactory(_service);
}

[Fact]
public void NullFrameworkCompatibilityServiceThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new PackageFrameworkCompatibilityFactory(null));
_factory = new PackageFrameworkCompatibilityFactory();
}

[Fact]
Expand Down