Skip to content

Commit

Permalink
Add unit tests for write-filters task & fix a bug with generating the…
Browse files Browse the repository at this point in the history
… filters file for instrumentation tests
  • Loading branch information
mannodermaus committed May 22, 2024
1 parent 5fedf9a commit 22371b2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public abstract class AndroidJUnit5WriteFilters : DefaultTask() {
.bufferedWriter()
.use { writer ->
// This format is a nod towards the real JUnit 5 ConsoleLauncher's arguments
includeTags.forEach { tag -> writer.write("-t $tag") }
excludeTags.forEach { tag -> writer.write("-T $tag") }
includeTags.forEach { tag -> writer.appendLine("-t $tag") }
excludeTags.forEach { tag -> writer.appendLine("-T $tag") }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ class InstrumentationSupportTests {
assertThat(project).configuration("androidTestRuntimeOnly").hasDependency(runnerLibrary())
}

@Test
fun `register the filter-write tasks`() {
project.addJUnitJupiterApi()
project.evaluate()

// AGP only registers androidTest tasks for the debug build type
assertThat(project).task("writeFiltersDebugAndroidTest").exists()
assertThat(project).task("writeFiltersReleaseAndroidTest").doesNotExist()
}

/* Private */

private fun Project.addJUnitJupiterApi() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package de.mannodermaus.gradle.plugins.junit5.tasks

import com.google.common.truth.Truth.assertThat
import de.mannodermaus.gradle.plugins.junit5.internal.config.INSTRUMENTATION_FILTER_RES_FILE_NAME
import de.mannodermaus.gradle.plugins.junit5.plugin.TestProjectProviderExtension
import de.mannodermaus.gradle.plugins.junit5.util.assertAll
import de.mannodermaus.gradle.plugins.junit5.util.evaluate
import org.gradle.api.Project
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import java.io.File
import java.nio.file.Paths
import kotlin.io.path.readLines

class AndroidJUnit5WriteFiltersTests {
@RegisterExtension
@JvmField
val projectExtension = TestProjectProviderExtension()

private lateinit var project: Project

@BeforeEach
fun beforeEach() {
project = projectExtension.newProject()
.asAndroidApplication()
.applyJUnit5Plugin(true) { junitPlatform ->
junitPlatform.filters().includeTags("included")
junitPlatform.filters().excludeTags("excluded", "another-group")
}
.build()
project.evaluate()
}

@Test
fun `generates file structure correctly`() {
// Expect a 'raw' folder inside the output, then the actual filters file in that sub-folder
val output = project.runTaskAndGetOutputFolder()

File(output, "raw").apply {
assertAll(
"output contains 'raw' folder",
{ assertThat(exists()).isTrue() },
{ assertThat(isDirectory).isTrue() },
)

File(this, INSTRUMENTATION_FILTER_RES_FILE_NAME).apply {
assertAll(
"'raw' folder contains filters file'",
{ assertThat(exists()).isTrue() },
{ assertThat(isFile).isTrue() },
)
}
}
}

@Test
fun `file contains expected content`() {
val output = project.runTaskAndGetOutputFolder()
val file = Paths.get(output.absolutePath, "raw", INSTRUMENTATION_FILTER_RES_FILE_NAME)

val content = file.readLines()
assertThat(content).containsExactly(
"-t included",
"-T excluded",
"-T another-group",
)
}

/* Private */

private fun Project.runTaskAndGetOutputFolder(): File {
val task = project.tasks.getByName("writeFiltersDebugAndroidTest") as AndroidJUnit5WriteFilters
task.execute()
return requireNotNull(task.outputFolder)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package de.mannodermaus.gradle.plugins.junit5.util
import com.google.common.truth.FailureMetadata
import com.google.common.truth.Subject
import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration

/* Methods */
Expand All @@ -22,6 +24,10 @@ class ProjectSubject(
fun configuration(name: String): ConfigurationSubject = check("configuration()")
.about(::ConfigurationSubject)
.that(actual?.configurations?.getByName(name))

fun task(name: String): TaskSubject = check("task()")
.about(::TaskSubject)
.that(actual?.tasks?.findByName(name))
}

class ConfigurationSubject(
Expand Down Expand Up @@ -66,3 +72,16 @@ class ConfigurationSubject(
).that(hasMatch).isEqualTo(expectExists)
}
}

class TaskSubject(
metadata: FailureMetadata,
private val actual: Task?,
) : Subject(metadata, actual) {
fun exists() {
assertThat(actual).isNotNull()
}

fun doesNotExist() {
assertThat(actual).isNull()
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.mannodermaus.gradle.plugins.junit5.util.projects

import de.mannodermaus.gradle.plugins.junit5.dsl.AndroidJUnitPlatformExtension
import de.mannodermaus.gradle.plugins.junit5.internal.extensions.android
import de.mannodermaus.gradle.plugins.junit5.internal.extensions.junitPlatform
import de.mannodermaus.gradle.plugins.junit5.util.TestEnvironment
import de.mannodermaus.gradle.plugins.junit5.util.applyPlugin
import de.mannodermaus.gradle.plugins.junit5.util.evaluate
Expand Down Expand Up @@ -40,7 +42,7 @@ class PluginSpecProjectCreator(private val environment: TestEnvironment) {

private var projectType = Type.Unset
private var appId = "com.example.android"
private var applyJUnit5Plugin = true
private var applyJUnit5Plugin: ((AndroidJUnitPlatformExtension) -> Unit)? = {}
private var applyJacocoPlugin = false
private var applyKotlinPlugin = false

Expand All @@ -60,8 +62,12 @@ class PluginSpecProjectCreator(private val environment: TestEnvironment) {

fun asAndroidLibrary() = setProjectTypeIfUnsetTo(Type.Library)

fun applyJUnit5Plugin(state: Boolean = true) = apply {
this.applyJUnit5Plugin = state
fun applyJUnit5Plugin(state: Boolean = true, configuration: ((AndroidJUnitPlatformExtension) -> Unit)? = null) = apply {
this.applyJUnit5Plugin = if (state) {
configuration ?: {}
} else {
null
}
}

fun applyJacocoPlugin(state: Boolean = true) = apply {
Expand Down Expand Up @@ -93,7 +99,7 @@ class PluginSpecProjectCreator(private val environment: TestEnvironment) {
project.applyPlugin("kotlin-android")
}

if (applyJUnit5Plugin) {
if (applyJUnit5Plugin != null) {
project.applyPlugin("de.mannodermaus.android-junit5")
}

Expand All @@ -116,6 +122,14 @@ class PluginSpecProjectCreator(private val environment: TestEnvironment) {
// swallow this particular error
}

// Configure JUnit 5 with custom configuration clause, if any
try {
applyJUnit5Plugin?.invoke(project.junitPlatform)
} catch (e: UnknownDomainObjectException) {
// Expected when the JUnit 5 plugin is not applied to a project;
// swallow this particular error
}

return project
}

Expand Down

0 comments on commit 22371b2

Please sign in to comment.