Skip to content

Commit 0fe35b1

Browse files
maennchenfviernau
authored andcommitted
refactor(scanner): Extract NONE-padding of scan result into functions
Prep work for upcoming PR #10502. Signed-off-by: Jonatan Männchen <jonatan@maennchen.ch>
1 parent f52093c commit 0fe35b1

File tree

2 files changed

+90
-25
lines changed

2 files changed

+90
-25
lines changed

scanner/src/main/kotlin/Scanner.kt

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class Scanner(
193193

194194
val vcsPathsForProvenances = getVcsPathsForProvenances(provenances)
195195

196-
val filteredScanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances)
196+
val scanResults = filterScanResultsByVcsPaths(controller.getAllScanResults(), vcsPathsForProvenances)
197197

198198
val files = controller.getAllFileLists().mapTo(mutableSetOf()) { (provenance, fileList) ->
199199
FileList(
@@ -208,42 +208,22 @@ class Scanner(
208208
}
209209
}
210210

211-
val scanResults = if (scannerConfig.includeFilesWithoutFindings) {
212-
filteredScanResults.mapTo(mutableSetOf()) { scanResult ->
213-
val allPaths = controller.getAllFileLists()[scanResult.provenance]?.files?.mapTo(mutableSetOf()) {
214-
it.path
215-
}.orEmpty()
216-
217-
val pathsWithFindings = scanResult.summary.licenseFindings.mapTo(mutableSetOf()) { it.location.path }
218-
val pathsWithoutFindings = allPaths - pathsWithFindings
219-
220-
val findingsThatAreNone = pathsWithoutFindings.mapTo(mutableSetOf()) {
221-
LicenseFinding(SpdxConstants.NONE, TextLocation(it, TextLocation.UNKNOWN_LINE))
222-
}
223-
224-
scanResult.copy(
225-
summary = scanResult.summary.copy(
226-
licenseFindings = scanResult.summary.licenseFindings + findingsThatAreNone
227-
)
228-
)
229-
}
230-
} else {
231-
filteredScanResults
232-
}
233-
234211
val scannerIds = scannerWrappers.mapTo(mutableSetOf()) { it.descriptor.id }
235212
val scanners = packages.associateBy({ it.id }) { scannerIds }
236213

237214
val issues = controller.getIssues()
238215

239-
return ScannerRun.EMPTY.copy(
216+
val scannerRun = ScannerRun.EMPTY.copy(
240217
config = scannerConfig,
241218
provenances = provenances,
242219
scanResults = scanResults,
243220
files = files,
244221
scanners = scanners,
245222
issues = issues
246223
)
224+
225+
return scannerRun.takeUnless { scannerConfig.includeFilesWithoutFindings }
226+
?: scannerRun.padNoneLicenseFindings()
247227
}
248228

249229
private suspend fun resolvePackageProvenances(controller: ScanController) {
@@ -819,3 +799,32 @@ private fun FileList.filterByVcsPaths(paths: Collection<String>): FileList =
819799
}
820800
)
821801
}
802+
803+
internal fun ScannerRun.padNoneLicenseFindings(): ScannerRun {
804+
val fileListByProvenance = files.associateBy { it.provenance }
805+
806+
val paddedScanResults = scanResults.mapTo(mutableSetOf()) { scanResult ->
807+
val allPaths = fileListByProvenance[scanResult.provenance]?.files?.mapTo(mutableSetOf()) {
808+
it.path
809+
}.orEmpty()
810+
811+
scanResult.padNoneLicenseFindings(allPaths)
812+
}
813+
814+
return copy(scanResults = paddedScanResults)
815+
}
816+
817+
internal fun ScanResult.padNoneLicenseFindings(paths: Set<String>): ScanResult {
818+
val pathsWithFindings = summary.licenseFindings.mapTo(mutableSetOf()) { it.location.path }
819+
val pathsWithoutFindings = paths - pathsWithFindings
820+
821+
val findingsThatAreNone = pathsWithoutFindings.mapTo(mutableSetOf()) {
822+
LicenseFinding(SpdxConstants.NONE, TextLocation(it, TextLocation.UNKNOWN_LINE))
823+
}
824+
825+
return copy(
826+
summary = summary.copy(
827+
licenseFindings = summary.licenseFindings + findingsThatAreNone
828+
)
829+
)
830+
}

scanner/src/test/kotlin/ScannerTest.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import io.kotest.assertions.throwables.shouldThrow
2323
import io.kotest.core.spec.style.WordSpec
2424
import io.kotest.matchers.collections.beEmpty
2525
import io.kotest.matchers.collections.containExactly
26+
import io.kotest.matchers.collections.containExactlyInAnyOrder
2627
import io.kotest.matchers.collections.shouldHaveSize
2728
import io.kotest.matchers.maps.beEmpty as beEmptyMap
2829
import io.kotest.matchers.maps.containExactly
@@ -44,6 +45,7 @@ import java.io.IOException
4445

4546
import org.ossreviewtoolkit.downloader.DownloadException
4647
import org.ossreviewtoolkit.model.ArtifactProvenance
48+
import org.ossreviewtoolkit.model.FileList
4749
import org.ossreviewtoolkit.model.Hash
4850
import org.ossreviewtoolkit.model.HashAlgorithm
4951
import org.ossreviewtoolkit.model.Identifier
@@ -52,11 +54,13 @@ import org.ossreviewtoolkit.model.LicenseFinding
5254
import org.ossreviewtoolkit.model.Package
5355
import org.ossreviewtoolkit.model.PackageType
5456
import org.ossreviewtoolkit.model.Provenance
57+
import org.ossreviewtoolkit.model.ProvenanceResolutionResult
5558
import org.ossreviewtoolkit.model.RemoteArtifact
5659
import org.ossreviewtoolkit.model.RepositoryProvenance
5760
import org.ossreviewtoolkit.model.ScanResult
5861
import org.ossreviewtoolkit.model.ScanSummary
5962
import org.ossreviewtoolkit.model.ScannerDetails
63+
import org.ossreviewtoolkit.model.ScannerRun
6064
import org.ossreviewtoolkit.model.SourceCodeOrigin
6165
import org.ossreviewtoolkit.model.TextLocation
6266
import org.ossreviewtoolkit.model.UnknownProvenance
@@ -73,6 +77,7 @@ import org.ossreviewtoolkit.scanner.provenance.NestedProvenanceScanResult
7377
import org.ossreviewtoolkit.scanner.provenance.PackageProvenanceResolver
7478
import org.ossreviewtoolkit.scanner.provenance.ProvenanceDownloader
7579
import org.ossreviewtoolkit.utils.ort.createOrtTempDir
80+
import org.ossreviewtoolkit.utils.spdx.SpdxConstants
7681

7782
@Suppress("LargeClass")
7883
class ScannerTest : WordSpec({
@@ -987,6 +992,57 @@ class ScannerTest : WordSpec({
987992
}
988993
}
989994

995+
"ScannerRun.padNoneLicenseFindings()" should {
996+
"add NONE license findings for files without findings" {
997+
val provenance = RepositoryProvenance(
998+
VcsInfo(type = VcsType.GIT, url = "https://github.com/example.git", revision = "revision"),
999+
"revision"
1000+
)
1001+
1002+
val run = ScannerRun.EMPTY.copy(
1003+
provenances = setOf(
1004+
ProvenanceResolutionResult(
1005+
id = Identifier("maven::example:1.0"),
1006+
packageProvenance = provenance
1007+
)
1008+
),
1009+
scanResults = setOf(
1010+
ScanResult(
1011+
provenance = provenance,
1012+
scanner = ScannerDetails.EMPTY,
1013+
summary = ScanSummary.EMPTY.copy(
1014+
licenseFindings = setOf(
1015+
LicenseFinding("MIT", TextLocation("file1.txt", 1, 1))
1016+
)
1017+
)
1018+
)
1019+
),
1020+
files = setOf(
1021+
FileList(
1022+
provenance = provenance,
1023+
files = setOf(
1024+
FileList.Entry(
1025+
path = "file1.txt",
1026+
sha1 = "1111111111111111111111111111111111111111"
1027+
),
1028+
FileList.Entry(
1029+
path = "file2.txt",
1030+
sha1 = "2222222222222222222222222222222222222222"
1031+
)
1032+
)
1033+
)
1034+
)
1035+
)
1036+
1037+
val paddedRun = run.padNoneLicenseFindings()
1038+
1039+
paddedRun.scanResults.single().summary.licenseFindings should containExactlyInAnyOrder(
1040+
LicenseFinding("MIT", TextLocation("file1.txt", 1, 1)),
1041+
LicenseFinding(SpdxConstants.NONE, TextLocation("file2.txt", TextLocation.UNKNOWN_LINE))
1042+
)
1043+
}
1044+
}
1045+
9901046
// TODO: Add tests for combinations of different types of storage readers and writers.
9911047
// TODO: Add tests for using multiple types of scanner wrappers at once.
9921048
// TODO: Add tests for a complex example with multiple types of scanner wrappers and storages.

0 commit comments

Comments
 (0)