1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Added ITestJvmFlagContributor.

This commit is contained in:
Cedric Beust 2016-05-04 21:42:58 -08:00
parent 8e52d3ccb6
commit 3f60aeba54
6 changed files with 39 additions and 9 deletions

View file

@ -0,0 +1,8 @@
package com.beust.kobalt.api
/**
* Plug-ins that add flags to the JVM used to run tests should implement this interface.
*/
interface ITestJvmFlagContributor : IContributor {
fun testJvmFlagsFor(project: Project, context: KobaltContext, currentFlags: List<String>) : List<String>
}

View file

@ -11,14 +11,14 @@ import java.util.*
* Base class for testing frameworks that are invoked from a main class with arguments. Test runners can * Base class for testing frameworks that are invoked from a main class with arguments. Test runners can
* subclass this class and override mainClass, args and the name of the dependency that should trigger this runner. * subclass this class and override mainClass, args and the name of the dependency that should trigger this runner.
*/ */
abstract class GenericTestRunner : ITestRunnerContributor { abstract class GenericTestRunner: ITestRunnerContributor {
abstract val dependencyName : String abstract val dependencyName : String
abstract val mainClass: String abstract val mainClass: String
abstract fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig) : List<String> abstract fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig) : List<String>
override fun run(project: Project, context: KobaltContext, configName: String, override fun run(project: Project, context: KobaltContext, configName: String,
classpath: List<IClasspathDependency>) classpath: List<IClasspathDependency>)
= TaskResult(runTests(project, classpath, configName)) = TaskResult(runTests(project, context, classpath, configName))
override fun affinity(project: Project, context: KobaltContext) = override fun affinity(project: Project, context: KobaltContext) =
if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY
@ -46,7 +46,8 @@ abstract class GenericTestRunner : ITestRunnerContributor {
/** /**
* @return true if all the tests passed * @return true if all the tests passed
*/ */
fun runTests(project: Project, classpath: List<IClasspathDependency>, configName: String) : Boolean { fun runTests(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
configName: String) : Boolean {
val jvm = JavaInfo.create(File(SystemProperties.javaBase)) val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val java = jvm.javaExecutable val java = jvm.javaExecutable
var result = false var result = false
@ -56,12 +57,30 @@ abstract class GenericTestRunner : ITestRunnerContributor {
if (testConfig != null) { if (testConfig != null) {
val args = args(project, classpath, testConfig) val args = args(project, classpath, testConfig)
if (args.size > 0) { if (args.size > 0) {
val allArgs = arrayListOf<String>().apply {
add(java!!.absolutePath) // Default JVM args
val jvmFlags = arrayListOf<String>().apply {
addAll(testConfig.jvmArgs) addAll(testConfig.jvmArgs)
add("-classpath") add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator)) add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
add(mainClass) add(mainClass)
}
// JVM args from the contributors
val pluginInfo = Kobalt.INJECTOR.getInstance(PluginInfo::class.java)
val flagsFromContributors = pluginInfo.testJvmFlagContributors.flatMap {
it.testJvmFlagsFor(project, context, jvmFlags)
}
if (flagsFromContributors.any()) {
log(2, "Adding JVM flags from contributors: " + flagsFromContributors)
}
val allArgs = arrayListOf<String>().apply {
add(java!!.absolutePath)
addAll(flagsFromContributors)
addAll(jvmFlags)
addAll(args) addAll(args)
} }

View file

@ -4,7 +4,7 @@ import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.IClasspathDependency import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
open public class JUnitRunner() : GenericTestRunner() { open class JUnitRunner() : GenericTestRunner() {
override val mainClass = "org.junit.runner.JUnitCore" override val mainClass = "org.junit.runner.JUnitCore"

View file

@ -79,6 +79,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
// Not documented yet // Not documented yet
val buildConfigContributors = arrayListOf<IBuildConfigContributor>() val buildConfigContributors = arrayListOf<IBuildConfigContributor>()
val mavenIdInterceptors = arrayListOf<IMavenIdInterceptor>() val mavenIdInterceptors = arrayListOf<IMavenIdInterceptor>()
val testJvmFlagContributors = arrayListOf<ITestJvmFlagContributor>()
companion object { companion object {
/** /**
@ -164,6 +165,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
if (this is IIncrementalAssemblyContributor) incrementalAssemblyContributors.add(this) if (this is IIncrementalAssemblyContributor) incrementalAssemblyContributors.add(this)
// Not documented yet // Not documented yet
if (this is ITestJvmFlagContributor) testJvmFlagContributors.add(this)
} }
} }
} }
@ -176,7 +178,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
compilerContributors, docContributors, sourceDirContributors, compilerContributors, docContributors, sourceDirContributors,
testSourceDirContributors, buildConfigFieldContributors, testSourceDirContributors, buildConfigFieldContributors,
taskContributors, assemblyContributors, taskContributors, assemblyContributors,
incrementalAssemblyContributors incrementalAssemblyContributors, testJvmFlagContributors
).forEach { ).forEach {
it.forEach { it.forEach {
it.cleanUpActors() it.cleanUpActors()
@ -212,6 +214,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
buildConfigContributors.addAll(pluginInfo.buildConfigContributors) buildConfigContributors.addAll(pluginInfo.buildConfigContributors)
assemblyContributors.addAll(pluginInfo.assemblyContributors) assemblyContributors.addAll(pluginInfo.assemblyContributors)
incrementalAssemblyContributors.addAll(pluginInfo.incrementalAssemblyContributors) incrementalAssemblyContributors.addAll(pluginInfo.incrementalAssemblyContributors)
testJvmFlagContributors.addAll(pluginInfo.testJvmFlagContributors)
} }
} }

View file

@ -4,7 +4,7 @@ package com.beust.kobalt.internal
* SpekRunner triggers if it finds a dependency on org.jetbrains.spek but other than that, it just * SpekRunner triggers if it finds a dependency on org.jetbrains.spek but other than that, it just
* uses the regular JUnitRunner. * uses the regular JUnitRunner.
*/ */
public class SpekRunner() : JUnitRunner() { class SpekRunner : JUnitRunner() {
override val dependencyName = "org.jetbrains.spek" override val dependencyName = "org.jetbrains.spek"
} }

View file

@ -7,7 +7,7 @@ import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import java.io.File import java.io.File
public class TestNgRunner() : GenericTestRunner() { class TestNgRunner : GenericTestRunner() {
override val mainClass = "org.testng.TestNG" override val mainClass = "org.testng.TestNG"