Skip to content
Permalink
Browse files

Added include / exclude tags to console launcher

  • Loading branch information...
sksamuel committed Jul 21, 2019
1 parent 58ad2b2 commit 10c36b0a4f1a572470b904f24b4d3b30be70fd6c
@@ -8,10 +8,12 @@ buildscript {
ext.dokkaVersion = '0.9.17'
repositories {
mavenCentral()
mavenLocal()
}

dependencies {
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"
// classpath "io.kotlintest:kotlintest-gradle-plugin:1.1.1-LOCAL"
}
}

@@ -22,9 +24,10 @@ plugins {
id 'maven-publish'
id 'signing'
id 'net.researchgate.release' version '2.8.0'
id "io.kotlintest" version "1.1.0"
}

// apply plugin: "io.kotlintest"

allprojects {

repositories {
@@ -26,12 +26,16 @@ interface TagExtension : ProjectLevelExtension {
fun tags(): Tags
}

/**
* This [TagExtension] includes and excludes tags using the system properties
* 'kotlintest.tags.include' and 'kotlintest.tags.exclude'
*/
object SystemPropertyTagExtension : TagExtension {

override fun tags(): Tags {

fun readTagsProperty(name: String): List<Tag> =
(System.getProperty(name) ?: "").split(',').filter { it.isNotBlank() }.map { StringTag(it.trim()) }
(System.getProperty(name) ?: "").split(',').filter { it.isNotBlank() }.map { StringTag(it.trim()) }

val includedTags = readTagsProperty("kotlintest.tags.include")
val excludedTags = readTagsProperty("kotlintest.tags.exclude")
@@ -55,4 +59,10 @@ object RuntimeTagExtension : TagExtension {
return Tags(included, excluded)
}

}
}

class SpecifiedTagsTagExtension(private val included: Set<Tag>, private val excluded: Set<Tag>) : TagExtension {
override fun tags(): Tags {
return Tags(included, excluded)
}
}
@@ -34,4 +34,4 @@ fun isActive(test: TestCase): Boolean {
val activeViaTags = Project.tags().isActive(test.config.tags + test.spec.tags())
val filtered = Project.testCaseFilters().map { it.filter(test.description) }.any { it == TestFilterResult.Exclude }
return focused || !hasFocused && enabledInConfig && activeViaTags && !disabledViaBang && !filtered
}
}
@@ -37,7 +37,7 @@ class KotlinTestUnit(val klass: KClass<out Spec>) : TestUnit {
}
}

val engine = TestEngine(listOf(klass), emptyList(), 1, listener)
val engine = TestEngine(listOf(klass), emptyList(), 1, emptySet(), emptySet(), listener)
engine.execute()
}
}
}
@@ -4,6 +4,7 @@ package io.kotlintest.runner.console

import io.kotlintest.Project
import io.kotlintest.Spec
import io.kotlintest.Tag
import io.kotlintest.runner.jvm.DiscoveryRequest
import io.kotlintest.runner.jvm.TestDiscovery
import io.kotlintest.runner.jvm.TestEngineListener
@@ -13,7 +14,7 @@ class KotlinTestConsoleRunner(private val writer: TestEngineListener) {

private val logger = LoggerFactory.getLogger(this.javaClass)

fun execute(specFQN: String?, test: String?) {
fun execute(specFQN: String?, test: String?, includeTags: Set<Tag>, excludeTags: Set<Tag>) {

val (specs, filter) = if (specFQN == null) {
val result = TestDiscovery.discover(DiscoveryRequest(emptyList()))
@@ -25,11 +26,13 @@ class KotlinTestConsoleRunner(private val writer: TestEngineListener) {
}

val runner = io.kotlintest.runner.jvm.TestEngine(
specs,
if (filter == null) emptyList() else listOf(filter),
Project.parallelism(),
writer
specs,
if (filter == null) emptyList() else listOf(filter),
Project.parallelism(),
includeTags,
excludeTags,
writer
)
runner.execute()
}
}
}
@@ -1,6 +1,8 @@
package io.kotlintest.runner.console

import com.github.ajalt.mordant.TermColors
import io.kotlintest.StringTag
import io.kotlintest.Tag
import net.sourceforge.argparse4j.ArgumentParsers
import kotlin.system.exitProcess

@@ -14,14 +16,20 @@ fun main(args: Array<String>) {
parser.addArgument("--source").help("Optional string describing how the launcher was invoked")
parser.addArgument("--slow-duration").help("Optional time in millis controlling when a test is marked as slow")
parser.addArgument("--very-slow-duration").help("Optional time in millis controlling when a test is marked as very slow")
// parser.addArgument("--max-test-duration").help("Optional time in millis controlling when the duration of a test should be marked as an error")
parser.addArgument("--include-tags").help("Optional string setting which tags to be included")
parser.addArgument("--exclude-tags").help("Optional string setting which tags to be excluded")
val ns = parser.parseArgs(args)

val writerClass: String? = ns.getString("writer")
val spec: String? = ns.getString("spec")
val test: String? = ns.getString("test")
val source: String? = ns.getString("source")
val includeTags: Set<Tag> = ns.getString("include-tags")?.split(',')?.map { StringTag(it) }?.toSet() ?: emptySet()
val excludeTags: Set<Tag> = ns.getString("exclude-tags")?.split(',')?.map { StringTag(it) }?.toSet() ?: emptySet()
val slowDuration: Int = ns.getString("slow-duration")?.toInt() ?: 1000
val verySlowDuration: Int = ns.getString("very-slow-duration")?.toInt() ?: 3000
// val maxTestDuration: Int = ns.getString("max-test-duration")?.toInt() ?: 0

val term = if (source == "kotlintest-gradle-plugin") TermColors(TermColors.Level.ANSI256) else TermColors()

@@ -32,7 +40,7 @@ fun main(args: Array<String>) {
}

val runner = KotlinTestConsoleRunner(writer)
runner.execute(spec, test)
runner.execute(spec, test, includeTags, excludeTags)

// there could be threads in the background that will stop the launcher shutting down
// for example if a test keeps a thread running
@@ -14,7 +14,14 @@ class KotlinTestRunner(private val testClass: Class<out Spec>) : Runner() {

override fun run(notifier: RunNotifier) {
val listener = JUnitTestRunnerListener(notifier)
val runner = TestEngine(listOf(testClass.kotlin), emptyList(), Project.parallelism(), listener)
val runner = TestEngine(
listOf(testClass.kotlin),
emptyList(),
Project.parallelism(),
emptySet(),
emptySet(),
listener
)
runner.execute()
}

@@ -39,7 +39,12 @@ class KotlinTestEngine : TestEngine {
logger.trace("JUnit execution request [configurationParameters=${request.configurationParameters}; rootTestDescriptor=${request.rootTestDescriptor}]")
val root = request.rootTestDescriptor as KotlinTestEngineDescriptor
val listener = IsolationTestEngineListener(JUnitTestRunnerListener(SynchronizedEngineExecutionListener(request.engineExecutionListener), root))
val runner = io.kotlintest.runner.jvm.TestEngine(root.classes, emptyList(), Project.parallelism(), listener)
val runner = io.kotlintest.runner.jvm.TestEngine(root.classes,
emptyList(),
Project.parallelism(),
emptySet(),
emptySet(),
listener)
runner.execute()
}

@@ -98,4 +103,4 @@ class KotlinTestEngine : TestEngine {
class KotlinTestEngineDescriptor(id: UniqueId, val classes: List<KClass<out Spec>>) : EngineDescriptor(id, "KotlinTest") {
override fun mayRegisterTests(): Boolean = true
}
}
}
@@ -4,7 +4,9 @@ import arrow.core.Try
import io.kotlintest.DoNotParallelize
import io.kotlintest.Project
import io.kotlintest.Spec
import io.kotlintest.Tag
import io.kotlintest.TestCaseFilter
import io.kotlintest.extensions.SpecifiedTagsTagExtension
import io.kotlintest.runner.jvm.internal.NamedThreadFactory
import io.kotlintest.runner.jvm.spec.SpecExecutor
import org.slf4j.LoggerFactory
@@ -14,10 +16,11 @@ import java.util.concurrent.TimeUnit
import kotlin.reflect.KClass
import kotlin.reflect.full.findAnnotation


class TestEngine(val classes: List<KClass<out Spec>>,
filters: List<TestCaseFilter>,
val parallelism: Int,
includedTags: Set<Tag>,
excludedTags: Set<Tag>,
val listener: TestEngineListener) {

private val logger = LoggerFactory.getLogger(this.javaClass)
@@ -29,6 +32,8 @@ class TestEngine(val classes: List<KClass<out Spec>>,

init {
Project.registerTestCaseFilter(filters)
if (includedTags.isNotEmpty() || excludedTags.isNotEmpty())
Project.registerExtension(SpecifiedTagsTagExtension(includedTags, excludedTags))
}

private fun afterAll() = Try { Project.afterAll() }
@@ -122,4 +127,4 @@ class TestEngine(val classes: List<KClass<out Spec>>,
}
}

fun KClass<*>.isDoNotParallelize(): Boolean = findAnnotation<DoNotParallelize>() != null
fun KClass<*>.isDoNotParallelize(): Boolean = findAnnotation<DoNotParallelize>() != null
@@ -3,9 +3,6 @@ buildscript {
mavenCentral()
mavenLocal()
}
dependencies {
// classpath "io.kotlintest:kotlintest-gradle-plugin:1.0.3-LOCAL"
}
}

plugins {
@@ -17,8 +14,6 @@ plugins {
id "io.kotlintest"
}

//apply plugin: "io.kotlintest"

repositories {
mavenCentral()
mavenLocal()

0 comments on commit 10c36b0

Please sign in to comment.
You can’t perform that action at this time.