Skip to content

Commit bba75ec

Browse files
fviernausschuberth
authored andcommitted
feat(model): Handle path excludes when resolving license files
Fixes #11869. Signed-off-by: Frank Viernau <frank.viernau@gmail.com>
1 parent a294aaf commit bba75ec

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

model/src/main/kotlin/licenses/LicenseInfoResolver.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ class LicenseInfoResolver(
7777
/**
7878
* Get the [ResolvedLicenseFileInfo] for the project or package identified by [id]. Requires an [archiver] to be
7979
* configured, otherwise always returns empty results. To determine the applicable license files the files in
80-
* the archive are matched against the configured license and patent file patterns (see [LicenseFilePatterns]) as
81-
* follows:
80+
* the archive are matched against path excludes and the configured license and patent file patterns (see
81+
* [LicenseFilePatterns]) as follows:
8282
*
8383
* 1. For a repository provenance, all filenames in the VCS path are matched. If there are matches, then these are
8484
* used as the result, otherwise that search is repeated recursively in the parent directory.
@@ -260,18 +260,19 @@ class LicenseInfoResolver(
260260
}
261261

262262
private fun createLicenseFileInfo(id: Identifier): ResolvedLicenseFileInfo {
263-
val provenances = provider.get(id).detectedLicenseInfo.findings.mapNotNull {
264-
it.provenance as? KnownProvenance
265-
}
266-
267-
val licenseFiles = provenances.flatMap { provenance ->
268-
resolveLicenseFiles(id, provenance)
263+
val licenseFiles = provider.get(id).detectedLicenseInfo.findings.flatMap { findings ->
264+
val provenance = findings.provenance as? KnownProvenance ?: return@flatMap emptySet()
265+
resolveLicenseFiles(id, provenance, findings.pathExcludes)
269266
}
270267

271268
return ResolvedLicenseFileInfo(id, licenseFiles)
272269
}
273270

274-
private fun resolveLicenseFiles(id: Identifier, provenance: KnownProvenance): List<ResolvedLicenseFile> {
271+
private fun resolveLicenseFiles(
272+
id: Identifier,
273+
provenance: KnownProvenance,
274+
pathExcludes: Collection<PathExclude>
275+
): List<ResolvedLicenseFile> {
275276
if (archiver == null) return emptyList()
276277
val archiveDir = createOrtTempDir("archive")
277278

@@ -286,6 +287,8 @@ class LicenseInfoResolver(
286287

287288
val relativeFilePaths = archiveDir.walk().filter { it.isFile }.mapTo(mutableSetOf()) {
288289
it.relativeTo(archiveDir).invariantSeparatorsPath
290+
}.filterNot { path ->
291+
pathExcludes.any { it.matches(path) }
289292
}
290293

291294
val rootLicenseFiles = when (provenance) {

model/src/test/kotlin/licenses/LicenseInfoResolverTest.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private val ARTIFACT_PROVENANCE = ArtifactProvenance(
9292
)
9393
)
9494

95+
@Suppress("LargeClass")
9596
class LicenseInfoResolverTest : WordSpec({
9697
"resolveLicenseInfo()" should {
9798
"resolve declared licenses" {
@@ -680,6 +681,15 @@ class LicenseInfoResolverTest : WordSpec({
680681

681682
result.files.map { it.path }.shouldContainExactlyInAnyOrder("path1/LICENSE")
682683
}
684+
685+
"return only non-excluded license files" {
686+
val archiver = createArchiver(REPOSITORY_PROVENANCE, "LICENSE", "LICENSE2")
687+
val licenseInfo = createLicenseInfo(ID, REPOSITORY_PROVENANCE, pathExcludes = listOf("LICENSE"))
688+
689+
val result = createResolver(data = listOf(licenseInfo), archiver = archiver).resolveLicenseFiles(ID)
690+
691+
result.files.map { it.path }.shouldContainExactlyInAnyOrder("LICENSE2")
692+
}
683693
}
684694

685695
"resolveLicenseFiles() for artifact provenance" should {
@@ -706,6 +716,15 @@ class LicenseInfoResolverTest : WordSpec({
706716
"path2/LICENSE"
707717
)
708718
}
719+
720+
"return only non-excluded license files" {
721+
val archiver = createArchiver(ARTIFACT_PROVENANCE, "LICENSE", "LICENSE2")
722+
val licenseInfo = createLicenseInfo(ID, ARTIFACT_PROVENANCE, pathExcludes = listOf("LICENSE"))
723+
724+
val result = createResolver(data = listOf(licenseInfo), archiver = archiver).resolveLicenseFiles(ID)
725+
726+
result.files.map { it.path }.shouldContainExactlyInAnyOrder("LICENSE2")
727+
}
709728
}
710729

711730
"resolveLicenseFiles()" should {
@@ -816,7 +835,7 @@ private fun createLicenseInfo(
816835
id: Identifier,
817836
provenance: KnownProvenance,
818837
vararg pathAndLicense: Pair<String, String>,
819-
pathExcludes: Collection<PathExclude> = emptyList()
838+
pathExcludes: Collection<String> = emptyList()
820839
) = createLicenseInfo(
821840
id = id,
822841
detectedLicenses = listOf(
@@ -826,7 +845,7 @@ private fun createLicenseInfo(
826845
license to listOf(TextLocation(path, 1))
827846
}.toFindingsSet(),
828847
copyrights = emptySet(),
829-
pathExcludes = pathExcludes.toList(),
848+
pathExcludes = pathExcludes.map { PathExclude(pattern = it, reason = PathExcludeReason.OTHER) },
830849
licenseFindingCurations = emptyList(),
831850
relativeFindingsPath = ""
832851
)

0 commit comments

Comments
 (0)