Skip to content

Commit

Permalink
Allow most package information fields to be overwritten (#68)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Ensslen <[email protected]>
  • Loading branch information
sensslen and Simon Ensslen authored Jul 10, 2024
1 parent 5019c72 commit d639901
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

namespace NuGetUtility.PackageInformationReader
{
public record struct CustomPackageInformation(string Id, INuGetVersion Version, string License);
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private PackageSearchResult TryGetPackageInfoFromCustomInformation(PackageIdenti
return new PackageSearchResult();
}

return new PackageSearchResult(new PackageMetadata(package, resolvedCustomInformation.License, LicenseType.Overwrite));
return new PackageSearchResult(new PackageMetadata(package, LicenseType.Overwrite, resolvedCustomInformation));
}

private static async Task<IPackageMetadataResource?> TryGetPackageMetadataResource(ISourceRepository repository)
Expand Down
19 changes: 11 additions & 8 deletions src/NuGetUtility/PackageInformationReader/PackageMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@ public PackageMetadata(PackageIdentity identity)
Identity = identity;
}

public PackageMetadata(PackageIdentity identity, string licenseIdentifier, LicenseType licenseType)
public PackageMetadata(PackageIdentity identity, LicenseType licenseType, CustomPackageInformation customPackageInformation)
{
Identity = identity;
LicenseMetadata = new LicenseMetadata(licenseType, licenseIdentifier);
LicenseMetadata = new LicenseMetadata(licenseType, customPackageInformation.License);
CustomPackageInformation = customPackageInformation;
}

private CustomPackageInformation? CustomPackageInformation { get; }

public PackageIdentity Identity { get; }

public string Title { get; } = string.Empty;
public string? Title => CustomPackageInformation?.Title;

public Uri? LicenseUrl => null;

public string ProjectUrl => string.Empty;
public string? ProjectUrl => CustomPackageInformation?.ProjectUrl;

public string Description => string.Empty;
public string? Description => CustomPackageInformation?.Description;

public string Summary => string.Empty;
public string? Summary => CustomPackageInformation?.Summary;

public string Copyright => string.Empty;
public string? Copyright => CustomPackageInformation?.Copyright;

public string Authors => string.Empty;
public string? Authors => CustomPackageInformation?.Authors;

public LicenseMetadata? LicenseMetadata { get; } = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ namespace NuGetUtility.Wrapper.NuGetWrapper.Packaging
public interface IPackageMetadata
{
PackageIdentity Identity { get; }
string Title { get; }
string? Title { get; }
Uri? LicenseUrl { get; }
string ProjectUrl { get; }
string Description { get; }
string Summary { get; }
string Copyright { get; }
string Authors { get; }
string? ProjectUrl { get; }
string? Description { get; }
string? Summary { get; }
string? Copyright { get; }
string? Authors { get; }
LicenseMetadata? LicenseMetadata { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ public WrappedPackageSearchMetadata(IPackageSearchMetadata searchMetadata)

public PackageIdentity Identity { get; }

public string Title => _searchMetadata.Title;
public string? Title => _searchMetadata.Title;

public Uri? LicenseUrl => _searchMetadata.LicenseUrl;

public string ProjectUrl => _searchMetadata.ProjectUrl?.ToString() ?? string.Empty;
public string? ProjectUrl => _searchMetadata.ProjectUrl?.ToString();

public string Description => _searchMetadata.Description;
public string? Description => _searchMetadata.Description;

public string Summary => _searchMetadata.Summary;
public string? Summary => _searchMetadata.Summary;

public string Copyright => string.Empty;
public string? Copyright => null;

public string Authors => _searchMetadata.Authors;
public string? Authors => _searchMetadata.Authors;

public LicenseMetadata? LicenseMetadata { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ public WrappedPackageMetadata(ManifestMetadata metadata)

public PackageIdentity Identity { get; }

public string Title => _metadata.Title;
public string? Title => _metadata.Title;

public Uri? LicenseUrl => _metadata.LicenseUrl;

public string ProjectUrl => _metadata.ProjectUrl?.ToString() ?? string.Empty;
public string? ProjectUrl => _metadata.ProjectUrl?.ToString();

public string Description => _metadata.Description;
public string? Description => _metadata.Description;

public string Summary => _metadata.Summary;
public string? Summary => _metadata.Summary;

public string Copyright => _metadata.Copyright;
public string? Copyright => _metadata.Copyright;

public string Authors => string.Join(",", _metadata.Authors); // https://learn.microsoft.com/en-us/nuget/reference/nuspec#authors
public string? Authors => string.Join(",", _metadata.Authors); // https://learn.microsoft.com/en-us/nuget/reference/nuspec#authors

public Packaging.LicenseMetadata? LicenseMetadata { get; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ private static void CheckResult(ReferencedPackageWithContext[] result,
{
CollectionAssert.AreEquivalent(packages,
result.Select(s => new CustomPackageInformation(s.PackageInfo.Identity.Id,
s.PackageInfo.Identity.Version,
s.PackageInfo.LicenseMetadata!.License)));
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)));
foreach (ReferencedPackageWithContext r in result)
{
Assert.AreEqual(project, r.Context);
Expand All @@ -113,6 +119,12 @@ public async Task GetPackageInfo_Should_PreferLocalPackageCacheOverRepositories(
var identity = new PackageIdentity(info.Id, info.Version);
IPackageMetadata mockedInfo = Substitute.For<IPackageMetadata>();
mockedInfo.Identity.Returns(identity);
mockedInfo.Copyright.Returns(info.Copyright);
mockedInfo.Authors.Returns(info.Authors);
mockedInfo.Title.Returns(info.Title);
mockedInfo.ProjectUrl.Returns(info.ProjectUrl);
mockedInfo.Summary.Returns(info.Summary);
mockedInfo.Description.Returns(info.Description);
mockedInfo.LicenseMetadata.Returns(new LicenseMetadata(LicenseType.Expression, info.License));
_globalPackagesFolderUtility.GetPackage(identity).Returns(mockedInfo);
Expand All @@ -136,6 +148,12 @@ private void SetupPackagesForRepositories(IEnumerable<CustomPackageInformation>
IPackageMetadata resultingInfo = Substitute.For<IPackageMetadata>();
resultingInfo.Identity.Returns(new PackageIdentity(package.Id, package.Version));
resultingInfo.LicenseMetadata.Returns(new LicenseMetadata(LicenseType.Expression, package.License));
resultingInfo.Copyright.Returns(package.Copyright);
resultingInfo.Authors.Returns(package.Authors);
resultingInfo.Title.Returns(package.Title);
resultingInfo.Summary.Returns(package.Summary);
resultingInfo.Description.Returns(package.Description);
resultingInfo.ProjectUrl.Returns(package.ProjectUrl);

metadataReturningProperInformation.TryGetMetadataAsync(new PackageIdentity(package.Id, package.Version), Arg.Any<CancellationToken>()).
Returns(_ => Task.FromResult<IPackageMetadata?>(resultingInfo));
Expand Down Expand Up @@ -193,8 +211,8 @@ public async Task GetPackageInfo_Should_ReturnDummyPackageMetadataForPackagesNot
Assert.AreEqual(expectation.Version, result.PackageInfo.Identity.Version);
Assert.IsNull(result.PackageInfo.LicenseMetadata);
Assert.IsNull(result.PackageInfo.LicenseUrl);
Assert.AreEqual(string.Empty, result.PackageInfo.Summary);
Assert.AreEqual(string.Empty, result.PackageInfo.Title);
Assert.IsNull(result.PackageInfo.Summary);
Assert.IsNull(result.PackageInfo.Title);
}
}
}
Expand Down

0 comments on commit d639901

Please sign in to comment.