Skip to content

Commit

Permalink
#198 Stabilise functional tests
Browse files Browse the repository at this point in the history
- Removed some unnecessary test config;
- Improved functional test setup;
- Improved coverage;
- Some post work of gradle update;
  • Loading branch information
mathze committed Jun 29, 2024
1 parent eb17970 commit bd7d31e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 42 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ jobs:
with:
remote-build-cache-proxy-enabled: false
save-local-build-cache: false
arguments: |
test
jacocoTestReport
arguments: test

- name: "Publish Unit Test Results"
uses: EnricoMi/[email protected]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea/
build/
out/
.kotlin/
52 changes: 22 additions & 30 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import de.qualersoft.parseSemVer
import io.gitlab.arturbosch.detekt.Detekt
import org.gradle.api.internal.artifacts.configurations.DefaultUnlockedConfiguration
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.owasp.dependencycheck.gradle.extension.AnalyzerExtension
import org.owasp.dependencycheck.gradle.extension.NvdExtension
import org.owasp.dependencycheck.gradle.extension.RetireJSExtension
import org.owasp.dependencycheck.reporting.ReportGenerator.Format

Expand All @@ -12,7 +12,6 @@ plugins {
kotlin("jvm") version "2.0.0"

// quality
jacoco
`jacoco-report-aggregation`
id("pl.droidsonroids.jacoco.testkit") version "1.0.12"
id("io.gitlab.arturbosch.detekt") version "1.23.6"
Expand All @@ -36,20 +35,25 @@ repositories {

@Suppress("UnstableApiUsage")
testing {
val junitVersion = "5.10.1"
val junitVersion = "5.10.2"
val kotestVersion = "5.9.1"
suites {
val test by getting(JvmTestSuite::class) {
useJUnitJupiter(junitVersion)

dependencies {
implementation("io.kotest:kotest-assertions-core:5.9.1")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
}

gradlePlugin.testSourceSet(sources)
}

register<JvmTestSuite>("functionalTest") {
useJUnitJupiter(junitVersion)

dependencies {
implementation(project())
implementation("io.kotest:kotest-assertions-core:5.9.1")
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
implementation(gradleTestKit())

implementation(platform("org.junit:junit-bom:5.10.2"))
Expand All @@ -70,18 +74,21 @@ testing {
targets.all {
testTask.configure {
dependsOn(tasks.jar)
mustRunAfter(test)

shouldRunAfter(test)
applyJacocoWorkaround()
}
tasks.check {
dependsOn(testTask)
}

jacocoTestKit {
@Suppress("UNCHECKED_CAST")
applyTo("functionalTestRuntimeOnly", testTask as TaskProvider<Task>)
}
tasks.check {
dependsOn(testTask)
}
}

gradlePlugin.testSourceSet(sources)
}
}
}
Expand Down Expand Up @@ -113,9 +120,6 @@ reporting {
}
}

// Add a source set for the functional test suite
val functionalTestSourceSet: SourceSet = sourceSets.getByName("functionalTest")

@Suppress("UnstableApiUsage")
gradlePlugin {
website.set("https://github.com/qualersoft/jmeter-gradle-plugin")
Expand All @@ -129,7 +133,6 @@ gradlePlugin {
description = "Plugin to execute JMeter tests."
tags.addAll("jmeter", "test", "performance")
}
testSourceSets(sourceSets.test.get(), functionalTestSourceSet)
}

detekt {
Expand Down Expand Up @@ -161,9 +164,9 @@ dependencyCheck {
})
System.getenv().getOrDefault("NVD_API_KEY", findProperty("NVD_API_KEY"))?.also {
if ((it as String).isNotBlank()) {
nvd(closureOf<NvdExtension> {
nvd.apply {
apiKey = it
})
}
}
}
})
Expand All @@ -189,8 +192,8 @@ changelog {
tasks {

withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = javaVersion.toString()
compilerOptions {
jvmTarget = JvmTarget.fromTarget(javaVersion.toString())
}
}

Expand All @@ -209,23 +212,12 @@ tasks {
sarif.required.set(true)
}
}

pluginUnderTestMetadata {
// https://discuss.gradle.org/t/how-to-make-gradle-testkit-depend-on-output-jar-rather-than-just-classes/18940/2
val gradlePlgExt = project.extensions.getByName<GradlePluginDevelopmentExtension>("gradlePlugin")
val additionalResources = pluginClasspath.files - files(
gradlePlgExt.pluginSourceSet.output.classesDirs,
gradlePlgExt.pluginSourceSet.output.resourcesDir
)
pluginClasspath.setFrom(
files(jar) + additionalResources
)
pluginClasspath.from(files(jar))
mustRunAfter(jar)
}

withType<Test>().configureEach {
useJUnitPlatform()
}

withType<JacocoReport> {
reports {
csv.required.set(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import java.io.File
import java.io.InputStream
import java.lang.management.ManagementFactory
import java.util.zip.ZipFile

open class JMeterPluginFunctionalTestBase {
Expand All @@ -29,21 +30,26 @@ open class JMeterPluginFunctionalTestBase {
*/
protected var rootFolder: () -> String? = { null }

protected fun setupTest(baseFileName: String, ext: String = "gradle.kts", debug: Boolean = false): GradleRunner {
protected fun setupTest(baseFileName: String, ext: String = "gradle.kts"): GradleRunner {
copyTestFileToTemp(baseFileName, ext)
return createRunner(debug)
return createRunner()
}

protected fun runShouldFail(result: BuildResult, reason: String = "") {
result.output shouldContain "FAILURE: $reason"
}

private fun createRunner(debug: Boolean) = GradleRunner.create()
private fun createRunner() = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withPluginClasspath()
.withDebug(debug)
.withDebug(ManagementFactory.getRuntimeMXBean().inputArguments.toString().indexOf("-agentlib:jdwp") > 0)
.withTestKitDir(testProjectDir.newFolder())
.withJaCoCo()
.also {
if (it.isDebug) {
it.forwardOutput()
}
}

/**
* Copies a jmx-file from `resource` to the default location.
Expand Down Expand Up @@ -91,7 +97,7 @@ open class JMeterPluginFunctionalTestBase {
}

val settings = testProjectDir.newFile("settings.$ext")
settings.writeText("")
settings.writeText("""rootProject.name = "$resource"""")

return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,8 @@ open class JMeterRunTask : JMeterExecBaseTask() {

@Option(option = "PP", description = "proxy server port")
fun setProxyPort(port: String) {
port.toIntOrNull()?.let {
proxyPort.value(it)
} ?: throw IllegalArgumentException("Port must be a valid number! Got >$port<.")
val iPort = checkNotNull(port.toIntOrNull()) { "Port must be a valid number! Got >$port<." }
proxyPort.set(iPort)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ class JMeterGuiTaskTest : JMeterTaskTestBase() {
(result as JMeterGuiTask).jmxFile.get() shouldBe "test.jmx"
}

@Test
fun `should never be up-to-date`() {
val task = createTask<JMeterGuiTask>().get()

val result = task.outputs.upToDateSpec.isSatisfiedBy(task)

result shouldBe false
}

private fun createProject() = ProjectBuilder.builder().build()
.also {
it.plugins.apply(PluginTestBase.PLUGIN_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class JMeterTaskTestBase {
}.tasks.register(T::class.simpleName!!, T::class.java, taskConfig)
}

protected inline fun <reified T : Task> createTask(config: JMeterExtension.() -> Unit): TaskProvider<T> {
protected inline fun <reified T : Task> createTask(config: JMeterExtension.() -> Unit = {}): TaskProvider<T> {
return ProjectBuilder.builder().build().also {
project = it
it.plugins.apply(PluginTestBase.PLUGIN_ID)
Expand Down

0 comments on commit bd7d31e

Please sign in to comment.