Skip to content

Commit 4daf896

Browse files
fviernautsteenbe
authored andcommitted
refactor(evaluator)!: Allow multiple license sources in a LicenseRule
Prepare for an upcoming change. Signed-off-by: Frank Viernau <frank.viernau@gmail.com>
1 parent c2fdbec commit 4daf896

File tree

5 files changed

+47
-34
lines changed

5 files changed

+47
-34
lines changed

evaluator/src/main/kotlin/OrtResultRule.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ open class OrtResultRule(
4343
severity = severity,
4444
pkgId = null,
4545
license = null,
46-
licenseSource = null,
46+
licenseSources = emptySet(),
4747
message = message,
4848
howToFix = howToFix
4949
)
@@ -52,7 +52,7 @@ open class OrtResultRule(
5252
hint(
5353
pkgId = null,
5454
license = null,
55-
licenseSource = null,
55+
licenseSources = emptySet(),
5656
message = message,
5757
howToFix = howToFix
5858
)
@@ -61,7 +61,7 @@ open class OrtResultRule(
6161
warning(
6262
pkgId = null,
6363
license = null,
64-
licenseSource = null,
64+
licenseSources = emptySet(),
6565
message = message,
6666
howToFix = howToFix
6767
)
@@ -70,7 +70,7 @@ open class OrtResultRule(
7070
error(
7171
pkgId = null,
7272
license = null,
73-
licenseSource = null,
73+
licenseSources = emptySet(),
7474
message = message,
7575
howToFix = howToFix
7676
)

evaluator/src/main/kotlin/PackageRule.kt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,28 @@ open class PackageRule(
207207
.applyChoices(ruleSet.ortResult.getPackageLicenseChoices(pkg.metadata.id), licenseView)
208208
.applyChoices(ruleSet.ortResult.getRepositoryLicenseChoices(), licenseView).forEach { resolvedLicense ->
209209
resolvedLicense.sources.forEach { licenseSource ->
210-
licenseRules += LicenseRule(name, resolvedLicense, licenseSource).apply(block)
210+
licenseRules += LicenseRule(name, resolvedLicense, setOf(licenseSource)).apply(block)
211211
}
212212
}
213213
}
214214

215215
fun issue(severity: Severity, message: String, howToFix: String) =
216-
issue(severity, pkg.metadata.id, null, null, message, howToFix)
216+
issue(severity, pkg.metadata.id, null, emptySet(), message, howToFix)
217217

218218
/**
219219
* Add a [hint][Severity.HINT] to the list of [violations].
220220
*/
221-
fun hint(message: String, howToFix: String) = hint(pkg.metadata.id, null, null, message, howToFix)
221+
fun hint(message: String, howToFix: String) = hint(pkg.metadata.id, null, emptySet(), message, howToFix)
222222

223223
/**
224224
* Add a [warning][Severity.WARNING] to the list of [violations].
225225
*/
226-
fun warning(message: String, howToFix: String) = warning(pkg.metadata.id, null, null, message, howToFix)
226+
fun warning(message: String, howToFix: String) = warning(pkg.metadata.id, null, emptySet(), message, howToFix)
227227

228228
/**
229229
* Add an [error][Severity.ERROR] to the list of [violations].
230230
*/
231-
fun error(message: String, howToFix: String) = error(pkg.metadata.id, null, null, message, howToFix)
231+
fun error(message: String, howToFix: String) = error(pkg.metadata.id, null, emptySet(), message, howToFix)
232232

233233
/**
234234
* A [Rule] to check a single license of the [package][pkg].
@@ -242,10 +242,21 @@ open class PackageRule(
242242
val resolvedLicense: ResolvedLicense,
243243

244244
/**
245-
* The source of the license.
245+
* The license sources to evaluate the license for. Must not be empty and contained in the [resolvedLicense].
246246
*/
247-
val licenseSource: LicenseSource
247+
val licenseSources: Set<LicenseSource>
248248
) : Rule(ruleSet, name) {
249+
init {
250+
require(licenseSources.isNotEmpty()) {
251+
"The given license sources must not be empty."
252+
}
253+
254+
val invalidLicenseSources = (licenseSources - resolvedLicense.sources)
255+
require(invalidLicenseSources.isEmpty()) {
256+
"The license sources $invalidLicenseSources are not part of the resolved license."
257+
}
258+
}
259+
249260
/**
250261
* A shortcut for the [license][ResolvedLicense.license] in [resolvedLicense].
251262
*/
@@ -257,11 +268,11 @@ open class PackageRule(
257268
*/
258269
fun pkg() = pkg
259270

260-
override val description = "\tEvaluating license rule '$name' for $licenseSource license " +
271+
override val description = "\tEvaluating license rule '$name' for $licenseSources license " +
261272
"'${resolvedLicense.license}'."
262273

263274
override fun issueSource() =
264-
"$name - ${pkg.metadata.id.toCoordinates()} - ${resolvedLicense.license} ($licenseSource)"
275+
"$name - ${pkg.metadata.id.toCoordinates()} - ${resolvedLicense.license} ($licenseSources})"
265276

266277
/**
267278
* A [RuleMatcher] that checks if a [detected][LicenseSource.DETECTED] license is
@@ -271,7 +282,8 @@ open class PackageRule(
271282
object : RuleMatcher {
272283
override val description = "isDetectedExcluded($license)"
273284

274-
override fun matches() = licenseSource == LicenseSource.DETECTED && resolvedLicense.isDetectedExcluded
285+
override fun matches() =
286+
licenseSources.singleOrNull() == LicenseSource.DETECTED && resolvedLicense.isDetectedExcluded
275287
}
276288

277289
/**
@@ -290,22 +302,23 @@ open class PackageRule(
290302
}
291303

292304
fun issue(severity: Severity, message: String, howToFix: String) =
293-
issue(severity, pkg.metadata.id, license, licenseSource, message, howToFix)
305+
issue(severity, pkg.metadata.id, license, licenseSources, message, howToFix)
294306

295307
/**
296308
* Add a [hint][Severity.HINT] to the list of [violations].
297309
*/
298-
fun hint(message: String, howToFix: String) = hint(pkg.metadata.id, license, licenseSource, message, howToFix)
310+
fun hint(message: String, howToFix: String) = hint(pkg.metadata.id, license, licenseSources, message, howToFix)
299311

300312
/**
301313
* Add a [warning][Severity.WARNING] to the list of [violations].
302314
*/
303315
fun warning(message: String, howToFix: String) =
304-
warning(pkg.metadata.id, license, licenseSource, message, howToFix)
316+
warning(pkg.metadata.id, license, licenseSources, message, howToFix)
305317

306318
/**
307319
* Add an [error][Severity.ERROR] to the list of [violations].
308320
*/
309-
fun error(message: String, howToFix: String) = error(pkg.metadata.id, license, licenseSource, message, howToFix)
321+
fun error(message: String, howToFix: String) =
322+
error(pkg.metadata.id, license, licenseSources, message, howToFix)
310323
}
311324
}

evaluator/src/main/kotlin/Rule.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ abstract class Rule(
130130
severity: Severity,
131131
pkgId: Identifier?,
132132
license: SpdxSingleLicenseExpression?,
133-
licenseSource: LicenseSource?,
133+
licenseSources: Set<LicenseSource>,
134134
message: String,
135135
howToFix: String
136136
) {
@@ -139,7 +139,7 @@ abstract class Rule(
139139
rule = name,
140140
pkg = pkgId,
141141
license = license,
142-
licenseSources = setOfNotNull(licenseSource),
142+
licenseSources = licenseSources,
143143
message = message,
144144
howToFix = howToFix
145145
)
@@ -151,32 +151,32 @@ abstract class Rule(
151151
fun hint(
152152
pkgId: Identifier?,
153153
license: SpdxSingleLicenseExpression?,
154-
licenseSource: LicenseSource?,
154+
licenseSources: Set<LicenseSource>,
155155
message: String,
156156
howToFix: String
157-
) = issue(Severity.HINT, pkgId, license, licenseSource, message, howToFix)
157+
) = issue(Severity.HINT, pkgId, license, licenseSources, message, howToFix)
158158

159159
/**
160160
* Add a [warning][Severity.WARNING] to the list of [violations].
161161
*/
162162
fun warning(
163163
pkgId: Identifier?,
164164
license: SpdxSingleLicenseExpression?,
165-
licenseSource: LicenseSource?,
165+
licenseSources: Set<LicenseSource>,
166166
message: String,
167167
howToFix: String
168-
) = issue(Severity.WARNING, pkgId, license, licenseSource, message, howToFix)
168+
) = issue(Severity.WARNING, pkgId, license, licenseSources, message, howToFix)
169169

170170
/**
171171
* Add an [error][Severity.ERROR] to the list of [violations].
172172
*/
173173
fun error(
174174
pkgId: Identifier?,
175175
license: SpdxSingleLicenseExpression?,
176-
licenseSource: LicenseSource?,
176+
licenseSources: Set<LicenseSource>,
177177
message: String,
178178
howToFix: String
179-
) = issue(Severity.ERROR, pkgId, license, licenseSource, message, howToFix)
179+
) = issue(Severity.ERROR, pkgId, license, licenseSources, message, howToFix)
180180

181181
/**
182182
* A DSL helper class, providing convenience functions for adding [RuleMatcher]s to this rule.

evaluator/src/test/kotlin/PackageRuleTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class PackageRuleTest : WordSpec() {
5656
originalExpressions = setOf(ResolvedOriginalExpression(license, licenseSource)),
5757
locations = emptySet()
5858
),
59-
licenseSource = licenseSource
59+
licenseSources = setOf(licenseSource)
6060
)
6161

6262
init {

evaluator/src/test/kotlin/RuleTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RuleTest : WordSpec() {
3434
private val ruleSet = ruleSet(ortResult)
3535
private val id = Identifier("type:namespace:name:version")
3636
private val license = SpdxLicenseIdExpression("license")
37-
private val licenseSource = LicenseSource.DECLARED
37+
private val licenseSources = setOf(LicenseSource.DECLARED)
3838
private val message = "violation message"
3939
private val howToFix = "how to fix"
4040

@@ -49,14 +49,14 @@ class RuleTest : WordSpec() {
4949
"add an issue with the correct severity" {
5050
val rule = createRule()
5151

52-
rule.hint(id, license, licenseSource, message, howToFix)
52+
rule.hint(id, license, licenseSources, message, howToFix)
5353

5454
rule.violations should haveSize(1)
5555
rule.violations.first().let { violation ->
5656
violation.rule shouldBe rule.name
5757
violation.pkg shouldBe id
5858
violation.license shouldBe license
59-
violation.licenseSources should containExactlyInAnyOrder(licenseSource)
59+
violation.licenseSources should containExactlyInAnyOrder(licenseSources)
6060
violation.severity shouldBe Severity.HINT
6161
violation.message shouldBe message
6262
violation.howToFix shouldBe howToFix
@@ -68,14 +68,14 @@ class RuleTest : WordSpec() {
6868
"add an issue with the correct severity" {
6969
val rule = createRule()
7070

71-
rule.warning(id, license, licenseSource, message, howToFix)
71+
rule.warning(id, license, licenseSources, message, howToFix)
7272

7373
rule.violations should haveSize(1)
7474
rule.violations.first().let { violation ->
7575
violation.rule shouldBe rule.name
7676
violation.pkg shouldBe id
7777
violation.license shouldBe license
78-
violation.licenseSources should containExactlyInAnyOrder(licenseSource)
78+
violation.licenseSources should containExactlyInAnyOrder(licenseSources)
7979
violation.severity shouldBe Severity.WARNING
8080
violation.message shouldBe message
8181
violation.howToFix shouldBe howToFix
@@ -87,14 +87,14 @@ class RuleTest : WordSpec() {
8787
"add an issue with the correct severity" {
8888
val rule = createRule()
8989

90-
rule.error(id, license, licenseSource, message, howToFix)
90+
rule.error(id, license, licenseSources, message, howToFix)
9191

9292
rule.violations should haveSize(1)
9393
rule.violations.first().let { violation ->
9494
violation.rule shouldBe rule.name
9595
violation.pkg shouldBe id
9696
violation.license shouldBe license
97-
violation.licenseSources should containExactlyInAnyOrder(licenseSource)
97+
violation.licenseSources should containExactlyInAnyOrder(licenseSources)
9898
violation.severity shouldBe Severity.ERROR
9999
violation.message shouldBe message
100100
violation.howToFix shouldBe howToFix

0 commit comments

Comments
 (0)