Skip to content

Commit

Permalink
Merge pull request #932 from gradle/tylerbertrand/add-kotest-to-pts-c…
Browse files Browse the repository at this point in the history
…ompatibility-scripts

Add kotest to supported engines in gradle-test-pts-support scripts
  • Loading branch information
tylerbertrand authored Nov 9, 2023
2 parents 5e3dbdd + 39cc16c commit 38a5601
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.gradle.scan.plugin.BuildScanExtension
import org.gradle.util.internal.VersionNumber
import java.nio.charset.StandardCharsets
import java.util.Collections
import java.util.Optional
Expand All @@ -15,11 +16,11 @@ def buildScanApi = project.extensions.findByName('buildScan')
if (!buildScanApi) {
return
}
def capture = new Capture(gradle.rootProject.logger)
def capture = new Capture(buildScanApi, gradle.rootProject.logger)
allprojects {
tasks.withType(Test).configureEach { t ->
doFirst {
capture.capturePts(t, buildScanApi)
capture.capturePts(t)
}
}
}
Expand All @@ -32,15 +33,18 @@ class Capture {
'org.spockframework.runtime.SpockEngine' : 'spock',
'net.jqwik.engine.JqwikTestEngine' : 'jqwik',
'com.tngtech.archunit.junit.ArchUnitTestEngine' : 'archunit',
'co.helmethair.scalatest.ScalatestEngine' : 'scalatest'
'co.helmethair.scalatest.ScalatestEngine' : 'scalatest',
'io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine' : 'kotest-runner'
]
private Logger logger
private BuildScanExtension buildScanApi

Capture(Logger logger) {
Capture(BuildScanExtension buildScanApi, Logger logger) {
this.buildScanApi = buildScanApi
this.logger = logger
}

void capturePts(Test t, BuildScanExtension buildScanApi) {
void capturePts(Test t) {
if (t.getTestFramework().getClass().getName() == 'org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestFramework') {
def engines = testEngines(t)
buildScanApi.value("${t.identityPath}#engines", "${engines}")
Expand All @@ -59,6 +63,7 @@ class Capture {
Stream<String> engines = t.classpath.files.stream()
.filter { f -> f.name.endsWith('.jar') }
.filter { f -> supportedEngines.values().stream().anyMatch { e -> f.name.contains(e) } }
.filter { f -> isCompatibleVersion(f, t) }
.map { f -> findTestEngine(f) }
.flatMap { o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty() }

Expand All @@ -84,4 +89,19 @@ class Capture {
.map { e -> jarFile.getInputStream(e).withCloseable { it.getText(StandardCharsets.UTF_8.name()).trim() } }
}
}

boolean isCompatibleVersion(File f, Test t) {
if (f.name.contains("kotest-runner")) {
def kotestVersionString = f.name.split("-")[f.name.split("-").length - 1].replace(".jar", "")
def kotestVersion = VersionNumber.parse(kotestVersionString)
if (VersionNumber.UNKNOWN == kotestVersion) {
logger.error("Unable to parse kotest version from file name ${f.name}")
buildScanApi.value("${t.identityPath}#unknownKotestVersion", "${f.name}")
return false
}
return VersionNumber.parse("5.6.0") <= kotestVersion
} else {
return true
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import com.gradle.enterprise.gradleplugin.GradleEnterpriseExtension
import com.gradle.scan.plugin.BuildScanExtension
import org.gradle.util.internal.VersionNumber
import java.nio.charset.StandardCharsets
import java.util.Collections
import java.util.Optional
Expand All @@ -16,29 +17,30 @@ import groovy.transform.Field
project.extensions.configure<GradleEnterpriseExtension>() {
buildScan {
val api = buildScan
val capture = Capture(gradle.rootProject.logger)
val capture = Capture(api, gradle.rootProject.logger)
allprojects {
tasks.withType<Test>().configureEach {
doFirst {
capture.capturePts(this as Test, api)
capture.capturePts(this as Test)
}
}
}
}
}

class Capture(val logger: Logger) {
class Capture(val api: BuildScanExtension, val logger: Logger) {
val supportedEngines: Map<String, String> = mapOf(
"org.junit.support.testng.engine.TestNGTestEngine" to "testng",
"org.junit.jupiter.engine.JupiterTestEngine" to "junit-jupiter",
"org.junit.vintage.engine.VintageTestEngine" to "junit-vintage",
"org.spockframework.runtime.SpockEngine" to "spock",
"net.jqwik.engine.JqwikTestEngine" to "jqwik",
"com.tngtech.archunit.junit.ArchUnitTestEngine" to "archunit",
"co.helmethair.scalatest.ScalatestEngine" to "scalatest"
"co.helmethair.scalatest.ScalatestEngine" to "scalatest",
"io.kotest.runner.junit.platform.KotestJunitPlatformTestEngine" to "kotest-runner"
)

fun capturePts(t: Test, api: BuildScanExtension) {
fun capturePts(t: Test) {
if (t.getTestFramework()::class.java.name == "org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestFramework") {
val engines = testEngines(t)
api.value("${t.identityPath}#engines", "${engines}")
Expand All @@ -56,6 +58,7 @@ class Capture(val logger: Logger) {
try {
var engines = t.classpath.files.stream().filter { f -> f.name.endsWith(".jar") }
.filter { f -> supportedEngines.values.stream().anyMatch { e -> f.name.contains(e) } }
.filter { f -> isCompatibleVersion(f, t) }
.map { f -> findTestEngine(f) }
.flatMap { o -> if (o.isPresent()) Stream.of(o.get()) else Stream.empty() }

Expand Down Expand Up @@ -85,4 +88,19 @@ class Capture(val logger: Logger) {
}
}
}

private fun isCompatibleVersion(f: File, t: Test): Boolean {
if (f.name.contains("kotest-runner")) {
val kotestVersionString = f.name.split("-")[f.name.split("-").lastIndex].replace(".jar", "")
val kotestVersion = VersionNumber.parse(kotestVersionString)
if (VersionNumber.UNKNOWN == kotestVersion) {
logger.error("Unable to parse kotest version from file name ${f.name}")
api.value("${t.identityPath}#unknownKotestVersion", "${f.name}")
return false
}
return VersionNumber.parse("5.6.0") <= kotestVersion
} else {
return true
}
}
}

0 comments on commit 38a5601

Please sign in to comment.