Skip to content

Commit fb1bec9

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 e73e960 commit fb1bec9

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-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.licenseTextProviders.licenseTextProviderApi)
27+
28+
implementation(projects.utils.spdxUtils)
29+
30+
ksp(projects.plugins.licenseTextProviders.licenseTextProviderApi)
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.licensetextproviders.spdx
21+
22+
import org.ossreviewtoolkit.plugins.api.OrtPlugin
23+
import org.ossreviewtoolkit.plugins.api.PluginDescriptor
24+
import org.ossreviewtoolkit.plugins.licensetextproviders.api.LicenseTextProvider
25+
import org.ossreviewtoolkit.plugins.licensetextproviders.api.LicenseTextProviderFactory
26+
27+
@OrtPlugin(
28+
id = "SPDX",
29+
displayName = "SPDX License Text Provider",
30+
description = "A provider for SPDX license texts.",
31+
factory = LicenseTextProviderFactory::class
32+
)
33+
class SpdxLicenseTextProvider(
34+
override val descriptor: PluginDescriptor = SpdxLicenseTextProviderFactory.descriptor
35+
) : LicenseTextProvider {
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: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.licensetextproviders.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+
import org.ossreviewtoolkit.utils.spdx.SpdxLicense
31+
32+
class SpdxLicenseTextProviderTest : WordSpec({
33+
val provider = SpdxLicenseTextProvider()
34+
35+
"getLicenseText()" should {
36+
"return the license text for Apache-2.0" {
37+
val text = provider.getLicenseText(SpdxLicense.APACHE_2_0.id)
38+
text should contain("Apache License")
39+
text should haveLength(11357)
40+
}
41+
42+
"return the license text for all SPDX licenses" {
43+
enumValues<SpdxLicense>().forAll { license ->
44+
provider.getLicenseText(license.id) shouldNot beEmpty()
45+
}
46+
}
47+
48+
"return null for an unknown license" {
49+
provider.getLicenseText("UnknownLicense") shouldBe null
50+
}
51+
}
52+
53+
"hasLicenseText()" should {
54+
"return true for all SPDX licenses" {
55+
enumValues<SpdxLicense>().forAll {
56+
provider.hasLicenseText(it.id) shouldBe true
57+
}
58+
}
59+
60+
"return false for an unknown license" {
61+
provider.hasLicenseText("UnknownLicense") shouldBe false
62+
}
63+
}
64+
})

0 commit comments

Comments
 (0)