From dac7205a50494a8399eca4599afd138a397d5a06 Mon Sep 17 00:00:00 2001 From: Simon Ensslen Date: Mon, 3 Feb 2025 07:54:20 +0100 Subject: [PATCH] Also test optional fields in custom package information --- .../CustomPackageInformation.cs | 11 +++++- ...ageInformationBuilderWithOptionalFileds.cs | 37 +++++++++++++++++++ .../UrlToLicenseMappingTest.cs | 9 +++-- .../PackageInformationReaderTest.cs | 21 ++++++----- 4 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 tests/NuGetUtility.Test/Helper/AutoFixture/NuGet/Versioning/CustomPackageInformationBuilderWithOptionalFileds.cs diff --git a/src/NuGetUtility/PackageInformationReader/CustomPackageInformation.cs b/src/NuGetUtility/PackageInformationReader/CustomPackageInformation.cs index 7e76612d..ce8ed9d3 100644 --- a/src/NuGetUtility/PackageInformationReader/CustomPackageInformation.cs +++ b/src/NuGetUtility/PackageInformationReader/CustomPackageInformation.cs @@ -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); } diff --git a/tests/NuGetUtility.Test/Helper/AutoFixture/NuGet/Versioning/CustomPackageInformationBuilderWithOptionalFileds.cs b/tests/NuGetUtility.Test/Helper/AutoFixture/NuGet/Versioning/CustomPackageInformationBuilderWithOptionalFileds.cs new file mode 100644 index 00000000..7d5f1523 --- /dev/null +++ b/tests/NuGetUtility.Test/Helper/AutoFixture/NuGet/Versioning/CustomPackageInformationBuilderWithOptionalFileds.cs @@ -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(), + context.Create(), + context.Create(), + CreateOptional(context), + CreateOptional(context), + CreateOptional(context), + CreateOptional(context), + CreateOptional(context), + CreateOptional(context), + CreateOptional(context)); + } + + return new NoSpecimen(); + } + + private static T? CreateOptional(ISpecimenContext context) where T : notnull + { + return context.Create() ? context.Create() : default; + } + } +} diff --git a/tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs b/tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs index f627630a..11137e68 100644 --- a/tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs +++ b/tests/NuGetUtility.Test/LicenseValidator/UrlToLicenseMappingTest.cs @@ -4,6 +4,7 @@ using NuGetUtility.LicenseValidator; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Firefox; namespace NuGetUtility.Test.LicenseValidator { @@ -44,15 +45,15 @@ private static Task 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() diff --git a/tests/NuGetUtility.Test/PackageInformationReader/PackageInformationReaderTest.cs b/tests/NuGetUtility.Test/PackageInformationReader/PackageInformationReaderTest.cs index 0d21a0e5..8dc57497 100644 --- a/tests/NuGetUtility.Test/PackageInformationReader/PackageInformationReaderTest.cs +++ b/tests/NuGetUtility.Test/PackageInformationReader/PackageInformationReaderTest.cs @@ -25,6 +25,7 @@ public void SetUp() _customPackageInformation = Enumerable.Empty().ToList(); _fixture = new Fixture().Customize(new AutoNSubstituteCustomization()); _fixture.Customizations.Add(new NuGetVersionBuilder()); + _fixture.Customizations.Add(new CustomPackageInformationBuilderWithOptionalFileds()); _repositories = Array.Empty(); _globalPackagesFolderUtility = Substitute.For(); @@ -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)); @@ -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); @@ -156,6 +158,7 @@ private static void SetupPackagesForRepositories(IEnumerable()). Returns(_ => Task.FromResult(resultingInfo));