Skip to content

Commit

Permalink
Also test optional fields in custom package information
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed Feb 3, 2025
1 parent d503079 commit dac7205
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,14 @@

namespace NuGetUtility.PackageInformationReader
{
public record struct CustomPackageInformation(string Id, INuGetVersion Version, string License, Uri? LicenseUrl = null, string? Copyright = null, string? Authors = null, string? Title = null, string? ProjectUrl = null, string? Summary = null, string? Description = null);
public record struct CustomPackageInformation(string Id,
INuGetVersion Version,
string License,
string? Copyright = null,
string? Authors = null,
string? Title = null,
string? ProjectUrl = null,
string? Summary = null,
string? Description = null,
Uri? LicenseUrl = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the projects contributors.
// The license conditions are provided in the LICENSE file located in the project root

using AutoFixture;
using AutoFixture.Kernel;
using NuGetUtility.PackageInformationReader;
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;

namespace NuGetUtility.Test.Helper.AutoFixture.NuGet.Versioning
{
internal class CustomPackageInformationBuilderWithOptionalFileds : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
if (request is System.Type t && t == typeof(CustomPackageInformation))
{
return new CustomPackageInformation(context.Create<string>(),
context.Create<INuGetVersion>(),
context.Create<string>(),
CreateOptional<string>(context),
CreateOptional<string>(context),
CreateOptional<string>(context),
CreateOptional<string>(context),
CreateOptional<string>(context),
CreateOptional<string>(context),
CreateOptional<Uri>(context));
}

return new NoSpecimen();
}

private static T? CreateOptional<T>(ISpecimenContext context) where T : notnull
{
return context.Create<bool>() ? context.Create<T>() : default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NuGetUtility.LicenseValidator;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;

namespace NuGetUtility.Test.LicenseValidator
{
Expand Down Expand Up @@ -44,15 +45,15 @@ private static Task<CompareResult> CompareLicense(string received, string verifi
return Task.FromResult(new CompareResult((!string.IsNullOrWhiteSpace(verified)) && received.Contains(verified)));
}

private sealed class DisposableWebDriver : IDisposable
private sealed class DisposableFirefoxWebDriver : IDisposable
{
private readonly IWebDriver _driver;

public DisposableWebDriver()
public DisposableFirefoxWebDriver()
{
var options = new ChromeOptions();
var options = new FirefoxOptions();
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
_driver = new ChromeDriver(options);
_driver = new FirefoxDriver(options);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void SetUp()
_customPackageInformation = Enumerable.Empty<CustomPackageInformation>().ToList();
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
_fixture.Customizations.Add(new NuGetVersionBuilder());
_fixture.Customizations.Add(new CustomPackageInformationBuilderWithOptionalFileds());
_repositories = Array.Empty<ISourceRepository>();
_globalPackagesFolderUtility = Substitute.For<IGlobalPackagesFolderUtility>();

Expand Down Expand Up @@ -93,15 +94,15 @@ private static void CheckResult(ReferencedPackageWithContext[] result,
LicenseType licenseType)
{
Assert.That(packages, Is.EquivalentTo(result.Select(s => new CustomPackageInformation(s.PackageInfo.Identity.Id,
s.PackageInfo.Identity.Version,
s.PackageInfo.LicenseMetadata!.License,
s.PackageInfo.LicenseUrl,
s.PackageInfo.Copyright,
s.PackageInfo.Authors,
s.PackageInfo.Title,
s.PackageInfo.ProjectUrl,
s.PackageInfo.Summary,
s.PackageInfo.Description))));
s.PackageInfo.Identity.Version,
s.PackageInfo.LicenseMetadata!.License,
s.PackageInfo.Copyright,
s.PackageInfo.Authors,
s.PackageInfo.Title,
s.PackageInfo.ProjectUrl,
s.PackageInfo.Summary,
s.PackageInfo.Description,
s.PackageInfo.LicenseUrl))));
foreach (ReferencedPackageWithContext r in result)
{
Assert.That(r.Context, Is.EqualTo(project));
Expand All @@ -125,6 +126,7 @@ public async Task GetPackageInfo_Should_PreferLocalPackageCacheOverRepositories(
mockedInfo.ProjectUrl.Returns(info.ProjectUrl);
mockedInfo.Summary.Returns(info.Summary);
mockedInfo.Description.Returns(info.Description);
mockedInfo.LicenseUrl.Returns(info.LicenseUrl);
mockedInfo.LicenseMetadata.Returns(new LicenseMetadata(LicenseType.Expression, info.License));
mockedInfo.LicenseUrl.Returns(info.LicenseUrl);
_globalPackagesFolderUtility.GetPackage(identity).Returns(mockedInfo);
Expand Down Expand Up @@ -156,6 +158,7 @@ private static void SetupPackagesForRepositories(IEnumerable<CustomPackageInform
resultingInfo.Summary.Returns(package.Summary);
resultingInfo.Description.Returns(package.Description);
resultingInfo.ProjectUrl.Returns(package.ProjectUrl);
resultingInfo.LicenseUrl.Returns(package.LicenseUrl);

metadataReturningProperInformation.TryGetMetadataAsync(new PackageIdentity(package.Id, package.Version), Arg.Any<CancellationToken>()).
Returns(_ => Task.FromResult<IPackageMetadata?>(resultingInfo));
Expand Down

0 comments on commit dac7205

Please sign in to comment.