Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 2301c43

Browse files
committed
Added --tags support to the plugin via extension properties and cli
1 parent 7ef69f3 commit 2301c43

File tree

9 files changed

+122
-13
lines changed

9 files changed

+122
-13
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[*]
2+
charset = utf-8
3+
end_of_line = lf
4+
indent_style = space
5+
indent_size = 3
6+
max_line_length = 120
7+
trim_trailing_whitespace = true
8+
ij_kotlin_enum_constants_wrap = on_every_item
9+
ij_any_space_after_comma = true
10+
11+
[*.yml]
12+
indent_size = 3
13+
14+
[*.gradle]
15+
indent_size = 3

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# BlueJ files
1313
*.ctxt
1414

15+
build/
16+
1517
# Mobile Tools for Java (J2ME)
1618
.mtj.tmp/
1719

build.gradle.kts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ dependencies {
3333
compileOnly("io.kotest:kotest-framework-api-jvm:4.4.3")
3434
implementation("io.kotest:kotest-framework-engine-jvm:4.4.3")
3535
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
36+
37+
testImplementation(Libs.Kotest.assertions)
38+
testImplementation(Libs.Kotest.junit5)
39+
}
40+
41+
tasks.named<Test>("test") {
42+
useJUnitPlatform()
43+
filter {
44+
isFailOnNoMatchingTests = false
45+
}
46+
testLogging {
47+
showExceptions = true
48+
showStandardStreams = true
49+
events = setOf(
50+
org.gradle.api.tasks.testing.logging.TestLogEvent.FAILED,
51+
org.gradle.api.tasks.testing.logging.TestLogEvent.PASSED
52+
)
53+
exceptionFormat = org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
54+
}
3655
}
3756

3857
if (JavaVersion.current() != JavaVersion.VERSION_1_8) {

buildSrc/src/main/kotlin/Libs.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ object Libs {
22

33
const val KotlinVersion = "1.4.32"
44
const val GradlePluginPublishVersion = "0.14.0"
5+
6+
object Kotest {
7+
private const val version = "4.4.3"
8+
const val shared = "io.kotest:kotest-assertions-shared:$version"
9+
const val assertions = "io.kotest:kotest-assertions-core:$version"
10+
const val junit5 = "io.kotest:kotest-runner-junit5:$version"
11+
}
12+
513
}

src/main/kotlin/io/kotest/gradle/Kotest.kt

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.gradle.api.internal.file.FileResolver
88
import org.gradle.api.internal.tasks.testing.DefaultTestSuiteDescriptor
99
import org.gradle.api.internal.tasks.testing.results.DefaultTestResult
1010
import org.gradle.api.tasks.TaskAction
11+
import org.gradle.api.tasks.options.Option
1112
import org.gradle.api.tasks.testing.Test
1213
import org.gradle.api.tasks.testing.TestListener
1314
import org.gradle.api.tasks.testing.TestOutputListener
@@ -31,6 +32,7 @@ open class Kotest @Inject constructor(
3132
private const val IntellijTestListenerClassName = "IJTestEventLogger"
3233
private const val ReporterArg = "--reporter"
3334
private const val TermArg = "--termcolor"
35+
private const val TagsArg = "--tags"
3436
private const val TeamCityReporter = "teamcity"
3537
private const val TaycanReporter = "io.kotest.engine.reporter.TaycanConsoleReporter"
3638
private const val PlainColours = "ansi16"
@@ -40,6 +42,14 @@ open class Kotest @Inject constructor(
4042
private val listeners = mutableListOf<TestListener>()
4143
private val outputListeners = mutableListOf<TestOutputListener>()
4244

45+
private var tags: String? = null
46+
47+
// gradle will call this if --tags was specified on the command line
48+
@Option(option = "tags", description = "Set tag expression to include or exclude tests")
49+
fun setTags(tags: String) {
50+
this.tags = tags
51+
}
52+
4353
// intellij will call this to register its listeners for the test event run window
4454
override fun addTestListener(listener: TestListener) {
4555
listeners.add(listener)
@@ -49,14 +59,29 @@ open class Kotest @Inject constructor(
4959
outputListeners.add(listener)
5060
}
5161

62+
/**
63+
* Returns args to be used for the tag expression.
64+
*
65+
* If --tags was passed as a command line arg, then that takes precedence over the value
66+
* set in the gradle build.
67+
*
68+
* Returns empty list if no tag expression was specified.
69+
*/
70+
private fun tagArgs(): List<String> {
71+
tags?.let { return listOf(TagsArg, it) }
72+
project.kotest()?.tags?.orNull?.let { return listOf(TagsArg, it) }
73+
return emptyList()
74+
}
75+
5276
// -- reporter was added in 4.2.1
5377
private fun args() = when {
54-
isIntellij() -> listOf(ReporterArg, TeamCityReporter, TermArg, PlainColours)
55-
else -> listOf(ReporterArg, TaycanReporter, TermArg, TrueColours)
78+
isIntellij() -> listOf(ReporterArg, TeamCityReporter, TermArg, PlainColours) + tagArgs()
79+
else -> listOf(ReporterArg, TaycanReporter, TermArg, TrueColours) + tagArgs()
5680
}
5781

5882
private fun exec(classpath: FileCollection): JavaExecAction {
59-
val exec = DefaultExecActionFactory.of(fileResolver, fileCollectionFactory, executorFactory, null).newJavaExecAction()
83+
val exec =
84+
DefaultExecActionFactory.of(fileResolver, fileCollectionFactory, executorFactory, null).newJavaExecAction()
6085
copyTo(exec)
6186

6287
exec.main = "io.kotest.engine.launcher.MainKt"
@@ -80,9 +105,6 @@ open class Kotest @Inject constructor(
80105
return listeners.map { it::class.java.name }.any { it.contains(IntellijTestListenerClassName) }
81106
}
82107

83-
fun setTag(expression: String) {
84-
}
85-
86108
private fun rerouteTeamCityListener(exec: JavaExecAction) {
87109
val input = PipedInputStream()
88110
exec.standardOutput = PipedOutputStream(input)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.kotest.gradle
2+
3+
import org.gradle.api.Project
4+
import org.gradle.api.provider.Property
5+
import javax.inject.Inject
6+
7+
abstract class KotestExtension @Inject constructor(project: Project) {
8+
9+
private val objects = project.objects
10+
11+
val tags: Property<String> = objects.property(String::class.java)
12+
}

src/main/kotlin/io/kotest/gradle/KotestOptions.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/main/kotlin/io/kotest/gradle/KotestPlugin.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import org.gradle.api.plugins.JavaPlugin
77
open class KotestPlugin : Plugin<Project> {
88

99
override fun apply(project: Project) {
10+
11+
val extension = project.extensions.create(
12+
"kotest",
13+
KotestExtension::class.java,
14+
project
15+
)
16+
1017
// project.plugins.withId("org.jetbrains.kotlin.multiplatform") {
1118
// project.mppTestTargets().forEach { (target, files) ->
1219
// applyPlugin(
@@ -49,3 +56,10 @@ open class KotestPlugin : Plugin<Project> {
4956
}
5057
}
5158

59+
internal fun Project.kotest(): KotestExtension? {
60+
return when (val ext = extensions.findByName("kotest")) {
61+
is KotestExtension -> ext
62+
null -> null
63+
else -> throw IllegalStateException("kotest is not of the correct type")
64+
}
65+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.kotest.gradle
2+
3+
import io.kotest.core.spec.style.WordSpec
4+
import io.kotest.matchers.shouldNotBe
5+
import org.gradle.testfixtures.ProjectBuilder
6+
7+
class KotestPluginTest : WordSpec({
8+
9+
"Using the Plugin ID" should {
10+
"Apply the Plugin" {
11+
val project = ProjectBuilder.builder().build()
12+
project.pluginManager.apply("io.kotest")
13+
14+
project.plugins.getPlugin(KotestPlugin::class.java) shouldNotBe null
15+
}
16+
"Register the 'kotest' extension" {
17+
val project = ProjectBuilder.builder().build()
18+
project.pluginManager.apply(KotestPlugin::class.java)
19+
20+
project.kotest() shouldNotBe null
21+
}
22+
}
23+
24+
})

0 commit comments

Comments
 (0)