Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed Violations #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open class GraphRulesExtension {
var maxHeight: Int = 0
var restricted = emptyArray<String>() // each restriction in format "regexp -X> regexp" e.g.: ":feature-[a-z]* -X> :forbidden-lib"
var allowed = emptyArray<String>() // each allowance in format "regexp -> regexp" e.g.: ":feature-[a-z]* -> :forbidden-lib"
var allowedViolations = emptyMap<String, List<String>>() // each allowed violation in the format ["feature-[a-z]*" : [forbidden-lib1, forbidden-lib2]]
var configurations: Set<String> = Api.API_IMPLEMENTATION_CONFIGURATIONS
var assertOnAnyBuild: Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}

private fun onlyAllowedAssert(graphRules: GraphRulesExtension) =
OnlyAllowedAssert(graphRules.allowed, aliases)
OnlyAllowedAssert(graphRules.allowed, aliases, graphRules.allowedViolations)
}

private fun Project.moduleNameForHeightAssert(): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import org.gradle.api.GradleException
class OnlyAllowedAssert(
private val allowedDependencies: Array<String>,
private val aliasMap: Map<String, String> = emptyMap(),
private val allowedViolations: Map<String, List<String>> = 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'" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down