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 import com.beust.kobalt.api.annotation.Directive
class TestConfig(val project: Project) { 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) { fun args(vararg arg: String) {
project.testArgs.addAll(arg) testArgs.addAll(arg)
} }
fun jvmArgs(vararg arg: String) { fun jvmArgs(vararg arg: String) {
project.testJvmArgs.addAll(arg) jvmArgs.addAll(arg)
} }
fun includes(vararg arg: String) { fun includes(vararg arg: String) {
project.testIncludes.apply { testIncludes.apply {
clear() clear()
addAll(arg) addAll(arg)
} }
} }
fun excludes(vararg arg: String) { fun excludes(vararg arg: String) {
project.testExcludes.apply { testExcludes.apply {
clear() clear()
addAll(arg) addAll(arg)
} }
@ -28,4 +33,4 @@ class TestConfig(val project: Project) {
} }
@Directive @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 package com.beust.kobalt.api
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.IProjectInfo import com.beust.kobalt.internal.IProjectInfo
import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.maven.dependency.MavenDependency
@ -23,14 +24,11 @@ open class Project(
@Directive open var packageName: String? = group, @Directive open var packageName: String? = group,
val projectInfo: IProjectInfo) : IBuildConfig { val projectInfo: IProjectInfo) : IBuildConfig {
val testConfigs = arrayListOf<TestConfig>()
override var buildConfig : BuildConfig? = null //BuildConfig() override var buildConfig : BuildConfig? = null //BuildConfig()
val testArgs = arrayListOf<String>()
val testJvmArgs = arrayListOf<String>()
val projectProperties = ProjectProperties() val projectProperties = ProjectProperties()
val testIncludes = arrayListOf("**/*Test.class")
val testExcludes = arrayListOf<String>()
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
return name == (other as Project).name return name == (other as Project).name

View file

@ -14,7 +14,7 @@ import java.util.*
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>) : 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, classpath: List<IClasspathDependency>)
= TaskResult(runTests(project, classpath)) = TaskResult(runTests(project, classpath))
@ -23,11 +23,11 @@ abstract class GenericTestRunner : ITestRunnerContributor {
if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY if (project.testDependencies.any { it.id.contains(dependencyName)}) IAffinity.DEFAULT_POSITIVE_AFFINITY
else 0 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 path = KFiles.joinDir(project.directory, project.buildDirectory, KFiles.TEST_CLASSES_DIR)
val result = IFileSpec.GlobSpec(toClassPaths(project.testIncludes)) val result = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
.toFiles(path, project.testExcludes.map { .toFiles(path, testConfig.testExcludes.map {
Glob(it) Glob(it)
}).map { }).map {
it.toString().replace("/", ".").replace("\\", ".").replace(".class", "") it.toString().replace("/", ".").replace("\\", ".").replace(".class", "")
@ -46,34 +46,39 @@ abstract class GenericTestRunner : ITestRunnerContributor {
fun runTests(project: Project, classpath: List<IClasspathDependency>) : Boolean { fun runTests(project: Project, classpath: List<IClasspathDependency>) : Boolean {
val jvm = JavaInfo.create(File(SystemProperties.javaBase)) val jvm = JavaInfo.create(File(SystemProperties.javaBase))
val java = jvm.javaExecutable val java = jvm.javaExecutable
val args = args(project, classpath) var result = false
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)
}
val pb = ProcessBuilder(allArgs) project.testConfigs.forEach { testConfig ->
pb.directory(File(project.directory)) val args = args(project, classpath, testConfig)
pb.inheritIO() if (args.size > 0) {
log(1, "Running tests with classpath size ${classpath.size}") val allArgs = arrayListOf<String>().apply {
log(2, "Launching " + allArgs.joinToString(" ")) add(java!!.absolutePath)
val process = pb.start() addAll(testConfig.jvmArgs)
val errorCode = process.waitFor() add("-classpath")
if (errorCode == 0) { add(classpath.map { it.jarFile.get().absolutePath }.joinToString(File.pathSeparator))
log(1, "All tests passed") 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 { } 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 package com.beust.kobalt.internal
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
@ -9,6 +10,7 @@ open public class JUnitRunner() : GenericTestRunner() {
override val dependencyName = "junit" 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 package com.beust.kobalt.internal
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
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
@ -14,25 +15,26 @@ public class TestNgRunner() : GenericTestRunner() {
fun defaultOutput(project: Project) = KFiles.joinDir(project.buildDirectory, "test-output") 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 var addOutput = true
project.testArgs.forEach { arg -> testConfig.testArgs.forEach { arg ->
if (arg == "-d") addOutput = false 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 // 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")) val testngXml = File(project.directory, KFiles.joinDir("src", "test", "resources", "testng.xml"))
if (testngXml.exists()) { if (testngXml.exists()) {
add(testngXml.absolutePath) add(testngXml.absolutePath)
} else { } else {
val testClasses = findTestClasses(project) val testClasses = findTestClasses(project, testConfig)
if (testClasses.size > 0) { if (testClasses.size > 0) {
if (addOutput) { if (addOutput) {
add("-d") add("-d")
add(defaultOutput(project)) add(defaultOutput(project))
} }
addAll(project.testArgs) addAll(testConfig.testArgs)
add("-testclass") add("-testclass")
add(testClasses.joinToString(",")) add(testClasses.joinToString(","))
@ -45,7 +47,7 @@ public class TestNgRunner() : GenericTestRunner() {
add("-d") add("-d")
add(defaultOutput(project)) add(defaultOutput(project))
} }
addAll(project.testArgs) addAll(testConfig.testArgs)
} }
} }
} }