mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 16:28:12 -07:00
Fix the test class finding bug.
Fixes https://github.com/cbeust/kobalt/issues/254
This commit is contained in:
parent
c6561d92eb
commit
4d23c1236b
4 changed files with 55 additions and 53 deletions
|
@ -2,12 +2,10 @@ package com.beust.kobalt.internal
|
||||||
|
|
||||||
import com.beust.kobalt.*
|
import com.beust.kobalt.*
|
||||||
import com.beust.kobalt.api.*
|
import com.beust.kobalt.api.*
|
||||||
import com.beust.kobalt.maven.DependencyManager
|
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
import com.google.common.annotations.VisibleForTesting
|
import com.google.common.annotations.VisibleForTesting
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.URLClassLoader
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -18,7 +16,8 @@ abstract class GenericTestRunner: ITestRunnerContributor {
|
||||||
abstract val dependencyName : String
|
abstract val dependencyName : String
|
||||||
abstract val mainClass: String
|
abstract val mainClass: String
|
||||||
abstract val annotationPackage: String
|
abstract val annotationPackage: String
|
||||||
abstract fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig) : List<String>
|
abstract fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
|
||||||
|
testConfig: TestConfig) : List<String>
|
||||||
|
|
||||||
override fun run(project: Project, context: KobaltContext, configName: String,
|
override fun run(project: Project, context: KobaltContext, configName: String,
|
||||||
classpath: List<IClasspathDependency>)
|
classpath: List<IClasspathDependency>)
|
||||||
|
@ -28,28 +27,28 @@ 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, testConfig: TestConfig): List<String> {
|
protected fun findTestClasses(project: Project, context: KobaltContext, testConfig: TestConfig): List<String> {
|
||||||
val testClassDir = KFiles.joinDir(project.buildDirectory, KFiles.TEST_CLASSES_DIR)
|
val testClassDir = KFiles.joinDir(project.buildDirectory, KFiles.TEST_CLASSES_DIR)
|
||||||
val path = testClassDir.apply {
|
val path = testClassDir.apply {
|
||||||
File(this).mkdirs()
|
File(this).mkdirs()
|
||||||
}
|
}
|
||||||
|
|
||||||
val dependencyManager = Kobalt.INJECTOR.getInstance(DependencyManager::class.java)
|
val testClasses = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
|
||||||
val allDeps = arrayListOf<IClasspathDependency>().apply {
|
|
||||||
addAll(project.testDependencies)
|
|
||||||
project.dependencies?.let {
|
|
||||||
addAll(it.dependencies)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val testClasspath = dependencyManager.transitiveClosure(allDeps)
|
|
||||||
val result = IFileSpec.GlobSpec(toClassPaths(testConfig.testIncludes))
|
|
||||||
.toFiles(project.directory, path, testConfig.testExcludes.map {
|
.toFiles(project.directory, path, testConfig.testExcludes.map {
|
||||||
Glob(it)
|
Glob(it)
|
||||||
}).map {
|
}).map {
|
||||||
Pair(it, it.toString().replace("/", ".").replace("\\", ".").replace(".class", ""))
|
File(KFiles.joinDir(project.directory, testClassDir, it.path))
|
||||||
}.filter {
|
|
||||||
acceptClass(it.first, it.second, testClasspath, File(testClassDir))
|
|
||||||
}
|
}
|
||||||
|
val result = testClasses.map {
|
||||||
|
val prefix = KFiles.joinDir(project.directory, testClassDir)
|
||||||
|
val className = it.toString().substring(prefix.length + 1)
|
||||||
|
.replace("/", ".").replace("\\", ".").replace(".class", "")
|
||||||
|
Pair(it, className)
|
||||||
|
}
|
||||||
|
// .filter {
|
||||||
|
// val result = acceptClass(it.first, it.second, testClasspath, File(testClassDir))
|
||||||
|
// result
|
||||||
|
// }
|
||||||
|
|
||||||
log(2, "Found ${result.size} test classes")
|
log(2, "Found ${result.size} test classes")
|
||||||
return result.map { it.second }
|
return result.map { it.second }
|
||||||
|
@ -59,30 +58,31 @@ abstract class GenericTestRunner: ITestRunnerContributor {
|
||||||
* Accept the given class if it contains an annotation of the current test runner's package. Annotations
|
* Accept the given class if it contains an annotation of the current test runner's package. Annotations
|
||||||
* are looked up on both the classes and methods.
|
* are looked up on both the classes and methods.
|
||||||
*/
|
*/
|
||||||
private fun acceptClass(cf: File, className: String, testClasspath: List<IClasspathDependency>,
|
// private fun acceptClass(cf: File, className: String, testClasspath: List<IClasspathDependency>,
|
||||||
testClassDir: File): Boolean {
|
// testClassDir: File): Boolean {
|
||||||
val cp = (testClasspath.map { it.jarFile.get() } + listOf(testClassDir)).map { it.toURI().toURL() }
|
// val cp = (testClasspath.map { it.jarFile.get() } + listOf(testClassDir)).map { it.toURI().toURL() }
|
||||||
try {
|
// try {
|
||||||
val cls = URLClassLoader(cp.toTypedArray()).loadClass(className)
|
// val cls = URLClassLoader(cp.toTypedArray()).loadClass(className)
|
||||||
val ann = cls.annotations.filter {
|
// val ann = cls.annotations.filter {
|
||||||
val qn = it.annotationClass.qualifiedName
|
// val qn = it.annotationClass.qualifiedName
|
||||||
qn != null && qn.contains(annotationPackage)
|
// qn != null && qn.contains(annotationPackage)
|
||||||
}
|
// }
|
||||||
if (ann.any()) {
|
// if (ann.any()) {
|
||||||
return true
|
// return true
|
||||||
} else {
|
// } else {
|
||||||
val ann2 = cls.declaredMethods.flatMap { it.declaredAnnotations.toList() }.filter { it.toString()
|
// val ann2 = cls.declaredMethods.flatMap { it.declaredAnnotations.toList() }.filter { it.toString()
|
||||||
.contains(annotationPackage)}
|
// .contains(annotationPackage)}
|
||||||
if (ann2.any()) {
|
// if (ann2.any()) {
|
||||||
val a0 = ann2[0]
|
// val a0 = ann2[0]
|
||||||
return true
|
// return true
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
} catch(ex: Throwable) {
|
// } catch(ex: Throwable) {
|
||||||
return false
|
// println("Exception: " + ex.message)
|
||||||
}
|
// return false
|
||||||
return false
|
// }
|
||||||
}
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
private fun toClassPaths(paths: List<String>): ArrayList<String> =
|
private fun toClassPaths(paths: List<String>): ArrayList<String> =
|
||||||
paths.map { if (it.endsWith("class")) it else it + "class" }.toCollection(ArrayList())
|
paths.map { if (it.endsWith("class")) it else it + "class" }.toCollection(ArrayList())
|
||||||
|
@ -97,7 +97,7 @@ abstract class GenericTestRunner: ITestRunnerContributor {
|
||||||
val testConfig = project.testConfigs.firstOrNull { it.name == configName }
|
val testConfig = project.testConfigs.firstOrNull { it.name == configName }
|
||||||
|
|
||||||
if (testConfig != null) {
|
if (testConfig != null) {
|
||||||
val args = args(project, classpath, testConfig)
|
val args = args(project, context, classpath, testConfig)
|
||||||
if (args.size > 0) {
|
if (args.size > 0) {
|
||||||
|
|
||||||
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable
|
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.beust.kobalt.internal
|
||||||
|
|
||||||
import com.beust.kobalt.TestConfig
|
import com.beust.kobalt.TestConfig
|
||||||
import com.beust.kobalt.api.IClasspathDependency
|
import com.beust.kobalt.api.IClasspathDependency
|
||||||
|
import com.beust.kobalt.api.KobaltContext
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
|
|
||||||
open class JUnitRunner() : GenericTestRunner() {
|
open class JUnitRunner() : GenericTestRunner() {
|
||||||
|
@ -12,7 +13,7 @@ open class JUnitRunner() : GenericTestRunner() {
|
||||||
|
|
||||||
override val dependencyName = "junit"
|
override val dependencyName = "junit"
|
||||||
|
|
||||||
override fun args(project: Project, classpath: List<IClasspathDependency>, testConfig: TestConfig)
|
override fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
|
||||||
= findTestClasses(project, testConfig)
|
testConfig: TestConfig) = findTestClasses(project, context, testConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -92,8 +92,8 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
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, configName, dependencyManager.testDependencies(project,
|
return runContributor.run(project, context, configName,
|
||||||
context))
|
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()
|
||||||
|
@ -125,8 +125,8 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sourceDirectories(project: Project, context: KobaltContext)
|
private fun sourceDirectories(project: Project, context: KobaltContext, isTest: Boolean)
|
||||||
= context.variant.sourceDirectories(project, context, SourceSet.of(isTest = false))
|
= context.variant.sourceDirectories(project, context, SourceSet.of(isTest))
|
||||||
|
|
||||||
@IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project", group = GROUP_BUILD,
|
@IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project", group = GROUP_BUILD,
|
||||||
runAfter = arrayOf(TASK_CLEAN))
|
runAfter = arrayOf(TASK_CLEAN))
|
||||||
|
@ -181,7 +181,7 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
var done = false
|
var done = false
|
||||||
allCompilersSorted.doWhile({ ! done }) { compiler ->
|
allCompilersSorted.doWhile({ ! done }) { compiler ->
|
||||||
val compilerResults = compilerUtils.invokeCompiler(project, context, compiler,
|
val compilerResults = compilerUtils.invokeCompiler(project, context, compiler,
|
||||||
sourceDirectories(project, context), isTest)
|
sourceDirectories(project, context, isTest), isTest)
|
||||||
results.addAll(compilerResults.successResults)
|
results.addAll(compilerResults.successResults)
|
||||||
if (failedResult == null) failedResult = compilerResults.failedResult
|
if (failedResult == null) failedResult = compilerResults.failedResult
|
||||||
compilerResults.failedResult?.let { failedResult ->
|
compilerResults.failedResult?.let { failedResult ->
|
||||||
|
@ -227,7 +227,7 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
it.compilersFor(project, context).forEach { compiler ->
|
it.compilersFor(project, context).forEach { compiler ->
|
||||||
result = docGenerator.generateDoc(project, context,
|
result = docGenerator.generateDoc(project, context,
|
||||||
compilerUtils.createCompilerActionInfo(project, context, compiler,
|
compilerUtils.createCompilerActionInfo(project, context, compiler,
|
||||||
isTest = false, sourceDirectories = sourceDirectories(project, context),
|
isTest = false, sourceDirectories = sourceDirectories(project, context, false),
|
||||||
sourceSuffixes = compiler.sourceSuffixes))
|
sourceSuffixes = compiler.sourceSuffixes))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,7 +241,7 @@ open class JvmCompilerPlugin @Inject constructor(
|
||||||
// ISourceDirectoryContributor
|
// ISourceDirectoryContributor
|
||||||
override fun sourceDirectoriesFor(project: Project, context: KobaltContext)
|
override fun sourceDirectoriesFor(project: Project, context: KobaltContext)
|
||||||
= if (accept(project)) {
|
= if (accept(project)) {
|
||||||
sourceDirectories(project, context)
|
sourceDirectories(project, context, isTest = false)
|
||||||
} else {
|
} else {
|
||||||
arrayListOf()
|
arrayListOf()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.beust.kobalt.internal
|
||||||
|
|
||||||
import com.beust.kobalt.TestConfig
|
import com.beust.kobalt.TestConfig
|
||||||
import com.beust.kobalt.api.IClasspathDependency
|
import com.beust.kobalt.api.IClasspathDependency
|
||||||
|
import com.beust.kobalt.api.KobaltContext
|
||||||
import com.beust.kobalt.api.Project
|
import com.beust.kobalt.api.Project
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.warn
|
import com.beust.kobalt.misc.warn
|
||||||
|
@ -17,8 +18,8 @@ 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>, testConfig: TestConfig)
|
override fun args(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>,
|
||||||
= arrayListOf<String>().apply {
|
testConfig: TestConfig) = arrayListOf<String>().apply {
|
||||||
var addOutput = true
|
var addOutput = true
|
||||||
testConfig.testArgs.forEach { arg ->
|
testConfig.testArgs.forEach { arg ->
|
||||||
if (arg == "-d") addOutput = false
|
if (arg == "-d") addOutput = false
|
||||||
|
@ -30,7 +31,7 @@ class TestNgRunner : GenericTestRunner() {
|
||||||
if (testngXml.exists()) {
|
if (testngXml.exists()) {
|
||||||
add(testngXml.absolutePath)
|
add(testngXml.absolutePath)
|
||||||
} else {
|
} else {
|
||||||
val testClasses = findTestClasses(project, testConfig)
|
val testClasses = findTestClasses(project, context, testConfig)
|
||||||
if (testClasses.size > 0) {
|
if (testClasses.size > 0) {
|
||||||
if (addOutput) {
|
if (addOutput) {
|
||||||
add("-d")
|
add("-d")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue