From fed3c29b14d3a977bbf47406e6ad323f63ef802b Mon Sep 17 00:00:00 2001 From: Rick Busarow Date: Thu, 12 May 2022 18:57:45 -0500 Subject: [PATCH 1/2] hook into the target project's `check` task fixes #594 --- CHANGELOG.md | 8 +- .../modulecheck/gradle/ModuleCheckPlugin.kt | 7 ++ .../modulecheck/gradle/BasePluginTest.kt | 10 ++- .../modulecheck/gradle/TaskLifecycleTest.kt | 81 +++++++++++++++++++ .../kotlin/modulecheck/testing/BaseTest.kt | 11 ++- website/src/pages/changelog.md | 8 +- 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/TaskLifecycleTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 036f07c276..069ce8d00f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,11 @@ with the `additionalCodeGenerators` property and `CodeGeneratorBinding`. See [the migrations guide](https://rbusarow.github.io/ModuleCheck/migrations#code-generator-binding) +### 💥 Breaking Changes + +- The base `:moduleCheck` task will now automatically hook into the root project's `:check` task, if + one exists. [@RBusarow](https://github.com/RBusarow) ([#611](https://github.com/rbusarow/ModuleCheck/pull/611)) + ### 🚀 Features - Added support @@ -38,7 +43,8 @@ ./gradlew moduleCheckAuto ``` - Tasks are no longer generated for most individual rules. Instead, rules should be toggled via - the [Gradle DSL](https://rbusarow.github.io/ModuleCheck/docs/next/configuration) and can be invoked + the [Gradle DSL](https://rbusarow.github.io/ModuleCheck/docs/next/configuration) and can be + invoked through `./gradlew modulecheck` or `./gradlew moduleCheckAuto`. ### 📐 New Rules diff --git a/modulecheck-gradle/plugin/src/main/kotlin/modulecheck/gradle/ModuleCheckPlugin.kt b/modulecheck-gradle/plugin/src/main/kotlin/modulecheck/gradle/ModuleCheckPlugin.kt index 3a9f1db339..3aeb8ec43f 100644 --- a/modulecheck-gradle/plugin/src/main/kotlin/modulecheck/gradle/ModuleCheckPlugin.kt +++ b/modulecheck-gradle/plugin/src/main/kotlin/modulecheck/gradle/ModuleCheckPlugin.kt @@ -30,6 +30,7 @@ import modulecheck.rule.FindingFactory import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.tasks.TaskProvider +import org.gradle.language.base.plugins.LifecycleBasePlugin class ModuleCheckPlugin : Plugin { @@ -89,6 +90,12 @@ class ModuleCheckPlugin : Plugin { includeAuto = true, disableConfigCache = disableConfigCache ) + + target.tasks + .matching { it.name == LifecycleBasePlugin.CHECK_TASK_NAME } + .configureEach { + it.dependsOn("moduleCheck") + } } private fun Project.registerTasks( diff --git a/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt index ff5ff9c63f..0016759afb 100644 --- a/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt +++ b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt @@ -19,12 +19,14 @@ import modulecheck.project.test.ProjectTest import modulecheck.specs.DEFAULT_AGP_VERSION import modulecheck.specs.DEFAULT_GRADLE_VERSION import modulecheck.specs.DEFAULT_KOTLIN_VERSION +import modulecheck.utils.child import modulecheck.utils.letIf import modulecheck.utils.remove import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.BeforeEach +import java.io.File import kotlin.text.RegexOption.IGNORE_CASE abstract class BasePluginTest : ProjectTest() { @@ -58,11 +60,17 @@ abstract class BasePluginTest : ProjectTest() { tasks.last().outcome shouldBe TaskOutcome.SUCCESS } - fun shouldSucceed(vararg tasks: String, stacktrace: Boolean = true): BuildResult { + fun shouldSucceed( + vararg tasks: String, + stacktrace: Boolean = true, + assertions: BuildResult.() -> Unit = {} + ): BuildResult { val result = build(*tasks, stacktrace = stacktrace) result.tasks.last().outcome shouldBe TaskOutcome.SUCCESS + result.assertions() + return result } diff --git a/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/TaskLifecycleTest.kt b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/TaskLifecycleTest.kt new file mode 100644 index 0000000000..a852f35e3e --- /dev/null +++ b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/TaskLifecycleTest.kt @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2021-2022 Rick Busarow + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package modulecheck.gradle + +import modulecheck.utils.child +import modulecheck.utils.createSafely +import org.gradle.testkit.runner.TaskOutcome.SUCCESS +import org.junit.jupiter.api.Test + +class TaskLifecycleTest : BasePluginTest() { + + @Test + fun `the modulecheck task should be invoked for the base plugin check task`() { + + kotlinProject(":") { + buildFile { + """ + plugins { + id("com.rickbusarow.module-check") + base + } + """ + } + + projectDir.child("settings.gradle.kts").createSafely() + } + + shouldSucceed("check") { + + task(":moduleCheck")!!.outcome shouldBe SUCCESS + + tasks shouldBe listOf( + task(":moduleCheck"), + task(":check") + ) + } + } + + @Test + fun `the modulecheck task should be invoked for a late, manually created check task`() { + + kotlinProject(":") { + buildFile { + """ + plugins { + id("com.rickbusarow.module-check") + } + + afterEvaluate { + task("check") + } + """ + } + + projectDir.child("settings.gradle.kts").createSafely() + } + + shouldSucceed("check") { + + task(":moduleCheck")!!.outcome shouldBe SUCCESS + + tasks shouldBe listOf( + task(":moduleCheck"), + task(":check") + ) + } + } +} diff --git a/modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/BaseTest.kt b/modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/BaseTest.kt index 1c099e9c12..5731f59b4e 100644 --- a/modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/BaseTest.kt +++ b/modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/BaseTest.kt @@ -18,6 +18,7 @@ package modulecheck.testing import hermit.test.junit.HermitJUnit5 import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.runBlocking +import modulecheck.utils.child import modulecheck.utils.remove import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.TestInfo @@ -32,18 +33,20 @@ abstract class BaseTest : HermitJUnit5(), FancyShould { // so use the FqName and trim the package name to get qualified nested names .let { it.canonicalName.removePrefix(it.packageName + ".") } .split(".") - .joinToString("/") + .joinToString(File.separator) .replace("[^a-zA-Z\\d/]".toRegex(), "_") val testName = testInfo.testMethod.get().name - .replace("[^a-zA-Z0-9]".toRegex(), "_") + .replace("[^a-zA-Z\\d]".toRegex(), "_") .replace("_{2,}".toRegex(), "_") .removeSuffix("_") - File("build/tests/$className/$testName").absoluteFile + File("build") + .child("tests", className, testName) + .absoluteFile } - private var testInfo: TestInfo by Delegates.notNull() + var testInfo: TestInfo by Delegates.notNull() fun File.relativePath() = path.removePrefix(testProjectDir.path) diff --git a/website/src/pages/changelog.md b/website/src/pages/changelog.md index 2a3cf9554f..35b0588966 100644 --- a/website/src/pages/changelog.md +++ b/website/src/pages/changelog.md @@ -10,6 +10,11 @@ with the `additionalCodeGenerators` property and `CodeGeneratorBinding`. See [the migrations guide](/migrations#code-generator-binding) +#### 💥 Breaking Changes + +- The base `:moduleCheck` task will now automatically hook into the root project's `:check` task, if + one exists. [@RBusarow](https://github.com/RBusarow) ([#611](https://github.com/rbusarow/ModuleCheck/pull/611)) + #### 🚀 Features - Added support @@ -38,7 +43,8 @@ ./gradlew moduleCheckAuto ``` - Tasks are no longer generated for most individual rules. Instead, rules should be toggled via - the [Gradle DSL](/docs/next/configuration) and can be invoked + the [Gradle DSL](/docs/next/configuration) and can be + invoked through `./gradlew modulecheck` or `./gradlew moduleCheckAuto`. #### 📐 New Rules From 1e272067718548f404741023de1b538417e57eb3 Mon Sep 17 00:00:00 2001 From: RBusarow Date: Fri, 13 May 2022 00:27:28 +0000 Subject: [PATCH 2/2] Apply KtLint format Signed-off-by: github-actions[bot] --- .../plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt index 0016759afb..77161c46a8 100644 --- a/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt +++ b/modulecheck-gradle/plugin/src/test/kotlin/modulecheck/gradle/BasePluginTest.kt @@ -19,14 +19,12 @@ import modulecheck.project.test.ProjectTest import modulecheck.specs.DEFAULT_AGP_VERSION import modulecheck.specs.DEFAULT_GRADLE_VERSION import modulecheck.specs.DEFAULT_KOTLIN_VERSION -import modulecheck.utils.child import modulecheck.utils.letIf import modulecheck.utils.remove import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.TaskOutcome import org.junit.jupiter.api.BeforeEach -import java.io.File import kotlin.text.RegexOption.IGNORE_CASE abstract class BasePluginTest : ProjectTest() {