Skip to content

Commit e1d45fb

Browse files
committed
feat(evaluator): Add the matcher PackageRule.hasConcludedLicense()
This can be useful to reduce the severity for declared license mapping issues, in case the package has a concluded license. Note: Add a way to dynamically add a package to the `OrtResult` defined in `TestData.kt` in order avoid enhancing that test data with the details of this particular test. Signed-off-by: Frank Viernau <[email protected]>
1 parent 8fd6e11 commit e1d45fb

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

evaluator/src/main/kotlin/PackageRule.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.ossreviewtoolkit.model.vulnerabilities.Cvss3Rating
3434
import org.ossreviewtoolkit.model.vulnerabilities.Cvss4Rating
3535
import org.ossreviewtoolkit.model.vulnerabilities.Vulnerability
3636
import org.ossreviewtoolkit.model.vulnerabilities.VulnerabilityReference
37+
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
3738
import org.ossreviewtoolkit.utils.spdx.SpdxExpression
3839
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseReferenceExpression
3940

@@ -134,6 +135,19 @@ open class PackageRule(
134135
override fun matches() = resolvedLicenseInfo.licenses.any { it.license.isPresent() }
135136
}
136137

138+
/**
139+
* A [RuleMatcher] that checks if the [package][pkg] has any concluded license.
140+
*/
141+
fun hasConcludedLicense() =
142+
object : RuleMatcher {
143+
override val description = "hasConcludedLicense()"
144+
145+
override fun matches(): Boolean {
146+
val concludedLicense = resolvedLicenseInfo.licenseInfo.concludedLicenseInfo.concludedLicense
147+
return concludedLicense != null && concludedLicense.toString() != SpdxConstants.NOASSERTION
148+
}
149+
}
150+
137151
/**
138152
* A [RuleMatcher] that checks if the [package][pkg] is [excluded][Excludes].
139153
*/

evaluator/src/test/kotlin/PackageRuleTest.kt

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,29 @@ import io.kotest.core.spec.style.WordSpec
2323
import io.kotest.matchers.shouldBe
2424

2525
import org.ossreviewtoolkit.model.CuratedPackage
26+
import org.ossreviewtoolkit.model.Identifier
2627
import org.ossreviewtoolkit.model.LicenseSource
28+
import org.ossreviewtoolkit.model.OrtResult
2729
import org.ossreviewtoolkit.model.Package
2830
import org.ossreviewtoolkit.model.licenses.ResolvedLicense
2931
import org.ossreviewtoolkit.model.licenses.ResolvedOriginalExpression
32+
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
33+
import org.ossreviewtoolkit.utils.spdx.SpdxExpression.Strictness
3034
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseIdExpression
3135
import org.ossreviewtoolkit.utils.spdx.SpdxSingleLicenseExpression
36+
import org.ossreviewtoolkit.utils.spdx.toSpdx
3237

3338
class PackageRuleTest : WordSpec() {
34-
private val ruleSet = ruleSet(ortResult)
39+
private fun createPackageRule(pkg: Package): PackageRule {
40+
val ruleSet = ruleSet(ortResult.addPackage(pkg))
3541

36-
private fun createPackageRule(pkg: Package) =
37-
PackageRule(
42+
return PackageRule(
3843
ruleSet = ruleSet,
3944
name = "test",
4045
pkg = CuratedPackage(pkg),
4146
resolvedLicenseInfo = ruleSet.licenseInfoResolver.resolveLicenseInfo(pkg.id)
4247
)
48+
}
4349

4450
private fun PackageRule.createLicenseRule(license: SpdxSingleLicenseExpression, licenseSource: LicenseSource) =
4551
LicenseRule(
@@ -98,6 +104,32 @@ class PackageRuleTest : WordSpec() {
98104
}
99105
}
100106

107+
"hasConcludedLicense()" should {
108+
"return true if the concluded license is a license expression" {
109+
val rule = createPackageRule(packageWithConcludedLicense("MIT"))
110+
111+
rule.hasConcludedLicense().matches() shouldBe true
112+
}
113+
114+
"return true if the concluded license is ${SpdxConstants.NONE}" {
115+
val rule = createPackageRule(packageWithConcludedLicense(SpdxConstants.NONE))
116+
117+
rule.hasConcludedLicense().matches() shouldBe true
118+
}
119+
120+
"return false if the concluded license is ${SpdxConstants.NOASSERTION}" {
121+
val rule = createPackageRule(packageWithConcludedLicense(SpdxConstants.NOASSERTION))
122+
123+
rule.hasConcludedLicense().matches() shouldBe false
124+
}
125+
126+
"return false if the concluded license is null" {
127+
val rule = createPackageRule(packageWithConcludedLicense(null))
128+
129+
rule.hasConcludedLicense().matches() shouldBe false
130+
}
131+
}
132+
101133
"isExcluded()" should {
102134
"return true if the package is excluded" {
103135
val rule = createPackageRule(packageExcluded)
@@ -243,3 +275,22 @@ class PackageRuleTest : WordSpec() {
243275
}
244276
}
245277
}
278+
279+
private fun packageWithConcludedLicense(license: String?): Package =
280+
Package.EMPTY.copy(
281+
id = Identifier("Maven:some:package:0.0.1"),
282+
concludedLicense = license?.toSpdx(Strictness.ALLOW_ANY)
283+
)
284+
285+
private fun OrtResult.addPackage(pkg: Package): OrtResult {
286+
val analyzerResult = analyzer?.result ?: return this
287+
if (pkg in analyzerResult.packages) return this
288+
289+
return copy(
290+
analyzer = analyzer!!.copy(
291+
result = analyzerResult.copy(
292+
packages = analyzerResult.packages + pkg
293+
)
294+
)
295+
)
296+
}

0 commit comments

Comments
 (0)