Skip to content

Commit

Permalink
OrtResult: Add a load() function to the companion object
Browse files Browse the repository at this point in the history
After introducing shared dependency graphs, it must be ensured that
the 'scopes' property of projects in the analyzer result has been
correctly initialized. OrtResult does this in its getProjects()
function, but if this function is called repeatedly, this is
inefficient.

Therefore provide a new function to load ORT results files that
returns an OrtResult with project scopes already resolved. On such an
object the getProjects() function is no longer problematic because the
dependency graph has already been converted to the scopes structure.

Signed-off-by: Oliver Heger <oliver.heger@bosch.io>
  • Loading branch information
oheger-bosch committed Apr 28, 2021
1 parent 756773a commit 775aaf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions model/src/main/kotlin/OrtResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<OrtResult>().withResolvedScopes()
}

private data class ProjectEntry(val project: Project, val isExcluded: Boolean)
Expand Down
18 changes: 18 additions & 0 deletions model/src/test/kotlin/OrtResultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}
})

0 comments on commit 775aaf5

Please sign in to comment.