Skip to content

Commit

Permalink
refactor(scanner): Move ScanResult.filterByVcsPath() to utils
Browse files Browse the repository at this point in the history
Prepare for re-use in an upcoming change.

Signed-off-by: Frank Viernau <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Nov 9, 2023
1 parent f5c556d commit f93e651
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 44 deletions.
11 changes: 1 addition & 10 deletions model/src/main/kotlin/licenses/DefaultLicenseInfoProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@

package org.ossreviewtoolkit.model.licenses

import java.io.File
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap

import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.config.LicenseFindingCuration
import org.ossreviewtoolkit.model.config.PathExclude
import org.ossreviewtoolkit.model.utils.PackageConfigurationProvider
import org.ossreviewtoolkit.model.utils.filterByVcsPath
import org.ossreviewtoolkit.utils.ort.ProcessedDeclaredLicense

/**
Expand Down Expand Up @@ -125,10 +123,3 @@ class DefaultLicenseInfoProvider(
)
}
}

internal fun ScanResult.filterByVcsPath(path: String): ScanResult {
if (provenance !is RepositoryProvenance) return this

return takeUnless { provenance.vcsInfo.path != path && File(path).startsWith(File(provenance.vcsInfo.path)) }
?: filterByPath(path)
}
9 changes: 9 additions & 0 deletions model/src/main/kotlin/utils/ScanResultUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

package org.ossreviewtoolkit.model.utils

import java.io.File
import java.time.Instant

import org.ossreviewtoolkit.model.CopyrightFinding
import org.ossreviewtoolkit.model.KnownProvenance
import org.ossreviewtoolkit.model.LicenseFinding
import org.ossreviewtoolkit.model.OrtResult
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.ScanSummary
import org.ossreviewtoolkit.model.SnippetFinding
Expand Down Expand Up @@ -118,3 +120,10 @@ private fun Map<String, List<ScanResult>>.mergeSnippetFindings(): Set<SnippetFin

return findings
}

fun ScanResult.filterByVcsPath(path: String): ScanResult {
if (provenance !is RepositoryProvenance) return this

return takeUnless { provenance.vcsInfo.path != path && File(path).startsWith(File(provenance.vcsInfo.path)) }
?: filterByPath(path)
}
34 changes: 0 additions & 34 deletions model/src/test/kotlin/licenses/DefaultLicenseInfoProviderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@
package org.ossreviewtoolkit.model.licenses

import io.kotest.core.spec.style.WordSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf

import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.ScanSummary
import org.ossreviewtoolkit.model.ScannerDetails
import org.ossreviewtoolkit.model.VcsInfo
import org.ossreviewtoolkit.model.utils.PackageConfigurationProvider

class DefaultLicenseInfoProviderTest : WordSpec({
Expand All @@ -43,31 +36,4 @@ class DefaultLicenseInfoProviderTest : WordSpec({
defaultLicenseInfoProvider.get(project.id).declaredLicenseInfo.authors shouldBe projectAuthors
}
}

"filterByVcsPath()" should {
"return identity if the target path is not a subdirectory of the scan result's VCS path" {
val scanResult = createScanResult("a/b")

listOf("", "a").forAll { path ->
scanResult.filterByVcsPath(path) shouldBe scanResult
}
}

"return filtered scan result if the target path is a subdirectory of the scan result's VCS path" {
val scanResult = createScanResult("a/b")

scanResult.filterByPath("a/b/c").provenance
.shouldBeTypeOf<RepositoryProvenance>().vcsInfo.path shouldBe "a/b/c"
}
}
})

private fun createScanResult(vcsPath: String) =
ScanResult(
provenance = RepositoryProvenance(
vcsInfo = VcsInfo.EMPTY.copy(path = vcsPath),
resolvedRevision = "0000000000000000000000000000000000000000"
),
summary = ScanSummary.EMPTY,
scanner = ScannerDetails.EMPTY
)
60 changes: 60 additions & 0 deletions model/src/test/kotlin/utils/ScanResultUtilsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2023 The ORT Project Authors (see <https://github.com/oss-review-toolkit/ort/blob/main/NOTICE>)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package org.ossreviewtoolkit.model.utils

import io.kotest.core.spec.style.WordSpec
import io.kotest.inspectors.forAll
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeTypeOf

import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.ScanSummary
import org.ossreviewtoolkit.model.ScannerDetails
import org.ossreviewtoolkit.model.VcsInfo

class ScanResultUtilsTest : WordSpec({
"filterByVcsPath()" should {
"return identity if the target path is not a subdirectory of the scan result's VCS path" {
val scanResult = createScanResult("a/b")

listOf("", "a").forAll { path ->
scanResult.filterByVcsPath(path) shouldBe scanResult
}
}

"return filtered scan result if the target path is a subdirectory of the scan result's VCS path" {
val scanResult = createScanResult("a/b")

scanResult.filterByPath("a/b/c").provenance
.shouldBeTypeOf<RepositoryProvenance>().vcsInfo.path shouldBe "a/b/c"
}
}
})

private fun createScanResult(vcsPath: String) =
ScanResult(
provenance = RepositoryProvenance(
vcsInfo = VcsInfo.EMPTY.copy(path = vcsPath),
resolvedRevision = "0000000000000000000000000000000000000000"
),
summary = ScanSummary.EMPTY,
scanner = ScannerDetails.EMPTY
)

0 comments on commit f93e651

Please sign in to comment.