Skip to content

Commit

Permalink
Extract RunConsoleLauncher task
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Oct 2, 2022
1 parent d8c3f9d commit 981c8ec
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.junit.gradle.exec

import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.the
import org.gradle.process.CommandLineArgumentProvider
import org.gradle.process.ExecOperations
import trackOperationSystemAsInput
import java.io.ByteArrayOutputStream
import javax.inject.Inject

abstract class RunConsoleLauncher @Inject constructor(private val execOperations: ExecOperations): DefaultTask() {

@get:Classpath
abstract val runtimeClasspath: ConfigurableFileCollection

@get:Input
abstract val args: ListProperty<String>

@get:OutputDirectory
abstract val reportsDir: DirectoryProperty

@get:Internal
abstract val debugging: Property<Boolean>

@get:Internal
abstract val hideOutput: Property<Boolean>

init {
runtimeClasspath.from(project.the<SourceSetContainer>()["test"].runtimeClasspath)
reportsDir.convention(project.layout.buildDirectory.dir("test-results"))

debugging.convention(
project.providers.gradleProperty("consoleLauncherTestDebug")
.map { it != "false" }
.orElse(false)
)
outputs.cacheIf { !debugging.get() }
outputs.upToDateWhen { !debugging.get() }

hideOutput.convention(debugging.map { !it })

trackOperationSystemAsInput()
}

@TaskAction
fun execute() {
val output = ByteArrayOutputStream()
val result = execOperations.javaexec {
classpath = runtimeClasspath
mainClass.set("org.junit.platform.console.ConsoleLauncher")
args("--scan-classpath")
args("--config=junit.platform.reporting.open.xml.enabled=true")
args(this@RunConsoleLauncher.args.get())
argumentProviders += CommandLineArgumentProvider {
listOf(
"--reports-dir=${reportsDir.get()}",
"--config=junit.platform.reporting.output.dir=${reportsDir.get()}"

)
}
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
debug = debugging.get()
if (hideOutput.get()) {
standardOutput = output
errorOutput = output
}
isIgnoreExitValue = true
}
if (result.exitValue != 0 && hideOutput.get()) {
System.out.write(output.toByteArray())
System.out.flush()
}
result.rethrowFailure().assertNormalExitValue()
}
}
67 changes: 13 additions & 54 deletions documentation/documentation.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask
import org.gradle.api.tasks.PathSensitivity.RELATIVE
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.junit.gradle.exec.ClasspathSystemPropertyProvider
import org.junit.gradle.exec.RunConsoleLauncher
import org.junit.gradle.javadoc.ModuleSpecificJavadocFileOption
import java.io.ByteArrayOutputStream
import java.nio.file.Files
Expand Down Expand Up @@ -114,63 +115,21 @@ require(externalModulesWithoutModularJavadoc.values.all { it.endsWith("/") }) {

tasks {

val consoleLauncherTest by registering {
val runtimeClasspath = sourceSets["test"].runtimeClasspath
inputs.files(runtimeClasspath).withNormalizer(ClasspathNormalizer::class)
val reportsDir = file("$buildDir/test-results")
outputs.dir(reportsDir)

val debugging = providers.gradleProperty("consoleLauncherTestDebug")
.map { it != "false" }
.orElse(false)
outputs.cacheIf { !debugging.get() }
outputs.upToDateWhen { !debugging.get() }

// Track OS as input so that tests are executed on all configured operating systems on CI
trackOperationSystemAsInput()
doFirst {
val output = ByteArrayOutputStream()
val result = javaexec {
debug = project.findProperty("debug") == "true"
classpath = runtimeClasspath
mainClass.set("org.junit.platform.console.ConsoleLauncher")
args("--scan-classpath")
args("--config", "enableHttpServer=true")
args("--include-classname", ".*Tests")
args("--include-classname", ".*Demo")
args("--exclude-tag", "exclude")
args("--reports-dir", reportsDir)
args("--config=junit.platform.reporting.output.dir=${reportsDir}")
args("--config=junit.platform.reporting.open.xml.enabled=true")
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
debug = debugging.get()
if (!debugging.get()) {
standardOutput = output
errorOutput = output
}
isIgnoreExitValue = true
}
if (result.exitValue != 0 && !debugging.get()) {
System.out.write(output.toByteArray())
System.out.flush()
}
result.rethrowFailure().assertNormalExitValue()
}
val consoleLauncherTest by registering(RunConsoleLauncher::class) {
args.addAll("--config", "enableHttpServer=true")
args.addAll("--include-classname", ".*Tests")
args.addAll("--include-classname", ".*Demo")
args.addAll("--exclude-tag", "exclude")
}

register<JavaExec>("consoleLauncher") {
val reportsDir = file("$buildDir/console-launcher")
outputs.dir(reportsDir)
register<RunConsoleLauncher>("consoleLauncher") {
hideOutput.set(false)
reportsDir.set(layout.buildDirectory.dir("console-launcher"))
outputs.upToDateWhen { false }
classpath = sourceSets["test"].runtimeClasspath
mainClass.set("org.junit.platform.console.ConsoleLauncher")
args("--scan-classpath")
args("--config", "enableHttpServer=true")
args("--include-classname", ".*Tests")
args("--include-classname", ".*Demo")
args("--exclude-tag", "exclude")
args("--reports-dir", reportsDir)
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
args.addAll("--config", "enableHttpServer=true")
args.addAll("--include-classname", ".*Tests")
args.addAll("--include-classname", ".*Demo")
args.addAll("--exclude-tag", "exclude")
}

test {
Expand Down

0 comments on commit 981c8ec

Please sign in to comment.