Skip to content

Commit

Permalink
Properly support license expressions in url to license mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed Aug 12, 2024
1 parent 165596b commit e751c14
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/NuGetUtility/LicenseValidator/LicenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ private async Task ValidateLicenseByUrl(IPackageMetadata info,

if (_licenseMapping.TryGetValue(info.LicenseUrl, out string? licenseId))
{
if (IsLicenseValid(licenseId))
SpdxExpression? licenseExpression = SpdxExpressionParser.Parse(licenseId, _ => true, _ => true);

if (IsValidLicenseExpression(licenseExpression))
{
AddOrUpdateLicense(result,
info,
Expand Down
179 changes: 179 additions & 0 deletions tests/NuGetUtility.Test/LicenseValidator/LicenseValidatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,185 @@ public async Task ValidatingLicensesWithNotMatchingUrlInformation_Should_GiveCor
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_GiveCorrectResult_WithOrExpression_If_OneAllowed(
string packageId,
INuGetVersion packageVersion,
Uri orLicenseUri,
string firstLicense,
string secondLicense)
{
string licenseExpression = $"{firstLicense} OR {secondLicense}";
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping.Add(orLicenseUri, licenseExpression),
_allowedLicenses.Append(firstLicense),
_fileDownloader,
_ignoredLicenses);
IPackageMetadata package = SetupPackageWithLicenseUrl(packageId, packageVersion, orLicenseUri);

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

Assert.That(result,
Is.EquivalentTo(new[]
{
new LicenseValidationResult(packageId,
packageVersion,
_projectUrl.ToString(),
licenseExpression,
orLicenseUri.AbsoluteUri,
null,
null,
LicenseInformationOrigin.Url)
})
.Using(new LicenseValidationResultValueEqualityComparer()));
}

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_GiveCorrectResult_WithAndExpression_If_AllAllowed(
string packageId,
INuGetVersion packageVersion,
Uri orLicenseUri,
string firstLicense,
string secondLicense)
{
string licenseExpression = $"{firstLicense} AND {secondLicense}";
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping.Add(orLicenseUri, licenseExpression),
_allowedLicenses.Append(firstLicense).Append(secondLicense),
_fileDownloader,
_ignoredLicenses);
IPackageMetadata package = SetupPackageWithLicenseUrl(packageId, packageVersion, orLicenseUri);

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

Assert.That(result,
Is.EquivalentTo(new[]
{
new LicenseValidationResult(packageId,
packageVersion,
_projectUrl.ToString(),
licenseExpression,
orLicenseUri.AbsoluteUri,
null,
null,
LicenseInformationOrigin.Url)
})
.Using(new LicenseValidationResultValueEqualityComparer()));
}
[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_Create_ValidationError_If_None_Is_Not_Supported(
string packageId,
INuGetVersion packageVersion,
Uri orLicenseUri,
string firstLicense,
string secondLicense)
{
string licenseExpression = $"{firstLicense} OR {secondLicense}";
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping.Add(orLicenseUri, licenseExpression),
_allowedLicenses,
_fileDownloader,
_ignoredLicenses);
IPackageMetadata package = SetupPackageWithLicenseUrl(packageId, packageVersion, orLicenseUri);

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

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

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_Create_ValidationError_If_Second_Is_Not_Supported(
string packageId,
INuGetVersion packageVersion,
Uri orLicenseUri,
string firstLicense,
string secondLicense)
{
string licenseExpression = $"{firstLicense} AND {secondLicense}";
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping.Add(orLicenseUri, licenseExpression),
_allowedLicenses.Append(firstLicense),
_fileDownloader,
_ignoredLicenses);
IPackageMetadata package = SetupPackageWithLicenseUrl(packageId, packageVersion, orLicenseUri);

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

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

[Test]
[ExtendedAutoData(typeof(NuGetVersionBuilder))]
public async Task ValidatingLicensesWithMatchingUrlInformation_Should_Create_ValidationError_If_First_Is_Not_Supported(
string packageId,
INuGetVersion packageVersion,
Uri orLicenseUri,
string firstLicense,
string secondLicense)
{
string licenseExpression = $"{firstLicense} AND {secondLicense}";
_uut = new NuGetUtility.LicenseValidator.LicenseValidator(_licenseMapping.Add(orLicenseUri, licenseExpression),
_allowedLicenses.Append(secondLicense),
_fileDownloader,
_ignoredLicenses);
IPackageMetadata package = SetupPackageWithLicenseUrl(packageId, packageVersion, orLicenseUri);

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

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

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

0 comments on commit e751c14

Please sign in to comment.