Skip to content

Commit

Permalink
Allow opt-out configuring detekt android and multiplatform (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
chao2zhang committed Mar 5, 2021
1 parent 21d0bd5 commit fe9c486
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions detekt-gradle-plugin/build.gradle.kts
Expand Up @@ -17,6 +17,7 @@ dependencies {
compileOnly(kotlin("gradle-plugin"))

testImplementation(project(":detekt-test-utils"))
testImplementation(kotlin("gradle-plugin"))
intTest(kotlin("gradle-plugin"))
intTest(androidGradlePlugin)

Expand Down
Expand Up @@ -29,8 +29,12 @@ class DetektPlugin : Plugin<Project> {

project.registerDetektPlainTask(extension)
project.registerDetektJvmTasks(extension)
project.registerDetektAndroidTasks(extension)
project.registerDetektMultiplatformTasks(extension)
if (project.findProperty(DETEKT_ANDROID_DISABLED_PROPERTY) != "true") {
project.registerDetektAndroidTasks(extension)
}
if (project.findProperty(DETEKT_MULTIPLATFORM_DISABLED_PROPERTY) != "true") {
project.registerDetektMultiplatformTasks(extension)
}
project.registerGenerateConfigTask(extension)
}

Expand Down Expand Up @@ -106,6 +110,9 @@ class DetektPlugin : Plugin<Project> {
internal val defaultIncludes = listOf("**/*.kt", "**/*.kts")
internal const val CONFIG_DIR_NAME = "config/detekt"
internal const val CONFIG_FILE = "detekt.yml"

internal const val DETEKT_ANDROID_DISABLED_PROPERTY = "detekt.android.disabled"
internal const val DETEKT_MULTIPLATFORM_DISABLED_PROPERTY = "detekt.multiplatform.disabled"
}
}

Expand Down
Expand Up @@ -57,6 +57,30 @@ object DetektAndroidTest : Spek({
}
}

it("configures detekt plain only for android application if user opts out") {
val projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 0).apply {
addSubmodule(
name = "app",
numberOfSourceFilesPerSourceDir = 1,
numberOfCodeSmells = 1,
buildFileContent = """
$APP_PLUGIN_BLOCK
$ANDROID_BLOCK
$DETEKT_BLOCK
""".trimIndent(),
srcDirs = listOf("src/main/java", "src/debug/java", "src/test/java", "src/androidTest/java")
)
}
val gradleRunner = createGradleRunnerAndSetupProject(projectLayout)
gradleRunner.writeProjectFile("gradle.properties", "detekt.android.disabled=true")
gradleRunner.writeProjectFile("app/src/main/AndroidManifest.xml", MANIFEST_CONTENT)

gradleRunner.runTasks(":app:detekt")
gradleRunner.runTasksAndExpectFailure(":app:detektMain") { result ->
assertThat(result.output).contains("Task 'detektMain' not found in project")
}
}

it("configures detekt plain and detekt type resolution tasks for android library") {
val projectLayout = ProjectLayout(numberOfSourceFilesInRootPerSourceDir = 0).apply {
addSubmodule(
Expand Down
Expand Up @@ -42,6 +42,35 @@ class DetektMultiplatformTest : Spek({
}
}

describe("multiplatform projects - detekt plain only if user opts out") {

val gradleRunner = setupProject {
addSubmodule("shared", 1, 1,
buildFileContent = """
$KMM_PLUGIN_BLOCK
kotlin {
jvm()
}
$DETEKT_BLOCK
""".trimIndent(),
srcDirs = listOf("src/commonMain/kotlin", "src/commonTest/kotlin")
)
}
gradleRunner.writeProjectFile("gradle.properties", "detekt.multiplatform.disabled=true")

it("does not configure baseline task") {
gradleRunner.runTasksAndExpectFailure(":shared:detektBaselineMetadataMain") { result ->
assertThat(result.output).contains("Task 'detektBaselineMetadataMain' not found in project")
}
}

it("does not configure detekt task") {
gradleRunner.runTasksAndExpectFailure(":shared:detektMetadataMain") { result ->
assertThat(result.output).contains("Task 'detektMetadataMain' not found in project")
}
}
}

describe("multiplatform projects - JVM target") {
val gradleRunner = setupProject {
addSubmodule("shared", 1, 1,
Expand Down
7 changes: 6 additions & 1 deletion docs/pages/gettingstarted/type-resolution.md
Expand Up @@ -73,7 +73,12 @@ Other than the aforementioned tasks for JVM projects, you can use the following
- `detekt<Variant>` - Runs detekt with type resolution on the specific build variant
- `detektBaseline<Variant>` - Creates a detekt baselines starting from a run of Detekt with type resolution enabled on the specific build variant.

Alternatively, you can create a **custom detekt task**, making sure to specify the `classpath` and `jvmTarget` properties correctly. Doing this on Android is more complicated due to build types/flavors (see [#2259](https://github.com/detekt/detekt/issues/2259) for further context). Therefore, we recommend using the `detekt<Variant>` tasks offered by the Gradle plugins.
Alternatively, you can create a **custom detekt task**, making sure to specify the `classpath` and `jvmTarget` properties correctly.
Doing this on Android is more complicated due to build types/flavors (see [#2259](https://github.com/detekt/detekt/issues/2259) for further context).
Therefore, we recommend using the `detekt<Variant>` tasks offered by the Gradle plugins.

In case of build related issues, you may try `detekt.android.disabled=true` in `gradle.properties` to prevent detekt
Gradle plugins from configuring Android-specific gradle tasks.

## Enabling on Detekt CLI

Expand Down

0 comments on commit fe9c486

Please sign in to comment.