Skip to content

Commit 981c8ec

Browse files
committed
Extract RunConsoleLauncher task
1 parent d8c3f9d commit 981c8ec

File tree

2 files changed

+99
-54
lines changed

2 files changed

+99
-54
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.junit.gradle.exec
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.file.ConfigurableFileCollection
5+
import org.gradle.api.file.DirectoryProperty
6+
import org.gradle.api.provider.ListProperty
7+
import org.gradle.api.provider.Property
8+
import org.gradle.api.tasks.Classpath
9+
import org.gradle.api.tasks.Input
10+
import org.gradle.api.tasks.Internal
11+
import org.gradle.api.tasks.OutputDirectory
12+
import org.gradle.api.tasks.SourceSetContainer
13+
import org.gradle.api.tasks.TaskAction
14+
import org.gradle.kotlin.dsl.get
15+
import org.gradle.kotlin.dsl.the
16+
import org.gradle.process.CommandLineArgumentProvider
17+
import org.gradle.process.ExecOperations
18+
import trackOperationSystemAsInput
19+
import java.io.ByteArrayOutputStream
20+
import javax.inject.Inject
21+
22+
abstract class RunConsoleLauncher @Inject constructor(private val execOperations: ExecOperations): DefaultTask() {
23+
24+
@get:Classpath
25+
abstract val runtimeClasspath: ConfigurableFileCollection
26+
27+
@get:Input
28+
abstract val args: ListProperty<String>
29+
30+
@get:OutputDirectory
31+
abstract val reportsDir: DirectoryProperty
32+
33+
@get:Internal
34+
abstract val debugging: Property<Boolean>
35+
36+
@get:Internal
37+
abstract val hideOutput: Property<Boolean>
38+
39+
init {
40+
runtimeClasspath.from(project.the<SourceSetContainer>()["test"].runtimeClasspath)
41+
reportsDir.convention(project.layout.buildDirectory.dir("test-results"))
42+
43+
debugging.convention(
44+
project.providers.gradleProperty("consoleLauncherTestDebug")
45+
.map { it != "false" }
46+
.orElse(false)
47+
)
48+
outputs.cacheIf { !debugging.get() }
49+
outputs.upToDateWhen { !debugging.get() }
50+
51+
hideOutput.convention(debugging.map { !it })
52+
53+
trackOperationSystemAsInput()
54+
}
55+
56+
@TaskAction
57+
fun execute() {
58+
val output = ByteArrayOutputStream()
59+
val result = execOperations.javaexec {
60+
classpath = runtimeClasspath
61+
mainClass.set("org.junit.platform.console.ConsoleLauncher")
62+
args("--scan-classpath")
63+
args("--config=junit.platform.reporting.open.xml.enabled=true")
64+
args(this@RunConsoleLauncher.args.get())
65+
argumentProviders += CommandLineArgumentProvider {
66+
listOf(
67+
"--reports-dir=${reportsDir.get()}",
68+
"--config=junit.platform.reporting.output.dir=${reportsDir.get()}"
69+
70+
)
71+
}
72+
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
73+
debug = debugging.get()
74+
if (hideOutput.get()) {
75+
standardOutput = output
76+
errorOutput = output
77+
}
78+
isIgnoreExitValue = true
79+
}
80+
if (result.exitValue != 0 && hideOutput.get()) {
81+
System.out.write(output.toByteArray())
82+
System.out.flush()
83+
}
84+
result.rethrowFailure().assertNormalExitValue()
85+
}
86+
}

documentation/documentation.gradle.kts

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask
22
import org.gradle.api.tasks.PathSensitivity.RELATIVE
33
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
44
import org.junit.gradle.exec.ClasspathSystemPropertyProvider
5+
import org.junit.gradle.exec.RunConsoleLauncher
56
import org.junit.gradle.javadoc.ModuleSpecificJavadocFileOption
67
import java.io.ByteArrayOutputStream
78
import java.nio.file.Files
@@ -114,63 +115,21 @@ require(externalModulesWithoutModularJavadoc.values.all { it.endsWith("/") }) {
114115

115116
tasks {
116117

117-
val consoleLauncherTest by registering {
118-
val runtimeClasspath = sourceSets["test"].runtimeClasspath
119-
inputs.files(runtimeClasspath).withNormalizer(ClasspathNormalizer::class)
120-
val reportsDir = file("$buildDir/test-results")
121-
outputs.dir(reportsDir)
122-
123-
val debugging = providers.gradleProperty("consoleLauncherTestDebug")
124-
.map { it != "false" }
125-
.orElse(false)
126-
outputs.cacheIf { !debugging.get() }
127-
outputs.upToDateWhen { !debugging.get() }
128-
129-
// Track OS as input so that tests are executed on all configured operating systems on CI
130-
trackOperationSystemAsInput()
131-
doFirst {
132-
val output = ByteArrayOutputStream()
133-
val result = javaexec {
134-
debug = project.findProperty("debug") == "true"
135-
classpath = runtimeClasspath
136-
mainClass.set("org.junit.platform.console.ConsoleLauncher")
137-
args("--scan-classpath")
138-
args("--config", "enableHttpServer=true")
139-
args("--include-classname", ".*Tests")
140-
args("--include-classname", ".*Demo")
141-
args("--exclude-tag", "exclude")
142-
args("--reports-dir", reportsDir)
143-
args("--config=junit.platform.reporting.output.dir=${reportsDir}")
144-
args("--config=junit.platform.reporting.open.xml.enabled=true")
145-
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
146-
debug = debugging.get()
147-
if (!debugging.get()) {
148-
standardOutput = output
149-
errorOutput = output
150-
}
151-
isIgnoreExitValue = true
152-
}
153-
if (result.exitValue != 0 && !debugging.get()) {
154-
System.out.write(output.toByteArray())
155-
System.out.flush()
156-
}
157-
result.rethrowFailure().assertNormalExitValue()
158-
}
118+
val consoleLauncherTest by registering(RunConsoleLauncher::class) {
119+
args.addAll("--config", "enableHttpServer=true")
120+
args.addAll("--include-classname", ".*Tests")
121+
args.addAll("--include-classname", ".*Demo")
122+
args.addAll("--exclude-tag", "exclude")
159123
}
160124

161-
register<JavaExec>("consoleLauncher") {
162-
val reportsDir = file("$buildDir/console-launcher")
163-
outputs.dir(reportsDir)
125+
register<RunConsoleLauncher>("consoleLauncher") {
126+
hideOutput.set(false)
127+
reportsDir.set(layout.buildDirectory.dir("console-launcher"))
164128
outputs.upToDateWhen { false }
165-
classpath = sourceSets["test"].runtimeClasspath
166-
mainClass.set("org.junit.platform.console.ConsoleLauncher")
167-
args("--scan-classpath")
168-
args("--config", "enableHttpServer=true")
169-
args("--include-classname", ".*Tests")
170-
args("--include-classname", ".*Demo")
171-
args("--exclude-tag", "exclude")
172-
args("--reports-dir", reportsDir)
173-
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
129+
args.addAll("--config", "enableHttpServer=true")
130+
args.addAll("--include-classname", ".*Tests")
131+
args.addAll("--include-classname", ".*Demo")
132+
args.addAll("--exclude-tag", "exclude")
174133
}
175134

176135
test {

0 commit comments

Comments
 (0)