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

Add option to assert on each build #184

Merged
merged 3 commits into from
Sep 1, 2022
Merged
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 @@ -5,4 +5,11 @@ open class GraphRulesExtension {
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 configurations: Set<String> = Api.API_IMPLEMENTATON_CONFIGURATIONS
var assertOnAnyBuild: Boolean = false

internal fun shouldAssertHeight() = maxHeight > 0

internal fun shouldAssertRestricted() = restricted.isNotEmpty()

internal fun shouldAssertAllowed() = allowed.isNotEmpty()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jraska.module.graph.assertion

import com.jraska.module.graph.DependencyGraph
import com.jraska.module.graph.assertion.Api.Tasks
import com.jraska.module.graph.assertion.tasks.AssertGraphTask
import com.jraska.module.graph.assertion.tasks.GenerateModulesGraphStatisticsTask
Expand Down Expand Up @@ -30,6 +31,12 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {

project.afterEvaluate {
addModulesAssertions(project, graphRules)

if (graphRules.assertOnAnyBuild) {
project.gradle.projectsEvaluated {
project.runAssertionsDirectly(graphRules)
}
}
}
}

Expand Down Expand Up @@ -60,6 +67,22 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}
}

private fun Project.runAssertionsDirectly(graphRules: GraphRulesExtension) {
val dependencyGraph = DependencyGraph.create(moduleGraph)

if (graphRules.shouldAssertHeight()) {
moduleTreeHeightAssert(graphRules).assert(dependencyGraph)
}

if (graphRules.shouldAssertRestricted()) {
restrictedDependenciesAssert(graphRules).assert(dependencyGraph)
}

if (graphRules.shouldAssertAllowed()) {
onlyAllowedAssert(graphRules).assert(dependencyGraph)
}
}

private fun Project.addModuleGraphGeneration(graphRules: GraphRulesExtension) {
tasks.register(Tasks.GENERATE_GRAPHVIZ, GenerateModulesGraphTask::class.java) {
it.configurationsToLook = graphRules.configurations
Expand All @@ -68,49 +91,61 @@ class ModuleGraphAssertionsPlugin : Plugin<Project> {
}

private fun Project.addModuleGraphStatisticsGeneration(graphRules: GraphRulesExtension) {
tasks.register(Tasks.GENERATE_GRAPH_STATISTICS, GenerateModulesGraphStatisticsTask::class.java) {
tasks.register(
Tasks.GENERATE_GRAPH_STATISTICS,
GenerateModulesGraphStatisticsTask::class.java
) {
it.configurationsToLook = graphRules.configurations
}
}

private fun Project.addMaxHeightTask(graphRules: GraphRulesExtension): TaskProvider<AssertGraphTask>? {
if (graphRules.maxHeight <= 0) {
if (graphRules.shouldAssertHeight()) {
return tasks.register(Tasks.ASSERT_MAX_HEIGHT, AssertGraphTask::class.java) {
it.assertion = moduleTreeHeightAssert(graphRules)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
} else {
return null
}

return tasks.register(Tasks.ASSERT_MAX_HEIGHT, AssertGraphTask::class.java) {
it.assertion = ModuleTreeHeightAssert(moduleNameForHeightAssert(), graphRules.maxHeight)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
}

private fun Project.moduleTreeHeightAssert(graphRules: GraphRulesExtension) =
ModuleTreeHeightAssert(moduleNameForHeightAssert(), graphRules.maxHeight)

private fun Project.addModuleRestrictionsTask(graphRules: GraphRulesExtension): TaskProvider<AssertGraphTask>? {
if (graphRules.restricted.isEmpty()) {
if (graphRules.shouldAssertRestricted()) {
return tasks.register(Tasks.ASSERT_RESTRICTIONS, AssertGraphTask::class.java) {
it.assertion = restrictedDependenciesAssert(graphRules)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
} else {
return null
}

return tasks.register(Tasks.ASSERT_RESTRICTIONS, AssertGraphTask::class.java) {
it.assertion = RestrictedDependenciesAssert(graphRules.restricted, aliases)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
}

private fun restrictedDependenciesAssert(graphRules: GraphRulesExtension) =
RestrictedDependenciesAssert(graphRules.restricted, aliases)

private fun Project.addModuleAllowedRulesTask(graphRules: GraphRulesExtension): TaskProvider<AssertGraphTask>? {
if (graphRules.allowed.isEmpty()) {
if (graphRules.shouldAssertAllowed()) {
return tasks.register(Tasks.ASSERT_ALLOWED, AssertGraphTask::class.java) {
it.assertion = onlyAllowedAssert(graphRules)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
} else {
return null
}

return tasks.register(Tasks.ASSERT_ALLOWED, AssertGraphTask::class.java) {
it.assertion = OnlyAllowedAssert(graphRules.allowed, aliases)
it.dependencyGraph = moduleGraph
it.outputs.upToDateWhen { true }
it.group = VERIFICATION_GROUP
}
}

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

private fun Project.moduleNameForHeightAssert(): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.jraska.module.graph.assertion

import org.gradle.testkit.runner.GradleRunner
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File

class OnAnyBuildAssertTest {
@get:Rule
val testProjectDir: TemporaryFolder = TemporaryFolder()

@Before
fun setup() {
testProjectDir.newFile("settings.gradle")
.writeText("include ':app', ':core', ':feature', 'core-api'")

createModule(
"core-api", content = """
apply plugin: 'java-library'

ext.moduleNameAssertAlias = "Api"
"""
)

createModule(
"core", content = """
apply plugin: 'java-library'

dependencies {
implementation project(":core-api")
}
ext.moduleNameAssertAlias = "Implementation"
"""
)

createModule(
"feature", content = """
apply plugin: 'java-library'

dependencies {
implementation project(":core-api")
}

ext.moduleNameAssertAlias = "Implementation"
"""
)
}

private fun createAppModule(moduleGraphAssertConfiguration: String) {
createModule(
"app", content = """
plugins {
id 'com.jraska.module.graph.assertion'
}
apply plugin: 'java-library'

$moduleGraphAssertConfiguration

dependencies {
implementation project(":core-api")
implementation project(":core")
implementation project(":feature")
}

ext.moduleNameAssertAlias = "App"
"""
)
}

@Test
fun failsOnEvaluationCheckMaxHeight() {
createAppModule(
"""
moduleGraphAssert {
maxHeight = 1
assertOnAnyBuild = true
}
"""
)

val output = setupGradle(testProjectDir.root, "help").buildAndFail().output

assert(output.contains("Module :app is allowed to have maximum height of 1, but has 2, problematic dependencies: :app -> :core -> :core-api"))
}

@Test
fun whenNotAssertOnEachEvaluation_succeedsOnEvaluationCheckMaxHeight() {
createAppModule(
"""
moduleGraphAssert {
maxHeight = 1
assertOnAnyBuild = false
}
"""
)

val output = setupGradle(testProjectDir.root, "help").build().output
assert(output.contains("BUILD SUCCESS"))

setupGradle(testProjectDir.root, "assertModuleGraph").buildAndFail()
}

@Test
fun failsOnEvaluationCheckAllowed() {
createAppModule(
"""
moduleGraphAssert {
allowed = ['Implementation -> Api', 'App -> Api']
assertOnAnyBuild = true
}
"""
)

val output = setupGradle(testProjectDir.root, "help").buildAndFail().output

assert(output.contains("""["App"(':app') -> "Implementation"(':core'), "App"(':app') -> "Implementation"(':feature')] not allowed by any of ['Implementation -> Api', 'App -> Api']"""))
}

@Test
fun whenNotAssertOnEachEvaluationDefault_succeedsOnEvaluationCheckAllowed() {
createAppModule(
"""
moduleGraphAssert {
allowed = ['Implementation -> Api', 'App -> Api']
}
"""
)

val output = setupGradle(testProjectDir.root, "help").build().output
assert(output.contains("BUILD SUCCESS"))

setupGradle(testProjectDir.root, "assertModuleGraph").buildAndFail()
}

@Test
fun failsOnEvaluationCheckRestricted() {
createAppModule(
"""
moduleGraphAssert {
restricted = ['App -X> Api', 'Implementation -X> Implementation']
assertOnAnyBuild = true
}
"""
)

val output = setupGradle(testProjectDir.root, "help").buildAndFail().output

assert(output.contains("""Dependency '"App"(':app') -> "Api"(':core-api') violates: 'App -X> Api'"""))
}

private fun createModule(dir: String, content: String) {
val newFolder = testProjectDir.newFolder(dir)
File(newFolder, "build.gradle").writeText(content)
}

private fun setupGradle(
dir: File,
vararg arguments: String
): GradleRunner {
return GradleRunner.create()
.withProjectDir(dir)
.withPluginClasspath()
.withArguments(arguments.asList())
}
}