diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/TestDirective.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/TestDirective.kt index ac7eed22..2f7f2956 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/TestDirective.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/TestDirective.kt @@ -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() + val jvmArgs = arrayListOf() + val testIncludes = arrayListOf("**/*Test.class") + val testExcludes = arrayListOf() + 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() }) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt index 48f6e871..3c218320 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -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() + override var buildConfig : BuildConfig? = null //BuildConfig() - val testArgs = arrayListOf() - val testJvmArgs = arrayListOf() - val projectProperties = ProjectProperties() - val testIncludes = arrayListOf("**/*Test.class") - val testExcludes = arrayListOf() override fun equals(other: Any?): Boolean { return name == (other as Project).name diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt index a8d84692..0df61b78 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/GenericRunner.kt @@ -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) : List + abstract fun args(project: Project, classpath: List, testConfig: TestConfig) : List override fun run(project: Project, context: KobaltContext, classpath: List) = 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 { + protected fun findTestClasses(project: Project, testConfig: TestConfig): List { 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) : Boolean { val jvm = JavaInfo.create(File(SystemProperties.javaBase)) val java = jvm.javaExecutable - val args = args(project, classpath) - if (args.size > 0) { - val allArgs = arrayListOf().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().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 } } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt index 940cf8e0..84381862 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JUnitRunner.kt @@ -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) = findTestClasses(project) + override fun args(project: Project, classpath: List, testConfig: TestConfig) + = findTestClasses(project, testConfig) } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt index 53a953f1..6b9c1b0b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt @@ -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) = arrayListOf().apply { + override fun args(project: Project, classpath: List, testConfig: TestConfig) + = arrayListOf().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) } } }