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

Allow multiple test{} directives.

This commit is contained in:
Cedric Beust 2016-01-05 02:52:16 +04:00
parent 000f4765f4
commit 4c8ae7b346
5 changed files with 57 additions and 45 deletions

View file

@ -4,23 +4,28 @@ import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive
class TestConfig(val project: Project) {
val testArgs = arrayListOf<String>()
val jvmArgs = arrayListOf<String>()
val testIncludes = arrayListOf("**/*Test.class")
val testExcludes = arrayListOf<String>()
fun args(vararg arg: String) {
project.testArgs.addAll(arg)
testArgs.addAll(arg)
}
fun jvmArgs(vararg arg: String) {
project.testJvmArgs.addAll(arg)
jvmArgs.addAll(arg)
}
fun includes(vararg arg: String) {
project.testIncludes.apply {
testIncludes.apply {
clear()
addAll(arg)
}
}
fun excludes(vararg arg: String) {
project.testExcludes.apply {
testExcludes.apply {
clear()
addAll(arg)
}
@ -28,4 +33,4 @@ class TestConfig(val project: Project) {
}
@Directive
fun Project.test(init: TestConfig.() -> Unit) = TestConfig(this).apply { init() }
fun Project.test(init: TestConfig.() -> Unit) = this.testConfigs.add(TestConfig(this).apply { init() })

View file

@ -1,5 +1,6 @@
package com.beust.kobalt.api
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.IProjectInfo
import com.beust.kobalt.maven.dependency.MavenDependency
@ -23,14 +24,11 @@ open class Project(
@Directive open var packageName: String? = group,
val projectInfo: IProjectInfo) : IBuildConfig {
val testConfigs = arrayListOf<TestConfig>()
override var buildConfig : BuildConfig? = null //BuildConfig()
val testArgs = arrayListOf<String>()
val testJvmArgs = arrayListOf<String>()
val projectProperties = ProjectProperties()
val testIncludes = arrayListOf("**/*Test.class")
val testExcludes = arrayListOf<String>()
override fun equals(other: Any?): Boolean {
return name == (other as Project).name

View file

@ -14,7 +14,7 @@ import java.util.*
abstract class GenericTestRunner : ITestRunnerContributor {
abstract val dependencyName : String
abstract val mainClass: String
abstract fun args(project: Project, classpath: List<IClasspathDependency>) : List<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))
@ -23,11 +23,11 @@ abstract class GenericTestRunner : ITestRunnerContributor {
if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY
else 0
protected fun findTestClasses(project: Project): List<String> {
protected fun findTestClasses(project: Project, testConfig: TestConfig): List<String> {
val path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR)
val result = IFileSpec.GlobSpec(toClassPaths(project.testIncludes))
.toFiles(path, project.testExcludes.map {
val result = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
.toFiles(path, testConfig.testExcludes.map {
Glob(it)
}).map {
it.toString().replace("/", ".").replace("\\", ".").replace(".class", "")
@ -46,34 +46,39 @@ abstract class GenericTestRunner : ITestRunnerContributor {
fun runTests(project: Project, classpath: List<IClasspathDependency>) : Boolean {
val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val java = jvm.javaExecutable
val args = args(project, classpath)
if (args.size > 0) {
val allArgs = arrayListOf<String>().apply {
add(java!!.absolutePath)
addAll(project.testJvmArgs)
add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
add(mainClass)
addAll(args)
}
var result = false
val pb = ProcessBuilder(allArgs)
pb.directory(File(project.directory))
pb.inheritIO()
log(1, "Running tests with classpath size ${classpath.size}")
log(2, "Launching " + allArgs.joinToString(" "))
val process = pb.start()
val errorCode = process.waitFor()
if (errorCode == 0) {
log(1, "All tests passed")
project.testConfigs.forEach { testConfig ->
val args = args(project, classpath, testConfig)
if (args.size > 0) {
val allArgs = arrayListOf<String>().apply {
add(java!!.absolutePath)
addAll(testConfig.jvmArgs)
add("-classpath")
add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
add(mainClass)
addAll(args)
}
val pb = ProcessBuilder(allArgs)
pb.directory(File(project.directory))
pb.inheritIO()
log(1, "Running tests with classpath size ${classpath.size}")
log(2, "Launching " + allArgs.joinToString(" "))
val process = pb.start()
val errorCode = process.waitFor()
if (errorCode == 0) {
log(1, "All tests passed")
} else {
log(1, "Test failures")
}
result = result || errorCode == 0
} else {
log(1, "Test failures")
log(2, "Couldn't find any test classes")
result = true
}
return errorCode == 0
} else {
log(2, "Couldn't find any test classes")
return true
}
return result
}
}

View file

@ -1,5 +1,6 @@
package com.beust.kobalt.internal
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Project
@ -9,6 +10,7 @@ open public class JUnitRunner() : GenericTestRunner() {
override val dependencyName = "junit"
override fun args(project: Project, classpath: List<IClasspathDependency>) = findTestClasses(project)
override fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig)
= findTestClasses(project, testConfig)
}

View file

@ -1,5 +1,6 @@
package com.beust.kobalt.internal
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Project
import com.beust.kobalt.misc.KFiles
@ -14,25 +15,26 @@ public class TestNgRunner() : GenericTestRunner() {
fun defaultOutput(project: Project) = KFiles.joinDir(project.buildDirectory, "test-output")
override fun args(project: Project, classpath: List<IClasspathDependency>) = arrayListOf<String>().apply {
override fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig)
= arrayListOf<String>().apply {
var addOutput = true
project.testArgs.forEach { arg ->
testConfig.testArgs.forEach { arg ->
if (arg == "-d") addOutput = false
}
if (project.testArgs.size == 0) {
if (testConfig.testArgs.size == 0) {
// No arguments, so we'll do it ourselves. Either testng.xml or the list of classes
val testngXml = File(project.directory, KFiles.joinDir("src", "test", "resources", "testng.xml"))
if (testngXml.exists()) {
add(testngXml.absolutePath)
} else {
val testClasses = findTestClasses(project)
val testClasses = findTestClasses(project, testConfig)
if (testClasses.size > 0) {
if (addOutput) {
add("-d")
add(defaultOutput(project))
}
addAll(project.testArgs)
addAll(testConfig.testArgs)
add("-testclass")
add(testClasses.joinToString(","))
@ -45,7 +47,7 @@ public class TestNgRunner() : GenericTestRunner() {
add("-d")
add(defaultOutput(project))
}
addAll(project.testArgs)
addAll(testConfig.testArgs)
}
}
}