Skip to content

Commit dac7205

Browse files
committed
Also test optional fields in custom package information
1 parent d503079 commit dac7205

File tree

4 files changed

+64
-14
lines changed

4 files changed

+64
-14
lines changed

src/NuGetUtility/PackageInformationReader/CustomPackageInformation.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,14 @@
55

66
namespace NuGetUtility.PackageInformationReader
77
{
8-
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);
8+
public record struct CustomPackageInformation(string Id,
9+
INuGetVersion Version,
10+
string License,
11+
string? Copyright = null,
12+
string? Authors = null,
13+
string? Title = null,
14+
string? ProjectUrl = null,
15+
string? Summary = null,
16+
string? Description = null,
17+
Uri? LicenseUrl = null);
918
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the projects contributors.
2+
// The license conditions are provided in the LICENSE file located in the project root
3+
4+
using AutoFixture;
5+
using AutoFixture.Kernel;
6+
using NuGetUtility.PackageInformationReader;
7+
using NuGetUtility.Wrapper.NuGetWrapper.Versioning;
8+
9+
namespace NuGetUtility.Test.Helper.AutoFixture.NuGet.Versioning
10+
{
11+
internal class CustomPackageInformationBuilderWithOptionalFileds : ISpecimenBuilder
12+
{
13+
public object Create(object request, ISpecimenContext context)
14+
{
15+
if (request is System.Type t && t == typeof(CustomPackageInformation))
16+
{
17+
return new CustomPackageInformation(context.Create<string>(),
18+
context.Create<INuGetVersion>(),
19+
context.Create<string>(),
20+
CreateOptional<string>(context),
21+
CreateOptional<string>(context),
22+
CreateOptional<string>(context),
23+
CreateOptional<string>(context),
24+
CreateOptional<string>(context),
25+
CreateOptional<string>(context),
26+
CreateOptional<Uri>(context));
27+
}
28+
29+
return new NoSpecimen();
30+
}
31+
32+
private static T? CreateOptional<T>(ISpecimenContext context) where T : notnull
33+
{
34+
return context.Create<bool>() ? context.Create<T>() : default;
35+
}
36+
}
37+
}

tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NuGetUtility.LicenseValidator;
55
using OpenQA.Selenium;
66
using OpenQA.Selenium.Chrome;
7+
using OpenQA.Selenium.Firefox;
78

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

47-
private sealed class DisposableWebDriver : IDisposable
48+
private sealed class DisposableFirefoxWebDriver : IDisposable
4849
{
4950
private readonly IWebDriver _driver;
5051

51-
public DisposableWebDriver()
52+
public DisposableFirefoxWebDriver()
5253
{
53-
var options = new ChromeOptions();
54+
var options = new FirefoxOptions();
5455
options.AddArguments("--no-sandbox", "--disable-dev-shm-usage", "--headless");
55-
_driver = new ChromeDriver(options);
56+
_driver = new FirefoxDriver(options);
5657
}
5758

5859
public void Dispose()

tests/NuGetUtility.Test/PackageInformationReader/PackageInformationReaderTest.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void SetUp()
2525
_customPackageInformation = Enumerable.Empty<CustomPackageInformation>().ToList();
2626
_fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
2727
_fixture.Customizations.Add(new NuGetVersionBuilder());
28+
_fixture.Customizations.Add(new CustomPackageInformationBuilderWithOptionalFileds());
2829
_repositories = Array.Empty<ISourceRepository>();
2930
_globalPackagesFolderUtility = Substitute.For<IGlobalPackagesFolderUtility>();
3031

@@ -93,15 +94,15 @@ private static void CheckResult(ReferencedPackageWithContext[] result,
9394
LicenseType licenseType)
9495
{
9596
Assert.That(packages, Is.EquivalentTo(result.Select(s => new CustomPackageInformation(s.PackageInfo.Identity.Id,
96-
s.PackageInfo.Identity.Version,
97-
s.PackageInfo.LicenseMetadata!.License,
98-
s.PackageInfo.LicenseUrl,
99-
s.PackageInfo.Copyright,
100-
s.PackageInfo.Authors,
101-
s.PackageInfo.Title,
102-
s.PackageInfo.ProjectUrl,
103-
s.PackageInfo.Summary,
104-
s.PackageInfo.Description))));
97+
s.PackageInfo.Identity.Version,
98+
s.PackageInfo.LicenseMetadata!.License,
99+
s.PackageInfo.Copyright,
100+
s.PackageInfo.Authors,
101+
s.PackageInfo.Title,
102+
s.PackageInfo.ProjectUrl,
103+
s.PackageInfo.Summary,
104+
s.PackageInfo.Description,
105+
s.PackageInfo.LicenseUrl))));
105106
foreach (ReferencedPackageWithContext r in result)
106107
{
107108
Assert.That(r.Context, Is.EqualTo(project));
@@ -125,6 +126,7 @@ public async Task GetPackageInfo_Should_PreferLocalPackageCacheOverRepositories(
125126
mockedInfo.ProjectUrl.Returns(info.ProjectUrl);
126127
mockedInfo.Summary.Returns(info.Summary);
127128
mockedInfo.Description.Returns(info.Description);
129+
mockedInfo.LicenseUrl.Returns(info.LicenseUrl);
128130
mockedInfo.LicenseMetadata.Returns(new LicenseMetadata(LicenseType.Expression, info.License));
129131
mockedInfo.LicenseUrl.Returns(info.LicenseUrl);
130132
_globalPackagesFolderUtility.GetPackage(identity).Returns(mockedInfo);
@@ -156,6 +158,7 @@ private static void SetupPackagesForRepositories(IEnumerable<CustomPackageInform
156158
resultingInfo.Summary.Returns(package.Summary);
157159
resultingInfo.Description.Returns(package.Description);
158160
resultingInfo.ProjectUrl.Returns(package.ProjectUrl);
161+
resultingInfo.LicenseUrl.Returns(package.LicenseUrl);
159162

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

0 commit comments

Comments
 (0)