diff --git a/model/src/main/kotlin/OrtResult.kt b/model/src/main/kotlin/OrtResult.kt index a0674ebaf2f65..d74dc8128d6dd 100644 --- a/model/src/main/kotlin/OrtResult.kt +++ b/model/src/main/kotlin/OrtResult.kt @@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.fasterxml.jackson.annotation.JsonInclude +import java.io.File import java.util.SortedSet import kotlin.time.measureTimedValue @@ -93,6 +94,13 @@ data class OrtResult( evaluator = null, labels = emptyMap() ) + + /** + * Load an [OrtResult] from the given [file]. This function deserializes the result from the specified input + * file and ensures that it has been properly initialized. It therefore should be always used rather than + * doing the deserialization manually. + */ + fun load(file: File): OrtResult = file.readValue().withResolvedScopes() } private data class ProjectEntry(val project: Project, val isExcluded: Boolean) diff --git a/model/src/test/kotlin/OrtResultTest.kt b/model/src/test/kotlin/OrtResultTest.kt index 87f67b1813cb8..f6bb71e59cbf1 100644 --- a/model/src/test/kotlin/OrtResultTest.kt +++ b/model/src/test/kotlin/OrtResultTest.kt @@ -159,4 +159,22 @@ class OrtResultTest : WordSpec({ } } } + + "load" should { + "resolve project scopes" { + val resultFile = File("src/test/assets/analyzer-result-with-dependency-graph.yml") + + val result = OrtResult.load(resultFile) + + result.analyzer?.result shouldNotBeNull { + dependencyGraphs.keys should beEmpty() + } + + val project = result.getProject(Identifier("Maven:com.vdurmont:semver4j:3.1.0")) + + project.shouldNotBeNull { + scopes shouldNot beEmpty() + } + } + } })