Skip to content

Commit aadd798

Browse files
committed
feat: Add an SPDX license text provider
Add a license text provider for SPDX licenses. The provider uses the license texts from the resources of the `:utils:spdx-utils` module. Signed-off-by: Martin Nonnenmacher <martin.nonnenmacher@bosch.com>
1 parent 8a95bad commit aadd798

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
plugins {
21+
// Apply precompiled plugins.
22+
id("ort-plugin-conventions")
23+
}
24+
25+
dependencies {
26+
api(projects.plugins.licenseFactProviders.licenseFactProviderApi)
27+
28+
implementation(projects.utils.spdxUtils)
29+
30+
ksp(projects.plugins.licenseFactProviders.licenseFactProviderApi)
31+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.licensefactproviders.spdx
21+
22+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
23+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
24+
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProvider
25+
import org.ossreviewtoolkit.plugins.licensefactproviders.api.LicenseFactProviderFactory
26+
27+
@OrtPlugin(
28+
id = "SPDX",
29+
displayName = "SPDX License Fact Provider",
30+
description = "A provider for SPDX license facts.",
31+
factory = LicenseFactProviderFactory::class
32+
)
33+
class SpdxLicenseFactProvider(
34+
override val descriptor: PluginDescriptor = SpdxLicenseFactProviderFactory.descriptor
35+
) : LicenseFactProvider {
36+
override fun getLicenseText(licenseId: String) = getLicenseTextResource(licenseId)?.readText()
37+
38+
override fun hasLicenseText(licenseId: String) = getLicenseTextResource(licenseId) != null
39+
40+
private fun getLicenseTextResource(licenseId: String) = object {}.javaClass.getResource("/licenses/$licenseId")
41+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2025 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
package org.ossreviewtoolkit.plugins.licensefactproviders.spdx
21+
22+
import io.kotest.core.spec.style.WordSpec
23+
import io.kotest.inspectors.forAll
24+
import io.kotest.matchers.should
25+
import io.kotest.matchers.shouldBe
26+
import io.kotest.matchers.shouldNot
27+
import io.kotest.matchers.string.beEmpty
28+
import io.kotest.matchers.string.contain
29+
import io.kotest.matchers.string.haveLength
30+
31+
import org.ossreviewtoolkit.utils.spdx.SpdxLicense
32+
33+
class SpdxLicenseFactProviderTest : WordSpec({
34+
val provider = SpdxLicenseFactProvider()
35+
36+
"getLicenseText()" should {
37+
"return the license text for Apache-2.0" {
38+
val text = provider.getLicenseText(SpdxLicense.APACHE_2_0.id)
39+
text should contain("Apache License")
40+
text should haveLength(11357)
41+
}
42+
43+
"return the license text for all SPDX licenses" {
44+
enumValues<SpdxLicense>().forAll { license ->
45+
provider.getLicenseText(license.id) shouldNot beEmpty()
46+
}
47+
}
48+
49+
"return null for an unknown license" {
50+
provider.getLicenseText("UnknownLicense") shouldBe null
51+
}
52+
}
53+
54+
"hasLicenseText()" should {
55+
"return true for all SPDX licenses" {
56+
enumValues<SpdxLicense>().forAll {
57+
provider.hasLicenseText(it.id) shouldBe true
58+
}
59+
}
60+
61+
"return false for an unknown license" {
62+
provider.hasLicenseText("UnknownLicense") shouldBe false
63+
}
64+
}
65+
})

0 commit comments

Comments
 (0)