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

Cannot configure reports for detektMain and detektTest #5721

Closed
frapontillo opened this issue Jan 23, 2023 · 6 comments
Closed

Cannot configure reports for detektMain and detektTest #5721

frapontillo opened this issue Jan 23, 2023 · 6 comments
Labels

Comments

@frapontillo
Copy link

I need to configure different report destinations for detektMain and detektTest, but it currently looks like this is unsupported.

This is my configuration for my Android project:

plugins {
    id 'com.android.library'
    id 'kotlin-android'

    id("io.gitlab.arturbosch.detekt")
}

dependencies {
    detektPlugins "io.gitlab.arturbosch.detekt:detekt-formatting:$detekt_version"
    detektPlugins "com.gitlab.cromefire:detekt-gitlab-report:$detekt_gitlab_version"
}

detekt {
    buildUponDefaultConfig = true
    allRules = false
    config = rootProject.files("config/detekt.yml")
    ignoreFailures = true
    basePath = rootDir.toString()
}

tasks.matching { task -> task.name.startsWith("detekt") }.all {
    println "$it ${it.class}"
    // ideally here I'd add reports { ... }
}

This returns the following:

> Configure project :myproject
':myproject:detekt' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektBaseline' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated
':myproject:detektGenerateConfig' class io.gitlab.arturbosch.detekt.DetektGenerateConfigTask_Decorated
':myproject:detektDebug' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektMain' class org.gradle.api.DefaultTask_Decorated
':myproject:detektBaselineDebug' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated
':myproject:detektBaselineMain' class org.gradle.api.DefaultTask_Decorated
':myproject:detektDebugAndroidTest' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektTest' class org.gradle.api.DefaultTask_Decorated
':myproject:detektBaselineDebugAndroidTest' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated
':myproject:detektBaselineTest' class org.gradle.api.DefaultTask_Decorated
':myproject:detektDebugUnitTest' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektBaselineDebugUnitTest' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated
':myproject:detektRelease' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektBaselineRelease' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated
':myproject:detektReleaseUnitTest' class io.gitlab.arturbosch.detekt.Detekt_Decorated
':myproject:detektBaselineReleaseUnitTest' class io.gitlab.arturbosch.detekt.DetektCreateBaselineTask_Decorated

So I try and configure reports for, say, detektMain:

tasks.matching { task -> task.name == "detektMain" }.configureEach {
    reports {
        custom {
            reportId = "DetektGitlabReport"
            destination = file("$buildDir/reports/detekt/gitlab.json")
        }
    }
}

Since detektMain and detektTest are NOT instances of io.gitlab.arturbosch.detekt.Detekt, this will fail with:

A problem occurred configuring project ':myproject'.
> Failed to notify project evaluation listener.
   > Could not create task ':myproject:detektMain'.
      > No signature of method: org.gradle.api.DefaultTask.reports() is applicable for argument types: (build_d2d0zznhsg5yadzyv9325pix$_run_closure4$_closure9) values: [build_d2d0zznhsg5yadzyv9325pix$_run_closure4$_closure9@4c7e042c]
        Possible solutions: property(java.lang.String), property(java.lang.String)
   > Could not create task ':myproject:detektMain'.
      > No signature of method: org.gradle.api.DefaultTask.reports() is applicable for argument types: (build_d2d0zznhsg5yadzyv9325pix$_run_closure4$_closure9) values: [build_d2d0zznhsg5yadzyv9325pix$_run_closure4$_closure9@4c7e042c]
        Possible solutions: property(java.lang.String), property(java.lang.String)

Is there any way to configure reports for detektMain and detektTest as well?

@3flex
Copy link
Member

3flex commented Jan 26, 2023

You should just be able to do it like this:

tasks.detektTest {
    reports {
        custom {
            reportId = "DetektGitlabReport"
            destination = file("$buildDir/reports/detektTest/gitlab.json")
        }
    }
}

@3flex 3flex added the support label Jan 26, 2023
@cortinico
Copy link
Member

Since detektMain and detektTest are NOT instances of io.gitlab.arturbosch.detekt.Detekt, this will fail with:

Also to clarify: they do are instances of io.gitlab.arturbosch.detekt.Detekt:

internal fun Project.registerDetektTask(
name: String,
extension: DetektExtension,
configuration: Detekt.() -> Unit
): TaskProvider<Detekt> =

@frapontillo
Copy link
Author

@cortinico detektMain and detektTest for Android are actually simple tasks:

private val mainTaskProvider: TaskProvider<Task> by lazy {
project.tasks.register("${DetektPlugin.DETEKT_TASK_NAME}Main") {
it.group = "verification"
it.description = "EXPERIMENTAL: Run detekt analysis for production classes across " +
"all variants with type resolution"
}
}
private val testTaskProvider: TaskProvider<Task> by lazy {
project.tasks.register("${DetektPlugin.DETEKT_TASK_NAME}Test") {
it.group = "verification"
it.description = "EXPERIMENTAL: Run detekt analysis for test classes across " +
"all variants with type resolution"
}
}

Detekt tasks are then added as dependencies:

project.registerAndroidDetektTask(bootClasspath, extension, variant)
.also { provider ->
mainTaskProvider.dependsOn(provider)
}
project.registerAndroidCreateBaselineTask(bootClasspath, extension, variant)
.also { provider ->
mainBaselineTaskProvider.dependsOn(provider)
}
variant.testVariants
.filter { !extension.matchesIgnoredConfiguration(it) }
.forEach { testVariant ->
project.registerAndroidDetektTask(bootClasspath, extension, testVariant)
.also { provider ->
testTaskProvider.dependsOn(provider)
}
project.registerAndroidCreateBaselineTask(
bootClasspath,
extension,
testVariant
)
.also { provider ->
testBaselineTaskProvider.dependsOn(provider)
}
}

@3flex
Copy link
Member

3flex commented Feb 4, 2023

Sorry, we overlooked that this was on Android - you're correct, detektTest and detektMain are not Detekt tasks when using Android.

What are you actually trying to accomplish with your report output settings?

@frapontillo
Copy link
Author

I am looking for a way to have the main and test detekt tasks generate reports of which I know the names of. Since the Android tasks depend on the variants available (and they can be different from module to module), I was hoping there was a way for Detekt main and test tasks to collate all reports and generate one single HTML report per module that I can then reference back to.

@3flex
Copy link
Member

3flex commented Feb 7, 2023

That's not currently supported but you might want to subscribe to updates to #5041 which may end up delivering the feature you're looking for.

I think this can be closed - if you have other questions about how to setup your config I'd suggest opening a discussion instead of a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants