Repository: Kotlin/kotlinx.coroutines
File: integration-testing/build.gradle.kts (Kotlin)

Code before:
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
    }

    create<Test>("coreAgentTest") {
        val sourceSet = sourceSets[name]
        val coroutinesDebugJar = sourceSet.runtimeClasspath.filter {
            it.name == "kotlinx-coroutines-core-jvm-$coroutinesVersion.jar"
        }.singleFile
        jvmArgs("-javaagent:$coroutinesDebugJar")
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
    }

    create<Test>("javaConsumersTest") {
        val sourceSet = sourceSets[name]
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
    }

    // todo assert that this task is failing
    // In this test coroutine debug agent is attached as a javaagent vm argument
    // to a pure Java project (safeDebugAgentTest) with no Kotlin stdlib dependency.
    // In this case the error should be thrown from AgetnPremain class:
    // "java.lang.IllegalStateException: kotlinx.coroutines debug agent failed to load."
    create<Test>("safeDebugAgentTest") {
        // take kotlinx-coroutines-core.jar from a sourceSet with coroutines dependency
        val sourceSet = sourceSets["coreAgentTest"]
        val coroutinesDebugJar = sourceSet.runtimeClasspath.filter {
            it.name == "kotlinx-coroutines-core-jvm-$coroutinesVersion.jar"
        }.singleFile
        project(":safeDebugAgentTest").extra["debug.agent"] = coroutinesDebugJar.absolutePath
        dependsOn(":safeDebugAgentTest:run")
    }

    check {
        dependsOn(
            "jvmCoreTest",
            "debugDynamicAgentTest",
            "mavenTest",
            "debugAgentTest",
            "coreAgentTest",
            "javaConsumersTest",
            ":jpmsTest:check",
            "smokeTest:build",
            "java8Test:check",
        )
    }

    // Drop this when node js version become stable
    withType(KotlinNpmInstallTask::class.java).configureEach {

Review comment (bug):
@dkhalanskyjb hi!)
I need some help with Gradle here 👀 

How could we check, that `:safeDebugAgentTest:run` fails with an error from `AgentPremain?`
Is there smth like `assertFailsWith ` in Gradle?

I've thought of an alternative to run `:safeDebugAgentTest:run` from a Kotlin test: to create a `ProcessBuilder` + try / catch, but maybe there is an easier option? 

Code after:
        val sourceSet = sourceSets[name]
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
    }

    create<Test>("coreAgentTest") {
        val sourceSet = sourceSets[name]
        val coroutinesDebugJar = sourceSet.runtimeClasspath.filter {
            it.name == "kotlinx-coroutines-core-jvm-$coroutinesVersion.jar"
        }.singleFile
        jvmArgs("-javaagent:$coroutinesDebugJar")
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
        System.setProperty("coroutines.debug.agent.path", coroutinesDebugJar.absolutePath) // todo: fixme
    }

    create<Test>("javaConsumersTest") {
        val sourceSet = sourceSets[name]
        testClassesDirs = sourceSet.output.classesDirs
        classpath = sourceSet.runtimeClasspath
    }

    check {
        dependsOn(
            "jvmCoreTest",
            "debugDynamicAgentTest",
            "mavenTest",
            "debugAgentTest",
            "coreAgentTest",
            "javaConsumersTest",
            ":jpmsTest:check",
            "smokeTest:build",
            "java8Test:check",
            "safeDebugAgentTest:runWithExpectedFailure"
        )
    }

    // Drop this when node js version become stable
    withType(KotlinNpmInstallTask::class.java).configureEach {
        args.add("--ignore-engines")
    }

    withType(KotlinJvmCompile::class.java).configureEach {
        jvmTargetValidationMode = JvmTargetValidationMode.WARNING
    }
}