Skip to content

Commit

Permalink
Add new license origin which makes clear that a license was actually …
Browse files Browse the repository at this point in the history
…validated with an overwrite
  • Loading branch information
sensslen committed Nov 24, 2023
1 parent ea1c1e9 commit a61630f
Show file tree
Hide file tree
Showing 264 changed files with 2,561 additions and 1,983 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public enum LicenseInformationOrigin
{
Overwrite,
Expression,
Url,
Unknown,
Expand Down
16 changes: 12 additions & 4 deletions src/NuGetUtility/LicenseValidator/LicenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<IEnumerable<LicenseValidationResult>> Validate(

private bool IsIgnoredPackage(PackageIdentity identity)
{
return _ignoredPackages.Any(ignored => identity.Id.Like(ignored));
return Array.Exists(_ignoredPackages, ignored => identity.Id.Like(ignored));
}

private void AddOrUpdateLicense(
Expand Down Expand Up @@ -123,19 +123,20 @@ private void ValidateLicenseByMetadata(IPackageMetadata info,
switch (info.LicenseMetadata!.Type)
{
case LicenseType.Expression:
case LicenseType.Overwrite:
string licenseId = info.LicenseMetadata!.License;
if (IsLicenseValid(licenseId))
{
AddOrUpdateLicense(result,
info,
LicenseInformationOrigin.Expression,
ToLicenseOrigin(info.LicenseMetadata.Type),
info.LicenseMetadata.License);
}
else
{
AddOrUpdateLicense(result,
info,
LicenseInformationOrigin.Expression,
ToLicenseOrigin(info.LicenseMetadata.Type),
new ValidationError(GetLicenseNotAllowedMessage(info.LicenseMetadata.License), context),
info.LicenseMetadata.License);
}
Expand Down Expand Up @@ -227,6 +228,13 @@ private string GetLicenseNotAllowedMessage(string license)
return $"License {license} not found in list of supported licenses";
}

private record LicenseNameAndVersion(string LicenseName, INuGetVersion Version);
private LicenseInformationOrigin ToLicenseOrigin(LicenseType type) => type switch
{
LicenseType.Overwrite => LicenseInformationOrigin.Overwrite,
LicenseType.Expression => LicenseInformationOrigin.Expression,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, $"This conversion method only supports the {nameof(LicenseType.Overwrite)} and {nameof(LicenseType.Expression)} types for conversion")
};

private sealed record LicenseNameAndVersion(string LicenseName, INuGetVersion Version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private PackageSearchResult TryGetPackageInfoFromCustomInformation(PackageIdenti
return new PackageSearchResult();
}

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

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

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

public PackageIdentity Identity { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace NuGetUtility.Wrapper.NuGetWrapper.Packaging
public enum LicenseType
{
File,
Expression
Expression,
Overwrite
}
}
104 changes: 95 additions & 9 deletions tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,20 @@ private IPackageMetadata SetupPackageWithLicenseInformationOfType(string package
return packageInfo;
}

private IPackageMetadata SetupPackageWithProperLicenseInformation(string packageId,
private IPackageMetadata SetupPackageWithExpressionLicenseInformation(string packageId,
INuGetVersion packageVersion,
string license)
{
return SetupPackageWithLicenseInformationOfType(packageId, packageVersion, license, LicenseType.Expression);
}

private IPackageMetadata SetupPackageWithOverwriteLicenseInformation(string packageId,
INuGetVersion packageVersion,
string license)
{
return SetupPackageWithLicenseInformationOfType(packageId, packageVersion, license, LicenseType.Overwrite);
}

private static IAsyncEnumerable<ReferencedPackageWithContext> CreateInput(IPackageMetadata metadata,
string context)
{
Expand Down Expand Up @@ -119,7 +126,7 @@ public async Task ValidatingLicenses_Should_NotIgnorePackage_If_PackageNameDoesN
_fileDownloader,
_ignoredLicenses.Append(packageId.Substring(1)).ToArray());

IPackageMetadata package = SetupPackageWithProperLicenseInformation(packageId, packageVersion, license);
IPackageMetadata package = SetupPackageWithExpressionLicenseInformation(packageId, packageVersion, license);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Expand Down Expand Up @@ -255,7 +262,7 @@ public async Task ValidatingLicenses_Should_IgnorePackage_If_IgnoreWildcardMatch

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorrectValidatedLicenseList(
public async Task ValidatingLicensesWithExpressionLicenseInformation_Should_GiveCorrectValidatedLicenseList(
string packageId,
INuGetVersion packageVersion,
string license)
Expand All @@ -265,7 +272,7 @@ public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorr
_fileDownloader,
_ignoredLicenses);

IPackageMetadata package = SetupPackageWithProperLicenseInformation(packageId, packageVersion, license);
IPackageMetadata package = SetupPackageWithExpressionLicenseInformation(packageId, packageVersion, license);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Expand All @@ -281,6 +288,34 @@ public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorr
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithOverwriteLicenseInformation_Should_GiveCorrectValidatedLicenseList(
string packageId,
INuGetVersion packageVersion,
string license)
{
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping,
Array.Empty<string>(),
_fileDownloader,
_ignoredLicenses);

IPackageMetadata package = SetupPackageWithOverwriteLicenseInformation(packageId, packageVersion, license);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Assert.That(result,
Is.EquivalentTo(new[]
{
new LicenseValidationResult(packageId,
packageVersion,
_projectUrl.ToString(),
license,
LicenseInformationOrigin.Overwrite)
})
.Using(new LicenseValidationResultValueEqualityComparer()));
}

private IPackageMetadata SetupPackageWithLicenseUrl(string packageId,
INuGetVersion packageVersion,
Uri url)
Expand Down Expand Up @@ -348,7 +383,7 @@ public async Task ValidatingLicensesWithMatchingLicenseUrl_Should_GiveCorrectVal

[Test]
public async Task ValidatingLicensesWithNotSupportedLicenseMetadata_Should_GiveCorrectResult(
[EnumValuesExcept(LicenseType.Expression)] LicenseType licenseType)
[EnumValuesExcept(LicenseType.Expression, LicenseType.Overwrite)] LicenseType licenseType)
{
var fixture = new Fixture();
fixture.Customizations.Add(new NuGetVersionBuilder());
Expand Down Expand Up @@ -416,12 +451,12 @@ public async Task ValidatingLicensesWithoutLicenseInformation_Should_GiveCorrect

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorrectResult_If_NotAllowed(
public async Task ValidatingLicensesWithExpressionLicenseInformation_Should_GiveCorrectResult_If_NotAllowed(
string packageId,
INuGetVersion packageVersion,
string license)
{
IPackageMetadata package = SetupPackageWithProperLicenseInformation(packageId, packageVersion, license);
IPackageMetadata package = SetupPackageWithExpressionLicenseInformation(packageId, packageVersion, license);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Expand All @@ -444,12 +479,40 @@ public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorr

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorrectResult_If_Allowed(
public async Task ValidatingLicensesWithOverwriteLicenseInformation_Should_GiveCorrectResult_If_NotAllowed(
string packageId,
INuGetVersion packageVersion,
string license)
{
IPackageMetadata package = SetupPackageWithOverwriteLicenseInformation(packageId, packageVersion, license);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Assert.That(result,
Is.EquivalentTo(new[]
{
new LicenseValidationResult(packageId,
packageVersion,
_projectUrl.ToString(),
license,
LicenseInformationOrigin.Overwrite,
new List<ValidationError>
{
new ValidationError($"License {license} not found in list of supported licenses",
_context)
})
})
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithExpressionLicenseInformation_Should_GiveCorrectResult_If_Allowed(
string packageId,
INuGetVersion packageVersion)
{
string validLicense = _allowedLicenses.Shuffle(135643).First();
IPackageMetadata package = SetupPackageWithProperLicenseInformation(packageId, packageVersion, validLicense);
IPackageMetadata package = SetupPackageWithExpressionLicenseInformation(packageId, packageVersion, validLicense);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Expand All @@ -465,6 +528,29 @@ public async Task ValidatingLicensesWithProperLicenseInformation_Should_GiveCorr
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithOverwriteLicenseInformation_Should_GiveCorrectResult_If_Allowed(
string packageId,
INuGetVersion packageVersion)
{
string validLicense = _allowedLicenses.Shuffle(135643).First();
IPackageMetadata package = SetupPackageWithOverwriteLicenseInformation(packageId, packageVersion, validLicense);

IEnumerable<LicenseValidationResult> result = await _uut.Validate(LicenseValidatorTest.CreateInput(package, _context));

Assert.That(result,
Is.EquivalentTo(new[]
{
new LicenseValidationResult(packageId,
packageVersion,
_projectUrl.ToString(),
validLicense,
LicenseInformationOrigin.Overwrite)
})
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_GiveCorrectResult_If_NotAllowed(
Expand Down
Loading

0 comments on commit a61630f

Please sign in to comment.