Skip to content
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 @@ -33,6 +33,7 @@ import org.gradle.api.artifacts.MinimalExternalModuleDependency
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.property
import javax.inject.Inject

abstract class ProjectGuardExtension @Inject constructor(
Expand All @@ -42,10 +43,8 @@ abstract class ProjectGuardExtension @Inject constructor(
private val guardSpecs = objects.listProperty<GuardSpec>()
private val moduleRestrictionSpecs = objects.listProperty<ModuleRestrictionSpec>()
private val dependencyRestrictionSpecs = objects.listProperty<DependencyRestrictionSpec>()
private var reportSpec = ReportSpec(showLibrariesInGraph = false)
private var options = PluginOptions(
lifecycleTask = null
)
private val reportSpec = objects.property<ReportSpec>().convention(ReportSpec(showLibrariesInGraph = false))
private val options = objects.property<PluginOptions>().convention(PluginOptions(lifecycleTask = null))

override fun restrictModule(modulePath: String, action: Action<ModuleRestrictionScope>) {
val scope = ModuleRestrictionScopeImpl()
Expand Down Expand Up @@ -127,26 +126,22 @@ abstract class ProjectGuardExtension @Inject constructor(
override fun report(action: Action<ReportScope>) {
val scope = ReportScopeImpl()
action.execute(scope)
reportSpec = reportSpec.copy(
showLibrariesInGraph = scope.showLibrariesInGraph
)
reportSpec.set(ReportSpec(showLibrariesInGraph = scope.showLibrariesInGraph))
}

override fun options(action: Action<OptionScope>) {
val scope = OptionScopeImpl()
action.execute(scope)
options = options.copy(
lifecycleTask = scope.lifecycleTask,
)
options.set(PluginOptions(lifecycleTask = scope.lifecycleTask))
}

internal fun getSpec(): ProjectGuardSpec {
return ProjectGuardSpec(
guardSpecs = guardSpecs.get(),
moduleRestrictionSpecs = moduleRestrictionSpecs.get(),
dependencyRestrictionSpecs = dependencyRestrictionSpecs.get(),
reportSpec = reportSpec,
options = options
reportSpec = reportSpec.get(),
options = options.get()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class ProjectGuardPlugin : Plugin<Project> {
) {
group = "verification"
description = "Verifies if there are any dependency restrictions"
reportSpec.set(extension.getSpec().reportSpec)
pluginSpec.set(project.provider { extension.getSpec() })
outputs.upToDateWhen { false }
}
}
Expand All @@ -307,7 +307,7 @@ class ProjectGuardPlugin : Plugin<Project> {
) {
group = "verification"
description = "Verifies if there are any dependency restrictions"
reportSpec.set(extension.getSpec().reportSpec)
pluginSpec.set(project.provider { extension.getSpec() })
outputs.upToDateWhen { false }
}
}
Expand All @@ -323,7 +323,7 @@ class ProjectGuardPlugin : Plugin<Project> {
group = "other"
description = "Generates a JSON report of all dependency restrictions for this module."
projectPath.set(project.path)
specProperty.set(extension.getSpec())
specProperty.set(project.provider { extension.getSpec() })
outputFile.set(
project.layout.buildDirectory.file(jsonReportFilePath)
)
Expand All @@ -340,7 +340,7 @@ class ProjectGuardPlugin : Plugin<Project> {
group = "other"
description = "Generates a JSON containing the dependencies of this module."
projectPath.set(project.path)
dependencyGraph.set(graphBuilder.buildFromProject(project))
dependencyGraph.set(project.provider { graphBuilder.buildFromProject(project) })
outputFile.set(
project.layout.buildDirectory.file(dependenciesFilePath)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import kotlinx.serialization.json.Json
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import java.io.File

@CacheableTask
internal abstract class TaskAggregateDependencyDump : DefaultTask() {

@get:InputFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import kotlinx.serialization.json.Json
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import java.io.File

@CacheableTask
internal abstract class TaskAggregateRestrictionDump : DefaultTask() {

@get:InputFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.rubensousa.projectguard.plugin.internal.task

import com.rubensousa.projectguard.plugin.internal.BaselineConfiguration
import com.rubensousa.projectguard.plugin.internal.DependencyGraphBuilder
import com.rubensousa.projectguard.plugin.internal.ProjectGuardSpec
import com.rubensousa.projectguard.plugin.internal.ReportSpec
import com.rubensousa.projectguard.plugin.internal.SuppressionMap
import com.rubensousa.projectguard.plugin.internal.YamlProcessor
Expand Down Expand Up @@ -54,7 +55,7 @@ internal abstract class TaskCheck : DefaultTask() {
internal abstract val reportFilePath: Property<String>

@get:Input
internal abstract val reportSpec: Property<ReportSpec>
internal abstract val pluginSpec: Property<ProjectGuardSpec>

@get:OutputDirectory
abstract val outputDir: DirectoryProperty
Expand All @@ -67,7 +68,7 @@ internal abstract class TaskCheck : DefaultTask() {
dependenciesFile = dependenciesFile.get().asFile,
reportDir = outputDir.get().asFile,
reportFilePath = reportFilePath.get(),
reportSpec = reportSpec.get()
reportSpec = pluginSpec.get().reportSpec
)
executor.execute().getOrThrow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.rubensousa.projectguard.plugin
import com.google.common.truth.Truth.assertThat
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -50,18 +51,36 @@ class PluginCacheTest {
}

@Test
fun `outputs from projectGuardDependencyDump are re-used`() {
fun `outputs from projectGuardDependencyDump are up-to-date on next execution`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val libraryDependencyTask = ":b:projectGuardDependencyDump"
pluginRunner.runTask(libraryDependencyTask)

// when
val nextResult = pluginRunner.runTask(libraryDependencyTask)

// then
assertThat(nextResult).isEqualTo(TaskOutcome.UP_TO_DATE)
}

@Test
fun `outputs from projectGuardDependencyDump are re-used from cache`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val libraryDependencyTask = ":b:projectGuardDependencyDump"
pluginRunner.runTask(libraryDependencyTask)

// when
pluginRunner.deleteBuildDirs()
val result = pluginRunner.runTask(libraryDependencyTask)

// then
assertThat(pluginRunner.runTask(libraryDependencyTask)).isEqualTo(TaskOutcome.UP_TO_DATE)
assertThat(result).isEqualTo(TaskOutcome.FROM_CACHE)
}

@Test
Expand All @@ -76,24 +95,45 @@ class PluginCacheTest {

// when
pluginRunner.addDependency(from = "b", to = "c")
pluginRunner.deleteBuildDirs()
val result = pluginRunner.runTask(libraryDependencyTask)

// then
assertThat(pluginRunner.runTask(libraryDependencyTask)).isEqualTo(TaskOutcome.SUCCESS)
assertThat(result).isEqualTo(TaskOutcome.SUCCESS)
}

@Test
fun `outputs from projectGuardAggregateDependencyDump are re-used`() {
fun `outputs from projectGuardAggregateDependencyDump are up-to-date on next execution`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val task = ":projectGuardAggregateDependencyDump"
pluginRunner.runTask(task)

// when
val result = pluginRunner.runTask(task)

// then
assertThat(result).isEqualTo(TaskOutcome.UP_TO_DATE)
}

@Ignore("Not working for now")
@Test
fun `outputs from projectGuardAggregateDependencyDump are re-used from cache`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val task = ":projectGuardAggregateDependencyDump"
pluginRunner.runTask(task)

// when
pluginRunner.deleteBuildDirs()
val result = pluginRunner.runTask(task)

// then
assertThat(pluginRunner.runTask(task)).isEqualTo(TaskOutcome.UP_TO_DATE)
assertThat(result).isEqualTo(TaskOutcome.FROM_CACHE)
}

@Test
Expand All @@ -114,18 +154,36 @@ class PluginCacheTest {
}

@Test
fun `outputs from projectGuardRestrictionDump are re-used if nothing changed`() {
fun `outputs from projectGuardRestrictionDump are up-to-date if nothing changed`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val task = ":a:projectGuardRestrictionDump"
pluginRunner.runTask(task)

// when
val result = pluginRunner.runTask(task)

// then
assertThat(result).isEqualTo(TaskOutcome.UP_TO_DATE)
}

@Test
fun `outputs from projectGuardRestrictionDump are cached`() {
// given
pluginRunner.createModule("a")
pluginRunner.createModule("b")
pluginRunner.addDependency(from = "a", to = "b")
val task = ":a:projectGuardRestrictionDump"
pluginRunner.runTask(task)

// when
pluginRunner.deleteBuildDirs()
val result = pluginRunner.runTask(task)

// then
assertThat(pluginRunner.runTask(task)).isEqualTo(TaskOutcome.UP_TO_DATE)
assertThat(result).isEqualTo(TaskOutcome.FROM_CACHE)
}

@Test
Expand All @@ -140,6 +198,7 @@ class PluginCacheTest {

// when
pluginRunner.addDependency(from = "b", to = "c")
pluginRunner.deleteBuildDirs()

// then
assertThat(pluginRunner.runTask(task)).isEqualTo(TaskOutcome.SUCCESS)
Expand All @@ -162,6 +221,7 @@ class PluginCacheTest {
}
""".trimIndent()
)
pluginRunner.deleteBuildDirs()

// then
assertThat(pluginRunner.runTask(task)).isEqualTo(TaskOutcome.SUCCESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,24 @@ class PluginRunner(
.withGradleVersion("8.13")
}
private var lastResult: BuildResult? = null
private val modules = mutableListOf<String>()

fun createModule(name: String) {
temporaryFolder.newFolder(name)
temporaryFolder.newFile("$name/build.gradle.kts")
settingsFile.appendText("\ninclude(\":$name\")")
modules.add(name)
}

fun deleteBuildDirs() {
modules.forEach { module ->
temporaryFolder.getRoot()
.resolve("$module/build/")
.deleteRecursively()
}
temporaryFolder.getRoot()
.resolve("build")
.deleteRecursively()
}

fun assertProjectGuardCheckFails(module: String) {
Expand Down Expand Up @@ -72,7 +85,7 @@ class PluginRunner(
}

fun runTask(task: String): TaskOutcome {
val result = gradleRunner.withArguments(task).build()
val result = gradleRunner.withArguments("--build-cache", task).build()
lastResult = result
return result.task(task)!!.outcome
}
Expand Down