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

Implement multiple test configs.

This commit is contained in:
Cedric Beust 2016-02-20 10:41:05 -08:00
parent 32b0d12770
commit c4f4366357
5 changed files with 43 additions and 14 deletions

View file

@ -9,6 +9,8 @@ class TestConfig(val project: Project) {
val testIncludes = arrayListOf("**/*Test.class")
val testExcludes = arrayListOf<String>()
var configName: String = ""
fun args(vararg arg: String) {
testArgs.addAll(arg)
}
@ -35,7 +37,12 @@ class TestConfig(val project: Project) {
@Directive
fun Project.test(init: TestConfig.() -> Unit) = let { project ->
with(testConfigs) {
clear()
add(TestConfig(project).apply { init() })
val tf = TestConfig(project).apply { init() }
if (! map { it.configName }.contains(tf.configName)) {
add(tf)
} else {
throw KobaltException("Test configuration \"${tf.configName}\" already exists, give it a different "
+ "name with test { configName = ... }")
}
}
}

View file

@ -7,8 +7,9 @@ import com.beust.kobalt.TaskResult
*/
interface ITestRunnerContributor : IContributor, IProjectAffinity {
/**
* Run the project.
* Run the tests. If [[configName]] is not empty, a specific test configuration is requested.
*/
fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>) : TaskResult
fun run(project: Project, context: KobaltContext, configName: String,
classpath: List<IClasspathDependency>) : TaskResult
}

View file

@ -40,7 +40,7 @@ open class Project(
*/
val projectExtra = ProjectExtra(this)
val testConfigs = arrayListOf(TestConfig(this))
val testConfigs = arrayListOf<TestConfig>()
// If one is specified by default, we only generate a BuildConfig, find a way to fix that
override var buildConfig : BuildConfig? = null // BuildConfig()

View file

@ -16,8 +16,9 @@ abstract class GenericTestRunner : ITestRunnerContributor {
abstract val mainClass: String
abstract fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig) : List<String>
override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>)
= TaskResult(runTests(project, classpath))
override fun run(project: Project, context: KobaltContext, configName: String,
classpath: List<IClasspathDependency>)
= TaskResult(runTests(project, classpath, configName))
override fun affinity(project: Project, context: KobaltContext) =
if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY
@ -45,12 +46,14 @@ abstract class GenericTestRunner : ITestRunnerContributor {
/**
* @return true if all the tests passed
*/
fun runTests(project: Project, classpath: List<IClasspathDependency>) : Boolean {
fun runTests(project: Project, classpath: List<IClasspathDependency>, configName: String) : Boolean {
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val java = jvm.javaExecutable
var result = false
project.testConfigs.forEach { testConfig ->
val testConfig = project.testConfigs.firstOrNull { it.configName == configName }
if (testConfig != null) {
val args = args(project, classpath, testConfig)
if (args.size > 0) {
val allArgs = arrayListOf<String>().apply {
@ -79,6 +82,8 @@ abstract class GenericTestRunner : ITestRunnerContributor {
log(2, "Couldn't find any test classes")
result = true
}
} else {
throw KobaltException("Couldn't find a test configuration named \"$configName\"")
}
return result
}

View file

@ -3,6 +3,7 @@ package com.beust.kobalt.internal
import com.beust.kobalt.IncrementalTaskInfo
import com.beust.kobalt.KobaltException
import com.beust.kobalt.TaskResult
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.ExportedProjectProperty
import com.beust.kobalt.api.annotation.IncrementalTask
@ -68,17 +69,32 @@ open class JvmCompilerPlugin @Inject constructor(
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
taskContributor.addIncrementalVariantTasks(this, project, context, "compile",
runTask = { taskCompile(project) })
//
// Add each test config as a test task. If none was specified, create a default one so that
// users don't have to specify a test{}
//
if (project.testConfigs.isEmpty()) {
project.testConfigs.add(TestConfig(project))
}
project.testConfigs.forEach { config ->
val taskName = if (config.configName.isEmpty()) "test" else "test" + config.configName
taskManager.addTask(this, project, taskName,
runAfter = listOf(JvmCompilerPlugin.TASK_COMPILE, JvmCompilerPlugin.TASK_COMPILE_TEST),
task = { taskTest(project, config.configName)} )
}
}
@Task(name = TASK_TEST, description = "Run the tests",
runAfter = arrayOf(JvmCompilerPlugin.TASK_COMPILE, JvmCompilerPlugin.TASK_COMPILE_TEST))
fun taskTest(project: Project): TaskResult {
lp(project, "Running tests")
private fun taskTest(project: Project, configName: String): TaskResult {
lp(project, "Running tests: $configName")
val runContributor = ActorUtils.selectAffinityActor(project, context,
context.pluginInfo.testRunnerContributors)
if (runContributor != null && runContributor.affinity(project, context) > 0) {
return runContributor.run(project, context, dependencyManager.testDependencies(project, context))
return runContributor.run(project, context, configName, dependencyManager.testDependencies(project,
context))
} else {
log(1, "Couldn't find a test runner for project ${project.name}, did you specify a dependenciesTest{}?")
return TaskResult()