All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javiersc.hubdle.project.extensions.config.testing.HubdleConfigTestingExtension.kt Maven / Gradle / Ivy

There is a newer version: 0.7.18
Show newest version
package com.javiersc.hubdle.project.extensions.config.testing

import com.gradle.develocity.agent.gradle.test.TestRetryConfiguration
import com.javiersc.hubdle.project.extensions.HubdleDslMarker
import com.javiersc.hubdle.project.extensions._internal.ApplicablePlugin.Scope
import com.javiersc.hubdle.project.extensions._internal.getHubdleExtension
import com.javiersc.hubdle.project.extensions.apis.HubdleConfigurableExtension
import com.javiersc.hubdle.project.extensions.apis.HubdleEnableableExtension
import com.javiersc.hubdle.project.extensions.config.hubdleConfig
import com.javiersc.hubdle.project.extensions.kotlin.android.hubdleAndroidAny
import com.javiersc.hubdle.project.extensions.kotlin.multiplatform.hubdleKotlinMultiplatform
import com.javiersc.hubdle.project.extensions.shared.PluginId
import javax.inject.Inject
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.provider.Property
import org.gradle.api.tasks.TaskCollection
import org.gradle.api.tasks.testing.Test
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.develocity
import org.gradle.kotlin.dsl.withType

@HubdleDslMarker
public abstract class HubdleConfigTestingExtension @Inject constructor(project: Project) :
    HubdleConfigurableExtension(project) {

    override val isEnabled: Property = property { true }

    override val requiredExtensions: Set
        get() = setOf(hubdleConfig)

    public val options: Property = property {
        if (hasAndroid.get()) Options.JUnit else Options.JUnitPlatform
    }

    public val maxParallelForks: Property = property {
        val isCI: Boolean = System.getenv("CI")?.toBoolean() ?: false
        val factor: Int = if (isCI) 1 else 2
        (Runtime.getRuntime().availableProcessors() / factor).takeIf { it > 0 } ?: 1
    }

    @HubdleDslMarker
    public fun maxParallelForks(forks: Int) {
        maxParallelForks.set(forks)
    }

    @HubdleDslMarker
    public fun retry(action: Action) {
        lazyConfigurable {
            tasks.withType().configureEach { test ->
                action.execute(test.develocity.testRetry)
            }
        }
    }

    public val showStandardStreams: Property = property { true }

    @HubdleDslMarker
    public fun showStandardStreams(enabled: Boolean = true) {
        showStandardStreams.set(enabled)
    }

    @HubdleDslMarker
    public fun test(action: Action) {
        lazyConfigurable {
            project.tasks.withType().configureEach { test -> action.execute(test) }
        }
    }

    override fun Project.defaultConfiguration() {
        pluginManager.apply(BasePlugin::class)

        applicablePlugin(scope = Scope.CurrentProject, pluginId = PluginId.AdarshrTestLogger)

        lazyConfigurable {
            val testTasks: TaskCollection = tasks.withType()
            testTasks.configureEach { task ->
                task.testLogging.showStandardStreams = showStandardStreams.get()
                task.maxParallelForks = maxParallelForks.get()

                when (options.get()) {
                    Options.JUnit -> task.useJUnit()
                    Options.JUnitPlatform -> task.useJUnitPlatform()
                    Options.TestNG -> task.useTestNG()
                    else -> task.useJUnit()
                }
            }
        }
    }

    public enum class Options {
        JUnit,
        JUnitPlatform,
        TestNG,
    }
}

private val HubdleConfigTestingExtension.hasAndroid: Property
    get() = property {
        hubdleAndroidAny.any { it.isFullEnabled.get() } ||
            hubdleKotlinMultiplatform.isFullEnabled.get()
    }

internal val HubdleEnableableExtension.hubdleTesting: HubdleConfigTestingExtension
    get() = getHubdleExtension()

internal val Project.hubdleTesting: HubdleConfigTestingExtension
    get() = getHubdleExtension()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy