Skip to content

Commit

Permalink
Fix flushing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ogolberg authored Sep 26, 2024
1 parent 1e1f5d3 commit 04afa16
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 99 deletions.
2 changes: 1 addition & 1 deletion coverage-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ gradlePlugin {

dependencies {
implementation(gradleApi())
implementation(projects.jacocoReflect)
implementation(libs.jacoco.agent) {
artifact {
classifier = "runtime"
extension = "jar"
}
}
implementation(projects.jacocoReflect)

testImplementation(projects.junit5)
testImplementation(libs.junit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class FlushJacocoPlugin @Inject constructor(

class DumpAction : FlowAction<FlowParameters.None> {
override fun execute(parameters: FlowParameters.None) {
JacocoRt.requiredAgent.run {
writeExecutionData(true)
shutdown()
}
JacocoRt.requiredAgent.dump(true)
}
}
15 changes: 8 additions & 7 deletions integration-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ plugins {

gradlePlugin {
plugins {
create("integration") {
id = "com.toasttab.testkit.integration.test"
implementationClass = "com.toasttab.gradle.testkit.TestPlugin"
description = ProjectInfo.description
displayName = ProjectInfo.name
tags = listOf("jacoco", "testkit")
for (i in 1..5) {
create("test$i") {
id = "com.toasttab.testkit.integration.test$i"
implementationClass = "com.toasttab.gradle.testkit.TestPlugin$i"
description = "test"
displayName = "test"
tags = listOf("test")
}
}
}
}
Expand All @@ -34,7 +36,6 @@ dependencies {
implementation(gradleApi())
testImplementation(libs.junit)
testImplementation(libs.strikt.core)
testImplementation(projects.jacocoReflect)
testImplementation(projects.junit5)
testImplementation(gradleTestKit())
testImplementation(libs.jacoco.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package com.toasttab.gradle.testkit
import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin : Plugin<Project> {
class TestPlugin1 : Plugin<Project> {
override fun apply(project: Project) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin2 : Plugin<Project> {
override fun apply(project: Project) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin3 : Plugin<Project> {
override fun apply(project: Project) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin4 : Plugin<Project> {
override fun apply(project: Project) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Toast Inc.
*
* 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 com.toasttab.gradle.testkit

import org.gradle.api.Plugin
import org.gradle.api.Project

class TestPlugin5 : Plugin<Project> {
override fun apply(project: Project) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.junit.jupiter.api.io.TempDir
import strikt.api.expectThat
import strikt.assertions.contains
import java.nio.file.Path
import kotlin.io.path.createDirectory
import kotlin.io.path.inputStream
import kotlin.io.path.writeText

Expand All @@ -32,23 +33,32 @@ class FlushJacocoPluginIntegrationTest {
fun `coverage is flushed`() {
val version = System.getProperty("version")

val file = dir.resolve("build/testkit.exec")
val file = dir.resolve("testkit.exec")
val recorder = CoverageRecorder(CoverageSettings("", "", file.toString()))

dir.resolve("gradle.properties").writeText(
"systemProp.jacoco-agent.destfile=$file"
)
for (i in 1..5) {
val projectDir = dir.resolve("project$i")
projectDir.createDirectory()

dir.resolve("build.gradle.kts").writeText(
"""
projectDir.resolve("build.gradle.kts").writeText(
"""
plugins {
java
id("com.toasttab.testkit.coverage") version("$version")
id("com.toasttab.testkit.integration.test") version("$version")
id("com.toasttab.testkit.integration.test$i") version("$version")
}
""".trimIndent()
)
""".trimIndent()
)

TestProjectExtension.createProject(dir, GradleVersionArgument.of("8.7")).build("build", "--configuration-cache", "--stacktrace")
TestProjectExtension.createProject(
projectDir = projectDir,
gradleVersion = GradleVersionArgument.of("8.7"),
cleanup = true,
coverageRecorder = recorder
).build("build", "--configuration-cache", "--stacktrace")
}

recorder.close()

val classes = hashSetOf<String>()

Expand All @@ -64,6 +74,8 @@ class FlushJacocoPluginIntegrationTest {
}.read()
}

expectThat(classes).contains(TestPlugin::class.java.name.replace('.', '/'))
expectThat(classes).contains(
(1..5).map { "com/toasttab/gradle/testkit/TestPlugin$it" }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ package com.toasttab.gradle.testkit.jacoco
interface JacocoAgent {
val location: String

fun writeExecutionData(reset: Boolean)
fun dump(reset: Boolean)

val includes: String

val excludes: String

fun shutdown()
}

private class ReflectiveJacocoAgent(
Expand All @@ -35,24 +33,15 @@ private class ReflectiveJacocoAgent(
agent.javaClass.getDeclaredField("options").apply { isAccessible = true }.get(agent)
}

private val output by lazy {
// agent.output
agent.javaClass.getDeclaredField("output").apply { isAccessible = true }.get(agent)
}

override val location: String
get() = agent.javaClass.protectionDomain.codeSource.location.file
override val includes: String
get() = options.javaClass.getMethod("getIncludes").invoke(options) as String
override val excludes: String
get() = options.javaClass.getMethod("getExcludes").invoke(options) as String

override fun writeExecutionData(reset: Boolean) {
output.javaClass.getMethod("writeExecutionData", Boolean::class.java).invoke(output, reset)
}

override fun shutdown() {
output.javaClass.getMethod("shutdown").invoke(output)
override fun dump(reset: Boolean) {
agent.javaClass.getMethod("dump", Boolean::class.java).invoke(agent, reset)
}
}

Expand Down
Loading

0 comments on commit 04afa16

Please sign in to comment.