Skip to content

Commit 04afa16

Browse files
authored
Fix flushing coverage
1 parent 1e1f5d3 commit 04afa16

File tree

14 files changed

+246
-99
lines changed

14 files changed

+246
-99
lines changed

coverage-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ gradlePlugin {
1818

1919
dependencies {
2020
implementation(gradleApi())
21-
implementation(projects.jacocoReflect)
2221
implementation(libs.jacoco.agent) {
2322
artifact {
2423
classifier = "runtime"
2524
extension = "jar"
2625
}
2726
}
27+
implementation(projects.jacocoReflect)
2828

2929
testImplementation(projects.junit5)
3030
testImplementation(libs.junit)

coverage-plugin/src/main/kotlin/com/toasttab/gradle/testkit/FlushJacocoPlugin.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class FlushJacocoPlugin @Inject constructor(
3333

3434
class DumpAction : FlowAction<FlowParameters.None> {
3535
override fun execute(parameters: FlowParameters.None) {
36-
JacocoRt.requiredAgent.run {
37-
writeExecutionData(true)
38-
shutdown()
39-
}
36+
JacocoRt.requiredAgent.dump(true)
4037
}
4138
}

integration-tests/build.gradle.kts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ plugins {
1010

1111
gradlePlugin {
1212
plugins {
13-
create("integration") {
14-
id = "com.toasttab.testkit.integration.test"
15-
implementationClass = "com.toasttab.gradle.testkit.TestPlugin"
16-
description = ProjectInfo.description
17-
displayName = ProjectInfo.name
18-
tags = listOf("jacoco", "testkit")
13+
for (i in 1..5) {
14+
create("test$i") {
15+
id = "com.toasttab.testkit.integration.test$i"
16+
implementationClass = "com.toasttab.gradle.testkit.TestPlugin$i"
17+
description = "test"
18+
displayName = "test"
19+
tags = listOf("test")
20+
}
1921
}
2022
}
2123
}
@@ -34,7 +36,6 @@ dependencies {
3436
implementation(gradleApi())
3537
testImplementation(libs.junit)
3638
testImplementation(libs.strikt.core)
37-
testImplementation(projects.jacocoReflect)
3839
testImplementation(projects.junit5)
3940
testImplementation(gradleTestKit())
4041
testImplementation(libs.jacoco.core)

integration-tests/src/main/kotlin/com/toasttab/gradle/testkit/TestPlugin.kt renamed to integration-tests/src/main/kotlin/com/toasttab/gradle/testkit/TestPlugin1.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package com.toasttab.gradle.testkit
1818
import org.gradle.api.Plugin
1919
import org.gradle.api.Project
2020

21-
class TestPlugin : Plugin<Project> {
21+
class TestPlugin1 : Plugin<Project> {
2222
override fun apply(project: Project) {
2323
}
2424
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.toasttab.gradle.testkit
17+
18+
import org.gradle.api.Plugin
19+
import org.gradle.api.Project
20+
21+
class TestPlugin2 : Plugin<Project> {
22+
override fun apply(project: Project) {
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.toasttab.gradle.testkit
17+
18+
import org.gradle.api.Plugin
19+
import org.gradle.api.Project
20+
21+
class TestPlugin3 : Plugin<Project> {
22+
override fun apply(project: Project) {
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.toasttab.gradle.testkit
17+
18+
import org.gradle.api.Plugin
19+
import org.gradle.api.Project
20+
21+
class TestPlugin4 : Plugin<Project> {
22+
override fun apply(project: Project) {
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024 Toast Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
package com.toasttab.gradle.testkit
17+
18+
import org.gradle.api.Plugin
19+
import org.gradle.api.Project
20+
21+
class TestPlugin5 : Plugin<Project> {
22+
override fun apply(project: Project) {
23+
}
24+
}

integration-tests/src/test/kotlin/com/toasttab/gradle/testkit/FlushJacocoPluginIntegrationTest.kt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.junit.jupiter.api.io.TempDir
2121
import strikt.api.expectThat
2222
import strikt.assertions.contains
2323
import java.nio.file.Path
24+
import kotlin.io.path.createDirectory
2425
import kotlin.io.path.inputStream
2526
import kotlin.io.path.writeText
2627

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

35-
val file = dir.resolve("build/testkit.exec")
36+
val file = dir.resolve("testkit.exec")
37+
val recorder = CoverageRecorder(CoverageSettings("", "", file.toString()))
3638

37-
dir.resolve("gradle.properties").writeText(
38-
"systemProp.jacoco-agent.destfile=$file"
39-
)
39+
for (i in 1..5) {
40+
val projectDir = dir.resolve("project$i")
41+
projectDir.createDirectory()
4042

41-
dir.resolve("build.gradle.kts").writeText(
42-
"""
43+
projectDir.resolve("build.gradle.kts").writeText(
44+
"""
4345
plugins {
4446
java
4547
id("com.toasttab.testkit.coverage") version("$version")
46-
id("com.toasttab.testkit.integration.test") version("$version")
48+
id("com.toasttab.testkit.integration.test$i") version("$version")
4749
}
48-
""".trimIndent()
49-
)
50+
""".trimIndent()
51+
)
5052

51-
TestProjectExtension.createProject(dir, GradleVersionArgument.of("8.7")).build("build", "--configuration-cache", "--stacktrace")
53+
TestProjectExtension.createProject(
54+
projectDir = projectDir,
55+
gradleVersion = GradleVersionArgument.of("8.7"),
56+
cleanup = true,
57+
coverageRecorder = recorder
58+
).build("build", "--configuration-cache", "--stacktrace")
59+
}
60+
61+
recorder.close()
5262

5363
val classes = hashSetOf<String>()
5464

@@ -64,6 +74,8 @@ class FlushJacocoPluginIntegrationTest {
6474
}.read()
6575
}
6676

67-
expectThat(classes).contains(TestPlugin::class.java.name.replace('.', '/'))
77+
expectThat(classes).contains(
78+
(1..5).map { "com/toasttab/gradle/testkit/TestPlugin$it" }
79+
)
6880
}
6981
}

jacoco-reflect/src/main/kotlin/com/toasttab/gradle/testkit/jacoco/JacocoRt.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ package com.toasttab.gradle.testkit.jacoco
1818
interface JacocoAgent {
1919
val location: String
2020

21-
fun writeExecutionData(reset: Boolean)
21+
fun dump(reset: Boolean)
2222

2323
val includes: String
2424

2525
val excludes: String
26-
27-
fun shutdown()
2826
}
2927

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

38-
private val output by lazy {
39-
// agent.output
40-
agent.javaClass.getDeclaredField("output").apply { isAccessible = true }.get(agent)
41-
}
42-
4336
override val location: String
4437
get() = agent.javaClass.protectionDomain.codeSource.location.file
4538
override val includes: String
4639
get() = options.javaClass.getMethod("getIncludes").invoke(options) as String
4740
override val excludes: String
4841
get() = options.javaClass.getMethod("getExcludes").invoke(options) as String
4942

50-
override fun writeExecutionData(reset: Boolean) {
51-
output.javaClass.getMethod("writeExecutionData", Boolean::class.java).invoke(output, reset)
52-
}
53-
54-
override fun shutdown() {
55-
output.javaClass.getMethod("shutdown").invoke(output)
43+
override fun dump(reset: Boolean) {
44+
agent.javaClass.getMethod("dump", Boolean::class.java).invoke(agent, reset)
5645
}
5746
}
5847

0 commit comments

Comments
 (0)