| 1 |
package junitbuild.exec |
| 2 |
|
| 3 |
import org.apache.tools.ant.types.Commandline |
| 4 |
import org.gradle.api.DefaultTask |
| 5 |
import org.gradle.api.file.ConfigurableFileCollection |
| 6 |
import org.gradle.api.plugins.JavaPluginExtension |
| 7 |
import org.gradle.api.provider.ListProperty |
| 8 |
import org.gradle.api.provider.Property |
| 9 |
import org.gradle.api.tasks.* |
| 10 |
import org.gradle.api.tasks.options.Option |
| 11 |
import org.gradle.jvm.toolchain.JavaLauncher |
| 12 |
import org.gradle.jvm.toolchain.JavaToolchainService |
| 13 |
import org.gradle.kotlin.dsl.get |
| 14 |
import org.gradle.kotlin.dsl.the |
| 15 |
import org.gradle.process.CommandLineArgumentProvider |
| 16 |
import org.gradle.process.ExecOperations |
| 17 |
import trackOperationSystemAsInput |
| 18 |
import java.io.ByteArrayOutputStream |
| 19 |
import java.util.* |
| 20 |
import javax.inject.Inject |
| 21 |
|
| 22 |
@CacheableTask |
| 23 |
abstract class RunConsoleLauncher @Inject constructor(private val execOperations: ExecOperations) : DefaultTask() { |
| 24 |
|
| 25 |
@get:Classpath |
| 26 |
abstract val runtimeClasspath: ConfigurableFileCollection |
| 27 |
|
| 28 |
@get:Input |
| 29 |
abstract val args: ListProperty<String> |
| 30 |
|
| 31 |
@get:Nested |
| 32 |
abstract val argumentProviders: ListProperty<CommandLineArgumentProvider> |
| 33 |
|
| 34 |
@get:Input |
| 35 |
abstract val commandLineArgs: ListProperty<String> |
| 36 |
|
| 37 |
@get:Nested |
| 38 |
abstract val javaLauncher: Property<JavaLauncher> |
| 39 |
|
| 40 |
@get:Internal |
| 41 |
abstract val debugging: Property<Boolean> |
| 42 |
|
| 43 |
@get:Internal |
| 44 |
abstract val hideOutput: Property<Boolean> |
| 45 |
|
| 46 |
init { |
| 47 |
runtimeClasspath.from(project.the<SourceSetContainer>()["test"].runtimeClasspath) |
| 48 |
javaLauncher.set(project.the<JavaToolchainService>().launcherFor(project.the<JavaPluginExtension>().toolchain)) |
| 49 |
|
| 50 |
debugging.convention(false) |
| 51 |
commandLineArgs.convention(emptyList()) |
| 52 |
outputs.cacheIf { !debugging.get() } |
| 53 |
outputs.upToDateWhen { !debugging.get() } |
| 54 |
|
| 55 |
hideOutput.convention(debugging.map { !it }) |
| 56 |
|
| 57 |
trackOperationSystemAsInput() |
| 58 |
} |
| 59 |
|
| 60 |
@TaskAction |
| 61 |
fun execute() { |
| 62 |
val output = ByteArrayOutputStream() |
| 63 |
val result = execOperations.javaexec { |
| 64 |
executable = javaLauncher.get().executablePath.asFile.absolutePath |
| 65 |
classpath = runtimeClasspath |
| 66 |
mainClass.set("org.junit.platform.console.ConsoleLauncher") |
| 67 |
args([email protected]()) |
| 68 |
args([email protected]()) |
| 69 |
argumentProviders.addAll([email protected]()) |
| 70 |
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager") |
| 71 |
debug = debugging.get() |
| 72 |
if (hideOutput.get()) { |
| 73 |
standardOutput = output |
| 74 |
errorOutput = output |
| 75 |
} |
| 76 |
isIgnoreExitValue = true |
| 77 |
} |
| 78 |
if (result.exitValue != 0 && hideOutput.get()) { |
| 79 |
System.out.write(output.toByteArray()) |
| 80 |
System.out.flush() |
| 81 |
} |
| 82 |
result.rethrowFailure().assertNormalExitValue() |
| 83 |
} |
| 84 |
|
| 85 |
@Suppress("unused") |
| 86 |
@Option(option = "args", description = "Additional command line arguments for the console launcher") |
| 87 |
fun setCliArgs(args: String) { |
| 88 |
commandLineArgs.set(Commandline.translateCommandline(args).toList()) |
| 89 |
} |
| 90 |
|
| 91 |
@Suppress("unused") |
| 92 |
@Option( |
| 93 |
option = "debug-jvm", |
| 94 |
description = "Enable debugging. The process is started suspended and listening on port 5005." |
| 95 |
) |
| 96 |
fun setDebug(enabled: Boolean) { |
| 97 |
debugging.set(enabled) |
| 98 |
} |
| 99 |
|
| 100 |
} |