mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
Implement multiple test configs.
This commit is contained in:
parent
32b0d12770
commit
c4f4366357
5 changed files with 43 additions and 14 deletions
|
@ -9,6 +9,8 @@ class TestConfig(val project: Project) {
|
||||||
val testIncludes = arrayListOf("**/*Test.class")
|
val testIncludes = arrayListOf("**/*Test.class")
|
||||||
val testExcludes = arrayListOf<String>()
|
val testExcludes = arrayListOf<String>()
|
||||||
|
|
||||||
|
var configName: String = ""
|
||||||
|
|
||||||
fun args(vararg arg: String) {
|
fun args(vararg arg: String) {
|
||||||
testArgs.addAll(arg)
|
testArgs.addAll(arg)
|
||||||
}
|
}
|
||||||
|
@ -35,7 +37,12 @@ class TestConfig(val project: Project) {
|
||||||
@Directive
|
@Directive
|
||||||
fun Project.test(init: TestConfig.() -> Unit) = let { project ->
|
fun Project.test(init: TestConfig.() -> Unit) = let { project ->
|
||||||
with(testConfigs) {
|
with(testConfigs) {
|
||||||
clear()
|
val tf = TestConfig(project).apply { init() }
|
||||||
add(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 = ... }")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,9 @@ import com.beust.kobalt.TaskResult
|
||||||
*/
|
*/
|
||||||
interface ITestRunnerContributor : IContributor, IProjectAffinity {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ open class Project(
|
||||||
*/
|
*/
|
||||||
val projectExtra = ProjectExtra(this)
|
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
|
// If one is specified by default, we only generate a BuildConfig, find a way to fix that
|
||||||
override var buildConfig : BuildConfig? = null // BuildConfig()
|
override var buildConfig : BuildConfig? = null // BuildConfig()
|
||||||
|
|
|
@ -16,8 +16,9 @@ abstract class GenericTestRunner : ITestRunnerContributor {
|
||||||
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, classpath: List<IClasspathDependency>)
|
override fun run(project: Project, context: KobaltContext, configName: String,
|
||||||
= TaskResult(runTests(project, classpath))
|
classpath: List<IClasspathDependency>)
|
||||||
|
= TaskResult(runTests(project, 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
|
||||||
|
@ -45,12 +46,14 @@ 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>) : Boolean {
|
fun runTests(project: Project, 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
|
||||||
|
|
||||||
project.testConfigs.forEach { testConfig ->
|
val testConfig = project.testConfigs.firstOrNull { it.configName == configName }
|
||||||
|
|
||||||
|
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 {
|
val allArgs = arrayListOf<String>().apply {
|
||||||
|
@ -79,6 +82,8 @@ abstract class GenericTestRunner : ITestRunnerContributor {
|
||||||
log(2, "Couldn't find any test classes")
|
log(2, "Couldn't find any test classes")
|
||||||
result = true
|
result = true
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw KobaltException("Couldn't find a test configuration named \"$configName\"")
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package com.beust.kobalt.internal
|
||||||
import com.beust.kobalt.IncrementalTaskInfo
|
import com.beust.kobalt.IncrementalTaskInfo
|
||||||
import com.beust.kobalt.KobaltException
|
import com.beust.kobalt.KobaltException
|
||||||
import com.beust.kobalt.TaskResult
|
import com.beust.kobalt.TaskResult
|
||||||
|
import com.beust.kobalt.TestConfig
|
||||||
import com.beust.kobalt.api.*
|
import com.beust.kobalt.api.*
|
||||||
import com.beust.kobalt.api.annotation.ExportedProjectProperty
|
import com.beust.kobalt.api.annotation.ExportedProjectProperty
|
||||||
import com.beust.kobalt.api.annotation.IncrementalTask
|
import com.beust.kobalt.api.annotation.IncrementalTask
|
||||||
|
@ -68,17 +69,32 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
||||||
taskContributor.addIncrementalVariantTasks(this, project, context, "compile",
|
taskContributor.addIncrementalVariantTasks(this, project, context, "compile",
|
||||||
runTask = { taskCompile(project) })
|
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 {
|
private fun taskTest(project: Project, configName: String): TaskResult {
|
||||||
lp(project, "Running tests")
|
lp(project, "Running tests: $configName")
|
||||||
|
|
||||||
val runContributor = ActorUtils.selectAffinityActor(project, context,
|
val runContributor = ActorUtils.selectAffinityActor(project, context,
|
||||||
context.pluginInfo.testRunnerContributors)
|
context.pluginInfo.testRunnerContributors)
|
||||||
if (runContributor != null && runContributor.affinity(project, context) > 0) {
|
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 {
|
} else {
|
||||||
log(1, "Couldn't find a test runner for project ${project.name}, did you specify a dependenciesTest{}?")
|
log(1, "Couldn't find a test runner for project ${project.name}, did you specify a dependenciesTest{}?")
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue