Skip to content

Commit

Permalink
DependencyGraph: Use Identifier instead of String as type for IDs
Browse files Browse the repository at this point in the history
The identifiers of type `String` eventually get parsed into `Identifer`
instances anyway. Consistently use the `Identifier` as type for
simplifying the code a bit.

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed May 18, 2021
1 parent 2a4c032 commit 68592c8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class DependencyGraphBuilder<D>(
* Construct the [DependencyGraph] from the dependencies passed to this builder so far.
*/
fun build(): DependencyGraph = DependencyGraph(
dependencyIds.map { it.toCoordinates() },
dependencyIds,
directDependencies,
scopeMapping
)
Expand Down
4 changes: 2 additions & 2 deletions analyzer/src/test/kotlin/AnalyzerResultBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class AnalyzerResultBuilderTest : WordSpec() {
scopeDependencies = null
)

private val dependencies1 = listOf(package1.id.toCoordinates(), package2.id.toCoordinates())
private val dependencies2 = listOf(package3.id.toCoordinates())
private val dependencies1 = listOf(package1.id, package2.id)
private val dependencies2 = listOf(package3.id)

private val depRef1 = DependencyReference(0)
private val depRef2 = DependencyReference(1)
Expand Down
4 changes: 2 additions & 2 deletions model/src/main/kotlin/DependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ data class DependencyGraph(
* A list with the identifiers of the packages that appear in the dependency graph. This list is used to resolve
* the numeric indices contained in the [DependencyReference] objects.
*/
val packages: List<String>,
val packages: List<Identifier>,

/**
* Stores the dependency graph as a list of root nodes for the direct dependencies referenced by scopes. Starting
Expand Down Expand Up @@ -153,7 +153,7 @@ data class DependencyGraph(
}

PackageReference(
id = Identifier(packages[ref.pkg]),
id = packages[ref.pkg],
dependencies = dependencies,
linkage = ref.linkage,
issues = ref.issues
Expand Down
22 changes: 8 additions & 14 deletions model/src/test/kotlin/DependencyGraphTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class DependencyGraphTest : WordSpec({
val scopes = graph.createScopes()

scopes.map { it.name } should containExactly("p1:scope1", "p2:scope2")
scopeDependencies(scopes, "p1:scope1") shouldBe "${pkgId(ids[1])}${pkgId(ids[0])}"
scopeDependencies(scopes, "p2:scope2") shouldBe "${pkgId(ids[1])}${pkgId(ids[2])}"
scopeDependencies(scopes, "p1:scope1") shouldBe "${ids[1]}${ids[0]}"
scopeDependencies(scopes, "p2:scope2") shouldBe "${ids[1]}${ids[2]}"
}

"support restricting the scopes to a specific set" {
Expand Down Expand Up @@ -82,7 +82,7 @@ class DependencyGraphTest : WordSpec({
val scopes = graph.createScopes(setOf(qualifiedScopeName))

scopes.map { it.name } should containExactly(DependencyGraph.unqualifyScope(scopeName))
scopeDependencies(scopes, scopeName) shouldBe "${pkgId(ids[1])}${pkgId(ids[0])}"
scopeDependencies(scopes, scopeName) shouldBe "${ids[1]}${ids[0]}"
}

"construct a tree with multiple levels" {
Expand All @@ -101,8 +101,7 @@ class DependencyGraphTest : WordSpec({
val graph = DependencyGraph(ids, fragments, scopeMap)
val scopes = graph.createScopes()

scopeDependencies(scopes, "s") shouldBe "${pkgId(ids[3])}<${pkgId(ids[2])}<${pkgId(ids[1])}" +
"${pkgId(ids[0])}>>"
scopeDependencies(scopes, "s") shouldBe "${ids[3]}<${ids[2]}<${ids[1]}${ids[0]}>>"
}

"construct scopes from different fragments" {
Expand All @@ -128,9 +127,8 @@ class DependencyGraphTest : WordSpec({
val graph = DependencyGraph(ids, fragments, scopeMap)
val scopes = graph.createScopes()

scopeDependencies(scopes, "s1") shouldBe "${pkgId(ids[2])}<${pkgId(ids[1])}${pkgId(ids[0])}>"
scopeDependencies(scopes, "s2") shouldBe "${pkgId(ids[2])}<${pkgId(ids[1])}<${pkgId(ids[3])}>" +
"${pkgId(ids[0])}>"
scopeDependencies(scopes, "s1") shouldBe "${ids[2]}<${ids[1]}${ids[0]}>"
scopeDependencies(scopes, "s2") shouldBe "${ids[2]}<${ids[1]}<${ids[3]}>${ids[0]}>"
}

"deal with attributes of package references" {
Expand Down Expand Up @@ -204,12 +202,8 @@ private const val MANAGER_NAME = "TestManager"
/**
* Create an identifier string with the given [group], [artifact] ID and [version].
*/
private fun id(group: String, artifact: String, version: String): String = "$MANAGER_NAME:$group:$artifact:$version"

/**
* Create an [Identifier] based on the given [id] string.
*/
private fun pkgId(id: String): Identifier = Identifier(id)
private fun id(group: String, artifact: String, version: String): Identifier =
Identifier("$MANAGER_NAME:$group:$artifact:$version")

/**
* Output the dependency tree of the given scope as a string.
Expand Down
15 changes: 5 additions & 10 deletions model/src/test/kotlin/ProjectTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ private val csvId = Identifier("$MANAGER:org.apache.commons:commons-csv:1.4")
*/
private fun createDependencyGraph(qualified: Boolean = false): DependencyGraph {
val dependencies = listOf(
langId.toDependencyId(),
textId.toDependencyId(),
strutsId.toDependencyId(),
csvId.toDependencyId(),
exampleId.toDependencyId()
langId,
textId,
strutsId,
csvId,
exampleId
)
val langRef = DependencyReference(0)
val textRef = DependencyReference(1, dependencies = sortedSetOf(langRef))
Expand All @@ -81,11 +81,6 @@ private fun createDependencyGraph(qualified: Boolean = false): DependencyGraph {
return DependencyGraph(dependencies, setOf(exampleRef, csvRef), scopeMapping)
}

/**
* Construct the short reference from this [Identifier] used internally by the dependency graph.
*/
private fun Identifier.toDependencyId() = "$MANAGER:$namespace:$name:$version"

class ProjectTest : WordSpec({
"init" should {
"fail if both scopeDependencies and scopeNames are provided" {
Expand Down

0 comments on commit 68592c8

Please sign in to comment.