Skip to content

Commit

Permalink
Fix failing graph height check on root project (#62)
Browse files Browse the repository at this point in the history
* Fix failing graph height check on root project

* Fix typo
  • Loading branch information
jraska authored Aug 3, 2020
1 parent 72de66b commit de7f247
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ task clean(type: Delete) {
}

moduleGraphAssert {
maxHeight = 3
configurations = ['api', 'implementation', 'testImplementation']
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}

val task = tasks.register(Tasks.ASSERT_MAX_HEIGHT, AssertGraphTask::class.java) {
it.assertion = ModuleTreeHeightAssert(moduleDisplayName(), graphRules.maxHeight)
it.assertion = ModuleTreeHeightAssert(moduleNameForHeightAssert(), graphRules.maxHeight)
it.configurationsToLook = graphRules.configurations
it.group = VERIFICATION_GROUP
}
Expand Down Expand Up @@ -115,6 +115,14 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}
}

private fun Project.moduleNameForHeightAssert(): String? {
if (this == rootProject) {
return null
} else {
return moduleDisplayName()
}
}

fun Project.moduleDisplayName(): String {
return displayName.replace("project", "")
.replace("'", "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ import com.jraska.module.graph.DependencyGraph
import org.gradle.api.GradleException

class ModuleTreeHeightAssert(
private val moduleName: String,
private val moduleName: String?,
private val maxHeight: Int
) : GraphAssert {
override fun assert(dependencyGraph: DependencyGraph) {
if (moduleName == null) {
assertWholeGraphHeight(dependencyGraph)
} else {
assertModuleHeight(dependencyGraph, moduleName)
}
}

private fun assertModuleHeight(dependencyGraph: DependencyGraph, moduleName: String) {
val height = dependencyGraph.heightOf(moduleName)
if (height > maxHeight) {
val longestPath = dependencyGraph.longestPath(moduleName)
throw GradleException("Module $moduleName is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}")
}
}

private fun assertWholeGraphHeight(dependencyGraph: DependencyGraph) {
val height = dependencyGraph.height()
if (height > maxHeight) {
val longestPath = dependencyGraph.longestPath()
throw GradleException("Module Graph is allowed to have maximum height of $maxHeight, but has $height, problematic dependencies: ${longestPath.pathString()}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@ class ModuleTreeHeightAssertTest {
}

@Test(expected = GradleException::class)
fun failsOnUnknownLayer() {
fun failsOnTooLargeHeight() {
val dependencyGraph = testGraph()

ModuleTreeHeightAssert("app", 2).assert(dependencyGraph)
}

@Test
fun passesOnCorrectRoot() {
val dependencyGraph = testGraph()

ModuleTreeHeightAssert(null, 3).assert(dependencyGraph)
}

@Test(expected = GradleException::class)
fun failsOnTooHighGraphHeight() {
val dependencyGraph = testGraph()

ModuleTreeHeightAssert(null, 2).assert(dependencyGraph)
}

@Test(expected = NoSuchElementException::class)
fun failsOnUnknownModule() {
val dependencyGraph = testGraph()
Expand Down

0 comments on commit de7f247

Please sign in to comment.