From 9185b096df1c240490140826e724ad1dcc0cf0aa Mon Sep 17 00:00:00 2001 From: Blake Polidore Date: Wed, 16 Oct 2024 13:00:42 -0500 Subject: [PATCH] Allowed Violations --- .../graph/assertion/GraphRulesExtension.kt | 1 + .../assertion/ModuleGraphAssertionsPlugin.kt | 2 +- .../graph/assertion/OnlyAllowedAssert.kt | 7 ++++++- .../graph/assertion/OnlyAllowedAssertTest.kt | 19 +++++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/GraphRulesExtension.kt b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/GraphRulesExtension.kt index 7ff9b1d..bed9d82 100644 --- a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/GraphRulesExtension.kt +++ b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/GraphRulesExtension.kt @@ -4,6 +4,7 @@ open class GraphRulesExtension { var maxHeight: Int = 0 var restricted = emptyArray() // each restriction in format "regexp -X> regexp" e.g.: ":feature-[a-z]* -X> :forbidden-lib" var allowed = emptyArray() // each allowance in format "regexp -> regexp" e.g.: ":feature-[a-z]* -> :forbidden-lib" + var allowedViolations = emptyMap>() // each allowed violation in the format ["feature-[a-z]*" : [forbidden-lib1, forbidden-lib2]] var configurations: Set = Api.API_IMPLEMENTATION_CONFIGURATIONS var assertOnAnyBuild: Boolean = false diff --git a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPlugin.kt b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPlugin.kt index ce68b07..3605fb9 100644 --- a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPlugin.kt +++ b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/ModuleGraphAssertionsPlugin.kt @@ -148,7 +148,7 @@ class ModuleGraphAssertionsPlugin : Plugin { } private fun onlyAllowedAssert(graphRules: GraphRulesExtension) = - OnlyAllowedAssert(graphRules.allowed, aliases) + OnlyAllowedAssert(graphRules.allowed, aliases, graphRules.allowedViolations) } private fun Project.moduleNameForHeightAssert(): String? { diff --git a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssert.kt b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssert.kt index 479d9e9..e3d962a 100644 --- a/plugin/src/main/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssert.kt +++ b/plugin/src/main/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssert.kt @@ -7,14 +7,19 @@ import org.gradle.api.GradleException class OnlyAllowedAssert( private val allowedDependencies: Array, private val aliasMap: Map = emptyMap(), + private val allowedViolations: Map> = emptyMap() ) : GraphAssert { override fun assert(dependencyGraph: DependencyGraph) { val matchers = allowedDependencies.map { Parse.matcher(it) } val disallowedDependencies = dependencyGraph.dependencyPairs() + .asSequence() + .filterNot { pair -> + allowedViolations[pair.first]?.contains(pair.second) == true + } .map { aliasMap.mapAlias(it) } .filterNot { dependency -> matchers.any { it.matches(dependency.pairToAssert()) } } - .map { it.assertDisplayText() } + .map { it.assertDisplayText() }.toList() if (disallowedDependencies.isNotEmpty()) { val allowedRules = allowedDependencies.joinToString(", ") { "'$it'" } diff --git a/plugin/src/test/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssertTest.kt b/plugin/src/test/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssertTest.kt index 678d140..24d7d93 100644 --- a/plugin/src/test/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssertTest.kt +++ b/plugin/src/test/kotlin/com/jraska/module/graph/assertion/OnlyAllowedAssertTest.kt @@ -70,6 +70,25 @@ class OnlyAllowedAssertTest { OnlyAllowedAssert(allowedDependencies, aliases).assert(dependencyGraph) } + @Test + fun passesWhenViolationIsAllowed() { + val dependencies = testGraph().dependencyPairs().toMutableList().apply { add("api" to "lib2") } + val dependencyGraph = DependencyGraph.create(dependencies) + + val allowedDependencies = arrayOf( + "app -> .*", + "feature[a-z]* -> lib[0-9]*", + "feature[a-z]* -> api[0-9]*", + "api[0-9]* -> lib", + ) + + val allowedViolations = mapOf( + "api" to listOf("lib2") + ) + + OnlyAllowedAssert(allowedDependencies, allowedViolations = allowedViolations).assert(dependencyGraph) + } + private fun testGraph(): DependencyGraph { return DependencyGraph.create( "app" to "feature",